package com.jwipc.inquiry.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jwipc.inquiry.convert.InquiryMaterialConvert;
import com.jwipc.inquiry.convert.InquiryMaterialExtendConvert;
import com.jwipc.inquiry.dto.HistoryPriceInfoDTO;
import com.jwipc.inquiry.dto.InquiryDTO;
import com.jwipc.inquiry.dto.InquiryMaterialDTO;
import com.jwipc.inquiry.dto.PurchaseApplyBillDetailClientDTO;
import com.jwipc.inquiry.entity.InquiryMaterial;
import com.jwipc.inquiry.logic.InquiryMaterialLogic;
import com.jwipc.inquiry.logic.PriceInfoLogic;
import com.jwipc.inquiry.logic.PurchaseApplyBillDetailLogic;
import com.jwipc.inquiry.model.po.InquiryMaterialPO;
import com.jwipc.inquiry.model.po.InquiryPO;
import com.jwipc.inquiry.model.query.InquiryMaterialQuery;
import com.jwipc.inquiry.service.IInquiryMaterialExtendService;
import com.jwipc.inquiry.service.IInquiryMaterialService;
import com.jwipc.page.PageView;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/2/22 11:35
 * @desc 询价物料
 */
@Service
@RequiredArgsConstructor
public class InquiryMaterialLogicImpl implements InquiryMaterialLogic {
    private final IInquiryMaterialService inquiryMaterialService;
    private final IInquiryMaterialExtendService inquiryMaterialExtendService;
    private final InquiryMaterialConvert inquiryMaterialConvert;
    private final PriceInfoLogic priceInfoLogic;
    private final PurchaseApplyBillDetailLogic purchaseApplyBillDetailLogic;
    private final InquiryMaterialExtendConvert inquiryMaterialExtendConvert;
    @Override
    public List<InquiryMaterialDTO> listAll(InquiryMaterialQuery query) {
        List<InquiryMaterialPO> inquiryPOS = inquiryMaterialService.listAll(query);
        inquiryPOS.forEach(inquiryMaterialDTO -> {
            //如果为空数据库可采购数量，则去报价单中查询计算一次
            if (null == inquiryMaterialDTO.getEnablePurchaseNum()) {
                if (StringUtils.isNotEmpty(inquiryMaterialDTO.getPurchaseBillCode()) && StringUtils.isNotEmpty(inquiryMaterialDTO.getPurchaseBillItemCode())) {
                    PurchaseApplyBillDetailClientDTO purchaseApplyBillDetailClientDTO = purchaseApplyBillDetailLogic.getByBillCodeAndItemCode(inquiryMaterialDTO.getPurchaseBillCode(),inquiryMaterialDTO.getPurchaseBillItemCode());
                    BigDecimal enablePurchaseNum = inquiryMaterialService.getPurchaseNumAmountByBillNoAndCode(inquiryMaterialDTO.getPurchaseBillCode(), inquiryMaterialDTO.getPurchaseBillItemCode(), inquiryMaterialDTO.getId());
                    if (Objects.nonNull(enablePurchaseNum)) {
                        //可采购数量 = 原始的需求数量（来自请购单的需求数量） - 已累计采购数量
                        inquiryMaterialDTO.setEnablePurchaseNum(purchaseApplyBillDetailClientDTO.getApprovalNumber().subtract(enablePurchaseNum));
                    } else {
                        inquiryMaterialDTO.setEnablePurchaseNum(purchaseApplyBillDetailClientDTO.getApprovalNumber());
                    }
                }
            }
        });
        return inquiryMaterialConvert.po2DTOList(inquiryPOS);
    }

    @Override
    public List<InquiryMaterialDTO> copyListAll(InquiryMaterialQuery query) {
        List<InquiryMaterialPO> inquiryPOS = inquiryMaterialService.listAll(query);
        inquiryPOS.forEach(inquiryMaterialPO -> {
            //如果为空数据库可采购数量，则去报价单中查询计算一次
//            if (null == inquiryMaterialPO.getEnablePurchaseNum()) {
                if (StringUtils.isNotEmpty(inquiryMaterialPO.getPurchaseBillCode()) && StringUtils.isNotEmpty(inquiryMaterialPO.getPurchaseBillItemCode())) {
                    PurchaseApplyBillDetailClientDTO purchaseApplyBillDetailClientDTO = purchaseApplyBillDetailLogic.getByBillCodeAndItemCode(inquiryMaterialPO.getPurchaseBillCode(),inquiryMaterialPO.getPurchaseBillItemCode());
                    BigDecimal enablePurchaseNum = inquiryMaterialService.getPurchaseNumAmountByBillNoAndCode(inquiryMaterialPO.getPurchaseBillCode(), inquiryMaterialPO.getPurchaseBillItemCode(), null);
                    if (Objects.nonNull(enablePurchaseNum)) {
                        //可采购数量 = 原始的需求数量（来自请购单的需求数量） - 已累计采购数量
                        inquiryMaterialPO.setEnablePurchaseNum(purchaseApplyBillDetailClientDTO.getApprovalNumber().subtract(enablePurchaseNum));
                    } else {
                        inquiryMaterialPO.setEnablePurchaseNum(purchaseApplyBillDetailClientDTO.getApprovalNumber());
                    }
                }
//            }
            //查询历史最低价
            HistoryPriceInfoDTO historyPriceInfoDTO = priceInfoLogic.getHistoryLowPrice(inquiryMaterialPO.getMaterialCode(),inquiryMaterialPO.getFactoryCode());
            if (ObjectUtil.isNotNull(historyPriceInfoDTO)){
                inquiryMaterialPO.setHistoryPriceId(historyPriceInfoDTO.getId());
                inquiryMaterialPO.setHistoryPrice(historyPriceInfoDTO.getTaxPrice());
            }
            //查询上次中标价
            HistoryPriceInfoDTO historyPriceInfoDTO1 = priceInfoLogic.getLastBidPrice(inquiryMaterialPO.getMaterialCode(),inquiryMaterialPO.getFactoryCode());
            if (ObjectUtil.isNotNull(historyPriceInfoDTO1)){
                inquiryMaterialPO.setLastBidPriceId(historyPriceInfoDTO.getId());
                inquiryMaterialPO.setLastBidPrice(historyPriceInfoDTO.getTaxPrice());
            }
        });
        return inquiryMaterialConvert.po2DTOList(inquiryPOS);
    }

    @Override
    public InquiryMaterialDTO getById(Long inquiryMaterialId) {
        InquiryMaterial inquiryMaterial = inquiryMaterialService.getById(inquiryMaterialId);
        return inquiryMaterialConvert.entity2DTO(inquiryMaterial);
    }
}
