package cn.hnthyy.thmz.controller.zy;

import cn.hnthyy.thmz.Utils.IDCardUtil;
import cn.hnthyy.thmz.Utils.StringUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.zy.APatientMi;
import cn.hnthyy.thmz.service.his.zy.APatientMiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class APatientMiController {
    @Autowired
    private APatientMiService aPatientMiService;






    /**
     * 根据病人住院号查询住院病人信息
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAPatientByNewInpatientNo", method = {RequestMethod.GET})
    public Map<String, Object> getAPatientByNewInpatientNo(@RequestParam("newInpatientNo") String newInpatientNo) {
        log.info("根据病人住院号查询住院病人信息");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(newInpatientNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人住院号不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询病人信息成功");
            resultMap.put("data", aPatientMiService.queryPatientMiByInPatientNo(newInpatientNo));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "根据病人住院号查询住院病人信息出错，请联系管理员");
            log.error("根据病人住院号查询住院病人信息异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }



    /**
     * 根据病人门诊id查询住院病人id
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAPatientByMzNo", method = {RequestMethod.GET})
    public Map<String, Object> getAPatientByMzNo(@RequestParam("mzNo") String mzNo) {
        log.info("根据病人门诊id查询住院病人id");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(mzNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人门诊id不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询病人信息成功");
            resultMap.put("data", aPatientMiService.queryAPatientByMzNo(mzNo));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "根据病人门诊id查询住院病人id出错，请联系管理员");
            log.error("根据病人门诊id查询住院病人id异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }




    /**
     * 保存住院病人信息
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveZyPatient", method = {RequestMethod.POST})
    public Map<String, Object> saveZyPatient(@RequestBody APatientMi aPatientMi) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (checkPatient(aPatientMi, resultMap)){
                return resultMap;
            }
            APatientMi aPatientMiDb=aPatientMiService.queryAPatientByMzNo(aPatientMi.getMzNo());
            int num=0;
            if(aPatientMiDb==null){
               //新增
                num=aPatientMiService.saveMzPatientMi(aPatientMi);
            }else {
                //修改
                num=aPatientMiService.modifyMzPatientMi(aPatientMi);
            }
            if(num==1){
                resultMap.put("code", 0);
                resultMap.put("message", "保存病人信息成功");
                resultMap.put("data",aPatientMi);
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "保存病人信息失败");
            return resultMap;
        } catch (Exception e) {
            if (e instanceof DataIntegrityViolationException){
                if(e.getMessage().contains("将截断字符串或二进制数据")){
                    resultMap.put("code", -1);
                    resultMap.put("message", "病人户口地址信息超长");
                    log.error("系统异常，错误信息{病人户口地址信息超长}");
                    return resultMap;
                }
            }
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", StringUtils.isBlank(e.getMessage())?"系统出错，请联系管理员":e.getMessage());
            log.error("系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }





    /**
     * 校验病人信息
     * @param aPatientMi
     * @param resultMap
     * @return
     */
    private boolean checkPatient(@RequestBody APatientMi aPatientMi, Map<String, Object> resultMap) {
        if (aPatientMi == null) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人信息不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getName())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人姓名不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getSex())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人性别不能为空");
            return true;
        }
        if (aPatientMi.getBirthDate() == null) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人出生日期不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getMzNo())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人门诊编号不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getHomeTel())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人手机号码不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getMarryCode())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人婚姻状态不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getOccupationCode())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人职业不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getCountry())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人国籍不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getNationCode())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人民族不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getBirthPlace())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人出生地址不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getAccountStreet())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人户口地址不能为空");
            return true;
        }
        if (aPatientMi.getInpatientNoType()==null) {
            resultMap.put("code", -1);
            resultMap.put("message", "住院证号码类型不能为空");
            return true;
        }
        if (StringUtils.isBlank(aPatientMi.getHomeStreet())) {
            aPatientMi.setHomeStreet("-");
        }
        if (StringUtils.isBlank(aPatientMi.getEmployerStreet())) {
            aPatientMi.setEmployerStreet("-");
        }
        if(StringUtils.isNotBlank(aPatientMi.getSocialNo()) && !aPatientMi.getSocialNo().startsWith("K")){
            //校验身份证号码格式
            String checkResult = IDCardUtil.IdentityCardVerification(aPatientMi.getSocialNo());
            if (StringUtils.isNotBlank(checkResult)) {
                resultMap.put("code", -1);
                resultMap.put("message", checkResult);
                return true;
            }
        }
        //校验手机号码格式
        String checkResult = StringUtil.isPhone(aPatientMi.getHomeTel());
        if (StringUtils.isNotBlank(checkResult)) {
            resultMap.put("code", -1);
            resultMap.put("message", checkResult);
            return true;
        }
        //如果紧急联系人电话不为空，校验紧急联系电话号码
        if(StringUtils.isNotBlank(aPatientMi.getRelationTel())){
            checkResult = StringUtil.isPhone(aPatientMi.getRelationTel());
            if(StringUtils.isNotBlank(checkResult)){
                resultMap.put("code", -1);
                resultMap.put("message", checkResult);
                return true;
            }
        }
        if (StringUtils.isBlank(aPatientMi.getSocialNo())) {
            resultMap.put("code", -1);
            resultMap.put("message", "病人身份证号码不能为空");
            return true;
        }
        return false;
    }

}
