package net.lab1024.sa.admin.module.business.wms.base.customer.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.customer.dao.CustomerDao;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.entity.CustomerEntity;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerSelect;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.vo.CustomerVO;
import net.lab1024.sa.admin.module.business.wms.base.customer.manager.CustomerManager;
import net.lab1024.sa.admin.module.business.wms.base.customer.service.CustomerQueryService;
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;
import java.util.stream.Collectors;

@Service

public class CustomerQueryServiceImpl implements CustomerQueryService {

    @Resource
    private CustomerDao customerDao;

    @Resource
    private CustomerManager customerManager;

    /**
     * 分页查询
     *
     * @param queryForm 查询参数
     * @return PageResult<CustomerVO>
     */
    public PageResult<CustomerVO> queryPage(CustomerQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CustomerVO> list = customerDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 查询所有客户信息
     *
     * @return List<CustomerEntity>
     */
    public List<CustomerEntity> queryCustomer(CustomerSelect customerSelect) {
        LambdaQueryWrapper<CustomerEntity> queryWrapper = new LambdaQueryWrapper<>();
        //是否启用
        if (customerSelect.getDisabledFlag() != null) {
            queryWrapper.eq(CustomerEntity::getDisabledFlag, customerSelect.getDisabledFlag());
        }
        return customerManager.list(queryWrapper);
    }

    /**
     * 根据客户id集合查询客户信息
     *
     * @param customerIdList 客户id集合
     * @return Map<Long, CustomerEntity>
     */
    public Map<Long, CustomerEntity> queryByCustomerIdsToMap(List<Long> customerIdList) {
        if (CollectionUtils.isEmpty(customerIdList)) {
            return Collections.emptyMap();
        }
        //去重
        customerIdList = customerIdList.stream().filter(Objects::nonNull).distinct().toList();
        //封装map
        Map<Long, CustomerEntity> customerMap = Maps.newHashMap();
        for (Long customerId : customerIdList) {
            CustomerEntity customer = customerManager.queryCustomer(customerId);
            if (customer != null) {
                customerMap.put(customerId, customer);
            }
        }
        return customerMap;
    }

    /**
     * 根据客户名称查询客户信息
     *
     * @param customerName 客户名称
     * @return CustomerEntity
     */
    public CustomerEntity queryByCustomerName(String customerName) {
        if (StringUtils.isBlank(customerName)) {
            return null;
        }
        LambdaQueryWrapper<CustomerEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerEntity::getCustomerName, customerName);
        return customerManager.getOne(queryWrapper);
    }
}
