package com.fjhx.wms.service.stock.impl;

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.entity.product.po.ProductClassify;
import com.fjhx.item.entity.product.po.ProductInfo;
import com.fjhx.item.service.product.ProductClassifyService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.wms.entity.stock.dto.StockSnapshotDetailsSelectDto;
import com.fjhx.wms.entity.stock.po.Stock;
import com.fjhx.wms.entity.stock.po.StockSnapshotDetails;
import com.fjhx.wms.entity.stock.vo.StockSnapshotDetailsVo;
import com.fjhx.wms.mapper.stock.StockSnapshotDetailsMapper;
import com.fjhx.wms.service.stock.StockSnapshotDetailsService;
import com.ruoyi.common.core.domain.BaseIdPo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * <p>
 * 库存快照明细 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-17
 */
@Service
public class StockSnapshotDetailsServiceImpl extends ServiceImpl<StockSnapshotDetailsMapper, StockSnapshotDetails> implements StockSnapshotDetailsService {

    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductClassifyService productClassifyService;

    @Override
    public Page<StockSnapshotDetailsVo> getPage(StockSnapshotDetailsSelectDto dto) {
        IWrapper<StockSnapshotDetails> wrapper = getWrapper();

        //快照id
        wrapper.eq("ssd", StockSnapshotDetails::getSnapshotId, dto.getSnapshotId());

        wrapper.eq(Stock::getWarehouseId, dto.getId());
        String keyword = dto.getKeyword();
        if (ObjectUtil.isNotEmpty(keyword)) {
            //根据 库存数量 产品自定义编码 产品名称 过滤
            List<Long> productIds = productInfoService.listObject(ProductInfo::getId,
                    q -> q.like(ProductInfo::getCustomCode, keyword).or().like(ProductInfo::getName, keyword));
            wrapper.and(q -> q.like(Stock::getQuantity, keyword).or().in(Stock::getProductId, productIds));
        }
        wrapper.in(Stock::getProductId, dto.getProductIds());
        wrapper.eq("pi.definition", dto.getDefinition());

        wrapper.eq("pi.product_classify_id", dto.getProductClassifyId());

        //权限过滤：库存快照-子公司看自己的,总公司看全部
        Long companyId = SecurityUtils.getCompanyId();
        if (!Objects.equals(companyId, 100L)) {
            wrapper.eq("ssd.company_id", companyId);
        } else {
            wrapper.eq("ssd.company_id", dto.getCompanyId());
        }

        wrapper.orderByDesc("ssd", StockSnapshotDetails::getId);
        Page<StockSnapshotDetailsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        List<StockSnapshotDetailsVo> records = page.getRecords();

        //赋值产品分类
        productClassifyService.attributeAssign(records, StockSnapshotDetailsVo::getProductClassifyId, (item, productClassify) -> {
            item.setProductClassifyName(productClassify.getName());
        });
        //赋值产品分类树
        List<ProductClassify> productClassifyList = productClassifyService.list();
        Map<Long, ProductClassify> productClassifyMap = productClassifyList.stream().collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
        for (StockSnapshotDetailsVo vo : records) {
            Long productClassifyId = vo.getProductClassifyId();
            ProductClassify productClassify = productClassifyMap.get(productClassifyId);
            if (productClassify == null) {
                continue;
            }
            List<String> classifyNameGroup = new ArrayList<>();

            while (productClassify != null) {
                classifyNameGroup.add(0, productClassify.getName());
                productClassify = productClassifyMap.get(productClassify.getParentId());
            }
            vo.setProductClassifyNameGroup(classifyNameGroup);
            vo.setProductClassifyNames(classifyNameGroup.stream().collect(Collectors.joining(" / ")));
        }

        return page;
    }

}
