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.MzyZdChargeType;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.service.his.mz.MzyZdChargeTypeService;
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.math.BigDecimal;
import java.util.*;

@Slf4j
@RestController
public class MzyZdChargeTypeController {
    @Autowired
    private MzyZdChargeTypeService mzyZdChargeTypeService;
    /**
     * 查询所有使用中的号别
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAllMzChargeType", method = {RequestMethod.GET,RequestMethod.POST})
    public Map<String, Object> getAllMzChargeType() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有使用中的号别成功");
            resultMap.put("data", mzyZdChargeTypeService.queryAllMzyZdChargeType());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有使用中的号别失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有使用中的号别失败");
            return resultMap;
        }
    }


    /**
     * 根据号别编号查询号别
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getMzChargeTypeByCode", method = {RequestMethod.GET})
    public Map<String, Object> getMzChargeTypeByCode(@RequestParam("code") String code) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(code)) {
                resultMap.put("code", -1);
                resultMap.put("message", "号别编号不能为空");
                return resultMap;
            }
            MzyZdChargeType mzyZdChargeType=mzyZdChargeTypeService.queryByCode(code);
           if(mzyZdChargeType==null){
               resultMap.put("code", -1);
               resultMap.put("message", "根据号别编号查询号别失败,没有对应的号别信息");
               return resultMap;
           }
            resultMap.put("code", 0);
            resultMap.put("message", "根据号别编号查询号别成功");
            resultMap.put("data", mzyZdChargeType);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据号别编号查询号别失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据号别编号查询号别失败");
            return resultMap;
        }
    }



    /**
     * 删除号别
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/removeMzyZdChargeType", method = {RequestMethod.GET})
    public Map<String, Object> removeMzyZdChargeType(@RequestParam("code") String code) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(code)){
                resultMap.put("code", -1);
                resultMap.put("message", "号别编码不能为空");
                return resultMap;
            }
            MzyZdChargeType mzyZdChargeType = new MzyZdChargeType();
            mzyZdChargeType.setCode(code);
            mzyZdChargeType.setDelFlag(YesNoEnum.YES.code);
            int num=mzyZdChargeTypeService.modifyMzyZdChargeType(mzyZdChargeType);
            if(num==0){
                resultMap.put("code", -1);
                resultMap.put("message", "删除失败");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "删除号别成功");
            resultMap.put("data", num);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除号别失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "删除号别失败");
            return resultMap;
        }
    }



    /**
     * 保存号别
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveMzyMzyZdChargeType", method = {RequestMethod.POST})
    public Map<String, Object> saveMzyMzyZdChargeType(@RequestBody MzyZdChargeType mzyZdChargeType) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(mzyZdChargeType==null){
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(mzyZdChargeType.getName())){
                resultMap.put("code", -1);
                resultMap.put("message", "号别名称不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(mzyZdChargeType.getReqType())){
                resultMap.put("code", -1);
                resultMap.put("message", "号别类别不能为空");
                return resultMap;
            }
            if(mzyZdChargeType.getReqFee()==null){
                mzyZdChargeType.setReqFee(BigDecimal.ZERO);
            }
            if(mzyZdChargeType.getClinicFee()==null){
                mzyZdChargeType.setClinicFee(BigDecimal.ZERO);
            }
            if(mzyZdChargeType.getOthFee()==null){
                mzyZdChargeType.setOthFee(BigDecimal.ZERO);
            }
            mzyZdChargeType.setPyCode(PyWbUtil.toBigFirstChar(mzyZdChargeType.getName()));
            mzyZdChargeType.setSelfCode(PyWbUtil.getWBCode(mzyZdChargeType.getName()));
            if(StringUtils.isBlank(mzyZdChargeType.getCode())){
                //新增
                mzyZdChargeType.setCode(mzyZdChargeTypeService.getNewCode());
                int num=mzyZdChargeTypeService.saveMzyZdChargeType(mzyZdChargeType);
                if(num==0){
                    resultMap.put("code", -1);
                    resultMap.put("message", "保存失败，请重新操作或者联系管理员");
                    return resultMap;
                }
            }else {
                //修改
                mzyZdChargeTypeService.modifyMzyZdChargeType(mzyZdChargeType);
            }
            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;
        }
    }

}
