liding
3 天以前 5edd011ad56b62f664dd980de4f1b6043723c97a
src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
@@ -2,9 +2,10 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.pojo.Customer;
import com.ruoyi.basic.service.ICustomerService;
@@ -18,6 +19,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@@ -51,19 +53,40 @@
     * @return 客户档案
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer) {
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(customer.getCustomerName())) {
            queryWrapper.eq(Customer::getCustomerName, customer.getCustomerName());
    public IPage<Customer> selectCustomerList(Page page, Customer customer) {
        // 1. 处理空值场景(参数校验)
        if (page == null) {
            page = Page.of(1, 10); // 默认第1页,每页10条数据
        }
        if (customer == null) {
            customer = new Customer(); // 避免空对象导致的NPE
        }
        List<Customer> customerList = customerMapper.selectList(queryWrapper);
        // 2. 构建查询条件(增强空值安全)
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        String customerName = customer.getCustomerName();
        if (StringUtils.isNotBlank(customerName)) {
            queryWrapper.like(Customer::getCustomerName, customerName);
        }
        // 使用 Stream 修改每个 Customer 的 addressPhone 字段
        return customerList.stream().peek(c ->
                c.setAddressPhone(c.getCompanyAddress() + "( " + c.getCompanyPhone() + " )")
        ).collect(Collectors.toList());
        // 3. 执行分页查询(保留分页元数据)
        IPage<Customer> customerPage = customerMapper.selectPage(page, queryWrapper);
        // 4. 数据处理(增强空值安全 & 代码可读性)
        List<Customer> processedList = customerPage.getRecords().stream()
                .filter(Objects::nonNull) // 过滤空对象(避免后续操作NPE)
                .peek(c -> {
                    // 安全获取字段,避免null值拼接
                    String address = StringUtils.defaultString(c.getCompanyAddress(), "");
                    String phone = StringUtils.defaultString(c.getCompanyPhone(), "");
                    c.setAddressPhone(address + "(" + phone + ")"); // 优化字符串拼接
                })
                .collect(Collectors.toList());
        // 5. 更新分页结果中的数据(保持分页信息完整)
        customerPage.setRecords(processedList);
        return customerPage; // 返回包含分页信息的IPage对象
    }
    /**
@@ -114,9 +137,14 @@
    }
    @Override
    public List<Customer> selectCustomerLists(Customer customer) {
        return customerMapper.selectList(null);
    }
    @Override
    public List<Map<String, Object>> customerList(Customer customer) {
        LambdaQueryWrapper<Customer> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(Customer::getId, Customer::getCustomerName,Customer::getTaxpayerIdentificationNumber);
        queryWrapper.select(Customer::getId, Customer::getCustomerName, Customer::getTaxpayerIdentificationNumber);
        // 获取原始查询结果
        List<Map<String, Object>> result = customerMapper.selectMaps(queryWrapper);