10、根据规则生成排班

2020-05-11  本文已影响0人  wqjcarnation

表名: Scheduling(排班表)

前台

0、整合两个功能为一个页

<template>
  <div>
    <!--新增加排班规则-->
    <div>
    <el-button type="text" @click="dialogFormVisible = true">新增排班规则</el-button>
<el-button type="text" @click="dialogFormVisible1 = true">根据规则进行排班</el-button>
    </div>
    <!--根据规则来排班-->
    <el-dialog title="新增排班规则" :visible.sync="dialogFormVisible" width="70%">
          <addRule></addRule>
     </el-dialog>
    <el-dialog title="根据规则进行排班" :visible.sync="dialogFormVisible1" width="90%">
          <div>3333333</div>
     </el-dialog>
  </div>

</template>

<script>
  import addRule from '@/components/scheduling/Rule'//1

  export default{
    data(){
      return {
        dialogFormVisible:false,
        dialogFormVisible1:false

      }
    },
    components:{//1
      addRule
    }

  }

</script>

<style>
</style>

1、SchedulingMgr.vue中增加链接

        <div class="nav">
          <el-button type="primary" icon="el-icon-plus" @click="dialogFormVisible = true" size="mini">新增排班规则</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="dialogFormVisible1 = true" size="mini">根据规则排班</el-button>
        </div>

        <el-dialog title="根据规则排班" :visible.sync="dialogFormVisible1" width="60%">
                <addSchedu></addSchedu>
        </el-dialog>

     <script>
     import addSchedu from '@/components/sys/scheduling/addSchedu'

