package thyyxxk.webserver.service.medicine;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.medicine.YpPurcinfoBoughtDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicine.YpCodgUpload;
import thyyxxk.webserver.entity.medicine.YpPurcinfoBought;
import thyyxxk.webserver.utils.ResultVoUtil;

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

/**
 * @ClassName YpPurcinfoBoughtService
 * @Author hsh
 * @Date 2025/5/28 0028 10:45
 * @Version 1.0
 * @Description 药品入库/退货信息上传
 **/
@Service
@Slf4j
public class YpPurcinfoBoughtService {

    private final YpPurcinfoBoughtDao dao;

    public YpPurcinfoBoughtService(YpPurcinfoBoughtDao dao) {
        this.dao = dao;
    }

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

    /**
     * @Description 查询药品采购入库/退货上传信息结果
     * @Author hsh
     * @param dto 参数
     * @return list 药品采购入库/退货上传信息结果
     * @Date 2025/5/28 0028 17:51
     */
    public ResultVo<List<YpPurcinfoBought>> selectYpPurchaseResult(YpCodgUpload dto) {
        List<YpPurcinfoBought> purcinfoList;
        if("1".equals(dto.getType())){
            purcinfoList = dao.selectYpPurchaseGoodsResult(dto, dto.getGroupNo());
        } else {
            purcinfoList = dao.selectYpPurchaseGoodsTkResult(dto, dto.getGroupNo());
        }
        return ResultVoUtil.success(purcinfoList);
    }

}
