package com.fjhx.purchase.service;

import cn.hutool.core.util.ObjectUtil;
import com.fjhx.purchase.entity.arrival.po.ArrivalDetail;
import com.fjhx.purchase.entity.purchase.po.Purchase;
import com.fjhx.purchase.entity.purchase.po.PurchaseDetail;
import com.fjhx.purchase.service.arrival.ArrivalDetailService;
import com.fjhx.purchase.service.purchase.PurchaseDetailService;
import com.fjhx.purchase.service.purchase.PurchaseService;
import com.fjhx.wms.entity.stock.po.StockWaitDetails;
import com.fjhx.wms.service.PurService;
import com.fjhx.wms.service.stock.StockWaitDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class purServiceImpl implements PurService {
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private ArrivalDetailService arrivalDetailService;
    @Autowired
    private StockWaitDetailsService stockWaitDetailsService;

    /**
     * 修改采购到货状态
     */
    @Override
    public void updatePurchaseArrivalStatus(Long id) {
        Purchase purchase = purchaseService.getById(id);
        //修改到货状态
        Integer flag = 0;
        List<PurchaseDetail> purchaseDetailsList1 = purchaseDetailService.list(q -> q.eq(PurchaseDetail::getPurchaseId, purchase.getId()));
        if (ObjectUtil.isNotEmpty(purchaseDetailsList1)) {
            List<Long> purchaseDetailsIds = purchaseDetailsList1.stream().map(PurchaseDetail::getId).collect(Collectors.toList());
            List<ArrivalDetail> arrivalDetailsList1 = arrivalDetailService.list(q -> q.in(ArrivalDetail::getPurchaseDetailId, purchaseDetailsIds));
            if (ObjectUtil.isNotEmpty(arrivalDetailsList1)) {
                Map<Long, List<ArrivalDetail>> arrivalDetailsMap = arrivalDetailsList1.stream().collect(Collectors.groupingBy(ArrivalDetail::getPurchaseDetailId));
                for (PurchaseDetail purchaseDetail : purchaseDetailsList1) {
                    //获取到货明细
                    List<ArrivalDetail> arrivalDetails = arrivalDetailsMap.get(purchaseDetail.getId());
                    List<Long> arrivalIds = arrivalDetails.stream().map(ArrivalDetail::getId).collect(Collectors.toList());
                    //根据到货明细获取入库明细
                    List<StockWaitDetails> stockWaitDetailsList = stockWaitDetailsService.list(q -> q.in(StockWaitDetails::getBusinessDetailsId, arrivalIds));
                    BigDecimal count = stockWaitDetailsList.stream()
                            .map(StockWaitDetails::getReceiptQuantity)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    if (count.compareTo(purchaseDetail.getCount()) >= 0) {
                        flag++;
                    }
                }
                if (purchaseDetailsList1.size() == flag) {
                    //完全到货
                    purchase.setArrivalStatus(20);
                } else {
                    //部分到货
                    purchase.setArrivalStatus(10);
                }
                purchaseService.updateById(purchase);
            }
        }
    }
}
