package com.jwipc.supplier.client.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jwipc.supplier.client.QuotaManagementClient;
import com.jwipc.supplier.client.convert.QuotaManagementClientConvert;
import com.jwipc.tender.client.dto.QuotaManagementClientDTO;
import com.jwipc.tender.client.dto.QuotaManagementParamClientDTO;
import com.jwipc.tender.entity.QuotaManagement;
import com.jwipc.tender.logic.QuotaManagementLogic;
import com.jwipc.tender.service.IQuotaManagementService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/4/1 12:52
 * @desc
 */
@Service
@RequiredArgsConstructor
public class QuotaManagementClientImpl implements QuotaManagementClient {
    private final QuotaManagementClientConvert quotaManagementClientConvert;
    private final QuotaManagementLogic quotaManagementLogic;
    private final IQuotaManagementService quotaManagementService;

    @Override
    public List<QuotaManagementClientDTO> getByMaterialCodeAndFactoryCode(String materialCode, String factoryCode) {
        List<QuotaManagement> quotaManagements = quotaManagementLogic.getByMaterialCodeAndFactoryCode(materialCode,
                factoryCode);
        return quotaManagementClientConvert.entity2DTOList(quotaManagements);

    }

    /**
     * 查询配额最高的供应商
     * @param materialCode
     * @param factoryCode
     * @return
     */
    @Override
    public String getByMaterialCodeAndFactoryCodeMaxQuote(String materialCode, String factoryCode) {
        QuotaManagement quotaManagement = quotaManagementLogic.getByMaterialCodeAndFactoryCodeMaxQuote(materialCode,
                factoryCode);
        return ObjectUtil.isNotNull(quotaManagement)?quotaManagement.getSupplierCode():"";
    }

    @Override
    public BigDecimal getQuota(String materialCode, String factoryCode, String supplierCode) {
        QuotaManagement quotaManagement = quotaManagementLogic.getQuota(materialCode,factoryCode,supplierCode);
        return ObjectUtil.isNotNull(quotaManagement) ? quotaManagement.getQuota() : null;
    }

    @Override
    public void deleteByMaterialCode(String materialCode) {
        quotaManagementLogic.deleteByMaterialCode(materialCode);
    }

    @Override
    public void deleteByMaterialCodeAndFactoryCode(String materialCode, String factoryCode) {
        quotaManagementLogic.deleteByMaterialCodeAndFactoryCode(materialCode,factoryCode);
    }

    @Override
    public void saveQuota(List<QuotaManagementParamClientDTO> list) {
        List<QuotaManagement> quotaManagements = quotaManagementClientConvert.paramDTO2EntityList(list);
        quotaManagementLogic.saveQuota(quotaManagements);
    }
}
