class ArrangementManagementServiceImpl implements ArrangementManagementService {

    public List<ArrangementConflictParam> saveArrangement(Date today, int week, Integer departmentId,
                                                          Integer arrangementRuleId, List<Arrangement> arrangements,
                                                          List<ArrangementConflictParam> conflict, Boolean toInsert,
                                                          List<Arrangement> toInsertArrangements){
        //当日现有排班有效信息 key: roleId    value: Arrangement
        HashMap<Integer, Arrangement> arrangementsToday = new HashMap<>();
        for(Arrangement arrangement : arrangements){
            if(0 == arrangement.getAppointmentDate().compareTo(today)){
                arrangementsToday.put(arrangement.getRoleId(), arrangement);
            }
        }

        //可用的排班规则信息
        List<HashMap> arrangementRules = arrangementRuleMapper.getValidById(arrangementRuleId);

        //生成一个人排班
        for(HashMap arrangementRule : arrangementRules) {
            Byte timeSlot = 0;
            String weekDate = "";
            switch (week) {
                case 1:
                    weekDate = "mon";
                    break;
                case 2:
                    weekDate = "tue";
                    break;
                case 3:
                    weekDate = "wed";
                    break;
                case 4:
                    weekDate = "thu";
                    break;
                case 5:
                    weekDate = "fri";
                    break;
                case 6:
                    weekDate = "sat";
                    break;
                case 7:
                    weekDate = "sun";
                    break;
            }

            Boolean am = (Boolean) arrangementRule.get(weekDate + "Am");
            Boolean pm = (Boolean) arrangementRule.get(weekDate + "Pm");

            //1.上午 2.下午 3.全天 0.无
            if(am == true && pm == false){
                timeSlot = 1;
            }
            else if(am == false && pm == true){
                timeSlot = 2;
            }
            else if (am == true && pm == true){
                timeSlot = 3;
            }
            //若与现有排班信息冲突，则生成失败，返回冲突提示
            Integer roleId = (Integer)arrangementRule.get("roleId");
            //当日该用户冲突排班信息
            Integer userId = roleMapper.getUserIdByRoleId(roleId);
            for (Map.Entry<Integer, Arrangement> entry : arrangementsToday.entrySet()) {
                Integer arrangedRoleId = entry.getKey();
                //用户user今日有其他排班
                Integer theUserId = roleMapper.getUserIdByRoleId(arrangedRoleId);
                if(userId.equals(theUserId)){
                    //是否存在冲突
                    Arrangement arrangement = entry.getValue();
                    //时间安排存在冲突
                    if((timeSlot != 0) && (timeSlot == arrangement.getTimeSlot() || timeSlot == 3 || arrangement.getTimeSlot() == 3)){
                        ArrangementConflictParam arrangementConflictParam = new ArrangementConflictParam();
                        arrangementConflictParam.setDate(today);
                        String departmentName = departmentMapper.getNameById(departmentId);
                        arrangementConflictParam.setDepartmentName(departmentName);
                        arrangementConflictParam.setUserName(arrangement.getUserName());
                        conflict.add(arrangementConflictParam);
                    }
                }
            }

            //从排班规则表获取信息
            String roleName = roleMapper.getUserNameByRoleId(roleId);
            Integer registrationLevelIdInt = (Integer)arrangementRule.get("registrationLevelId");
            //todo
            Integer maxAppointmentInt = (Integer)arrangementRule.get("maxAppointment");
            Byte registrationLevelId = registrationLevelIdInt.byteValue();
            Byte maxAppointment = maxAppointmentInt.byteValue();

            if(timeSlot != 0) {
                //插入数据库
                Arrangement arrangement = new Arrangement();
                arrangement.setAppointmentDate(today);
                arrangement.setDepartmentId(departmentId);
                arrangement.setRoleId(roleId);//角色id
                arrangement.setUserName(roleName);//角色姓名
                arrangement.setRegistrationLevelId(registrationLevelId);//挂号级别
                arrangement.setIsValid(true);//默认有效
                arrangement.setTimeSlot(timeSlot);//午别
                arrangement.setMaxAppointment(maxAppointment);//看诊限额
                arrangement.setAppointmentLeft(maxAppointment);//剩余看诊限额
//                arrangementMapper.insertSelective(arrangement);
                toInsertArrangements.add(arrangement);//待插入排班结果
            }
        }
        return conflict;
    }

}