package net.lab1024.sa.admin.module.business.wms.shipping.pick.service.impl;

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.wms.base.address.domain.entity.AddressEntity;
import net.lab1024.sa.admin.module.business.wms.base.address.service.AddressQueryService;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.entity.CustomerEntity;
import net.lab1024.sa.admin.module.business.wms.base.customer.service.CustomerQueryService;
import net.lab1024.sa.admin.module.business.wms.receive.asn.domain.entity.AsnEntity;
import net.lab1024.sa.admin.module.business.wms.receive.asn.domain.vo.AsnVO;
import net.lab1024.sa.admin.module.business.wms.shipping.pick.dao.PickDao;
import net.lab1024.sa.admin.module.business.wms.shipping.pick.domain.entity.PickEntity;
import net.lab1024.sa.admin.module.business.wms.shipping.pick.domain.form.PickQueryForm;
import net.lab1024.sa.admin.module.business.wms.shipping.pick.domain.vo.PickVO;
import net.lab1024.sa.admin.module.business.wms.shipping.pick.manager.PickManager;
import net.lab1024.sa.admin.module.business.wms.shipping.pick.service.PickQueryService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.util.SmartPageUtil;
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.Objects;

@Service
public class PickQueryServiceImpl implements PickQueryService {

    @Resource
    private PickDao pickDao;

    @Resource
    private PickManager pickManager;

    @Resource
    private CustomerQueryService customerQueryService;

    @Resource
    private AddressQueryService addressQueryService;

    /**
     * 分页查询
     */
    public PageResult<PickVO> queryPage(PickQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PickVO> list = pickDao.queryPage(page, queryForm);

        // 查询客户名称
        List<Long> customerIdList = list.stream().map(PickVO::getCustomerId).toList();
        Map<Long, CustomerEntity> customerMap = customerQueryService.queryByCustomerIdsToMap(customerIdList);

        // 查询收货单位
        List<Long> addressIdList = list.stream().map(PickVO::getAddressId).toList();
        Map<Long, AddressEntity> addressMap = addressQueryService.queryByAddressIdsToMap(addressIdList);

        list.forEach(pickVO -> {
            CustomerEntity customer = customerMap.get(pickVO.getCustomerId());
            if (customer != null) {
                pickVO.setCustomerName(customer.getCustomerName());
            }
            AddressEntity address = addressMap.get(pickVO.getAddressId());
            if (address != null) {
                pickVO.setName(address.getName());
            }
        });
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 根据客户订单号查询出库单
     *
     * @param customerNumber 客户订单号
     * @return PickEntity
     */
    @Override
    public PickEntity queryByCustomerNumber(String customerNumber) {
        if (StringUtils.isBlank(customerNumber)) {
            return null;
        }
        LambdaQueryWrapper<PickEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PickEntity::getCustomerNumber, customerNumber);
        return pickManager.getOne(queryWrapper);
    }

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


}
