package com.fjhx.wms.service.arrival.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.wms.entity.QualityDetailsPo;
import com.fjhx.wms.entity.arrival.dto.ArrivalStockRecordsDto;
import com.fjhx.wms.entity.arrival.dto.ArrivalStockRecordsSelectDto;
import com.fjhx.wms.entity.arrival.po.ArrivalStockRecords;
import com.fjhx.wms.entity.arrival.po.ArrivalStockRecordsDetails;
import com.fjhx.wms.entity.arrival.vo.ArrivalStockRecordsDetailsVo;
import com.fjhx.wms.entity.arrival.vo.ArrivalStockRecordsVo;
import com.fjhx.wms.mapper.arrival.ArrivalStockRecordsMapper;
import com.fjhx.wms.service.WmsService;
import com.fjhx.wms.service.arrival.ArrivalStockRecordsDetailsService;
import com.fjhx.wms.service.arrival.ArrivalStockRecordsService;
import com.googlecode.aviator.utils.ArrayHashMap;
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-26
 */
@Service
public class ArrivalStockRecordsServiceImpl extends ServiceImpl<ArrivalStockRecordsMapper, ArrivalStockRecords> implements ArrivalStockRecordsService {

    @Autowired
    ArrivalStockRecordsDetailsService arrivalStockRecordsDetailsService;
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    private WmsService wmsService;

    @Override
    public Page<ArrivalStockRecordsVo> getPage(ArrivalStockRecordsSelectDto dto) {
        IWrapper<ArrivalStockRecords> wrapper = getWrapper();
        wrapper.orderByDesc("asr", ArrivalStockRecords::getId);
        Page<ArrivalStockRecordsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

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

        List<ArrivalStockRecordsDetails> arrivalStockRecordsDetails = arrivalStockRecordsDetailsService.list(q -> q.eq(ArrivalStockRecordsDetails::getArrivalStockRecordsId, result.getId()));
        List<ArrivalStockRecordsDetailsVo> arrivalStockRecordsDetailsVos = BeanUtil.copyToList(arrivalStockRecordsDetails, ArrivalStockRecordsDetailsVo.class);

        //获取已质检信息
        List<Long> ids = arrivalStockRecordsDetailsVos.stream().map(ArrivalStockRecordsDetails::getId).distinct().collect(Collectors.toList());
        List<QualityDetailsPo> qualityDetailsList = wmsService.getQualityDetailsList(ids);
        Map<Long, List<QualityDetailsPo>> qualityDetailsMap = new ArrayHashMap<>();
        if (ObjectUtil.isNotEmpty(qualityDetailsList)) {
            qualityDetailsMap = qualityDetailsList.stream().collect(Collectors.groupingBy(QualityDetailsPo::getArrivalStockRecordsDetailsId));
        }
        for (ArrivalStockRecordsDetailsVo arrivalStockRecordsDetailsVo : arrivalStockRecordsDetailsVos) {
            List<QualityDetailsPo> qualityDetailsPos = qualityDetailsMap.get(arrivalStockRecordsDetailsVo);
            if (ObjectUtil.isEmpty(qualityDetailsPos)) {
                arrivalStockRecordsDetailsVo.setTotalQuantity(BigDecimal.ZERO);
                arrivalStockRecordsDetailsVo.setQualifiedQuantity(BigDecimal.ZERO);
                arrivalStockRecordsDetailsVo.setDisqualificationQuantity(BigDecimal.ZERO);
                continue;
            }
            BigDecimal totalQuantity = qualityDetailsPos.stream().map(QualityDetailsPo::getTotalQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal qualifiedQuantity = qualityDetailsPos.stream().map(QualityDetailsPo::getQualifiedQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal disqualificationQuantity = qualityDetailsPos.stream().map(QualityDetailsPo::getDisqualificationQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);

            arrivalStockRecordsDetailsVo.setTotalQuantity(totalQuantity);
            arrivalStockRecordsDetailsVo.setQualifiedQuantity(qualifiedQuantity);
            arrivalStockRecordsDetailsVo.setDisqualificationQuantity(disqualificationQuantity);
        }

        //赋值产品信息
        productInfoService.attributeAssign(arrivalStockRecordsDetailsVos, ArrivalStockRecordsDetailsVo::getProductInfoId, (item, productInfo) -> {
            item.setProductName(productInfo.getName());
            item.setProductType(productInfo.getType());
            item.setProductCode(productInfo.getCode());
            item.setProductUnit(productInfo.getUnit());
            item.setProductSpec(productInfo.getSpec());
            item.setProductDefinition(productInfo.getDefinition());
            item.setProductCustomCode(productInfo.getCustomCode());
        });
        result.setArrivalStockRecordsDetailsList(arrivalStockRecordsDetailsVos);
        return result;
    }

    @Override
    public void add(ArrivalStockRecordsDto arrivalStockRecordsDto) {
        this.save(arrivalStockRecordsDto);
    }

    @Override
    public void edit(ArrivalStockRecordsDto arrivalStockRecordsDto) {
        this.updateById(arrivalStockRecordsDto);
    }

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

}
