package net.lab1024.sa.admin.module.business.wms.receive.asnDetail.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.item.domain.entity.ItemEntity;
import net.lab1024.sa.admin.module.business.wms.base.item.service.ItemQueryService;
import net.lab1024.sa.admin.module.business.wms.receive.asnDetail.dao.AsnDetailDao;
import net.lab1024.sa.admin.module.business.wms.receive.asnDetail.domain.entity.AsnDetailEntity;
import net.lab1024.sa.admin.module.business.wms.receive.asnDetail.domain.form.AsnDetailQueryForm;
import net.lab1024.sa.admin.module.business.wms.receive.asnDetail.domain.vo.AsnDetailVO;
import net.lab1024.sa.admin.module.business.wms.receive.asnDetail.manager.AsnDetailManager;
import net.lab1024.sa.admin.module.business.wms.receive.asnDetail.service.AsnDetailQueryService;
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.Objects;

@Service
public class AsnDetailQueryServiceImpl implements AsnDetailQueryService {

    @Resource
    private AsnDetailDao asnDetailDao;

    @Resource
    private AsnDetailManager asnDetailManager;

    @Resource
    private ItemQueryService itemQueryService;

    /**
     * 分页查询
     *
     * @param queryForm 查询条件
     * @return PageResult<AsnDetailVO>
     */
    public PageResult<AsnDetailVO> queryPage(AsnDetailQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<AsnDetailVO> list = asnDetailDao.queryPage(page, queryForm);
        // 查询物料名称
        List<Long> itemIdList = list.stream().map(AsnDetailVO::getItemId).toList();
        Map<Long, ItemEntity> itemMap = itemQueryService.queryByItemIdsToMap(itemIdList);
        list.forEach(asnDetailVO -> {
            ItemEntity item = itemMap.get(asnDetailVO.getItemId());
            if (item != null) {
                asnDetailVO.setItemCode(item.getItemCode());
                asnDetailVO.setItemName(item.getItemName());
            }
        });

        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 根据入库单id查询入库单详情
     *
     * @param asnId 入库单id
     * @return List<AsnDetailEntity>
     */
    public List<AsnDetailEntity> queryAsnDetailByAsnId(Long asnId) {
        if (asnId == null) {
            return null;
        }
        LambdaQueryWrapper<AsnDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AsnDetailEntity::getAsnId, asnId);
        return asnDetailManager.list(queryWrapper);
    }

    /**
     * 根据入库单ids查询入库单详情
     *
     * @param asnIds 入库单id
     * @return List<AsnDetailEntity>
     */
    public List<AsnDetailEntity> queryAsnDetailByAsnIds(List<Long> asnIds) {
        if (CollectionUtils.isEmpty(asnIds)) {
            return Collections.emptyList();
        }
        //去重
        asnIds = asnIds.stream().filter(Objects::nonNull).distinct().toList();
        LambdaQueryWrapper<AsnDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AsnDetailEntity::getAsnId, asnIds);
        return asnDetailManager.list(queryWrapper);
    }

    /**
     * 根据ids查询入库明细
     *
     * @param idList 入库明细集合
     * @return List<AsnDetailEntity>
     */
    public List<AsnDetailEntity> queryByAsnDetailIds(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyList();
        }
        //去重
        idList = idList.stream().filter(Objects::nonNull).distinct().toList();
        return asnDetailManager.listByIds(idList);
    }

    /**
     * 根据明细集合查询明细信息
     *
     * @param idList 明细集合
     * @return Map<String, AsnDetailEntity>
     */
    public Map<Long, AsnDetailEntity> queryByAsnDetailIdsToMap(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyMap();
        }
        //查询明细
        List<AsnDetailEntity> asnDetails = queryByAsnDetailIds(idList);
        //封装map
        Map<Long, AsnDetailEntity> asnDetailMap = Maps.newHashMap();
        for (AsnDetailEntity asnDetail : asnDetails) {
            asnDetailMap.put(asnDetail.getAsnDetailId(), asnDetail);
        }
        return asnDetailMap;
    }
}
