package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.DateUtil;
import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.his.mz.MzPatientMi;
import cn.hnthyy.thmz.entity.his.mz.MzYjReq;
import cn.hnthyy.thmz.entity.his.zy.APatientMi;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.entity.his.YshYjReq;
import cn.hnthyy.thmz.entity.his.zd.ZdUnitCode;
import cn.hnthyy.thmz.entity.thmz.ScheduleOfMedical;
import cn.hnthyy.thmz.entity.thmz.ScheduleOfMedicalApply;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.GenderEnum;
import cn.hnthyy.thmz.enums.ScheduleOfMedicalEnum;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.service.his.mz.MzPatientMiService;
import cn.hnthyy.thmz.service.his.mz.MzYjReqService;
import cn.hnthyy.thmz.service.his.zy.APatientMiService;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.service.his.YshYjReqService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import cn.hnthyy.thmz.service.thmz.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
public class ScheduleOfMedicalController {
    @Autowired
    private ScheduleOfMedicalService scheduleOfMedicalService;
    @Autowired
    private ScheduleOfMedicalApplyServicve scheduleOfMedicalApplyServicve;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private YshYjReqService yshYjReqService;
    @Autowired
    private APatientMiService aPatientMiService;
    @Autowired
    private MzYjReqService mzYjReqService;
    @Autowired
    private  MzPatientMiService mzPatientMiService;
    @Autowired
    private HaiCiAdapterService haiCiAdapterService;
    //消化内科编码
    public static String XHNK="1020201";

