package net.lab1024.sa.admin.module.business.receive.asnDetail.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.address.domain.entity.AddressEntity;
import net.lab1024.sa.admin.module.business.base.area.domain.entity.AreaEntity;
import net.lab1024.sa.admin.module.business.base.customer.domain.entity.CustomerEntity;
import net.lab1024.sa.admin.module.business.base.item.domain.entity.ItemEntity;
import net.lab1024.sa.admin.module.business.base.item.manager.ItemManager;
import net.lab1024.sa.admin.module.business.base.item.service.ItemQueryService;
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.receive.asn.domain.vo.AsnVO;
import net.lab1024.sa.admin.module.business.receive.asnDetail.dao.AsnDetailDao;
import net.lab1024.sa.admin.module.business.receive.asnDetail.domain.entity.AsnDetailEntity;
import net.lab1024.sa.admin.module.business.receive.asnDetail.domain.form.AsnDetailQueryForm;
import net.lab1024.sa.admin.module.business.receive.asnDetail.domain.vo.AsnDetailVO;
import net.lab1024.sa.admin.module.business.receive.asnDetail.manager.AsnDetailManager;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AsnDetailQueryService {

    @Resource
    private AsnDetailDao asnDetailDao;

    @Resource
    private ItemManager itemManager;

    @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).distinct().collect(Collectors.toList());
        Map<Long, ItemEntity> itemMap = itemQueryService.queryItemList(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> queryByAsnId(Long asnId) {
        if (asnId == null) {
            return null;
        }
        LambdaQueryWrapper<AsnDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AsnDetailEntity::getAsnId, asnId);
        return asnDetailDao.selectList(queryWrapper);
    }

    /**
     * 根据入库单ids查询入库单详情
     *
     * @param asnIds 入库单id
     * @return List<AsnDetailEntity>
     */
    public List<AsnDetailEntity> queryByAsnIds(List<Long> asnIds) {
        if (CollectionUtils.isEmpty(asnIds)) {
            return null;
        }
        LambdaQueryWrapper<AsnDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AsnDetailEntity::getAsnId, asnIds);
        return asnDetailDao.selectList(queryWrapper);
    }

    /**
     * 根据ids查询入库明细
     *
     * @param idList 入库明细集合
     * @return List<AsnDetailEntity>
     */
    public List<AsnDetailEntity> queryAsnDetailList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return null;
        }
        LambdaQueryWrapper<AsnDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AsnDetailEntity::getAsnDetailId, idList);
        return asnDetailDao.selectList(queryWrapper);
    }
}
