package thyyxxk.webserver.service.medicine;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.medicine.YpCodgLineDao;
import thyyxxk.webserver.dao.his.medicine.YpInDetlDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicine.YpCodgLine;
import thyyxxk.webserver.entity.medicine.YpInDetlVo;
import thyyxxk.webserver.entity.medicine.vo.YpDetlVo;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName YpCodgLineService
 * @Author hsh
 * @Date 2025/6/26 0026 16:48
 * @Version 1.0
 * @Description 企业追溯信息下载
 **/
@Service
@Slf4j
public class YpCodgLineService {

    private final YpCodgLineDao dao;

    private final YpInDetlDao inDao;

    public YpCodgLineService(YpCodgLineDao dao, YpInDetlDao inDao) {
        this.dao = dao;
        this.inDao = inDao;
    }

    public ResultVo<Map<String, Object>> updateYpCodgLineBatch(List<YpCodgLine> list) {
        if(null == list || list.isEmpty()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品销售/销售退货上传信息不存在，请检查！");
        }
        Map<String, Object> map = new HashMap<>();
        int count = 0;
        List<List<YpCodgLine>> split = CollUtil.split(list, 40);
        for(List<YpCodgLine> l : split){
            count += dao.deleteYpCodgLineByIds(l);
            dao.insert(l);
        }
        map.put("count", count);
        return ResultVoUtil.success(map);
    }

    public ResultVo<List<YpInDetlVo>> selectYpInDetlData(YpDetlVo vo) {
        if(null == vo || StringUtils.isBlank(vo.getGroupNo())){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药库为空，请检查！");
        }
        QueryWrapper<YpInDetlVo> qw = new QueryWrapper<>();
        qw.ge("de.in_date", vo.getStartTime());
        qw.le("de.in_date", vo.getEndTime());
        qw.eq("de.group_no", vo.getGroupNo());
        if(StringUtils.isNotBlank(vo.getDetlNo())){
            qw.eq("de.in_docu_no", vo.getDetlNo());
        }
        if(StringUtils.isNotBlank(vo.getSupplyCode())){
            qw.eq("de.supply_code", vo.getSupplyCode());
        }
        if(StringUtils.isNotBlank(vo.getReptNo())){
            qw.eq("de.rept_no", vo.getReptNo());
        }
        List<YpInDetlVo> list = inDao.selectYpInDetlVoList(qw);
        for(YpInDetlVo inVo : list){
            QueryWrapper<YpCodgLine> qwLine = new QueryWrapper<>();
            qwLine.ge("delv_bchno", inVo.getReptNo());
            qwLine.eq("manu_bchno", inVo.getManuNo());
            qwLine.le("delventp_uscc", inVo.getSupplyId());
            List<YpCodgLine> s = dao.selectList(qwLine);
            if(null != s && !s.isEmpty()){
                inVo.setIsMatch("1");
            } else {
                inVo.setIsMatch("0");
            }
        }
        return ResultVoUtil.success(list);
    }

    public ResultVo<List<YpCodgLine>> selectYpCodgLineData(YpDetlVo vo) {
        QueryWrapper<YpCodgLine> qw = new QueryWrapper<>();
        if(StringUtils.isNotBlank(vo.getStartTime())){
            qw.ge("create_time", vo.getStartTime());
        }
        if(StringUtils.isNotBlank(vo.getEndTime())){
            qw.le("create_time", vo.getEndTime());
        }
        if(StringUtils.isNotBlank(vo.getSupplyId())){
            qw.eq("delventp_uscc", vo.getSupplyId());
        }
        if(StringUtils.isNotBlank(vo.getReptNo())){
            qw.eq("delv_bchno", vo.getReptNo());
        }
        if(StringUtils.isNotBlank(vo.getManuNo())){
            qw.eq("manu_bchno", vo.getManuNo());
        }
        if(StringUtils.isNotBlank(vo.getUploadFlag())){
            qw.eq("ref_flag", vo.getUploadFlag());
        }
        qw.orderByDesc("create_time");
        List<YpCodgLine> list = dao.selectList(qw);
        return ResultVoUtil.success(list);
    }

}