export default {
data() {
return {
............

                    dialogFormVisible1:false,
               ..............
                  }
                }

                            components:{
                  addRule,addSchedu
                }
  </script>

排班页:addSchedu.vue

step 1 根据科室查询排班规则

  <template>
    <div>
      <el-form :model="form" ref="form" label-width="100px">
        <el-row>
          <el-col :span="4">&nbsp;</el-col>
          <el-col :span="6">
              <el-form-item label="所在科室" prop="deptID">
              <!--科室下拉列表-->
                  <el-select  size="mini" v-model="form.deptID">
                    <!--[{"id":1,"deptCode":"XXGNK","deptName":"心血管内科","deptCategoryId":"402880ed71ecf9f30171ecff858d0000","deptType":1}]-->
                    <el-option v-for="item in deptOptions" :key="item.id" :value="item.id" :label="item.deptName"></el-option>
                  </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item>
              <el-button type="primary" @click="submitForm('form')" size="mini">查询</el-button>
              <el-button @click="resetForm('form')" size="mini">重置</el-button>
            </el-form-item>
          </el-col>
         <el-col :span="4">&nbsp;</el-col>
        </el-row>



        <el-row>
          <el-col :span="24" >
            <el-table
                ref="multipleTable"
                :data="tableData"
                height="250"
                border
                style="width: 100%"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" @selection-change="handleSelectionChange"></el-table-column>
                  label="规则名称"
                  width="160" prop="ruleName">
                </el-table-column>
                <el-table-column
                  label="科室名称"
                   width="160" prop="deptName">
                </el-table-column>
                <el-table-column
                  label="医生名称"
                   width="160" prop="realName">
                </el-table-column>
                <el-table-column
                  label="规则"
                   width="160" prop="week">
                </el-table-column>
                </el-table>
           </el-col>
       </el-row>

       <el-row>
         <el-col :span="12" >
       <el-form-item label="开始时间" prop="startDate">
  <el-date-picker
       v-model="form.startDate"
       type="date"
       value-format="yyyy-MM-dd"
       placeholder="开始时间"
        size="mini">
     </el-date-picker>
     </el-form-item>
     </el-col>

<el-col :span="12" >
<el-form-item label="结束时间" prop="startDate" >
<el-date-picker
v-model="form.endDate"
type="date"
value-format="yyyy-MM-dd"
placeholder="结束时间"
size="mini">
</el-date-picker>
</el-form-item>
</el-col>
</el-row>
<el-button type="primary" @click="save()" size="mini">选取规则生成排班</el-button>
<el-button @click="resetForm('form')" size="mini">清空</el-button>
</el-form>

    </div>
  </template>

  <script>

    export default {
      data() {
        return {
          multipleSelection: [],
          deptOptions: [],
          tableData:[],
          scheduArray:[],//step1 定义一个数组,用于存放多条规则对象
          form: {
               deptID:'',
               startDate:'',
               endDate:''
          }
        }
      },
      mounted() {
        //查询部门信息,下拉列表用
        this.$axios.get("http://localhost:8080/department/findAll")
          .then(response => {
            this.deptOptions = response.data;
          })

      },
      methods: {
        handleSelectionChange(val) {
          //这个也是清理工作
          this.multipleSelection=val;
        },
        save() {
          //step2 每次先清空这个规则数组,防止无限累加
           this.scheduArray=[];
           console.log("form:",this.form);
           //selItems是对象数组{}
           let selItems=this.multipleSelection;//选中的排班人员
           console.log("选中的行:",selItems)
             for(var i=0;i<selItems.length;i++){
               //未来存储再想办法
               this.scheduArray.push({startDate:this.form.startDate,endDate:this.form.endDate,userID:selItems[i].userID,deptID:this.form.deptID,ruleId:selItems[i].id});
               }
               //向后台发送数组
               this.$axios.post("http://localhost:8080/rule/addScheduling",{schdeus:this.scheduArray})
               .then(response=>{
                 this.$message.info(response.data);
               })
               .catch()
          }
        ,
        getDeptId(deptid){
          //alert("父类收到的id"+deptid);
          this.form.deptID=deptid;
        }
        ,
        resetForm(formName) {
        this.$refs[formName].resetFields();
      },
        submitForm(form){
             //alert("submit");
            this.$axios.get("http://localhost:8080/rule/findRuleByDeptId?deptid="+this.form.deptID)
            .then(response=>{
              this.tableData=response.data;
            })
        }
      }
    }
  </script>

  <style>
  </style>

后台

关联三张表进行查询
1、根据科室查询排班规则

http://localhost:8080/rule/findRuleByDeptId

@RequestMapping("findRuleByDeptId")
  public List<Map<String, Object>> findRuleByDeptId(String deptid){
        System.out.println(deptid);
        //后台将来做批量添加,注意事务控制
        List<Map<String, Object>> ruleList=ruleService.findRuleByDeptId(deptid);
    return ruleList;
  }

@Query(value=" select a.*,b.deptName,c.realName "+
        " from t_rule a,t_department b,t_user c "+
        " where a.deptID=b.id "+
        " and a.userID=c.id "+
        " and a.deptID=?1",nativeQuery=true)
List<Map<String,Object>> findRuleByDeptId(String deptid);

2、生成排班
2.1实体类

    package com.neuedu.his.pojo;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Transient;
    
    import org.hibernate.annotations.GenericGenerator;
    
    @Entity
    @Table(name = "t_scheduling")
    @GenericGenerator(name = "jpa-uuid", strategy = "uuid")
    public class Scheduling {
        @Id
        @GeneratedValue(generator = "jpa-uuid")
        @Column(name = "id", nullable = false, length = 32)
        private String id;
        private String schedDate;
        @Column(name = "userID", nullable = false, length = 32)
        private String userID;
        private int deptID;
        @Column(name = "noon", nullable = false, length = 2)
        private String noon;
        @Column(name = "ruleId", nullable = false, length = 32)
        private String ruleId;
        @Transient
        private String startDate;   
        @Transient
        private String endDate;
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getSchedDate() {
            return schedDate;
        }
        public void setSchedDate(String schedDate) {
            this.schedDate = schedDate;
        }
        public String getUserID() {
            return userID;
        }
        public void setUserID(String userID) {
            this.userID = userID;
        }
        public int getDeptID() {
            return deptID;
        }
        public void setDeptID(int deptID) {
            this.deptID = deptID;
        }
        public String getNoon() {
            return noon;
        }
        public void setNoon(String noon) {
            this.noon = noon;
        }
        public String getRuleId() {
            return ruleId;
        }
        public void setRuleId(String ruleId) {
            this.ruleId = ruleId;
        }
        public String getStartDate() {
            return startDate;
        }
        public void setStartDate(String startDate) {
            this.startDate = startDate;
        }
        public String getEndDate() {
            return endDate;
        }
        public void setEndDate(String endDate) {
            this.endDate = endDate;
        }
        
    }

2.2 vo:

    public class ScheduVo {
    private List<Scheduling> schdeus;
    
    public List<Scheduling> getSchdeus() {
        return schdeus;
    }
    
    public void setSchdeus(List<Scheduling> schdeus) {
        this.schdeus = schdeus;
    }
    }

2.3 controller

@RequestMapping("addScheduling")
  public String addScheduling(@RequestBody ScheduVo vo){
        //排班计划在vo.schdeus
        System.out.println("得到的数据为"+vo.getSchdeus());
        List<Scheduling> schedus=vo.getSchdeus();
        try {
            //后台将来做批量添加,注意事务控制
            ruleService.addScheduling(schedus);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "保存失败";
        }
    return "保存成功";
  }

2.4学生练习根据生成排班并入库

其他代码:service

@Override
public void addScheduling(List<Scheduling> list) throws Exception {
    try {
        for (Scheduling schedu : list) {
            //开始时间 结束时间    按天,按上下午进行入库
            saveScheduling(schedu);
        }
    } catch (Exception e) {
        throw new Exception(e.getMessage());
    }

}
        //2020-05-11入库    (算当前日期是星期几,截取字符串(上下午),判断1-入库  0放弃)
        //2020-05-17与2020-05-11相差的天数   6
        //循环6次   2020-05-12  2020-05-13  
        //每次把当前的时间加1天之后存到排班表  --入库
public void saveScheduling(Scheduling schedu) throws ParseException {
    // 把开始结束时间转换成时间形式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = sdf.parse(schedu.getStartDate());
            //公共的calendar
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            // 把startDate这一天的情况先入库
            saveCurdate(schedu.getStartDate(),schedu);
            // startDate算完后,计算开始结束时间相差的天数,每次加一天,加n次,入库即可
            // 计算相差的天数
            long between = betweenDays(schedu.getEndDate(), schedu.getStartDate());
            System.out.println("日期相差的天数:" + between);
            if (between > 0) {
                for (int i = 0; i < between; i++) {
                    calendar.add(calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
                    System.out.println("相加后的日期:" + calendar.getTime());
                    saveCurdate(sdf.format(calendar.getTime()),schedu);
                }
            }
}



 //计算两个日期相差的天数
public long betweenDays(String startDate, String endDate) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = sdf.parse(startDate);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date1);
    Date date2 = sdf.parse(endDate);
    calendar.setTime(date2);
    long betweenDays = (date1.getTime() - date2.getTime()) / (60 * 60 * 24 * 1000);
    // 打印控制台相差的天数
    // System.out.println(betweenDays);
    return betweenDays;
}