    /**
     * 保存医技排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveScheduleOfMedical", method = {RequestMethod.POST})
    public Map<String, Object> saveScheduleOfMedical(@RequestBody ScheduleOfMedical scheduleOfMedical, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存医技排班失败,参数为空");
                return resultMap;
            }
            if (StringUtils.isBlank(scheduleOfMedical.getRecordDate())) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存医技排班失败,日期为空");
                return resultMap;
            }
            if (StringUtils.isBlank(scheduleOfMedical.getDepNo())) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存医技排班失败,科室为空");
                return resultMap;
            }
            //消化内镜
            if (XHNK.equals(scheduleOfMedical.getDepNo()) && StringUtils.isBlank(scheduleOfMedical.getJcxm())) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存医技排班失败,检查项目为空");
                return resultMap;
            }
            if (scheduleOfMedical.getRoomNum() == null) {
//                resultMap.put("code", -1);
//                resultMap.put("message", "保存医技排班失败,诊室为空");
//                return resultMap;
                scheduleOfMedical.setRoomNum(0);
            }
            if (StringUtils.isBlank(scheduleOfMedical.getBeginTime())) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存医技排班失败,开始时间为空");
                return resultMap;
            }
            if (StringUtils.isBlank(scheduleOfMedical.getEndTime())) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存医技排班失败,结束时间为空");
                return resultMap;
            }
            if (scheduleOfMedical.getId()==null && scheduleOfMedical.getTotalNum() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存医技排班失败,可预约号数为空");
                return resultMap;
            }
            ScheduleOfMedical scheduleOfMedicalParmas = new ScheduleOfMedical();
            scheduleOfMedicalParmas.setRecordDate(scheduleOfMedical.getRecordDate());
            scheduleOfMedicalParmas.setDepNo(scheduleOfMedical.getDepNo());
            scheduleOfMedicalParmas.setRoomNum(scheduleOfMedical.getRoomNum());
            List<ScheduleOfMedical> scheduleOfMedicals = scheduleOfMedicalService.queryByCommonParams(scheduleOfMedicalParmas);
            if (scheduleOfMedicals != null && scheduleOfMedicals.size() > 0) {
                List<Timestamp> periodStart = new ArrayList<>();
                List<Timestamp> periodend = new ArrayList<>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                StringBuffer sbf = new StringBuffer();
                for (ScheduleOfMedical sc : scheduleOfMedicals) {
                    sbf.append(sc.getRecordDate()).append(" ").append(sc.getBeginTime()).append(":00");
                    periodStart.add(new Timestamp(sdf.parse(sbf.toString()).getTime()));
                    sbf.setLength(0);
                    sbf.append(sc.getRecordDate()).append(" ").append(sc.getEndTime()).append(":00");
                    periodend.add(new Timestamp(sdf.parse(sbf.toString()).getTime()));
                    sbf.setLength(0);
                }
                sbf.append(scheduleOfMedical.getRecordDate()).append(" ").append(scheduleOfMedical.getBeginTime()).append(":00");
                periodStart.add(new Timestamp(sdf.parse(sbf.toString()).getTime()));
                sbf.setLength(0);
                sbf.append(scheduleOfMedical.getRecordDate()).append(" ").append(scheduleOfMedical.getEndTime()).append(":00");
                periodend.add(new Timestamp(sdf.parse(sbf.toString()).getTime()));
                sbf.setLength(0);
                boolean isOverlap = DateUtil.isOverlap(periodStart, periodend);
                if (isOverlap) {
                    resultMap.put("code", -1);
                    resultMap.put("message", "保存医技排班数据失败,当前新增的排班时间范围与该科室已存在档期有重叠，请调整后重试！");
                    return resultMap;
                }
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            resultMap.put("code", 0);
            resultMap.put("message", "保存医技排班数据成功");
            scheduleOfMedicalService.saveScheduleOfMedical(scheduleOfMedical, tokenUser.getId());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存医技排班数据失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "保存医技排班数据失败,错误原因【"+e.getMessage()+"】");
            return resultMap;
        }
    }


    /**
     * 根据id查询医技预约排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getScheduleOfMedicalById", method = {RequestMethod.GET})
    public Map<String, Object> getScheduleOfMedicalById(@RequestParam("id") Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (id == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询医技预约排班失败,参数为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根查询医技预约排班成功");
            ScheduleOfMedical scheduleOfMedical = scheduleOfMedicalService.queryScheduleOfMedicalById(id);
            resultMap.put("data", scheduleOfMedical);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据id查询医技预约排班失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据id查询医技预约排班失败");
            return resultMap;
        }
    }


    /**
     * 删除id查询医技预约排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/removeScheduleOfMedicalById", method = {RequestMethod.GET})
    public Map<String, Object> removeScheduleOfMedicalById(@RequestParam("id") Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (id == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "删除id查询医技预约排班失败,参数为空");
                return resultMap;
            }
            ScheduleOfMedical scheduleOfMedical = scheduleOfMedicalService.queryScheduleOfMedicalById(id);
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "删除id查询医技预约排班失败,不存在对应的排班");
                return resultMap;
            }
            if (!ScheduleOfMedicalEnum.FREE.code.equals(scheduleOfMedical.getStatus())) {
                resultMap.put("code", -1);
                resultMap.put("message", "无法删除已经被预约的排班信息！");
                return resultMap;
            }
            int num = scheduleOfMedicalService.removeScheduleOfMedical(id);
            if (num == 1) {
                resultMap.put("code", 0);
                resultMap.put("message", "删除医技预约排班成功");
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "删除医技预约排班失败");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除医技预约排班失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "删除医技预约排班失败");
            return resultMap;
        }
    }


    /**
     * 查询医技预约排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getScheduleOfMedical", method = {RequestMethod.POST})
    public Map<String, Object> getScheduleOfMedical(@RequestBody ScheduleOfMedical scheduleOfMedical) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询医技预约排班失败,参数为空");
                return resultMap;
            }
            if (StringUtils.isBlank(scheduleOfMedical.getDepNo())) {
                scheduleOfMedical.setDepNo(null);
            }
            if (scheduleOfMedical.getBeginDateParams() != null && scheduleOfMedical.getEndDateParams() != null) {
                List<String> dates = DateUtil.getBetweenDatesDesc(scheduleOfMedical.getBeginDateParams(), scheduleOfMedical.getEndDateParams());
                if (dates != null && dates.size() > 0) {
                    scheduleOfMedical.setRecordDates(dates);
                }
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根查询医技预约排班成功");
            Integer count = scheduleOfMedicalService.queryCountScheduleOfMedical(scheduleOfMedical);
            List<ScheduleOfMedical> scheduleOfMedicals = scheduleOfMedicalService.queryScheduleOfMedicalWithPage(scheduleOfMedical);
            fitScheduleOfMedical(scheduleOfMedicals);
            resultMap.put("data", scheduleOfMedicals);
            resultMap.put("count", count);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询医技预约排班失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询医技预约排班失败");
            return resultMap;
        }
    }


    private void fitScheduleOfMedical(List<ScheduleOfMedical> scheduleOfMedicals) {
        scheduleOfMedicals.stream().forEach(m -> {
            if (StringUtils.isNotBlank(m.getDepNo())) {
                String unitName = zdUnitCodeService.queryDeptNameByIdInCache(m.getDepNo());
                if (StringUtils.isNotBlank(unitName)) {
                    m.setDeptName(unitName);
                }
            }
        });
    }


    /**
     * 根据科室和日期查询诊室
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getRoomNums", method = {RequestMethod.POST})
    public Map<String, Object> getRoomNums(@RequestBody ScheduleOfMedical scheduleOfMedical) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据科室和日期查询诊室失败,参数为空");
                return resultMap;
            }
            if (StringUtils.isBlank(scheduleOfMedical.getDepNo())) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据科室和日期查询诊室失败,科室为空");
                return resultMap;
            }
            if (scheduleOfMedical.getBeginDateParams() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据科室和日期查询诊室失败,开始时间为空");
                return resultMap;
            }
            if (scheduleOfMedical.getEndDateParams() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据科室和日期查询诊室失败,结束时间为空");
                return resultMap;
            }
            List<String> dates = DateUtil.getBetweenDatesDesc(scheduleOfMedical.getBeginDateParams(), scheduleOfMedical.getEndDateParams());
            if (dates != null && dates.size() > 0) {
                scheduleOfMedical.setRecordDates(dates);
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根据科室和日期查询诊室成功");
            List<ScheduleOfMedical> scheduleOfMedicals = scheduleOfMedicalService.queryByCommonParams(scheduleOfMedical);
            Set<Integer> roomNums = new HashSet<>();
            if (scheduleOfMedicals != null && scheduleOfMedicals.size() > 0) {
                for (ScheduleOfMedical scm : scheduleOfMedicals) {
                    roomNums.add(scm.getRoomNum());
                }
            }
            fitScheduleOfMedical(scheduleOfMedicals);
            resultMap.put("data", roomNums);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据科室和日期查询诊室失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据科室和日期查询诊室失败");
            return resultMap;
        }
    }


    /**
     * 根据科室和日期查询所有的排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getFullScheduleOfMedical", method = {RequestMethod.POST})
    public Map<String, Object> getFullScheduleOfMedical(@RequestBody ScheduleOfMedical scheduleOfMedical) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据科室和日期查询所有的排班失败,参数为空");
                return resultMap;
            }
            if (StringUtils.isBlank(scheduleOfMedical.getDepNo())) {
                scheduleOfMedical.setDepNo(null);
            }
            if (scheduleOfMedical.getBeginDateParams() != null && scheduleOfMedical.getEndDateParams() != null) {
                List<String> dates = DateUtil.getBetweenDatesDesc(scheduleOfMedical.getBeginDateParams(), scheduleOfMedical.getEndDateParams());
                if (dates != null && dates.size() > 0) {
                    scheduleOfMedical.setRecordDates(dates);
                }
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根据科室和日期查询所有的排班成功");
            List<ScheduleOfMedical> scheduleOfMedicals = scheduleOfMedicalService.queryByCommonParams(scheduleOfMedical);
            fitScheduleOfMedical(scheduleOfMedicals);
            resultMap.put("data", scheduleOfMedicals);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据科室和日期查询所有的排班失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据科室和日期查询所有的排班失败");
            return resultMap;
        }
    }


    /**
     * 预约排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/applyScheduleOfMedical", method = {RequestMethod.POST})
    public Map<String, Object> applyScheduleOfMedical(@RequestBody ScheduleOfMedicalApply scheduleOfMedicalApply, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedicalApply == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "预约排班失败,参数为空");
                return resultMap;
            }
            if (scheduleOfMedicalApply.getScheduleId() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "预约排班失败,需要被预约的排班id为空");
                return resultMap;
            }
            ScheduleOfMedical scheduleOfMedical= scheduleOfMedicalService.queryScheduleOfMedicalById(scheduleOfMedicalApply.getScheduleId());
            if(scheduleOfMedical==null){
                resultMap.put("code", -1);
                resultMap.put("message", "预约排班失败,需要被预约的排班不存在");
                return resultMap;
            }
            //消化内镜科   心电图检查不合格
            if (XHNK.equals(scheduleOfMedical.getDepNo())) {
                if( YesNoEnum.YES.code.equals(scheduleOfMedicalApply.getXdtjcjg())){
                    resultMap.put("code", -1);
                    resultMap.put("message", "保存医技排班失败,心电图检查结果不正常，无法进行预约");
                    return resultMap;
                }
            }else {
                scheduleOfMedicalApply.setXdtjcjg(null);
            }
            if (StringUtils.isBlank(scheduleOfMedicalApply.getActOrderNo())) {
                resultMap.put("code", -1);
                resultMap.put("message", "预约排班失败,医嘱号为空");
                return resultMap;
            }
            List<YshYjReq> yshYjReqs= yshYjReqService.queryYshYjReqsBYActOrderNo(scheduleOfMedicalApply.getActOrderNo());
            if(yshYjReqs==null || yshYjReqs.size()==0){
                resultMap.put("code", -1);
                resultMap.put("message", "预约排班失败,医嘱号查询不到对应的医嘱");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            resultMap.put("code", 0);
            resultMap.put("message", "预约排班成功");
            scheduleOfMedicalService.applyScheduleOfMedical(scheduleOfMedicalApply, tokenUser);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("预约排班失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "预约排班失败");
            return resultMap;
        }
    }



    /**
     * 取消预约排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/cancelApplyScheduleOfMedical", method = {RequestMethod.POST})
    public Map<String, Object> cancelApplyScheduleOfMedical(@RequestBody ScheduleOfMedical scheduleOfMedical, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "取消预约排班失败,参数为空");
                return resultMap;
            }
            if (scheduleOfMedical.getId() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "取消预约排班失败,需要被预约的排班id为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            resultMap.put("code", 0);
            resultMap.put("message", "取消预约排班成功");
            scheduleOfMedicalService.cancelApplyScheduleOfMedical(scheduleOfMedical.getId(), tokenUser);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("取消预约排班失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "取消预约排班失败");
            return resultMap;
        }
    }




    /**
     * 确认预约排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/confirmApplyScheduleOfMedical", method = {RequestMethod.POST})
    public Map<String, Object> confirmApplyScheduleOfMedical(@RequestBody ScheduleOfMedical scheduleOfMedical, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "确认预约排班失败,参数为空");
                return resultMap;
            }
            if (scheduleOfMedical.getId() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "确认预约排班失败,需要被预约的排班id为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            resultMap.put("code", 0);
            resultMap.put("message", "确认预约排班成功");
            scheduleOfMedical.setStatus(ScheduleOfMedicalEnum.ALREADY.code);
            scheduleOfMedicalService.modifyScheduleOfMedical(scheduleOfMedical, tokenUser.getId());

            //发送取消预约提示消息
            ScheduleOfMedical dbScheduleOfMedical=scheduleOfMedicalService.queryScheduleOfMedicalById(scheduleOfMedical.getId());
            ScheduleOfMedicalApply  scheduleOfMedicalApply= scheduleOfMedicalApplyServicve.queryByScheduleId(scheduleOfMedical.getId());
            User applyUser=userService.queryUserById(scheduleOfMedicalApply.getCreateId());
            //手机自助预约
            if (Constants.BRZZJF_CODE.equals(applyUser.getUserIdCode())){
                MzYjReq mzYjReq= mzYjReqService.queryMzYjReqByReqNo(scheduleOfMedicalApply.getReqNo(scheduleOfMedicalApply.getActOrderNo()));
                if(mzYjReq!=null){
                    MzPatientMi mzPatientMi=mzPatientMiService.queryByPatientId(mzYjReq.getPatientId());
                    String first= "您已成功预约医技申请";
                    if(StringUtils.isNotBlank(dbScheduleOfMedical.getJcxm())){
                        first = "您已成功预约"+dbScheduleOfMedical.getJcxm();
                    }
                    haiCiAdapterService.commonMessageNotice(mzPatientMi.getIcCardNo(), "{\"data\":{\"first\":{\"color\":\"#FF0000\",\"value\":\""+first+"\"},\"keyword1\":{\"color\":\"#173177\",\"value\":\"" + mzPatientMi.getName() + "\"},\"keyword2\":{\"color\":\"#173177\",\"value\":\"\"},\"keyword3\":{\"color\":\"#173177\",\"value\":\"" + dbScheduleOfMedical.getRecordDate()+" "+dbScheduleOfMedical.getBeginTime()+"-"+dbScheduleOfMedical.getRecordDate()+" "+dbScheduleOfMedical.getEndTime() + "\"},\"keyword4\":{\"color\":\"#173177\",\"value\":\"\"}},\"remark\":{\"color\":\"#FF0000\",\"value\":\" 您已成功预约医技检查,请您按时至科室进行医技检查！\"},\"miniprogram\":{\"appid\":\"\",\"pagepath\":\"\"},\"template_id\":\"jIctKOo_Jl9CmoXe-fK54e6z6sScZfb1jN22uKyh3IY\",\"touser\":\"\",\"url\":\"\"}", new Date());
                }
                return resultMap;
            }
            //住院科室预约
            if(applyUser!=null && StringUtils.isNotBlank(applyUser.getUserCode())){
                ZdUnitCode zdUnitCode= zdUnitCodeService.queryByCode(dbScheduleOfMedical.getDepNo());
                String message= "您申请的科室【"+zdUnitCode.getName()+"】,日期【"+dbScheduleOfMedical.getRecordDate()+"】,开始时间【"+dbScheduleOfMedical.getBeginTime()+"】,结束时间【"+dbScheduleOfMedical.getEndTime()+"】对应的医技排班已经被";
                Employee employee= employeeService.queryByUserCode(tokenUser.getUserIdCode());
                if(employee!=null && StringUtils.isNotBlank(employee.getDeptCode())){
                    zdUnitCode= zdUnitCodeService.queryByCode(employee.getDeptCode());
                    message+="【"+zdUnitCode.getName()+"】【"+tokenUser.getUserName()+"】审核通过，请您按时就诊检查！";
                }
                messageService.sendWxMessage(applyUser.getUserCode(),message);
            }
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("确认预约排班失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "确认预约排班失败");
            return resultMap;
        }
    }


    /**
     * 驳回预约排班
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/rejectApplyScheduleOfMedical", method = {RequestMethod.POST})
    public Map<String, Object> rejectApplyScheduleOfMedical(@RequestBody ScheduleOfMedical scheduleOfMedical, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "驳回预约排班失败,参数为空");
                return resultMap;
            }
            if (scheduleOfMedical.getId() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "驳回预约排班失败,需要被预约的排班id为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            resultMap.put("code", 0);
            resultMap.put("message", "驳回预约排班成功");
            scheduleOfMedical.setStatus(ScheduleOfMedicalEnum.FREE.code);
            scheduleOfMedicalService.rejectApplyScheduleOfMedical(scheduleOfMedical, tokenUser);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("驳回预约排班失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "驳回预约排班失败");
            return resultMap;
        }
    }



    /**
     * 根据预约主键查询申请科室或者申请人信息
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getApplyInfoByScheduleId", method = {RequestMethod.GET})
    public Map<String, Object> getApplyInfoByScheduleId(@RequestParam("scheduleId") Long scheduleId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleId == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据预约主键查询申请科室或者申请人信息失败,参数为空");
                return resultMap;
            }

            resultMap.put("code", 0);
            resultMap.put("message", "根据预约主键查询申请科室或者申请人信息成功");
            ScheduleOfMedicalApply scheduleOfMedicalApply=scheduleOfMedicalApplyServicve.queryByScheduleId(scheduleId);
            if(scheduleOfMedicalApply==null){
                resultMap.put("code", -1);
                resultMap.put("message", "根据预约主键查询申请科室或者申请人信息失败,没有找到对应的申请记录");
                return resultMap;
            }
            ScheduleOfMedical scheduleOfMedical= scheduleOfMedicalService.queryScheduleOfMedicalById(scheduleId);
            if(scheduleOfMedical==null){
                resultMap.put("code", -1);
                resultMap.put("message", "根据预约主键查询申请科室或者申请人信息失败,没有找到对应的排班记录");
                return resultMap;
            }
            resultMap.put("scheduleOfMedical", scheduleOfMedical);
            if(StringUtils.isNotBlank(scheduleOfMedicalApply.getCreateDeptNo())){
                scheduleOfMedicalApply.setCreateDeptName(zdUnitCodeService.queryDeptNameByIdInCache(scheduleOfMedicalApply.getCreateDeptNo()));
            }
            if(scheduleOfMedicalApply.getCreateId()!=null){
                User user = userService.queryUserById(scheduleOfMedicalApply.getCreateId());
                if (user != null) {
                    scheduleOfMedicalApply.setCreateName(user.getUserName());
                }
            }
            resultMap.put("data", scheduleOfMedicalApply);
            if(StringUtils.isNotBlank(scheduleOfMedicalApply.getActOrderNo())){
                //门诊预约申请
                if(scheduleOfMedicalApply.getActOrderNo().startsWith(ScheduleOfMedicalApply.REQNO_PREFIX)){
                    String reqNo =scheduleOfMedicalApply.getActOrderNo().replaceAll(ScheduleOfMedicalApply.REQNO_PREFIX,"");
                    MzYjReq mzYjReq=mzYjReqService.queryMzYjReqByReqNo(reqNo);
                    resultMap.put("mzYjReq", mzYjReq);
                    MzPatientMi mzPatientMi=mzPatientMiService.queryByPatientId(mzYjReq.getPatientId());
                    resultMap.put("gender", GenderEnum.getGenderByCode(mzPatientMi.getSex()).name);
                }else {
                    //住院医技申请
                    List<YshYjReq>  yshYjReqs=yshYjReqService.queryYshYjReqsBYActOrderNo(scheduleOfMedicalApply.getActOrderNo());
                    if(yshYjReqs!=null && yshYjReqs.size()>0){
                        YshYjReq firstYshYjReq= yshYjReqs.get(0);
                        APatientMi aPatientMi=aPatientMiService.queryPatientMiByInPatientNo(firstYshYjReq.getInpatientNo());
                        if(aPatientMi!=null){
                            firstYshYjReq.setGender(GenderEnum.WOMAN.code.toString().equals(aPatientMi.getSex())?GenderEnum.WOMAN.name:GenderEnum.MAN.name);
                        }
                        if(StringUtils.isNotBlank(firstYshYjReq.getReqWard())){
                            firstYshYjReq.setReqWardName(zdUnitCodeService.queryDeptNameByIdInCache(firstYshYjReq.getReqWard()));
                        }
                        resultMap.put("yshYjReq", firstYshYjReq);
                        List<String> groupNames= new ArrayList<>();
                        for(YshYjReq yr:yshYjReqs){
                            groupNames.add(yr.getGroupName());
                        }
                        resultMap.put("groupNames", groupNames);
                    }
                }
            }
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据预约主键查询申请科室或者申请人信息失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据预约主键查询申请科室或者申请人信息失败");
            return resultMap;
        }
    }




    /**
     * 根据选定日期和当前登陆人员的id查询科室列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getDeptNos", method = {RequestMethod.POST})
    public Map<String, Object> getDeptNos(@RequestBody ScheduleOfMedical scheduleOfMedical, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleOfMedical == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据选定日期和当前登陆人员的id查询科室列表失败,参数为空");
                return resultMap;
            }
            if (scheduleOfMedical.getBeginDateParams() != null && scheduleOfMedical.getEndDateParams() != null) {
                List<String> dates = DateUtil.getBetweenDatesDesc(scheduleOfMedical.getBeginDateParams(), scheduleOfMedical.getEndDateParams());
                if (dates != null && dates.size() > 0) {
                    scheduleOfMedical.setRecordDates(dates);
                }
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            Employee employee=employeeService.queryByUserCode(tokenUser.getUserIdCode());
            if(employee!=null){
                if(!"3100000".equals(employee.getDeptCode())){
                    scheduleOfMedical.setDepNo(employee.getDeptCode());
                }else {
                    scheduleOfMedical.setDepNo(null);
                }
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根据选定日期和当前登陆人员的id查询科室列表成功");
            List<String> deptNos = scheduleOfMedicalService.queryDeptNos(scheduleOfMedical);
            if(deptNos!=null && deptNos.size()>0){
                List<ZdUnitCode> zdUnitCodes=zdUnitCodeService.queryZdUnitCodeByCodes(deptNos);
                resultMap.put("data", zdUnitCodes);
            }
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据选定日期和当前登陆人员的id查询科室列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据选定日期和当前登陆人员的id查询科室列表失败");
            return resultMap;
        }
    }


}
