package net.lab1024.sa.admin.module.business.wms.base.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.business.wms.base.area.dao.AreaDao;
import net.lab1024.sa.admin.module.business.wms.base.area.domain.entity.AreaEntity;
import net.lab1024.sa.admin.module.business.wms.base.area.domain.form.AreaQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.area.domain.form.AreaSelect;
import net.lab1024.sa.admin.module.business.wms.base.area.domain.vo.AreaVO;
import net.lab1024.sa.admin.module.business.wms.base.area.manager.AreaManager;
import net.lab1024.sa.admin.module.business.wms.base.area.service.AreaQueryService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class AreaQueryServiceImpl implements AreaQueryService {

    @Resource
    private AreaDao areaDao;

    @Resource
    private AreaManager areaManager;

    /**
     * 分页查询
     *
     * @param queryForm 查询参数
     * @return PageResult<AreaVO>
     */
    public PageResult<AreaVO> queryPage(AreaQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<AreaVO> list = areaDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 库区下拉查询
     *
     * @param areaSelect 入参
     * @return List<AreaEntity>
     */
    public List<AreaEntity> queryArea(AreaSelect areaSelect) {
        LambdaQueryWrapper<AreaEntity> queryWrapper = new LambdaQueryWrapper<>();
        //是否启用
        if (areaSelect.getDisabledFlag() != null) {
            queryWrapper.eq(AreaEntity::getDisabledFlag, areaSelect.getDisabledFlag());
        }
        //库区名称
        if (CollectionUtils.isNotEmpty(areaSelect.getAreaNames())) {
            queryWrapper.in(AreaEntity::getAreaName, areaSelect.getAreaNames());
        }
        return areaManager.list(queryWrapper);
    }

    /**
     * 根据库区id集合查询库区信息
     *
     * @param areaIdList 库区id集合
     * @return Map<Long, AreaEntity>
     */
    public Map<Long, AreaEntity> queryByAreaIds(List<Long> areaIdList) {
        if (CollectionUtils.isEmpty(areaIdList)) {
            return Collections.emptyMap();
        }
        //去重
        areaIdList = areaIdList.stream().filter(Objects::nonNull).distinct().toList();
        //封装map
        Map<Long, AreaEntity> areaMap = Maps.newHashMap();
        for (Long areaId : areaIdList) {
            AreaEntity area = areaManager.queryArea(areaId);
            if (area != null) {
                areaMap.put(areaId, area);
            }
        }
        return areaMap;
    }

    /**
     * 根据库区name集合查询库区信息
     *
     * @param areaNameList 库区name集合
     * @return List<AreaEntity>
     */
    public List<AreaEntity> queryByAreaNames(List<String> areaNameList) {
        if (CollectionUtils.isEmpty(areaNameList)) {
            return Collections.emptyList();
        }
        //去重
        areaNameList = areaNameList.stream().filter(Objects::nonNull).distinct().toList();
        //查询库区信息
        LambdaQueryWrapper<AreaEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AreaEntity::getAreaName, areaNameList);
        return areaManager.list(queryWrapper);
    }

    /**
     * 根据库区name集合查询库区信息
     *
     * @param areaNameList 库区name集合
     * @return Map<Long, AreaEntity>
     */
    public Map<String, AreaEntity> queryByAreaNamesToMap(List<String> areaNameList) {
        if (CollectionUtils.isEmpty(areaNameList)) {
            return Collections.emptyMap();
        }
        //查询库区信息
        List<AreaEntity> areaList = queryByAreaNames(areaNameList);
        //封装map
        Map<String, AreaEntity> areaMap = Maps.newHashMap();
        for (AreaEntity area : areaList) {
            areaMap.put(area.getAreaName(), area);
        }
        return areaMap;
    }

    /**
     * 根据库区名称查询库区信息
     *
     * @param areaName 库区名称
     * @return AreaEntity
     */
    public AreaEntity queryByAreaName(String areaName) {
        if (StringUtils.isBlank(areaName)) {
            return null;
        }
        LambdaQueryWrapper<AreaEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AreaEntity::getAreaName, areaName);
        return areaManager.getOne(queryWrapper);
    }

}