public void saveCurdate(String curdate, Scheduling schedu) throws ParseException {
     //1、根据日期计算星期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date curdate_date=sdf.parse(curdate);
    // 把开始结束时间转换成字符串
    Calendar c = Calendar.getInstance();
    c.setTime(curdate_date);
    // 原始返回值:星期1-2 星期2-3 星期3-4 星期5-6 星期6-7 星期7-1
    // 减1后:星期1-1 星期2-1 星期3-3 星期7-0
    int week = c.get(Calendar.DAY_OF_WEEK) - 1;//
    if (week == 0) {
        week = 7;
    }
    System.out.println("星期:" + week);
    String weekStr = "10101101101011";
    // 10101101101011
    // 上午截取i*2-2,下午截取i*2-1
    char noon1 = weekStr.charAt(week * 2 - 2);
    
    Scheduling db_sch=new Scheduling();
    db_sch.setDeptID(schedu.getDeptID());
    db_sch.setRuleId(schedu.getRuleId());
    db_sch.setUserID(schedu.getUserID());
    db_sch.setSchedDate(curdate);
    
    if (noon1 == '1') {
        
        db_sch.setNoon("上午");
        System.out.println(curdate + "上午有班");
        // SAVE
        schedulingRepository.save(db_sch);
    }
    char noon2 = weekStr.charAt(week * 2 - 1);
    if (noon2 == '1') {
        schedu.setNoon("下午");
        System.out.println(curdate + "下午有班");
        // SAVE
        schedulingRepository.save(schedu);
    }
}
上一篇下一篇

猜你喜欢

热点阅读