package cn.hnthyy.thmz.controller.api;

import cn.hnthyy.thmz.Utils.DateUtil;
import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.controller.mz.ScheduleOfMedicalController;
import cn.hnthyy.thmz.entity.his.mz.MzYjReq;
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.YesNoEnum;
import cn.hnthyy.thmz.service.his.mz.MzYjReqService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import cn.hnthyy.thmz.service.thmz.ScheduleOfMedicalService;
import cn.hnthyy.thmz.service.thmz.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/api/v1")
public class ScheduleOfMedicalApiController {
    @Autowired
    private ScheduleOfMedicalService scheduleOfMedicalService;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;
    @Autowired
    private MzYjReqService mzYjReqService;
    @Autowired
    private UserService userService;


    /**
     * 预约排班 门诊预约
     *
     * @return
     */
    @RequestMapping(value = "/applyScheduleOfMedicalForPlatform", method = {RequestMethod.POST})
    public Map<String, Object> applyScheduleOfMedicalForPlatform(@RequestBody ScheduleOfMedicalApply scheduleOfMedicalApply) {
        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 (ScheduleOfMedicalController.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;
            }
            MzYjReq mzYjReq= mzYjReqService.queryMzYjReqByReqNo(scheduleOfMedicalApply.getActOrderNo());
            if(mzYjReq==null){
                resultMap.put("code", -1);
                resultMap.put("message", "预约排班失败,医技申请单号查询不到对应的医技申请单");
                return resultMap;
            }
            scheduleOfMedicalApply.setActOrderNo(ScheduleOfMedicalApply.REQNO_PREFIX+scheduleOfMedicalApply.getActOrderNo());
            User tokenUser = userService.queryUserByUserIdCode(Constants.BRZZJF_CODE);
            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
     */
    @RequestMapping(value = "/getFullScheduleOfMedicalForPlatform", method = {RequestMethod.GET})
    public Map<String, Object> getFullScheduleOfMedicalForPlatform(@RequestParam("beginTime")String beginTime,@RequestParam("endTime")String endTime,@RequestParam("depNo")String depNo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(depNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "预约科室不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(beginTime)) {
                resultMap.put("code", -1);
                resultMap.put("message", "预约的开始日期不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(endTime)) {
                resultMap.put("code", -1);
                resultMap.put("message", "预约的结束日期不能为空");
                return resultMap;
            }
            ScheduleOfMedical scheduleOfMedical =new ScheduleOfMedical();
            scheduleOfMedical.setDepNo(depNo);
            List<String> dates = DateUtil.getBetweenDatesDesc(DateUtil.pase(beginTime,"yyyy-MM-dd"), DateUtil.pase(endTime,"yyyy-MM-dd"));
            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
     */
    @RequestMapping(value = "/cancelApplyScheduleOfMedical", method = {RequestMethod.GET})
    public Map<String, Object> cancelApplyScheduleOfMedical(@RequestParam("scheduleId") Long scheduleId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (scheduleId == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "取消预约排班失败,需要被预约的排班id为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "取消预约排班成功");
            User tokenUser = userService.queryUserByUserIdCode(Constants.BRZZJF_CODE);
            scheduleOfMedicalService.cancelApplyScheduleOfMedical(scheduleId, tokenUser);
            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);
                }
            }
        });
    }

}
