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.entity.his.zd.ZdChargeItem;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.pageDto.ZdChargeItemDto;
import cn.hnthyy.thmz.service.his.zd.ZdChargeItemService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import cn.hnthyy.thmz.service.his.zy.ZyConfigService;
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.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class ZdChargeItemController {
    @Autowired
    private ZdChargeItemService zdChargeItemService;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;
    @Autowired
    private ZyConfigService zyConfigService;

    /**
     * 分页查询收费项目字典列表
     * @param zdChargeItemDto 收费项目查询条件
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/listZdChargeItem", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> listZdChargeItem(@RequestBody ZdChargeItemDto zdChargeItemDto) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(zdChargeItemDto==null){
                resultMap.put("code", -1);
                resultMap.put("message", "查询收费项目字典列表失败，参数为空");
                return resultMap;
            }
            ZdChargeItem zdChargeItem = zdChargeItemDto.getZdChargeItem();
            if(zdChargeItem==null){
                resultMap.put("code", -1);
                resultMap.put("message", "查询收费项目字典列表失败，参数为空");
                return resultMap;
            }

            if(StringUtils.isBlank(zdChargeItem.getName())){
                zdChargeItem.setName(null);
            }else {
                zdChargeItem.setName("%"+zdChargeItem.getName()+"%");
            }
            if(StringUtils.isBlank(zdChargeItem.getCode())){
                zdChargeItem.setCode(null);
            }
            if(StringUtils.isBlank(zdChargeItem.getClassCode())){
                zdChargeItem.setClassCode(null);
            }
            if(StringUtils.isBlank(zdChargeItem.getAuditCode())){
                zdChargeItem.setAuditCode(null);
            }
            if(StringUtils.isBlank(zdChargeItem.getBillItemZy())){
                zdChargeItem.setBillItemZy(null);
            }
            if(StringUtils.isBlank(zdChargeItem.getBillItemMz())){
                zdChargeItem.setBillItemMz(null);
            }
            if(StringUtils.isBlank(zdChargeItem.getYnCode())){
                zdChargeItem.setYnCode(null);
            }
            if(StringUtils.isBlank(zdChargeItemDto.getOrderByCase())){
                zdChargeItemDto.setOrderByCase("code");
            }
            log.info("查询收费项目字典，参数信息");
            int total = zdChargeItemService.countZdChargeItem(zdChargeItem);
            List<ZdChargeItem> list= zdChargeItemService.queryZdChargeItemWithPage(zdChargeItemDto);
            if(list!=null && list.size()>0){
                list.stream().forEach(zd->{
                    zd.setExecUnitName(zdUnitCodeService.queryDeptNameByIdInCache(zd.getExecUnit()));
                });
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询收费项目字典成功");
            resultMap.put("total", total);
            resultMap.put("data", list);
            return resultMap;
        }catch (Exception e){
            resultMap.put("code", -1);
            resultMap.put("message", "查询收费项目字典列表失败");
            return resultMap;
        }
    }


    /**
     * 按照编码查询收费项目字典
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getZdChargeItemByCode", method = {RequestMethod.GET})
    public Map<String, Object> getZdChargeItemByCode(@RequestParam("code") String code) {
        Map<String, Object> resultMap = new HashMap<>();
        if(StringUtils.isBlank(code)){
            resultMap.put("code", -1);
            resultMap.put("message", "项目编码不能为空");
            return resultMap;
        }
        try {
            ZdChargeItem zdChargeItem=zdChargeItemService.queryZdChargeItemByCode(code);
            if(zdChargeItem==null){
                resultMap.put("code", -1);
                resultMap.put("message", "收费编码不存在");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询收费项目成功");
            resultMap.put("data", zdChargeItem);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询收费项目失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询收费项目失败");
            return resultMap;
        }
    }



    /**
     * 停用或者启用收费项目字典
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/updateZdChargeItemDelFlag", method = {RequestMethod.GET})
    public Map<String, Object> updateZdChargeItemDelFlag(@RequestParam("code") String code,@RequestParam("delFlag") Integer delFlag, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        if(StringUtils.isBlank(code)){
            resultMap.put("code", -1);
            resultMap.put("message", "项目编码不能为空");
            return resultMap;
        }
        if(!YesNoEnum.YES.code.equals(delFlag) && !YesNoEnum.NO.code.equals(delFlag)){
            resultMap.put("code", -1);
            resultMap.put("message", "停用标志错误");
            return resultMap;
        }
        String str = YesNoEnum.YES.code.equals(delFlag)?"停用":"启用";
        try {
            ZdChargeItem zdChargeItem = new ZdChargeItem();
            zdChargeItem.setCode(code);
            zdChargeItem.setDelFlag(delFlag);
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            zdChargeItem.setOperId(tokenUser.getUserIdCode());
            int num=zdChargeItemService.modifyZdChargeItem(zdChargeItem);
            if(num==0){
                resultMap.put("code", -1);
                resultMap.put("message", str+"失败");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", str+"项目成功");
            resultMap.put("data", num);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(str+"项目失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", str+"项目失败");
            return resultMap;
        }
    }


    /**
     * 保存收费项目字典
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveZdChargeItem", method = {RequestMethod.POST})
    public Map<String, Object> saveZdChargeItem(@RequestBody ZdChargeItem zdChargeItem, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(zdChargeItem==null){
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(zdChargeItem.getClassCode())){
                resultMap.put("code", -1);
                resultMap.put("message", "项目类型不能为空");
                return resultMap;
            }
//            if(StringUtils.isBlank(zdChargeItem.getYbCode())){
//                resultMap.put("code", -1);
//                resultMap.put("message", "医保扩展码不能为空");
//                return resultMap;
//            }
//            if(StringUtils.isBlank(zdChargeItem.getXnhCode())){
//                resultMap.put("code", -1);
//                resultMap.put("message", "新农合医保编码不能为空");
//                return resultMap;
//            }
//            if(StringUtils.isBlank(zdChargeItem.getXnhName())){
//                resultMap.put("code", -1);
//                resultMap.put("message", "新农合医保名称不能为空");
//                return resultMap;
//            }
            if(StringUtils.isBlank(zdChargeItem.getName())){
                resultMap.put("code", -1);
                resultMap.put("message", "项目名称不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(zdChargeItem.getChargeUnit())){
                resultMap.put("code", -1);
                resultMap.put("message", "单位描述不能为空");
                return resultMap;
            }
            if(zdChargeItem.getChargeAmount()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "收费金额不能为空");
                return resultMap;
            }
            if(zdChargeItem.getPercentag1()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "第一折扣率不能为空");
                return resultMap;
            }
            if(zdChargeItem.getPercentag2()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "第二折扣率不能为空");
                return resultMap;
            }
            if(zdChargeItem.getPercentag3()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "第三折扣率不能为空");
                return resultMap;
            }
            if(zdChargeItem.getPercentag3()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "第四折扣率不能为空");
                return resultMap;
            }
            if(zdChargeItem.getPercentag5()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "第五折扣率不能为空");
                return resultMap;
            }
//            if(zdChargeItem.getEffectiveDate()==null){
//                resultMap.put("code", -1);
//                resultMap.put("message", "生效时间不能为空");
//                return resultMap;
//            }
            if(zdChargeItem.getCo()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "是否加价不能为空");
                return resultMap;
            }
            if(YesNoEnum.YES.code.equals(zdChargeItem.getCo())){
                if(StringUtils.isBlank(zdChargeItem.getXlCode())){
                    resultMap.put("code", -1);
                    resultMap.put("message", "加价率不能为空");
                    return resultMap;
                }
                Double xlCode = Double.valueOf(zdChargeItem.getXlCode());
                if(xlCode==null){
                    resultMap.put("code", -1);
                    resultMap.put("message", "加价率格式不正确");
                    return resultMap;
                }
            }
            if(StringUtils.isBlank(zdChargeItem.getAuditCode())){
                resultMap.put("code", -1);
                resultMap.put("message", "核算码不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(zdChargeItem.getBillItemZy())){
                resultMap.put("code", -1);
                resultMap.put("message", "住院发票码不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(zdChargeItem.getBillItemMz())){
                resultMap.put("code", -1);
                resultMap.put("message", "门诊发票码不能为空");
                return resultMap;
            }
            zdChargeItem.setPyCode(PyWbUtil.toBigFirstChar(zdChargeItem.getName()));
            zdChargeItem.setWbCode(PyWbUtil.getWBCode(zdChargeItem.getName()));
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            zdChargeItem.setOperId(tokenUser.getUserIdCode());
            if(StringUtils.isBlank(zdChargeItem.getCode())){
                //新增
                zdChargeItem.setCode(zyConfigService.getChargeCode(zdChargeItem.getClassCode()));
                int num=zdChargeItemService.saveZdChargeItem(zdChargeItem);
                if(num==0){
                    resultMap.put("code", -1);
                    resultMap.put("message", "保存失败，请重新操作或者联系管理员");
                    return resultMap;
                }
            }else {
                //修改
                zdChargeItemService.modifyZdChargeItem(zdChargeItem);
            }
            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
     */
    @UserLoginToken
    @RequestMapping(value = "/getAllInUsedZdCharge", method = {RequestMethod.GET})
    public Map<String, Object> getAllInUsedZdCharge() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<ZdChargeItem> list = zdChargeItemService.queryAllInUsedZdCharge();
            resultMap.put("code", 0);
            resultMap.put("message", "查询收费项目字典成功");
            resultMap.put("total", list);
            resultMap.put("data", list);
            return resultMap;
        }catch (Exception e){
            resultMap.put("code", -1);
            resultMap.put("message", "查询收费项目字典列表失败");
            return resultMap;
        }
    }

}
