package thyyxxk.webserver.service.dictionary;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.dictionary.OpLevelManagementDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.OpLevelManagement;
import thyyxxk.webserver.entity.zhuyuanyisheng.shoushu.ZdIcd9Cm3;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/6/8
 */
@Service
@Slf4j
public class OpLevelManagementService {
    @Autowired
    private OpLevelManagementDao dao;
    @Autowired
    private RedisLikeService redisLikeService;

    public ResultVo<IPage<ZdIcd9Cm3>> queryZdIcd9Cm3(ZdIcd9Cm3 param) {
        IPage<ZdIcd9Cm3> page = new Page<>(param.getCurrentPage(),param.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("del_flag",0);
        queryWrapper.in(CollUtil.isNotEmpty(param.getOpScaleList()), "op_scale", param.getOpScaleList());
        String sqlTemplate = StrUtil.format("(code like '%{}%' or name like '%{}%' or py_code like '%{}%' )",param.getCode(),param.getCode(),param.getCode());
        queryWrapper.apply(StrUtil.isNotBlank(param.getCode()),sqlTemplate);
        dao.queryZdIcd9Cm3Page(page, queryWrapper);
        page.setRecords(page.getRecords());
        page.setTotal(page.getTotal());
        return ResultVoUtil.success(page);
    }


    public ResultVo<String> saveZdIcd9Cm3(ZdIcd9Cm3 saveParam){
        AssertUtil.isnotBlank(saveParam.getCode(),"手术编码不能为空");
        AssertUtil.isnotBlank(saveParam.getOpScale(),"手术级别不能为空");
        dao.updateZdIcd9Cm3OpScaleByCode(saveParam.getCode(),saveParam.getOpScale());
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "保存成功");
    }

    public ResultVo<IPage<OpLevelManagement>> queryOpLevelManagement(OpLevelManagement param) {
        IPage<OpLevelManagement> page = new Page<>(param.getCurrentPage(),param.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StrUtil.isNotBlank(param.getNameDoctor()), "name_doctor", param.getNameDoctor());
        queryWrapper.like(StrUtil.isNotBlank(param.getCodeDoctor()),"code_doctor",param.getCodeDoctor());
        dao.selectDoctorPage(page,queryWrapper);
        page.setRecords(page.getRecords());
        page.setTotal(page.getTotal());
        return ResultVoUtil.success(page);
    }


    public ResultVo<IPage<OpLevelManagement>> queryOprtInfo(OpLevelManagement param){
        AssertUtil.isnotBlank(param.getCode(),"医生编号不能为空");
        IPage<OpLevelManagement> page = new Page<>(param.getCurrentPage(),param.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code",param.getCode());
        queryWrapper.like(StrUtil.isNotBlank(param.getCodeOp()),"code_op",param.getCodeOp());
        queryWrapper.like(StrUtil.isNotBlank(param.getCodeOpName()),"code_op_name",param.getCodeOpName());
        dao.selectPage(page,queryWrapper);
        page.setRecords(page.getRecords());
        page.setTotal(page.getTotal());
        return ResultVoUtil.success(page);
    }

    public ResultVo<String> saveOpLevelManagement(OpLevelManagement saveParam){
        AssertUtil.isnotBlank(saveParam.getCode(),"医生编号不能为空");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code",saveParam.getCode());
        dao.delete(queryWrapper);
        Date now = new Date();
        if (CollUtil.isNotEmpty(saveParam.getSaveData())) {
            saveParam.getSaveData().forEach(item -> {
                item.setCode(saveParam.getCode());
                item.setCodeDoctor(saveParam.getCodeDoctor());
                item.setNameDoctor(saveParam.getNameDoctor());
                item.setInputTime(now);
                item.setInputUser(redisLikeService.getUserInfoByToken().getCode());
            });
            dao.batchSaveOpLevelManagement(saveParam.getSaveData().stream().distinct().collect(Collectors.toList()));
        }
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "保存成功");
    }


    public ResultVo<String> getEmpCodeRs(String code) {
        return ResultVoUtil.success(redisLikeService.getUserInfoByCode(code).getCodeRs());
    }
}
