package net.lab1024.sa.admin.module.business.wms.receive.asn.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.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.dao.AsnDao;
import net.lab1024.sa.admin.module.business.wms.receive.asn.domain.entity.AsnEntity;
import net.lab1024.sa.admin.module.business.wms.receive.asn.domain.form.AsnQueryForm;
import net.lab1024.sa.admin.module.business.wms.receive.asn.domain.vo.AsnVO;
import net.lab1024.sa.admin.module.business.wms.receive.asn.manager.AsnManager;
import net.lab1024.sa.admin.module.business.wms.receive.asn.service.AsnQueryService;
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 AsnQueryServiceImpl implements AsnQueryService {

    @Resource
    private AsnDao asnDao;

    @Resource
    private AsnManager asnManager;

    @Resource
    private CustomerQueryService customerQueryService;

    @Resource
    private AddressQueryService addressQueryService;

    /**
     * 分页查询
     *
     * @param queryForm 查询条件
     * @return PageResult<AsnVO>
     */
    public PageResult<AsnVO> queryPage(AsnQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<AsnVO> list = asnDao.queryPage(page, queryForm);

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

        // 查询收货单位
        List<Long> addressIdList = list.stream().map(AsnVO::getAddressId).toList();
        Map<Long, AddressEntity> addressMap = addressQueryService.queryByAddressIdsToMap(addressIdList);
        list.forEach(asnVO -> {
            CustomerEntity customer = customerMap.get(asnVO.getCustomerId());
            if (customer != null) {
                asnVO.setCustomerName(customer.getCustomerName());
            }
            AddressEntity address = addressMap.get(asnVO.getAddressId());
            if (address != null) {
                asnVO.setName(address.getName());
            }
        });
        return SmartPageUtil.convert2PageResult(page, list);
    }

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

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

    /**
     * 根据入库单集合查询入库单信息
     *
     * @param idList 入库单集合
     * @return Map<String, AsnEntity>
     */
    public Map<Long, AsnEntity> queryByAsnIdsToMap(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyMap();
        }
        //查询入库单
        List<AsnEntity> asnList = queryByAsnIds(idList);
        //封装map
        Map<Long, AsnEntity> asnMap = Maps.newHashMap();
        for (AsnEntity asn : asnList) {
            asnMap.put(asn.getAsnId(), asn);
        }
        return asnMap;

    }


}
