package com.zzsmart.qomo.kn.cost.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostPurchaseRecord;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostPurchaseRecordMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostPurchaseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description: app_scene_cost_purchase_record
 * @Author: jeecg-boot
 * @Date: 2024-09-08
 * @Version: V1.0
 */
@Service
public class AppSceneCostPurchaseRecordServiceImpl extends ServiceImpl<AppSceneCostPurchaseRecordMapper, AppSceneCostPurchaseRecord> implements IAppSceneCostPurchaseRecordService {
    @Autowired
    AppSceneCostPurchaseRecordMapper appSceneCostPurchaseRecordMapper;

    /**
     * 查询满足物料编号的最低采购价
     *
     * @param materialNos
     */
    @Override
    public Map<String, BigDecimal> getMinCostPurchasePriceByMaterialNo(List<String> materialNos) {
        Map<String, BigDecimal> map = new HashMap<>();
        QueryWrapper<AppSceneCostPurchaseRecord> queryWrapper = new QueryWrapper();
        if (materialNos != null && materialNos.size() > 0) {
            queryWrapper.in("INFNR", materialNos);
            List<AppSceneCostPurchaseRecord> costPurchasePrices = appSceneCostPurchaseRecordMapper.selectList(queryWrapper);
            for (int i = 0; i < costPurchasePrices.size(); i++) {
                AppSceneCostPurchaseRecord costPurchasePrice = costPurchasePrices.get(i);
                if(map.containsKey(costPurchasePrice.getInfnr())){
                    if(costPurchasePrice.getNetpr().doubleValue()<map.get(costPurchasePrice.getInfnr()).doubleValue()){
                        map.put(costPurchasePrice.getInfnr(), costPurchasePrice.getNetpr());
                    }
                }else{
                    map.put(costPurchasePrice.getInfnr(), costPurchasePrice.getNetpr());
                }
                //map.put(costPurchasePrice.getInfnr(), costPurchasePrice.getNetpr());
            }
        }
        return map;
    }
}
