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.YpSelinfoSoldDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicine.YpCodgUpload;
import thyyxxk.webserver.entity.medicine.YpSelinfoSold;
import thyyxxk.webserver.utils.ResultVoUtil;

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

/**
 * @ClassName YpSelinfoSoldService
 * @Author hsh
 * @Date 2025/6/3 0003 11:12
 * @Version 1.0
 * @Description 药品销售/销售退货上传信息处理
 **/
@Service
@Slf4j
public class YpSelinfoSoldService {

    private final YpSelinfoSoldDao dao;

    public YpSelinfoSoldService(YpSelinfoSoldDao dao) {
        this.dao = dao;
    }

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

    /**
     * @Description 查询药品销售/销售退货上传信息结果
     * @Author hsh
     * @param dto 参数
     * @return list 药品销售/销售退货上传信息结果
     * @Date 2025/6/3 0003 11:24
     */
    public ResultVo<List<YpSelinfoSold>> selectYpSelinfoResult(YpCodgUpload dto) {
        List<YpSelinfoSold> purcinfoList;
        if("3".equals(dto.getType())){
            // 销售上传数据
            purcinfoList = dao.selectYpSelinfoSoldResult(dto);
        } else {
            // 销售退货上传数据
            purcinfoList = dao.selectYpSelinfoSoldTkResult(dto);
        }
        return ResultVoUtil.success(purcinfoList);
    }

    public ResultVo<Map<String, Object>> deleteYpSelinfoSoldBatchByCode(List<YpCodgUpload> list) {
        if(null == list || list.isEmpty()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品销售/销售退货上传信息不存在，请检查！");
        }
        Map<String, Object> map = new HashMap<>();
        int count = dao.deleteYpSelinfoSoldBatchByCodes(list);
        map.put("count", count);
        map.put("msg", "删除药品销售/销售退货上传信息成功！");
        return ResultVoUtil.success(map);
    }

    /**
     * @Description 更新上传状态为删除
     * @Author hsh
     * @param list 上传数据
     * @return map
     * @Date 2025/7/2 0002 17:28
     */
    public ResultVo<Map<String, Object>> updateYpSelinfoSoldBatchByCode(List<YpCodgUpload> list) {
        if(null == list || list.isEmpty()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品销售/销售退货上传信息不存在，请检查！");
        }
        Map<String, Object> map = new HashMap<>();
        int count = dao.updateYpSelinfoSoldBatchByCode(list);
        map.put("count", count);
        map.put("msg", "更新药品销售/销售退货上传状态成功！");
        return ResultVoUtil.success(map);
    }

}
