| | |
| | | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import com.ruoyi.basic.service.ICustomerService; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.security.LoginUser; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.sales.mapper.SalesLedgerMapper; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedger; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | |
| | |
| | | @AllArgsConstructor |
| | | @Slf4j |
| | | public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService { |
| | | private final SalesLedgerMapper salesLedgerMapper; |
| | | private final SalesLedgerProductMapper salesLedgerProductMapper; |
| | | private CustomerMapper customerMapper; |
| | | |
| | | /** |
| | |
| | | String address = StringUtils.defaultString(c.getCompanyAddress(), ""); |
| | | String phone = StringUtils.defaultString(c.getCompanyPhone(), ""); |
| | | c.setAddressPhone(address + "(" + phone + ")"); // 优化字符串拼接 |
| | | // 查询该客户关联的销售台账中,购买次数、平均金额、最近购买时间 |
| | | List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new QueryWrapper<SalesLedger>().lambda().eq(SalesLedger::getCustomerId, c.getId())); |
| | | if (!CollectionUtils.isEmpty(salesLedgers)) { |
| | | // 计算购买次数 |
| | | int purchaseCount = salesLedgers.size(); |
| | | c.setPurchaseCount(purchaseCount); |
| | | |
| | | // 计算平均金额 |
| | | if (purchaseCount > 0) { |
| | | BigDecimal totalAmount = salesLedgers.stream() |
| | | .map(SalesLedger::getContractAmount) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | BigDecimal averageAmount = totalAmount.divide(BigDecimal.valueOf(purchaseCount), 2, BigDecimal.ROUND_HALF_UP); |
| | | c.setAverageAmount(averageAmount); |
| | | } else { |
| | | c.setAverageAmount(BigDecimal.ZERO); |
| | | } |
| | | |
| | | // 计算最近购买时间 |
| | | SalesLedger latestLedger = salesLedgers.stream() |
| | | .max((l1, l2) -> l1.getExecutionDate().compareTo(l2.getExecutionDate())) |
| | | .orElse(null); |
| | | if (latestLedger != null) { |
| | | c.setLatestPurchaseTime(latestLedger.getExecutionDate()); |
| | | } |
| | | |
| | | // 计算常购产品(top 3) |
| | | List<SalesLedgerProduct> allProducts = new ArrayList<>(); |
| | | for (SalesLedger ledger : salesLedgers) { |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList( |
| | | new QueryWrapper<SalesLedgerProduct>().lambda() |
| | | .eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()) |
| | | ); |
| | | if (!CollectionUtils.isEmpty(products)) { |
| | | allProducts.addAll(products); |
| | | } |
| | | } |
| | | |
| | | if (!CollectionUtils.isEmpty(allProducts)) { |
| | | // 按产品类别和规格型号分组,统计购买次数 |
| | | Map<String, Long> productCountMap = allProducts.stream() |
| | | .collect(Collectors.groupingBy( |
| | | p -> p.getProductCategory() + "-" + p.getSpecificationModel(), |
| | | Collectors.counting() |
| | | )); |
| | | |
| | | // 排序并取前3 |
| | | List<String> topProducts = productCountMap.entrySet().stream() |
| | | .sorted(Map.Entry.<String, Long>comparingByValue().reversed()) |
| | | .limit(3) |
| | | .map(Map.Entry::getKey) |
| | | .collect(Collectors.toList()); |
| | | |
| | | c.setTopProducts(topProducts); |
| | | } else { |
| | | c.setTopProducts(new ArrayList<>()); |
| | | } |
| | | } else { |
| | | // 没有销售记录时设置默认值 |
| | | c.setPurchaseCount(0); |
| | | c.setAverageAmount(BigDecimal.ZERO); |
| | | c.setLatestPurchaseTime(null); |
| | | c.setTopProducts(new ArrayList<>()); |
| | | } |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | |
| | | @Override |
| | | public int insertCustomer(Customer customer) { |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | Integer tenantId = loginUser.getTenantId(); |
| | | customer.setTenantId(Long.valueOf(tenantId)); |
| | | Long tenantId = loginUser.getTenantId(); |
| | | customer.setTenantId(tenantId); |
| | | return customerMapper.insert(customer); |
| | | } |
| | | |
| | |
| | | @Override |
| | | public int updateCustomer(Customer customer) { |
| | | LoginUser loginUser = SecurityUtils.getLoginUser(); |
| | | Integer tenantId = loginUser.getTenantId(); |
| | | customer.setTenantId(Long.valueOf(tenantId)); |
| | | Long tenantId = loginUser.getTenantId(); |
| | | customer.setTenantId(tenantId); |
| | | return customerMapper.updateById(customer); |
| | | } |
| | | |
| | |
| | | @Override |
| | | public int deleteCustomerByIds(Long[] ids) { |
| | | List<Long> idList = Arrays.asList(ids); |
| | | List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new QueryWrapper<SalesLedger>().lambda().in(SalesLedger::getCustomerId, idList)); |
| | | if (!salesLedgers.isEmpty()) { |
| | | throw new RuntimeException("客户档案下有销售合同,请先删除销售合同"); |
| | | } |
| | | return customerMapper.deleteBatchIds(idList); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public AjaxResult importData(MultipartFile file) { |
| | | try { |
| | | ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class); |
| | | List<Customer> userList = util.importExcel(file.getInputStream()); |
| | | if(CollectionUtils.isEmpty(userList)){ |
| | | return AjaxResult.warn("模板错误或导入数据为空"); |
| | | } |
| | | this.saveOrUpdateBatch(userList); |
| | | return AjaxResult.success(true); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return AjaxResult.error("导入失败"); |
| | | } |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> customerList(Customer customer) { |
| | | LambdaQueryWrapper<Customer> queryWrapper = Wrappers.lambdaQuery(); |
| | | queryWrapper.select(Customer::getId, Customer::getCustomerName, Customer::getTaxpayerIdentificationNumber); |