src/main/java/com/ruoyi/basic/controller/CustomerController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/ruoyi/basic/service/ICustomerService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/ruoyi/basic/controller/CustomerController.java
@@ -1,5 +1,7 @@ package com.ruoyi.basic.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.basic.pojo.Customer; import com.ruoyi.basic.service.ICustomerService; import com.ruoyi.common.utils.poi.ExcelUtil; @@ -7,7 +9,6 @@ import com.ruoyi.framework.aspectj.lang.enums.BusinessType; import com.ruoyi.framework.web.controller.BaseController; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.framework.web.page.TableDataInfo; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -30,10 +31,8 @@ * 查询客户档案列表 */ @GetMapping("/list") public TableDataInfo list(Customer customer) { startPage(); List<Customer> list = customerService.selectCustomerList(customer); return getDataTable(list); public IPage<Customer> list(Page page, Customer customer) { return customerService.selectCustomerList(page, customer); } /** @@ -47,8 +46,7 @@ if (ids != null && ids.length > 0) { list = customerService.selectCustomerListByIds(ids); } else { list = customerService.selectCustomerList(customer); list = customerService.selectCustomerLists(customer); } ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class); util.exportExcel(response, list, "客户档案数据"); @@ -97,6 +95,6 @@ */ @GetMapping("/customerList") public List customerList(Customer customer) { return customerService.customerList(customer); return customerService.customerList(customer); } } src/main/java/com/ruoyi/basic/service/ICustomerService.java
@@ -1,5 +1,7 @@ package com.ruoyi.basic.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; import com.ruoyi.basic.pojo.Customer; @@ -26,7 +28,7 @@ * @param customer 客户档案 * @return 客户档案集合 */ List<Customer> selectCustomerList(Customer customer); IPage<Customer> selectCustomerList(Page page, Customer customer); /** * 新增客户档案 @@ -60,4 +62,6 @@ * @return 结果 */ List customerList(Customer customer); List<Customer> selectCustomerLists(Customer customer); } 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.like(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);