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

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.base.area.dao.AreaDao;
import net.lab1024.sa.admin.module.business.base.area.domain.entity.AreaEntity;
import net.lab1024.sa.admin.module.business.base.area.domain.form.AreaQueryForm;
import net.lab1024.sa.admin.module.business.base.area.domain.form.AreaSelect;
import net.lab1024.sa.admin.module.business.base.area.domain.vo.AreaVO;
import net.lab1024.sa.admin.module.business.base.area.manager.AreaManager;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class 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 areaDao.selectList(queryWrapper);
    }

    /**
     * 根据库区id集合查询库区信息
     *
     * @param areaIdList 库区id集合
     * @return Map<Long, AreaEntity>
     */
    public Map<Long, AreaEntity> queryAreaList(List<Long> areaIdList) {
        if (CollectionUtils.isEmpty(areaIdList)) {
            return Collections.emptyMap();
        }
        areaIdList = areaIdList.stream().distinct().collect(Collectors.toList());
        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 Map<Long, AreaEntity>
     */
    public Map<String, AreaEntity> queryAreaByNameList(List<String> areaNameList) {
        if (CollectionUtils.isEmpty(areaNameList)) {
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<AreaEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AreaEntity::getAreaName, areaNameList);

        List<AreaEntity> areaList = areaDao.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(areaList)) {
            return Collections.emptyMap();
        }
        return areaList.stream()
                .collect(Collectors.toMap(AreaEntity::getAreaName, area -> area, (existing, replacement) -> existing));
    }

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

}
