package thyyxxk.webserver.service.datamodify;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.dictionary.DoctorBaseZdDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.DoctorBaseZd;
import thyyxxk.webserver.entity.executeItem.ZdChargeItem;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.CommonUtil;
import thyyxxk.webserver.utils.PingYinUtils;
import thyyxxk.webserver.utils.ResultVoUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2025/5/20
 */
@Service
@Slf4j
public class DoctorBaseZdDaoService {
    @Resource
    private DoctorBaseZdDao dao;

    public List<DoctorBaseZd> queryDoctorBaseZd(DoctorBaseZd param) {
        AssertUtil.isnotBlank(param.getTableName(), "表名不能为空");
        if(StrUtil.isNotBlank(param.getKeyWard())){
            param.setKeyWard(param.getKeyWard().toUpperCase());
        }
        return dao.selectDoctorBaseZd(param);
    }

    public int updateDelFlag(DoctorBaseZd param){
        return dao.updateDelFlag(param);
    }

    public List<DoctorBaseZd> queryJyZdTubeChargeByCode(String code) {
        List<DoctorBaseZd> doctorBaseZds = dao.selectJyZdTubeChargeByCode(code);
        CommonUtil.BeanTrim(doctorBaseZds);
        return doctorBaseZds;
    }

    public int saveDoctorBaseZd(DoctorBaseZd param){
        AssertUtil.isnotBlank(param.getName(),"名称不能为空");
        AssertUtil.isnotBlank(param.getTableName(),"表名不能为空");
        AssertUtil.isnotBlank(param.getNewCode(),"编码不能为空");
        //修改
        param.setPyCode(PingYinUtils.pyShouZiMuDaXie(param.getName()));
        param.setDCode(PingYinUtils.getWBCode(param.getName()));
        if(StrUtil.isNotBlank(param.getCode())){
            return dao.updateDoctorBaseZd(param);
        }
        //新增
        Integer count = dao.countBycode(param.getNewCode(),param.getTableName());
        if(count > 0){
            throw new BizException(ExceptionEnum.INTERNAL_SERVER_ERROR, "该编码已经存在");
        }
        param.setDelFlag("0");
        return dao.insertDoctorBaseZd(param);
    }
    @Transactional(rollbackFor = Exception.class)
    public int batchJyZdTubeCharge(DoctorBaseZd param){
        AssertUtil.isnotBlank(param.getSaveData(),"保存的试管收费数据不能为空");
        AssertUtil.isnotBlank(param.getCode(),"保存的试管编码不能为空");
        for(DoctorBaseZd doctorBaseZd: param.getSaveData()){
            AssertUtil.isnotBlank(doctorBaseZd,"试管收费数据不能为空");
            AssertUtil.isnotBlank(doctorBaseZd.getChargeCode(),"试管收费编码不能为空");
            AssertUtil.isnotBlank(doctorBaseZd.getAmount(),"试管收费数量不能为空");
            doctorBaseZd.setCode(param.getCode());
        }
        Map<String, List<DoctorBaseZd>> chargeCodeMap = param.getSaveData().stream().collect(Collectors.groupingBy(DoctorBaseZd::getChargeCode));
        for(String chargeCode : chargeCodeMap.keySet()){
            if(chargeCodeMap.get(chargeCode).size() > 1){
                throw new BizException(ExceptionEnum.INTERNAL_SERVER_ERROR, "收费编码为[" + chargeCode + "]项目有重复!");
            }
        }
        dao.deleteJyZdTubeCharge(param.getCode(),null);
        return dao.batchJyZdTubeCharge(param.getSaveData());
    }

    public int deleteZdTubeCharge(DoctorBaseZd param) {
        AssertUtil.isnotBlank(param.getCode(),"试管组编码不能为空");
        AssertUtil.isnotBlank(param.getChargeCode(),"收费编码不能为空");
        return dao.deleteJyZdTubeCharge(param.getCode(),param.getChargeCode());
    }

    public ResultVo queryZdItemChargePage(DoctorBaseZd param) {
        IPage page = new Page(param.getCurrentPage(), param.getPageSize());
        Map<String, Object> res = new HashMap<>(10);
        if(StrUtil.isNotBlank(param.getKeyWard())){
            param.setKeyWard(param.getKeyWard().toUpperCase());
        }
        List<ZdChargeItem> list = dao.selectZdItemChargePage(page,param);
        CommonUtil.BeanTrim(list);
        res.put("data", list);
        res.put("total", page.getTotal());
      return ResultVoUtil.success(res);
    }
}
