package thyyxxk.webserver.service.medicine;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.medicine.YpInventoryDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicine.YpInventoryVo;
import thyyxxk.webserver.entity.medicine.YpZdDictVo;
import thyyxxk.webserver.utils.DecimalUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName YpInventoryService
 * @Author hsh
 * @Date 2024/2/29 14:59
 * @Version 1.0
 * @Description 药品库存管理
 **/
@Service
@Slf4j
public class YpInventoryService {

    private final YpInventoryDao dao;

    public YpInventoryService(YpInventoryDao dao) {
        this.dao = dao;
    }

    public ResultVo<Map<String, Object>> selectYpUsedQuantity(YpInventoryVo vo){
        Map<String, Object> map = new HashMap<>();
        List<YpZdDictVo> resultData = new ArrayList<>();
        List<YpZdDictVo> list = dao.selectYpUsedQuantity(vo);
        // 药品有拆分的情况  需要合并在一起显示
        Map<String, List<YpZdDictVo>> mapKey = list.stream().collect(Collectors.groupingBy(YpZdDictVo::getCode));
        for(Map.Entry<String, List<YpZdDictVo>> entry: mapKey.entrySet()) {
            String key = entry.getKey();
            List<YpZdDictVo> value = entry.getValue();
            if(!value.isEmpty() && value.size() > 1){
                BigDecimal minSl = BigDecimal.ZERO;
                BigDecimal packSize = BigDecimal.ZERO;
                YpZdDictVo ypVo = new YpZdDictVo();
                for(YpZdDictVo ypZdDictVo : value){
                    if("01".equals(ypZdDictVo.getSerial())){
                        minSl = DecimalUtil.add(minSl, ypZdDictVo.getAmount());
                    } else if("99".equals(ypZdDictVo.getSerial())){
                        packSize = DecimalUtil.add(packSize, ypZdDictVo.getPackSize());
                        BeanUtils.copyProperties(ypZdDictVo, ypVo);
                    }
                }
                // 小包装转成大包装数量
                BigDecimal add = DecimalUtil.divide(minSl, packSize, 0);
                ypVo.setAmount(DecimalUtil.add(ypVo.getAmount(), add));
                resultData.add(ypVo);
            } else {
                resultData.addAll(value);
            }

        }
        map.put("resultData", resultData.stream().sorted(Comparator.comparing(YpZdDictVo::getAmount).reversed()).collect(Collectors.toList()));
        return ResultVoUtil.success(map);
    }

    public ResultVo<Map<String, Object>> selectYpUsedMx(YpInventoryVo vo){
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = dao.selectYpUsedMx(vo);
        map.put("resultData", list);
        return ResultVoUtil.success(map);
    }

    public ResultVo<Map<String, Object>> selectYpOutInStat(YpInventoryVo vo){
        Map<String, Object> map = new HashMap<>();



        return ResultVoUtil.success(map);
    }


}
