package com.jwipc.inquiry.logic.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.inquiry.convert.InquirySupplierCostQuoteDetailConvert;
import com.jwipc.inquiry.dto.InquirySupplierCostQuoteDetailDTO;
import com.jwipc.inquiry.entity.InquiryCostTemplate;
import com.jwipc.inquiry.entity.InquirySupplierCostQuoteDetail;
import com.jwipc.inquiry.logic.InquirySupplierCostQuoteDetailLogic;
import com.jwipc.inquiry.model.po.InquiryMaterialSupplierPO;
import com.jwipc.inquiry.model.po.InquirySupplierCostQuoteDetailPO;
import com.jwipc.inquiry.service.IInquiryCostTemplateService;
import com.jwipc.inquiry.service.IInquirySupplierCostQuoteDetailService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author shenyang.ou
 * @date 2024/2/23 14:11
 * @desc
 */
@Service
@RequiredArgsConstructor
public class InquirySupplierCostQuoteDetailLogicImpl implements InquirySupplierCostQuoteDetailLogic {
    private final IInquiryCostTemplateService inquiryCostTemplateService;
    private final IInquirySupplierCostQuoteDetailService inquirySupplierCostQuoteDetailService;
    private final InquirySupplierCostQuoteDetailConvert inquirySupplierCostQuoteDetailConvert;
    /**
     * 获取供应商成本报价列表
     * @param inquiryMaterialSupplierId 询价物料供应商id
     * @param costTemplateCode 成本构成模板
     * @return 供应商成本报价列表
     */
    @Override
    public List<InquirySupplierCostQuoteDetailDTO> listByInquiryMaterialSupplierIdAndCostTemplateCode(Long inquiryMaterialSupplierId,String costTemplateCode) {
        List<InquirySupplierCostQuoteDetail> inquirySupplierCostQuoteDetails;
         inquirySupplierCostQuoteDetails = inquirySupplierCostQuoteDetailService.list(Wrappers.lambdaQuery(InquirySupplierCostQuoteDetail.class).eq(InquirySupplierCostQuoteDetail::getInquiryMaterialSupplierId,inquiryMaterialSupplierId));
        if (CollectionUtil.isEmpty(inquirySupplierCostQuoteDetails)){
            InquiryCostTemplate inquiryCostTemplate = inquiryCostTemplateService.getOne(Wrappers.lambdaQuery(InquiryCostTemplate.class).eq(InquiryCostTemplate::getTemplateCode,costTemplateCode));
            String[] costTypeNameList = StringUtils.split(inquiryCostTemplate.getCostTypeName(), ",");
            for (String costTypeName : costTypeNameList){
                InquirySupplierCostQuoteDetail inquirySupplierCostQuoteDetail = new InquirySupplierCostQuoteDetail();
                inquirySupplierCostQuoteDetail.setInquiryMaterialSupplierId(inquiryMaterialSupplierId);
                inquirySupplierCostQuoteDetail.setCostTypeName(costTypeName);
                inquirySupplierCostQuoteDetail.setSource(0);
                inquirySupplierCostQuoteDetails.add(inquirySupplierCostQuoteDetail);
            }
        }
        return inquirySupplierCostQuoteDetailConvert.entity2DTOList(inquirySupplierCostQuoteDetails);
    }
}
