package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.PyWbUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.zd.MzZdInstructionCode;
import cn.hnthyy.thmz.pageDto.MzZdInstructionCodePageDto;
import cn.hnthyy.thmz.service.his.zd.MzZdInstructionCodeService;
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
public class MzZdInstructionCodeController {
    @Autowired
    private MzZdInstructionCodeService mzZdInstructionCodeService;

    /**
     * 根据嘱托类型查询嘱托
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getMzZdInstructionByType", method = {RequestMethod.GET})
    public Map<String, Object> getMzZdInstructionByType(@RequestParam("instructionType") Integer instructionType) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (instructionType == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "根据嘱托类型查询嘱托失败,嘱托类型为空");
                return resultMap;
            }
            List<MzZdInstructionCode> list = mzZdInstructionCodeService.queryMzZdInstructionByType(instructionType);
            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);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据嘱托类型查询嘱托失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据嘱托类型查询嘱托失败");
            return resultMap;
        }
    }

    /**
     * 查询嘱托
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getMzZdInstruction", method = {RequestMethod.POST})
    public Map<String, Object> getMzZdInstruction(@RequestBody MzZdInstructionCodePageDto mzZdInstructionCodePageDto) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (mzZdInstructionCodePageDto.getMzZdInstructionCode() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询嘱托失败,参数为空");
                return resultMap;
            }
            if(mzZdInstructionCodePageDto.getSearchText() != null){
                String searchText = mzZdInstructionCodePageDto.getSearchText();
                if(searchText.matches("[a-zA-Z]+")){
                    mzZdInstructionCodePageDto.setSearchText(searchText.toUpperCase());
                }
            }
            List<MzZdInstructionCode> list = mzZdInstructionCodeService.queryMzZdInstructionPage(mzZdInstructionCodePageDto);
            int total = mzZdInstructionCodeService.queryMzZdInstructionCount(mzZdInstructionCodePageDto);
            resultMap.put("code", 0);
            resultMap.put("message", "查询嘱托成功");
            resultMap.put("data", list);
            resultMap.put("total", total);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询嘱托失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询嘱托失败");
            return resultMap;
        }
    }

    /**
     * 保存嘱托
     * @param mzZdInstructionCode
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveMzZdInstruction",method = {RequestMethod.POST})
    public Map<String,Object> saveMzZdInstruction(@RequestBody MzZdInstructionCode mzZdInstructionCode){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(mzZdInstructionCode==null){
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(mzZdInstructionCode.getDelFlag())){
                resultMap.put("code", -1);
                resultMap.put("message", "嘱托状态不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(mzZdInstructionCode.getInstructionText())){
                resultMap.put("code", -1);
                resultMap.put("message", "嘱托内容不能为空");
                return resultMap;
            }
            if(mzZdInstructionCode.getInstructionType() == null){
                resultMap.put("code", -1);
                resultMap.put("message", "嘱托类型不能为空");
                return resultMap;
            }
            mzZdInstructionCode.setPyCode(PyWbUtil.toBigFirstChar(mzZdInstructionCode.getInstructionText()));
            mzZdInstructionCode.setWbCode(PyWbUtil.getWBCode(mzZdInstructionCode.getInstructionText()));
            mzZdInstructionCode.setDelFlag("0");
            if(StringUtils.isBlank(mzZdInstructionCode.getCode())){
                int num=mzZdInstructionCodeService.saveMzZdInstructionCode(mzZdInstructionCode);
                if(num==0){
                    resultMap.put("code", -1);
                    resultMap.put("message", "保存失败，请重新操作或者联系管理员");
                    return resultMap;
                }
            }else {
                //修改
                int num = mzZdInstructionCodeService.modifyMzZdInstructionCode(mzZdInstructionCode);
                if(num>=0){
                    resultMap.put("code", 0);
                    resultMap.put("message", "保存嘱托成功");
                    return resultMap;
                }
            }
            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;
        }
    }

    /**
     * 更新嘱托状态
     * @param code
     * @param delFlag
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/updateDelFlag",method = {RequestMethod.GET})
    public Map<String,Object> updateDelFlag(@RequestParam("code") String code,@RequestParam("delFlag") Integer delFlag){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(delFlag == null){
                resultMap.put("code", -1);
                resultMap.put("message", "嘱托状态不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(code)){
                resultMap.put("code", -1);
                resultMap.put("message", "嘱托排序码不能为空");
                return resultMap;
            }
            int num=mzZdInstructionCodeService.modifyDelFlag(code,delFlag);
            if(num==0){
                resultMap.put("code", -1);
                resultMap.put("message", (delFlag==0?"启用":"停用")+"失败，请重新操作或者联系管理员");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", (delFlag==0?"启用":"停用")+"嘱托成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error((delFlag==0?"启用":"停用")+"嘱托失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", (delFlag==0?"启用":"停用")+"嘱托失败");
            return resultMap;
        }
    }
}
