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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.business.base.area.domain.entity.AreaEntity;
import net.lab1024.sa.admin.module.business.base.location.domain.entity.LocationEntity;
import net.lab1024.sa.admin.module.business.base.location.domain.vo.LocationVO;
import net.lab1024.sa.admin.module.business.base.location.manager.LocationManager;
import net.lab1024.sa.admin.module.business.base.location.service.LocationQueryService;
import net.lab1024.sa.admin.module.business.base.stock.dao.StockDao;
import net.lab1024.sa.admin.module.business.base.stock.domain.entity.StockEntity;
import net.lab1024.sa.admin.module.business.base.stock.domain.form.StockQueryForm;
import net.lab1024.sa.admin.module.business.base.stock.domain.form.StockSelect;
import net.lab1024.sa.admin.module.business.base.stock.domain.vo.StockVO;
import net.lab1024.sa.admin.module.business.base.stock.domain.vo.StocksExcelVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.module.support.dict.constant.DictConst;
import net.lab1024.sa.base.module.support.dict.service.DictService;
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.stream.Collectors;

@Service
public class StockQueryService {

    @Resource
    private StockDao stockDao;

    @Resource
    private LocationManager locationManager;

    @Resource
    private DictService dictService;

    @Resource
    private LocationQueryService locationQueryService;

    /**
     * 分页查询
     *
     * @param queryForm 查询参数
     * @return PageResult<StockVO>
     */
    public PageResult<StockVO> queryPage(StockQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<StockVO> list = stockDao.queryPage(page, queryForm);

        // 查询库位编码
        List<Long> locationIdList = list.stream().map(StockVO::getLocationId).distinct().collect(Collectors.toList());
        Map<Long, LocationEntity> locationMap = locationQueryService.queryLocationList(locationIdList);
        list.forEach(StockVO -> {
            LocationEntity location = locationMap.get(StockVO.getLocationId());
            if (location != null) {
                StockVO.setLocationCode(location.getLocationCode());
            }
        });
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 容器下拉查询
     *
     * @param stockSelect 入参
     * @return List<StockEntity>
     */
    public List<StockEntity> queryStock(StockSelect stockSelect) {
        LambdaQueryWrapper<StockEntity> queryWrapper = new LambdaQueryWrapper<>();
        //是否启用
        if (stockSelect.getDisabledFlag() != null) {
            queryWrapper.eq(StockEntity::getDisabledFlag, stockSelect.getDisabledFlag());
        }
        //状态
        if (StringUtils.isNotBlank(stockSelect.getStatus())) {
            queryWrapper.eq(StockEntity::getStatus, stockSelect.getStatus());
        }
        return stockDao.selectList(queryWrapper);
    }

    /**
     * 根据容器编码查询容器信息
     *
     * @param stockCode 容器编码
     * @return StockEntity
     */
    public StockEntity queryByStockCode(String stockCode) {
        LambdaQueryWrapper<StockEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StockEntity::getStockCode, stockCode);
        return stockDao.selectOne(queryWrapper);
    }

    /**
     * 根据容器编码集合查询容器信息
     *
     * @param stockCodes 容器料编码集合
     * @return List<StockEntity>
     */
    public List<StockEntity> queryByItemCodes(List<String> stockCodes) {
        LambdaQueryWrapper<StockEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(StockEntity::getStockCode, stockCodes);
        return stockDao.selectList(queryWrapper);
    }

    /**
     * 根据容器编码集合查询容器信息
     *
     * @param stockCodes 容器编码集合
     * @return Map<String, StockEntity>
     */
    public Map<String, StockEntity> queryStockListToMap(List<String> stockCodes) {
        if (CollectionUtils.isEmpty(stockCodes)) {
            return Collections.emptyMap();
        }
        List<StockEntity> stocks = queryByItemCodes(stockCodes);
        return stocks.stream()
                .collect(Collectors.toMap(StockEntity::getStockCode, stock -> stock, (existing, replacement) -> existing));

    }

    /**
     * 根据库位id查询容器信息
     *
     * @param locationId 库位
     * @return StockEntity
     */
    public StockEntity queryStockByLocationId(Long locationId) {
        LambdaQueryWrapper<StockEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StockEntity::getLocationId, locationId);
        return stockDao.selectOne(queryWrapper);
    }

    /**
     * 容器导出
     *
     * @return List<StocksExcelVO>
     */
    public List<StocksExcelVO> getStocksExcelVOList() {
        List<StockEntity> stocksList = stockDao.selectList(null);
        return stocksList.stream()
                .map(stock ->
                        StocksExcelVO.builder()
                                .stockCode(stock.getStockCode())
                                .stockType(dictService.getDictDataLabel(DictConst.STOCK_TYPE.getValue(), stock.getStockType()))
                                .locationCode(locationManager.queryLocation(stock.getLocationId()) == null ? "" : locationManager.queryLocation(stock.getLocationId()).getLocationCode())
                                .disabledFlag(stock.getDisabledFlag() ? "启用" : "禁用")
                                .build()
                )
                .collect(Collectors.toList());
    }
}
