package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.PyWbUtil;
import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.thmz.TCommonParams;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.ParamsTypeEnum;
import cn.hnthyy.thmz.service.his.mz.*;
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 javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@RestController
public class TCommonParamsController {
    @Autowired
    private TCommonParamsService tCommonParamsService;


    /**
     * 保存通用参数
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveTCommonParams", method = {RequestMethod.POST})
    public Map<String, Object> saveTCommonParams(@RequestBody TCommonParams tCommonParams, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (tCommonParams == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存通用参数失败,参数为空");
                return resultMap;
            }
            if (tCommonParams.getParamsType() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存通用参数失败,参数类型为空");
                return resultMap;
            }
            if (StringUtils.isBlank(tCommonParams.getParamsName())) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存通用参数失败,参数名称为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            TCommonParams tCommonParamsDb = tCommonParamsService.queryByParamsName(tCommonParams.getParamsName(), tCommonParams.getParamsType(), tokenUser.getUserIdCode());
            if (tCommonParamsDb != null) {
                resultMap.put("code", -1);
                resultMap.put("message", "保存通用参数失败,已经存在名称相同且类型相同的的参数");
                return resultMap;
            }
            tCommonParams.setWbCode(PyWbUtil.getWBCode(tCommonParams.getParamsName()));
            tCommonParams.setPyCode(PyWbUtil.toBigFirstChar(tCommonParams.getParamsName()));
            tCommonParams.setCreateIdCode(tokenUser.getUserIdCode());
            tCommonParams.setUsedTimes(0L);
            tCommonParamsService.saveTCommonParams(tCommonParams);
            resultMap.put("code", 0);
            resultMap.put("message", "保存通用参数成功");
            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 = "/getTCommonParamsById", method = {RequestMethod.GET})
    public Map<String, Object> getTCommonParamsById(@RequestParam("id") Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (id == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据id查询通用参数失败,参数为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根据id查询通用参数成功");
            resultMap.put("data", tCommonParamsService.queryById(id));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据id查询通用参数失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据id查询通用参数失败");
            return resultMap;
        }
    }


    /**
     * 查询通用参数
     *
     * @param paramsType         参数类型
     * @param commonParams       通用查询参数
     * @param httpServletRequest
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getTCommonParamsByParamsType", method = {RequestMethod.GET})
    public Map<String, Object> getTCommonParamsByParamsType(@RequestParam("paramsType") Integer paramsType, @RequestParam("commonParams") String commonParams, @RequestParam("pageSize") Integer pageSize, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (paramsType == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询通用参数失败,参数类型为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            List<TCommonParams> tCommonParamsList = tCommonParamsService.queryTCommonParamsByParamsType(commonParams, paramsType, pageSize, tokenUser.getUserIdCode());
            resultMap.put("code", 0);
            resultMap.put("data", tCommonParamsList);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询通用参数失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询通用参数失败");
            return resultMap;
        }
    }


    /**
     * 查询主诉模块所有通用参数
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/loadSymptom", method = {RequestMethod.GET})
    public Map<String, Object> loadSymptom(HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            List<TCommonParams> featureList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.FEATURE.code, tokenUser.getUserIdCode());
            resultMap.put("featureList", featureList);
            List<TCommonParams> timeList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.COMMONLY_TIME.code, tokenUser.getUserIdCode());
            resultMap.put("timeList", timeList);
            List<TCommonParams> characterList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.COMMONLY_PUNCTUATION.code, tokenUser.getUserIdCode());
            resultMap.put("characterList", characterList);
            List<TCommonParams> wordsList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.GENERAL_SERVICE_WORD.code, tokenUser.getUserIdCode());
            resultMap.put("wordsList", wordsList);
            resultMap.put("code", 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 = "/loadHealthCheckup", method = {RequestMethod.GET})
    public Map<String, Object> loadHealthCheckup(HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            List<TCommonParams> weightList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.WEIGHT.code, tokenUser.getUserIdCode());
            resultMap.put("weightList", weightList);
            List<TCommonParams> temperatureList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.ANIMAL_HEAT.code, tokenUser.getUserIdCode());
            resultMap.put("temperatureList", temperatureList);
            List<TCommonParams> sphygmusList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.PULSE.code, tokenUser.getUserIdCode());
            resultMap.put("sphygmusList", sphygmusList);
            List<TCommonParams> breatheList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.BREATHE.code, tokenUser.getUserIdCode());
            resultMap.put("breatheList", breatheList);
            List<TCommonParams> pressureList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.BLOOD_PRESSURE.code, tokenUser.getUserIdCode());
            resultMap.put("pressureList", pressureList);
            resultMap.put("code", 0);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("加载体格检查参数列表失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "加载体格检查参数列表失败");
            return resultMap;
        }
    }


    /**
     * 初始化诊断记录
     *
     * @return
     */
    @RequestMapping(value = "/initDiagnose", method = {RequestMethod.GET})
    public Map<String, Object> initDiagnose() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<TCommonParams> tCommonParamsList = tCommonParamsService.queryTCommonParamsByParamsType(ParamsTypeEnum.DIAGNOSE.code, Constants.QYZH_CODE);
            if (tCommonParamsList != null && tCommonParamsList.size() > 0) {
                resultMap.put("code", 0);
                resultMap.put("message", "已经初始化诊断记录,请勿重复操作");
            }
            tCommonParamsService.initDiagnose();
            log.info("=====>>>>>全部初始化完成");
            resultMap.put("code", 0);
            resultMap.put("message", "已经初始化诊断记录成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("初始化诊断记录失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "初始化诊断记录失败");
            return resultMap;
        }
    }


    /**
     * 初始化主诉记录
     *
     * @return
     */
    @RequestMapping(value = "/initEmrChiefComplaint", method = {RequestMethod.GET})
    public Map<String, Object> initEmrChiefComplaint() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            tCommonParamsService.initEmrChiefComplaint();
            log.info("=====>>>>>全部初始化完成");
            resultMap.put("code", 0);
            resultMap.put("message", "已经初始化主诉记录成功");
            return resultMap;
        } catch (MzException e) {
            resultMap.put("code", 0);
            resultMap.put("message", e.getMessage());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("初始化主诉记录失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "初始化主诉记录失败");
            return resultMap;
        }
    }


    /**
     * 初始化现病史记录
     *
     * @return
     */
    @RequestMapping(value = "/initEmrHpi", method = {RequestMethod.GET})
    public Map<String, Object> initEmrHpi() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            tCommonParamsService.initEmrHpi();
            log.info("=====>>>>>全部初始化完成");
            resultMap.put("code", 0);
            resultMap.put("message", "已经初始化现病史记录成功");
            return resultMap;
        } catch (MzException e) {
            resultMap.put("code", 0);
            resultMap.put("message", e.getMessage());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("初始化现病史记录失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "初始化现病史记录失败");
            return resultMap;
        }
    }


    /**
     * 初始化既往史记录
     *
     * @return
     */
    @RequestMapping(value = "/initEmrPs", method = {RequestMethod.GET})
    public Map<String, Object> initEmrPs() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            tCommonParamsService.initEmrPs();
            log.info("=====>>>>>全部初始化完成");
            resultMap.put("code", 0);
            resultMap.put("message", "已经初始化既往史记录成功");
            return resultMap;
        } catch (MzException e) {
            resultMap.put("code", 0);
            resultMap.put("message", e.getMessage());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("初始化既往史记录失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "初始化既往史记录失败");
            return resultMap;
        }
    }
}
