package com.fjhx.victoriatourist.service.deliver.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.purchase.po.PurchaseDetail;
import com.fjhx.purchase.entity.purchase.vo.PurchaseDetailVo;
import com.fjhx.purchase.entity.purchase.vo.PurchaseVo;
import com.fjhx.purchase.service.purchase.PurchaseDetailService;
import com.fjhx.purchase.service.purchase.PurchaseService;
import com.fjhx.supply.entity.supplier.po.SupplierPrice;
import com.fjhx.victoriatourist.entity.deliver.dto.DeliverGoodsDetailsDto;
import com.fjhx.victoriatourist.entity.deliver.dto.DeliverGoodsDetailsSelectDto;
import com.fjhx.victoriatourist.entity.deliver.po.DeliverGoodsDetails;
import com.fjhx.victoriatourist.entity.deliver.vo.DeliverGoodsDetailsVo;
import com.fjhx.victoriatourist.mapper.deliver.DeliverGoodsDetailsMapper;
import com.fjhx.victoriatourist.service.deliver.DeliverGoodsDetailsService;
import com.ruoyi.common.utils.wrapper.IWrapper;
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>
 * 发货明细 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-13
 */
@Service
public class DeliverGoodsDetailsServiceImpl extends ServiceImpl<DeliverGoodsDetailsMapper, DeliverGoodsDetails> implements DeliverGoodsDetailsService {

    @Autowired
    PurchaseDetailService purchaseDetailService;
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    ProductInfoService productInfoService;

    @Override
    public Page<DeliverGoodsDetailsVo> getPage(DeliverGoodsDetailsSelectDto dto) {
        IWrapper<DeliverGoodsDetails> wrapper = getWrapper();
        wrapper.orderByDesc("dgd", DeliverGoodsDetails::getId);
        Page<DeliverGoodsDetailsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public List<PurchaseDetailVo> detail(Long id) {
        PurchaseVo detail = purchaseService.detail(id);
        List<PurchaseDetail> purchaseDetailList1 = detail.getPurchaseDetailList();
        List<PurchaseDetailVo> purchaseDetailVoList = BeanUtil.copyToList(purchaseDetailList1, PurchaseDetailVo.class);
        //根据采购明细查发货明细
        List<Long> pdids = purchaseDetailVoList.stream().map(PurchaseDetail::getId).collect(Collectors.toList());
        List<DeliverGoodsDetails> list = list(q -> q.in(DeliverGoodsDetails::getPurchaseDetailId, pdids));
        Map<Long, List<DeliverGoodsDetails>> collect = list.stream().collect(Collectors.groupingBy(DeliverGoodsDetails::getPurchaseDetailId));
        for (PurchaseDetailVo purchaseDetail:purchaseDetailVoList){
            //赋值已发货数量
            purchaseDetail.setDeliverGoodsQuantity(BigDecimal.ZERO);
            List<DeliverGoodsDetails> deliverGoodsDetailsList1 = collect.get(purchaseDetail.getId());
            if (ObjectUtil.isEmpty(deliverGoodsDetailsList1)){
                continue;
            }
            BigDecimal count = deliverGoodsDetailsList1.stream()
                    .map(DeliverGoodsDetails::getDeliverGoodsQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            purchaseDetail.setDeliverGoodsQuantity(count);
        }
        //赋值产品信息
        productInfoService.attributeAssign(purchaseDetailVoList, PurchaseDetailVo::getBussinessId, (item, product) -> {
            item.setProductDefinition(product.getDefinition());
            item.setProductType(product.getType());
            item.setProductCode(product.getCode());
            item.setProductName(product.getName());
            item.setProductSpec(product.getSpec());
            item.setProductUnit(product.getUnit());
        });

        return purchaseDetailVoList;
    }

    @Override
    public void add(DeliverGoodsDetailsDto deliverGoodsDetailsDto) {
        this.save(deliverGoodsDetailsDto);
    }

    @Override
    public void edit(DeliverGoodsDetailsDto deliverGoodsDetailsDto) {
        this.updateById(deliverGoodsDetailsDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
