package net.lab1024.sa.admin.module.business.receive.asn.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.address.service.AddressQueryService;
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.customer.service.CustomerQueryService;
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.dao.AsnDao;
import net.lab1024.sa.admin.module.business.receive.asn.domain.entity.AsnEntity;
import net.lab1024.sa.admin.module.business.receive.asn.domain.form.AsnQueryForm;
import net.lab1024.sa.admin.module.business.receive.asn.domain.vo.AsnVO;
import net.lab1024.sa.admin.module.business.receive.asnDetail.domain.entity.AsnDetailEntity;
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.stream.Collectors;

@Service
public class AsnQueryService {

    @Resource
    private AsnDao asnDao;

    @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).distinct().collect(Collectors.toList());
        Map<Long, CustomerEntity> customerMap = customerQueryService.queryCustomerList(customerIdList);

        // 查询收货单位
        List<Long> addressIdList = list.stream().map(AsnVO::getAddressId).distinct().collect(Collectors.toList());
        Map<Long, AddressEntity> addressMap = addressQueryService.queryAddressList(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 asnDao.selectOne(queryWrapper);
    }

    /**
     * 根据ids查询入库
     *
     * @param idList 入库集合
     * @return List<AsnEntity>
     */
    public List<AsnEntity> queryAsnList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return null;
        }
        LambdaQueryWrapper<AsnEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AsnEntity::getAsnId, idList);
        return asnDao.selectList(queryWrapper);
    }

    /**
     * 根据入库单集合查询入库单信息
     *
     * @param idList 入库单集合
     * @return Map<String, AsnEntity>
     */
    public Map<Long, AsnEntity> queryAsnListToMap(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyMap();
        }
        List<AsnEntity> asnList = queryAsnList(idList);
        return asnList.stream()
                .collect(Collectors.toMap(AsnEntity::getAsnId, asn -> asn, (existing, replacement) -> existing));

    }


}
