package net.lab1024.sa.admin.module.business.base.item.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.item.dao.ItemDao;
import net.lab1024.sa.admin.module.business.base.item.domain.entity.ItemEntity;
import net.lab1024.sa.admin.module.business.base.item.domain.form.ItemQueryForm;
import net.lab1024.sa.admin.module.business.base.item.domain.form.ItemSelect;
import net.lab1024.sa.admin.module.business.base.item.domain.vo.ItemVO;
import net.lab1024.sa.admin.module.business.base.item.domain.vo.ItemsExcelVO;
import net.lab1024.sa.admin.module.business.base.item.manager.ItemManager;
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.springframework.stereotype.Service;

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

@Service
public class ItemQueryService {

    @Resource
    private ItemDao itemDao;

    @Resource
    private ItemManager itemManager;

    @Resource
    private DictService dictService;

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

    /**
     * 物料下拉查询
     *
     * @param itemSelect 入参
     * @return List<ItemEntity>
     */
    public List<ItemEntity> queryItem(ItemSelect itemSelect) {
        LambdaQueryWrapper<ItemEntity> queryWrapper = new LambdaQueryWrapper<>();
        //是否启用
        if (itemSelect.getDisabledFlag() != null) {
            queryWrapper.eq(ItemEntity::getDisabledFlag, itemSelect.getDisabledFlag());
        }
        return itemDao.selectList(queryWrapper);
    }

    /**
     * 根据物料id集合查询物料信息
     *
     * @param itemIdList 客户id集合
     * @return Map<Long, AreaEntity>
     */
    public Map<Long, ItemEntity> queryItemList(List<Long> itemIdList) {
        if (CollectionUtils.isEmpty(itemIdList)) {
            return Collections.emptyMap();
        }
        itemIdList = itemIdList.stream().distinct().collect(Collectors.toList());
        Map<Long, ItemEntity> itemMap = Maps.newHashMap();
        for (Long itemId : itemIdList) {
            ItemEntity item = itemManager.queryItem(itemId);
            if (item != null) {
                itemMap.put(itemId, item);
            }
        }
        return itemMap;
    }

    /**
     * 根据物料编码查询
     *
     * @param itemCode 物料编码
     * @return ItemEntity
     */
    public ItemEntity queryByItemCode(String itemCode) {
        LambdaQueryWrapper<ItemEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ItemEntity::getItemCode, itemCode);
        return itemDao.selectOne(queryWrapper);
    }

    /**
     * 根据物料编码集合查询物料信息
     *
     * @param itemCodes 物料编码集合
     * @return List<ItemEntity>
     */
    public List<ItemEntity> queryByItemCodes(List<String> itemCodes) {
        LambdaQueryWrapper<ItemEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ItemEntity::getItemCode, itemCodes);
        return itemDao.selectList(queryWrapper);
    }

    /**
     * 根据物料编码集合查询物料信息
     *
     * @param itemCodes 物料集合
     * @return Map<String, ItemEntity>
     */
    public Map<String, ItemEntity> queryItemListToMap(List<String> itemCodes) {
        if (CollectionUtils.isEmpty(itemCodes)) {
            return Collections.emptyMap();
        }
        List<ItemEntity> items = queryByItemCodes(itemCodes);
        return items.stream()
                .collect(Collectors.toMap(ItemEntity::getItemCode, item -> item, (existing, replacement) -> existing));

    }

    /**
     * 物料导出
     *
     * @return List<LocationsExcelVO>
     */
    public List<ItemsExcelVO> getItemsExcelVOList() {
        List<ItemEntity> itemsList = itemDao.selectList(null);
        return itemsList.stream()
                .map(item ->
                        ItemsExcelVO.builder()
                                .itemCode(item.getItemCode())
                                .itemName(item.getItemName())
                                .itemType(dictService.getDictDataLabel(DictConst.ITEM_TYPE.getValue(), item.getItemType()))
                                .unit(dictService.getDictDataLabel(DictConst.ITEM_UNIT.getValue(), item.getUnit()))
                                .disabledFlag(item.getDisabledFlag() ? "启用" : "禁用")
                                .build()
                )
                .collect(Collectors.toList());
    }
}
