package cn.hnthyy.thmz.controller;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.MzZyReq;
import cn.hnthyy.thmz.enums.ClassCodeEnum;
import cn.hnthyy.thmz.service.his.mz.*;
import cn.hnthyy.thmz.service.his.zd.ZdChargeItemService;
import cn.hnthyy.thmz.service.his.zd.ZdMaritalStatusService;
import cn.hnthyy.thmz.service.his.zd.ZdMzClassService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 CommonController {
    @Autowired
    private ZdMzClassService zdMzClassService;
    @Autowired
    private MzZdWorkTimeService mzZdWorkTimeService;
    @Autowired
    private MzZdDeptNoService mzZdDeptNoService;
    @Autowired
    private ZdChargeItemService zdChargeItemService;
    @Autowired
    private MzBillItemService mzBillItemService;
    @Autowired
    private ZdMaritalStatusService zdMaritalStatusService;
    @Autowired
    private MzZdCommonService mzZdCommonService;
    @Autowired
    private MzZyReqService mzZyReqService;

    /**
     * 查询科室分类
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/listClassCode", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> listClassCode() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("message", "查询科室分类成功");
        List<Map<String, Object>> data = new ArrayList<>();
        Arrays.stream(ClassCodeEnum.values()).forEach(classCodeEnum -> {
            Map<String, Object> temp = new HashMap<>();
            temp.put("code", classCodeEnum.code);
            temp.put("name", classCodeEnum.name);
            data.add(temp);
        });
        resultMap.put("data", data);
        return resultMap;
    }


    /**
     * 查询门诊分类码
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getMzClass", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getMzClass() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询门诊分类码成功");
            resultMap.put("data", zdMzClassService.queryAllZdMzClass());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询门诊分类码失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询门诊分类码失败");
            return resultMap;
        }
    }


    /**
     * 查询所有可以使用的门诊挂号时间区间
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getMzWorkTime", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getMzWorkTime() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询门诊挂号时间区间成功");
            resultMap.put("data", mzZdWorkTimeService.queryAllMzZdWorkTime());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询门诊挂号时间区间失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询门诊挂号时间区间失败");
            return resultMap;
        }
    }


    /**
     * 查询所有院区列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getMzZdDeptNos", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getMzZdDeptNos() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有院区列表成功");
            resultMap.put("data", mzZdDeptNoService.queryMzZdDeptNos());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有院区列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有院区列表失败");
            return resultMap;
        }
    }


    /**
     * 查询所有的核算码名称
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAllHsmName", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getAllHsmName() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的核算码名称成功");
            resultMap.put("data", zdChargeItemService.queryAllHsmName());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有的核算码名称失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有的核算码名称失败");
            return resultMap;
        }
    }


    /**
     * 查询所有门诊收费类型
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAllBillItems", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getAllBillItems() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有收费类型成功");
            resultMap.put("data", mzBillItemService.queryMzBillItem());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有收费类型失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有收费类型失败");
            return resultMap;
        }
    }


    /**
     * 查询所有父类码
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAllParentCodes", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getAllParentCodes() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有父类码成功");
            resultMap.put("data", mzBillItemService.queryZyBillItem());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有父类码失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有父类码失败");
            return resultMap;
        }
    }


    /**
     * 获取所有的婚姻状态
     */
    @UserLoginToken
    @RequestMapping(value = "/queryAllMaritalStatus", method = {RequestMethod.GET})
    public Map<String, Object> queryAllMaritalStatus() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("message", "查询科室分类成功");
        resultMap.put("data", zdMaritalStatusService.queryAll());
        return resultMap;
    }


    /**
     * 获取所有的国家列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getCountry", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getCountry() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "获取所有的国家列表成功");
            resultMap.put("data", mzZdCommonService.queryCountry());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取所有的国家列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "获取所有的国家列表失败");
            return resultMap;
        }
    }


    /**
     * 查询所有的民族列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getNation", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getNation() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的民族列表成功");
            resultMap.put("data", mzZdCommonService.queryNation());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有的民族列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有的民族列表失败");
            return resultMap;
        }
    }


    /**
     * 查询所有的诊断列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getDiagnose", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getDiagnose() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的诊断列表成功");
            resultMap.put("data", mzZdCommonService.queryDiagnose());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有的诊断列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有的诊断列表失败");
            return resultMap;
        }
    }


    /**
     * 查询所有的省份列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getDistrict", method = {RequestMethod.GET})
    public Map<String, Object> getDistrict() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的省份列表成功");
            resultMap.put("data", mzZdCommonService.queryDistrict());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有的省份列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有的省份列表失败");
            return resultMap;
        }
    }


    /**
     * 查询所有的关系列表
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getRelation", method = {RequestMethod.GET})
    public Map<String, Object> getRelation() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的关系列表成功");
            resultMap.put("data", mzZdCommonService.queryRelation());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有的关系列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有的关系列表失败");
            return resultMap;
        }
    }

    /**
     * 查询所有的职业
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getOccupation", method = {RequestMethod.GET})
    public Map<String, Object> getOccupation() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的职业列表成功");
            resultMap.put("data", mzZdCommonService.queryOccupation());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有的职业列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有的职业列表失败");
            return resultMap;
        }
    }


    /**
     * 查询所有的入院状态
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAdmissStatus", method = {RequestMethod.GET})
    public Map<String, Object> getAdmissStatus() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的入院状态列表成功");
            resultMap.put("data", mzZdCommonService.queryAdmissStatus());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有的入院状态列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有的入院状态列表失败");
            return resultMap;
        }
    }


    /**
     * 查询病人的住院证
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getMzZyReqByPatientId", method = {RequestMethod.GET})
    public Map<String, Object> getMzZyReqByPatientId(@RequestParam("patientId") String patientId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(patientId)) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询病人的住院证失败,病人id为空");
                return resultMap;
            }
            List<MzZyReq> list = mzZyReqService.queryMzZyReqByPatientId(patientId);
            if (list == null || list.size() == 0) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询病人的住院证失败,当前病人没有对应的住院证");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询病人的住院证成功");
            resultMap.put("data", list.get(0));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询病人的住院证失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询病人的住院证失败");
            return resultMap;
        }
    }


    /**
     * 查询所有住院收费码
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getZyBillItems", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getZyBillItems() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有住院收费码");
            resultMap.put("data", mzBillItemService.queryZyBillItemOri());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有住院收费码失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有住院收费码失败");
            return resultMap;
        }
    }
}
