package com.ruoyi.basic.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.dto.CustomerDto;
|
import com.ruoyi.basic.entity.Customer;
|
import com.ruoyi.basic.mapper.CustomerMapper;
|
import com.ruoyi.basic.service.CustomerService;
|
import com.ruoyi.common.utils.bean.BeanUtils;
|
import com.ruoyi.common.utils.poi.ExcelUtil;
|
import jakarta.servlet.http.HttpServletResponse;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.StringUtils;
|
|
import java.util.List;
|
import java.util.Objects;
|
|
/**
|
* <p>
|
* 服务实现类
|
* </p>
|
*
|
* @author ruoyi
|
* @since 2025-06-03
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
|
|
private final CustomerMapper customerMapper;
|
|
@Override
|
public IPage<Customer> selectCustomerList(Page page, CustomerDto customerDto) {
|
LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
|
// 全局模糊搜索字段
|
if (StringUtils.hasText(customerDto.getSearchAll())) {
|
String keyword = customerDto.getSearchAll();
|
queryWrapper.and(wrapper -> wrapper
|
.like(Customer::getCustomerName, keyword)
|
.or()
|
.like(Customer::getTaxpayerId, keyword)
|
.or()
|
.like(Customer::getBusinessAddress, keyword)
|
);
|
} else {
|
// 单独条件查询
|
if (StringUtils.hasText(customerDto.getCustomerName())) {
|
queryWrapper.like(Customer::getCustomerName, customerDto.getCustomerName());
|
}
|
if (StringUtils.hasText(customerDto.getTaxpayerId())) {
|
queryWrapper.like(Customer::getTaxpayerId, customerDto.getTaxpayerId());
|
}
|
if (StringUtils.hasText(customerDto.getBusinessAddress())) {
|
queryWrapper.like(Customer::getBusinessAddress, customerDto.getBusinessAddress());
|
}
|
}
|
// 默认按创建时间倒序排列
|
queryWrapper.orderByDesc(Customer::getCreateTime);
|
return customerMapper.selectPage(page, queryWrapper);
|
}
|
|
@Override
|
public List<Customer> customerList() {
|
return customerMapper.selectList(null);
|
}
|
|
@Override
|
public int addOrEditCustomer(CustomerDto customerDto) {
|
Customer customer = new Customer();
|
BeanUtils.copyProperties(customerDto, customer);
|
if (customerDto.getBids().size() != 3) {
|
throw new RuntimeException("请选择经营地址省市区");
|
}
|
|
if (customerDto.getCids().size() != 3) {
|
throw new RuntimeException("请选择联系地址省市区");
|
}
|
|
customer.setBusinessProvinceId(customerDto.getBids().get(0));
|
customer.setBusinessCityId(customerDto.getBids().get(1));
|
customer.setBusinessDistrictId(customerDto.getBids().get(2));
|
|
customer.setProvinceId(customerDto.getCids().get(0));
|
customer.setCityId(customerDto.getCids().get(1));
|
customer.setDistrictId(customerDto.getCids().get(2));
|
|
if (Objects.isNull(customerDto.getId())) {
|
return customerMapper.insert(customer);
|
} else {
|
return customerMapper.updateById(customer);
|
}
|
}
|
|
@Override
|
public int delCustomerByIds(Long[] ids) {
|
// 检查参数
|
if (ids == null || ids.length == 0) {
|
return 0;
|
}
|
// 构造更新条件
|
UpdateWrapper<Customer> updateWrapper = new UpdateWrapper<>();
|
updateWrapper.in("id", ids)
|
.set("deleted", 1); // 设置 deleted 为 1 表示已删除
|
// 执行批量逻辑删除
|
return customerMapper.update(null, updateWrapper);
|
}
|
|
@Override
|
public void customerExport(HttpServletResponse response, CustomerDto customerDto) {
|
List<Long> ids = customerDto.getIds();
|
List<Customer> list;
|
if (ids != null && ids.size() > 0) {
|
list = customerMapper.selectByIds(ids);
|
} else {
|
list = customerMapper.selectList(null);
|
}
|
ExcelUtil<Customer> util = new ExcelUtil<>(Customer.class);
|
util.exportExcel(response, list, "客户数据");
|
}
|
}
|