| | |
| | | |
| | | |
| | | 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; |
| | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | |
| | |
| | | * @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对象 |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | @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); |