package cn.hnthyy.thmz.controller;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.Employee;
import cn.hnthyy.thmz.entity.his.MzyRequest;
import cn.hnthyy.thmz.entity.his.ZdUnitCode;
import cn.hnthyy.thmz.service.his.EmployeeService;
import cn.hnthyy.thmz.service.his.MzyRequestService;
import cn.hnthyy.thmz.service.his.ZdUnitCodeService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
public class MzyRequestController {
    @Autowired
    private MzyRequestService mzyRequestService;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 按日期和时间区间查询可以挂号的科室
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getUnitCodeByRequestDay", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getUnitCodeByRequestDay(@RequestParam("ampm") String ampm) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(ampm)) {
                resultMap.put("code", -1);
                resultMap.put("message", "挂号时间区间不能为空");
                return resultMap;
            }
            List<String> unitCodes = mzyRequestService.queryUnitCodeByRequestDay(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), ampm);
            List<ZdUnitCode> data = new ArrayList<>();
            if (unitCodes != null && unitCodes.size() > 0) {
                unitCodes.forEach(u -> {
                    if (StringUtils.isNotBlank(u)) {
                        ZdUnitCode zdUnitCode = new ZdUnitCode(u, zdUnitCodeService.queryDeptNameByIdInCache(u));
                        data.add(zdUnitCode);
                    }
                });
            }
            resultMap.put("code", 0);
            resultMap.put("message", "按日期和时间区间查询可以挂号的科室成功");
            resultMap.put("data", data);
            return resultMap;
        } catch (Exception e) {
            log.error("按日期和时间区间查询可以挂号的科室失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "按日期和时间区间查询可以挂号的科室失败");
            return resultMap;
        }
    }

    /**
     * 按日期和时间区间以及部门编码查询可以挂号的号别
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getChargeTypeByRequestDay", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getChargeTypeByRequestDay(@RequestParam("unitCode") String unitCode, @RequestParam("ampm") String ampm) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(unitCode)) {
                resultMap.put("code", -1);
                resultMap.put("message", "挂号科室不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(ampm)) {
                resultMap.put("code", -1);
                resultMap.put("message", "挂号时间区间不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "按日期和时间区间以及部门编码查询可以挂号的号别成功");
            resultMap.put("data", mzyRequestService.queryChargeTypeByRequestDay(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), unitCode, ampm));
            return resultMap;
        } catch (Exception e) {
            log.error("按日期和时间区间以及部门编码查询可以挂号的号别失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "按日期和时间区间以及部门编码查询可以挂号的号别失败");
            return resultMap;
        }
    }



    /**
     * 按日期和时间区间医技科室id查询可以挂号的医生
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getDoctorCodeByRequestDay", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getDoctorCodeByRequestDay(@RequestParam("unitCode") String unitCode, @RequestParam("ampm") String ampm, @RequestParam("chargeType") String chargeType) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(unitCode)) {
                resultMap.put("code", -1);
                resultMap.put("message", "挂号科室不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(ampm)) {
                resultMap.put("code", -1);
                resultMap.put("message", "挂号时间区间不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(chargeType)) {
                resultMap.put("code", -1);
                resultMap.put("message", "号别不能为空");
                return resultMap;
            }
            List<MzyRequest> mzyRequests = mzyRequestService.queryDoctorCodeByRequestDay(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), unitCode, ampm, chargeType);
            List<Employee> data = new ArrayList<>();
            if (mzyRequests != null && mzyRequests.size() > 0) {
                mzyRequests.forEach(d -> {
                    if (d!=null) {
                        Employee employee =null;
                        if (StringUtils.isNotBlank(d.getDoctorCode())) {
                            employee = employeeService.queryByUserCode(d.getDoctorCode());
                            if(employee==null){
                                log.error("没有查询到对应的医生，医生编码是{}",d.getDoctorCode());
                            }
                        }else {
                            employee = new Employee();
                            employee.setEmployeeCode("-1");
                            employee.setEmployeeName("无需选择医生");
                        }
                        if(employee!=null){
                            employee.setChargeType(d.getChargeType());
                            employee.setCheckFee(d.getCheckFee());
                            data.add(employee);
                        }
                    }
                });
            }
            resultMap.put("code", 0);
            resultMap.put("message", "按日期和时间区间医技科室id查询可以挂号的医生成功");
            resultMap.put("data", data);
            return resultMap;
        } catch (Exception e) {
            log.error("按日期和时间区间医技科室id查询可以挂号的医生失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "按日期和时间区间医技科室id查询可以挂号的医生失败");
            return resultMap;
        }
    }
}
