package com.fjhx.sale.service.purchase.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.purchase.entity.pay.po.PayDetail;
import com.fjhx.purchase.service.pay.PayDetailService;
import com.fjhx.sale.entity.purchase.dto.PurchaseBackSelectDto;
import com.fjhx.sale.entity.purchase.po.EhsdPurchase;
import com.fjhx.sale.entity.purchase.po.EhsdPurchaseProduct;
import com.fjhx.sale.entity.purchase.po.PurchaseBack;
import com.fjhx.sale.entity.purchase.po.PurchaseBackDetails;
import com.fjhx.sale.entity.purchase.vo.PurchaseBackDetailsVo;
import com.fjhx.sale.entity.purchase.vo.PurchaseBackVo;
import com.fjhx.sale.mapper.purchase.PurchaseBackMapper;
import com.fjhx.sale.service.purchase.EhsdPurchaseProductService;
import com.fjhx.sale.service.purchase.EhsdPurchaseService;
import com.fjhx.sale.service.purchase.PurchaseBackDetailsService;
import com.fjhx.sale.service.purchase.PurchaseBackService;
import com.fjhx.supply.entity.supplier.po.SupplierInfo;
import com.fjhx.supply.service.supplier.SupplierInfoService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
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;


/**
 * <p>
 * sale_采购退货 服务实现类
 * </p>
 *
 * @since 2024-01-30
 */
@Service
public class PurchaseBackServiceImpl extends ServiceImpl<PurchaseBackMapper, PurchaseBack> implements PurchaseBackService {

    private final PurchaseBackDetailsService purchaseBackDetailsService;
    private final ProductInfoService productInfoService;
    private final EhsdPurchaseProductService purchaseProductService;
    private final EhsdPurchaseService ehsdPurchaseService;
    private final PayDetailService payDetailService;
    private final SupplierInfoService supplierInfoService;

    @Autowired
    public PurchaseBackServiceImpl(PurchaseBackDetailsService purchaseBackDetailsService, ProductInfoService productInfoService, EhsdPurchaseProductService purchaseProductService, EhsdPurchaseService ehsdPurchaseService, PayDetailService payDetailService, SupplierInfoService supplierInfoService) {
        this.purchaseBackDetailsService = purchaseBackDetailsService;
        this.productInfoService = productInfoService;
        this.purchaseProductService = purchaseProductService;
        this.ehsdPurchaseService = ehsdPurchaseService;
        this.payDetailService = payDetailService;
        this.supplierInfoService = supplierInfoService;
    }

    @Override
    public Page<PurchaseBackVo> getPage(PurchaseBackSelectDto dto) {
        IWrapper<PurchaseBack> wrapper = getWrapper();

        wrapper.keyword(dto.getKeyword(), new SqlField("pb.code"), new SqlField("p.code"));

        wrapper.orderByDesc("pb", PurchaseBack::getId);
        return this.baseMapper.getPage(dto.getPage(), wrapper);
    }

    @Override
    public PurchaseBackVo detail(Long id) {
        PurchaseBack PurchaseBack = this.getById(id);
        PurchaseBackVo result = BeanUtil.toBean(PurchaseBack, PurchaseBackVo.class);

        //赋值采购信息
        EhsdPurchase purchase = ehsdPurchaseService.getById(result.getPurchaseId());
        if (ObjectUtil.isNotEmpty(purchase)) {
            result.setPurchaseAmount(purchase.getAmount());

            //获取供应商
            SupplierInfo supplierInfo = supplierInfoService.getById(purchase.getSellCorporationId());
            if (ObjectUtil.isNotEmpty(supplierInfo)) {
                result.setSupplierId(supplierInfo.getId());
                result.setSupplierName(supplierInfo.getName());
            }

            List<PayDetail> payDetails = payDetailService.list(q -> q.eq(PayDetail::getPurchaseId, purchase.getId()));
            if (ObjectUtil.isNotEmpty(payDetails)) {
                BigDecimal payAmount = payDetails.stream().map(PayDetail::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                result.setPayAmount(payAmount);
            }
        }


        List<PurchaseBackDetails> list = purchaseBackDetailsService.list(q -> q.eq(PurchaseBackDetails::getPurchaseBackId, id));
        if (ObjectUtil.isEmpty(list)) {
            return result;
        }
        List<PurchaseBackDetailsVo> purchaseBackDetailsVos = BeanUtil.copyToList(list, PurchaseBackDetailsVo.class);
        //赋值产品信息
        productInfoService.attributeAssign(purchaseBackDetailsVos, PurchaseBackDetailsVo::getProductId, (detail, product) -> {
            detail.setProductCode(product.getCustomCode());
            detail.setProductName(product.getName());
            detail.setProductLength(product.getLength());
            detail.setProductWidth(product.getWidth());
            detail.setProductHeight(product.getHeight());
        });

        //赋值采购数量
        List<Long> pdIds = purchaseBackDetailsVos.stream().map(PurchaseBackDetails::getPurchaseDetailsId).collect(Collectors.toList());
        Map<Long, BigDecimal> ppMap = purchaseProductService.mapKV(EhsdPurchaseProduct::getId, EhsdPurchaseProduct::getQuantity, q -> q.in(EhsdPurchaseProduct::getId, pdIds));
        for (PurchaseBackDetailsVo purchaseBackDetailsVo : purchaseBackDetailsVos) {
            purchaseBackDetailsVo.setPurchaseQuantity(ppMap.get(purchaseBackDetailsVo.getPurchaseDetailsId()));
        }

        result.setPurchaseBackDetailsList(purchaseBackDetailsVos);

        return result;
    }

}
