package com.zg.project.information.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.zg.common.utils.StringUtils;
import com.zg.project.information.domain.vo.WarehouseChildVO;
import com.zg.project.information.domain.vo.WarehouseGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zg.project.information.mapper.WarehouseInfoMapper;
import com.zg.project.information.domain.WarehouseInfo;
import com.zg.project.information.service.IWarehouseInfoService;

/**
 * 仓库信息Service业务层处理
 * 
 * @author zg
 * @date 2025-05-27
 */
@Service
public class WarehouseInfoServiceImpl implements IWarehouseInfoService 
{
    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;

    /**
     * 查询仓库信息
     * 
     * @param id 仓库信息主键
     * @return 仓库信息
     */
    @Override
    public WarehouseInfo selectWarehouseInfoById(Long id)
    {
        return warehouseInfoMapper.selectWarehouseInfoById(id);
    }

    /**
     * 查询仓库信息列表
     * 
     * @param warehouseInfo 仓库信息
     * @return 仓库信息
     */
    @Override
    public List<WarehouseInfo> selectWarehouseInfoList(WarehouseInfo warehouseInfo)
    {
        return warehouseInfoMapper.selectWarehouseInfoList(warehouseInfo);
    }

    /**
     * 新增仓库信息
     * 
     * @param warehouseInfo 仓库信息
     * @return 结果
     */
    @Override
    public int insertWarehouseInfo(WarehouseInfo warehouseInfo)
    {
        return warehouseInfoMapper.insertWarehouseInfo(warehouseInfo);
    }

    /**
     * 修改仓库信息
     * 
     * @param warehouseInfo 仓库信息
     * @return 结果
     */
    @Override
    public int updateWarehouseInfo(WarehouseInfo warehouseInfo)
    {
        return warehouseInfoMapper.updateWarehouseInfo(warehouseInfo);
    }

    /**
     * 批量删除仓库信息
     * 
     * @param ids 需要删除的仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseInfoByIds(Long[] ids)
    {
        return warehouseInfoMapper.deleteWarehouseInfoByIds(ids);
    }

    /**
     * 删除仓库信息信息
     * 
     * @param id 仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseInfoById(Long id)
    {
        return warehouseInfoMapper.deleteWarehouseInfoById(id);
    }

    /**
     * ⭐ 查询大仓及其包含的小仓（嵌套结构）
     */
    @Override
    public List<WarehouseGroupVO> selectWarehouseGroupList() {

        // 1. 查询全部仓库
        List<WarehouseInfo> allList = warehouseInfoMapper.selectWarehouseInfoList(new WarehouseInfo());
        if (allList == null || allList.isEmpty()) {
            return new ArrayList<>();
        }

        // 2. 按 parent_warehouse_code 分组（大仓 → 小仓）
        Map<String, WarehouseGroupVO> groupMap = new LinkedHashMap<>();

        for (WarehouseInfo w : allList) {

            // 所属大仓编码/名称
            String parentCode = w.getParentWarehouseCode();
            String parentName = w.getParentWarehouseName();

            // 如果 parent_warehouse_code 为空，则自己就是一个大仓
            if (StringUtils.isBlank(parentCode)) {
                parentCode = w.getWarehouseCode();
                parentName = w.getWarehouseName();
            }

            // 2.1 获取或创建大仓节点
            WarehouseGroupVO groupVO = groupMap.get(parentCode);
            if (groupVO == null) {
                groupVO = new WarehouseGroupVO();
                groupVO.setWarehouseCode(parentCode);
                groupVO.setWarehouseName(parentName);
                groupVO.setChildren(new ArrayList<>());
                groupMap.put(parentCode, groupVO);
            }

            // 2.2 把当前仓库作为“小仓”加入 children
            WarehouseChildVO childVO = new WarehouseChildVO();
            childVO.setId(w.getId());
            childVO.setWarehouseCode(w.getWarehouseCode());
            childVO.setWarehouseName(w.getWarehouseName());
            childVO.setAddress(w.getAddress());
            childVO.setParentWarehouseCode(w.getParentWarehouseCode());
            childVO.setParentWarehouseName(w.getParentWarehouseName());
            childVO.setRemark(w.getRemark());
            childVO.setCreatedAt(w.getCreatedAt());
            childVO.setUpdatedAt(w.getUpdatedAt());

            groupVO.getChildren().add(childVO);
        }

        // 3. 返回树结构
        return new ArrayList<>(groupMap.values());
    }

}
