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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.entity.product.vo.ProductInfoVo;
import com.fjhx.item.service.product.ProductClassifyService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.wms.entity.warehouse.dto.WarehouseLocationInfoDto;
import com.fjhx.wms.entity.warehouse.po.WarehouseLocationInfo;
import com.fjhx.wms.entity.warehouse.vo.WarehouseLocationInfoVo;
import com.fjhx.wms.mapper.warehouse.WarehouseLocationInfoMapper;
import com.fjhx.wms.service.warehouse.WarehouseLocationInfoService;
import com.ruoyi.common.core.domain.BaseIdPo;
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.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库库位信息 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-18
 */
@Service
public class WarehouseLocationInfoServiceImpl extends ServiceImpl<WarehouseLocationInfoMapper, WarehouseLocationInfo> implements WarehouseLocationInfoService {

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

    @Override
    public List<WarehouseLocationInfoVo> getList(WarehouseLocationInfoDto dto) {
        List<WarehouseLocationInfo> list = list(q -> q.eq(WarehouseLocationInfo::getWarehouseId, dto.getWarehouseId()));
        List<WarehouseLocationInfoVo> warehouseLocationInfoVos = BeanUtil.copyToList(list, WarehouseLocationInfoVo.class);
        for (WarehouseLocationInfoVo warehouseLocationInfoVo : warehouseLocationInfoVos) {
            String productIds = warehouseLocationInfoVo.getProductIds();
            if (ObjectUtil.isNotEmpty(productIds)) {
                String[] split = productIds.split(",");
                List<ProductInfo> productInfoList = productInfoService.list(q -> q.in(ProductInfo::getId, split));
                List<ProductInfoVo> productInfoVos = BeanUtil.copyToList(productInfoList, ProductInfoVo.class);
                //赋值产品分类
                List<ProductClassify> productClassifyList = productClassifyService.list();
                Map<Long, ProductClassify> productClassifyMap = productClassifyList.stream().collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
                for (ProductInfoVo productInfo : productInfoVos) {
                    Long productClassifyId = productInfo.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());
                    }
                    productInfo.setClassifyNameGroup(classifyNameGroup);
                    productInfo.setProductClassifyNames(classifyNameGroup.stream().collect(Collectors.joining(" / ")));
                }
                warehouseLocationInfoVo.setProductInfoList(productInfoVos);
            }
        }
        return warehouseLocationInfoVos;
    }

    @Override
    public void edit(WarehouseLocationInfoDto warehouseLocationInfoDto) {
        List<WarehouseLocationInfo> warehouseLocationInfoList = warehouseLocationInfoDto.getWarehouseLocationInfoList();
        warehouseLocationInfoList.forEach(item -> item.setWarehouseId(warehouseLocationInfoDto.getWarehouseId()));
        this.editLinked(warehouseLocationInfoList, WarehouseLocationInfo::getWarehouseId, warehouseLocationInfoDto.getWarehouseId());
    }

}
