package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.PyWbUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.MzyZdReqtype;
import cn.hnthyy.thmz.service.his.mz.MzyZdChargeTypeService;
import cn.hnthyy.thmz.service.his.mz.MzyZdReqtypeService;
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.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class MzyZdReqtypeController {
    @Autowired
    private MzyZdReqtypeService mzyZdReqtypeService;
    @Autowired
    private MzyZdChargeTypeService mzyZdChargeTypeService;

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

    /**
     * 删除号别类别
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/removeZdReqType", method = {RequestMethod.GET})
    public Map<String, Object> removeZdReqType(@RequestParam("code") String code) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(code)){
                resultMap.put("code", -1);
                resultMap.put("message", "号别类别编码不能为空");
                return resultMap;
            }
            int count=mzyZdChargeTypeService.queryCountByReqType(code);
            if(count>0){
                resultMap.put("code", -1);
                resultMap.put("message", "当前删除的号别类别有在使用，不能删除，如需删除，请先删除对应的号别");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "删除号别类别成功");
            resultMap.put("data", mzyZdReqtypeService.removeMzyZdReqtype(code));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除号别类别失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "删除号别类别失败");
            return resultMap;
        }
    }



    /**
     * 保存号别类别
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveMzyZdReqType", method = {RequestMethod.POST})
    public Map<String, Object> saveMzyZdReqType(@RequestBody MzyZdReqtype mzyZdReqtype) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(mzyZdReqtype==null){
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(mzyZdReqtype.getName())){
                resultMap.put("code", -1);
                resultMap.put("message", "类别名称不能为空");
                return resultMap;
            }
            mzyZdReqtype.setPyCode(PyWbUtil.toBigFirstChar(mzyZdReqtype.getName()));
            mzyZdReqtype.setWbCode(PyWbUtil.getWBCode(mzyZdReqtype.getName()));
            if(StringUtils.isBlank(mzyZdReqtype.getCode())){
                //新增
                mzyZdReqtype.setCode(mzyZdReqtypeService.getNewCode());
                int num=mzyZdReqtypeService.saveMzyZdReqtype(mzyZdReqtype);
                if(num==0){
                    resultMap.put("code", -1);
                    resultMap.put("message", "保存失败，请重新操作或者联系管理员");
                    return resultMap;
                }
            }else {
                //修改
                mzyZdReqtypeService.modifyMzyZdReqtype(mzyZdReqtype);
            }
            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;
        }
    }



}
