package com.jwipc.supplier.client.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.jwipc.contract.base.ContractDetailClientDTO;
import com.jwipc.contract.client.ContractDetailClient;
import com.jwipc.inquiry.dto.InquiryDetailByIdDetailClientDTO;
import com.jwipc.supplier.client.TenderClient;
import com.jwipc.supplier.client.convert.TenderClientConvert;
import com.jwipc.tender.client.dto.TenderDetailByIdDetailDTO;
import com.jwipc.tender.model.po.TenderDetailByIdDetailPO;
import com.jwipc.tender.service.ITenderBidService;
import com.jwipc.tender.service.ITenderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/4/3 0003 10:03
 */
@Service
@RequiredArgsConstructor
public class TenderClientImpl implements TenderClient {

    private final ITenderService tenderService;
    private final ITenderBidService bidService;

    private final TenderClientConvert tenderClientConvert;

    @Autowired
    private ContractDetailClient contractDetailClient;


    @Override
    public List<TenderDetailByIdDetailDTO> formDeatilById(Long zbId, String supplierId) {
        List<TenderDetailByIdDetailPO> inquiryDTO = tenderService.formMaterialDeatilById(zbId, supplierId);
        List<TenderDetailByIdDetailDTO> tenderDetailByIdDetailDTOS = tenderClientConvert.pos2Dtos(inquiryDTO);
        for(TenderDetailByIdDetailDTO tenderDetailByIdDetailDTO :tenderDetailByIdDetailDTOS){
            List<ContractDetailClientDTO> contractDetailClientDTOS = contractDetailClient.getDetailListByPurchaseCodeAndeItem(tenderDetailByIdDetailDTO.getPurchaseCode(), tenderDetailByIdDetailDTO.getItemCode());
            if (CollectionUtil.isNotEmpty(contractDetailClientDTOS)) {
                List<BigDecimal> numSum = contractDetailClientDTOS.stream().map(ContractDetailClientDTO::getNum).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(numSum)){
                    BigDecimal sum = numSum.stream()
                            .filter(Objects::nonNull)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal subtract = tenderDetailByIdDetailDTO.getApprovalNumber()==null?BigDecimal.ZERO:tenderDetailByIdDetailDTO.getApprovalNumber().subtract(sum);
                    tenderDetailByIdDetailDTO.setEnablePurchaseNum(subtract==null?BigDecimal.ZERO:subtract);
                }else{
                    tenderDetailByIdDetailDTO.setEnablePurchaseNum(BigDecimal.ZERO);
                }
            }else{
                tenderDetailByIdDetailDTO.setEnablePurchaseNum(tenderDetailByIdDetailDTO.getApprovalNumber());
            }
        }
        return tenderDetailByIdDetailDTOS;
    }

    @Override
    public void updateConState(Long bidDetailId, Integer state) {
        bidService.updateConState(bidDetailId, state);
    }


}
