package thyyxxk.webserver.service.medicine;

import cn.hutool.core.collection.CollUtil;
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.YpInvinfoDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicine.YpCodgUpload;
import thyyxxk.webserver.entity.medicine.YpInvinfoModify;
import thyyxxk.webserver.utils.ResultVoUtil;

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

/**
 * @ClassName YpInvinfoService
 * @Author hsh
 * @Date 2025/6/5 0005 23:10
 * @Version 1.0
 * @Description TODO
 **/
@Slf4j
@Service
public class YpInvinfoService {

    private final YpInvinfoDao dao;

    public YpInvinfoService(YpInvinfoDao dao) {
        this.dao = dao;
    }

    public ResultVo<Map<String, Object>> updateYpInvinfoBatch(List<YpInvinfoModify> list) {
        if(null == list || list.isEmpty()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品库存变更上传信息不存在，请检查！");
        }
        Map<String, Object> map = new HashMap<>();
        int count = 0;
        List<List<YpInvinfoModify>> split = CollUtil.split(list, 20);
        for(List<YpInvinfoModify> l : split){
            dao.deleteYpInvinfoBatchByIds(l);
            count += dao.saveYpInvinfoBatch(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<YpInvinfoModify>> selectYpInvinfoResult(YpCodgUpload dto) {
        if(StringUtils.isNotEmpty(dto.getDocuNo())){
            dto.setStartTime(null);
            dto.setEndTime(null);
        }
        List<YpInvinfoModify> invinfoList = dao.selectYpInvinfoResult(dto);
        return ResultVoUtil.success(invinfoList);
    }

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

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

}
