|
|
@@ -0,0 +1,365 @@
|
|
|
+package org.dromara.customer.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import org.dromara.common.core.utils.MapstructUtils;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
+import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.dromara.customer.domain.*;
|
|
|
+import org.dromara.customer.domain.bo.*;
|
|
|
+import org.dromara.customer.domain.vo.*;
|
|
|
+import org.dromara.customer.mapper.*;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.dromara.customer.service.ICustomerInfoService;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 客户信息Service业务层处理
|
|
|
+ *
|
|
|
+ * @author LionLi
|
|
|
+ * @date 2025-12-11
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Service
|
|
|
+public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {
|
|
|
+
|
|
|
+ private final CustomerInfoMapper baseMapper;
|
|
|
+ private final CustomerBusinessInfoMapper customerBusinessInfoMapper;
|
|
|
+ private final CustomerInvoiceInfoMapper customerInvoiceInfoMapper;
|
|
|
+ private final CustomerContactMapper customerContactMapper;
|
|
|
+ private final CustomerSalesInfoMapper customerSalesInfoMapper;
|
|
|
+ private final IndustryCategoryMapper industryCategoryMapper;
|
|
|
+ private final EnterpriseScaleMapper enterpriseScaleMapper;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询客户信息
|
|
|
+ *
|
|
|
+ * @param id 主键
|
|
|
+ * @return 客户信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public CustomerInfoVo queryById(Long id){
|
|
|
+ if (id == null || id <= 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 查询主表
|
|
|
+ CustomerInfoVo vo = baseMapper.selectVoById(id);
|
|
|
+ if (vo == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ EnterpriseScaleVo enterpriseScaleVo = enterpriseScaleMapper.selectVoById(vo.getEnterpriseScaleId());
|
|
|
+ if (enterpriseScaleVo != null){
|
|
|
+ vo.setEnterpriseScale(enterpriseScaleVo.getEnterpriseScaleName());
|
|
|
+ }
|
|
|
+
|
|
|
+ IndustryCategoryVo industryCategoryVo = industryCategoryMapper.selectVoById(vo.getIndustryCategoryId());
|
|
|
+ if (industryCategoryVo != null){
|
|
|
+ vo.setIndustryCategory(industryCategoryVo.getIndustryCategoryName());
|
|
|
+ }
|
|
|
+ // 2. 查询关联信息
|
|
|
+ vo.setCustomerBusinessInfoVo(
|
|
|
+ Optional.ofNullable(customerBusinessInfoMapper.selectByCustomerId(id))
|
|
|
+ .map(entity -> MapstructUtils.convert(entity, CustomerBusinessInfoVo.class))
|
|
|
+ .orElse(null)
|
|
|
+ );
|
|
|
+
|
|
|
+ vo.setCustomerSalesInfoVo(
|
|
|
+ Optional.ofNullable(customerSalesInfoMapper.selectByCustomerId(id))
|
|
|
+ .map(entity -> MapstructUtils.convert(entity, CustomerSalesInfoVo.class))
|
|
|
+ .orElse(null)
|
|
|
+ );
|
|
|
+
|
|
|
+ List<CustomerContact> contactEntities = customerContactMapper.selectListByCustomerId(id);
|
|
|
+ vo.setCustomerContactVoList(
|
|
|
+ contactEntities != null
|
|
|
+ ? contactEntities.stream()
|
|
|
+ .map(contact -> MapstructUtils.convert(contact, CustomerContactVo.class))
|
|
|
+ .collect(Collectors.toList())
|
|
|
+ : Collections.emptyList()
|
|
|
+ );
|
|
|
+
|
|
|
+ List<CustomerInvoiceInfo> invoiceEntities = customerInvoiceInfoMapper.selectListByCustomerId(id);
|
|
|
+ vo.setCustomerInvoiceInfoVoList(
|
|
|
+ invoiceEntities != null
|
|
|
+ ? invoiceEntities.stream()
|
|
|
+ .map(invoice -> MapstructUtils.convert(invoice, CustomerInvoiceInfoVo.class))
|
|
|
+ .collect(Collectors.toList())
|
|
|
+ : Collections.emptyList()
|
|
|
+ );
|
|
|
+
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询客户信息列表
|
|
|
+ *
|
|
|
+ * @param bo 查询条件
|
|
|
+ * @param pageQuery 分页参数
|
|
|
+ * @return 客户信息分页列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public TableDataInfo<CustomerInfoVo> queryPageList(CustomerInfoBo bo, PageQuery pageQuery) {
|
|
|
+ LambdaQueryWrapper<CustomerInfo> lqw = buildQueryWrapper(bo);
|
|
|
+ Page<CustomerInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
|
|
+ List<CustomerInfoVo> records = result.getRecords();
|
|
|
+ if (CollUtil.isNotEmpty( records)){
|
|
|
+ Set<Long>industryIds = records.stream()
|
|
|
+ .map(CustomerInfoVo::getIndustryCategoryId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ List<IndustryCategoryVo> industryList = industryCategoryMapper.selectVoByIds(industryIds);
|
|
|
+
|
|
|
+ Map<Long, String> industryMap = industryList.stream()
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ IndustryCategoryVo::getId,
|
|
|
+ IndustryCategoryVo::getIndustryCategoryName,
|
|
|
+ (existing, replacement) -> existing
|
|
|
+ ));
|
|
|
+
|
|
|
+ records.forEach(vo -> {
|
|
|
+ vo.setIndustryCategory(industryMap.get(vo.getIndustryCategoryId()));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return TableDataInfo.build(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询符合条件的客户信息列表
|
|
|
+ *
|
|
|
+ * @param bo 查询条件
|
|
|
+ * @return 客户信息列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<CustomerInfoVo> queryList(CustomerInfoBo bo) {
|
|
|
+ LambdaQueryWrapper<CustomerInfo> lqw = buildQueryWrapper(bo);
|
|
|
+ return baseMapper.selectVoList(lqw);
|
|
|
+ }
|
|
|
+
|
|
|
+ private LambdaQueryWrapper<CustomerInfo> buildQueryWrapper(CustomerInfoBo bo) {
|
|
|
+ Map<String, Object> params = bo.getParams();
|
|
|
+ LambdaQueryWrapper<CustomerInfo> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.orderByAsc(CustomerInfo::getId);
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getCustomerNo()), CustomerInfo::getCustomerNo, bo.getCustomerNo());
|
|
|
+ lqw.eq(bo.getBelongCompanyId() != null, CustomerInfo::getBelongCompanyId, bo.getBelongCompanyId());
|
|
|
+ lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), CustomerInfo::getCompanyName, bo.getCompanyName());
|
|
|
+ lqw.like(StringUtils.isNotBlank(bo.getBusinessCustomerName()), CustomerInfo::getBusinessCustomerName, bo.getBusinessCustomerName());
|
|
|
+ lqw.like(StringUtils.isNotBlank(bo.getShortName()), CustomerInfo::getShortName, bo.getShortName());
|
|
|
+ lqw.eq(bo.getInvoiceTypeId() != null, CustomerInfo::getInvoiceTypeId, bo.getInvoiceTypeId());
|
|
|
+ lqw.eq(bo.getEnterpriseScaleId() != null, CustomerInfo::getEnterpriseScaleId, bo.getEnterpriseScaleId());
|
|
|
+ lqw.eq(bo.getCustomerTypeId() != null, CustomerInfo::getCustomerTypeId, bo.getCustomerTypeId());
|
|
|
+ lqw.eq(bo.getIndustryCategoryId() != null, CustomerInfo::getIndustryCategoryId, bo.getIndustryCategoryId());
|
|
|
+ lqw.eq(bo.getCustomerLevelId() != null, CustomerInfo::getCustomerLevelId, bo.getCustomerLevelId());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getLandline()), CustomerInfo::getLandline, bo.getLandline());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getFax()), CustomerInfo::getFax, bo.getFax());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getUrl()), CustomerInfo::getUrl, bo.getUrl());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getPostCode()), CustomerInfo::getPostCode, bo.getPostCode());
|
|
|
+ lqw.eq(bo.getValidityFromDate() != null, CustomerInfo::getValidityFromDate, bo.getValidityFromDate());
|
|
|
+ lqw.eq(bo.getValidityToDate() != null, CustomerInfo::getValidityToDate, bo.getValidityToDate());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getInvoiceTop()), CustomerInfo::getInvoiceTop, bo.getInvoiceTop());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getAddress()), CustomerInfo::getAddress, bo.getAddress());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getRegProvincialNo()), CustomerInfo::getRegProvincialNo, bo.getRegProvincialNo());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getRegCityNo()), CustomerInfo::getRegCityNo, bo.getRegCityNo());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getRegCountyNo()), CustomerInfo::getRegCountyNo, bo.getRegCountyNo());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getProvincialCityCounty()), CustomerInfo::getProvincialCityCounty, bo.getProvincialCityCounty());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CustomerInfo::getStatus, bo.getStatus());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getPlatformCode()), CustomerInfo::getPlatformCode, bo.getPlatformCode());
|
|
|
+ return lqw;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增客户信息
|
|
|
+ *
|
|
|
+ * @param bo 客户信息
|
|
|
+ * @return 是否新增成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean insertByBo(CustomerInfoBo bo) {
|
|
|
+ if (bo == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean isNew = bo.getId() == null || bo.getId() <= 0;
|
|
|
+
|
|
|
+ // 1. 处理主表 CustomerInfo
|
|
|
+ CustomerInfo entity = MapstructUtils.convert(bo, CustomerInfo.class);
|
|
|
+ validEntityBeforeSave(entity);
|
|
|
+
|
|
|
+ boolean success;
|
|
|
+ if (isNew) {
|
|
|
+ success = baseMapper.insert(entity) > 0;
|
|
|
+ if (success) {
|
|
|
+ bo.setId(entity.getId()); // 回填ID
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ success = baseMapper.updateById(entity) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!success) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long customerId = entity.getId();
|
|
|
+
|
|
|
+ // 2. 获取各子对象
|
|
|
+ CustomerBusinessInfoBo businessBo = bo.getCustomerBusinessBo();
|
|
|
+ CustomerSalesInfoBo salesBo = bo.getCustomerSalesInfoBo();
|
|
|
+ List<CustomerContactBo> contactList = bo.getCustomerContactBoList();
|
|
|
+ List<CustomerInvoiceInfoBo> invoiceList = bo.getCustomerInvoiceInfoBoList();
|
|
|
+
|
|
|
+ // 3. 如果是更新,先删除旧的关联数据
|
|
|
+ if (!isNew) {
|
|
|
+ // 删除业务信息(一对一)
|
|
|
+ customerBusinessInfoMapper.deleteByCustomerId(customerId);
|
|
|
+ // 删除销售信息(一对一)
|
|
|
+ customerSalesInfoMapper.deleteByCustomerId(customerId);
|
|
|
+ // 删除联系人(一对多)
|
|
|
+ customerContactMapper.deleteByCustomerId(customerId);
|
|
|
+ // 删除发票信息(一对多)
|
|
|
+ customerInvoiceInfoMapper.deleteByCustomerId(customerId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 插入新的关联数据(即使为空也安全处理)
|
|
|
+ // 业务信息
|
|
|
+ if (businessBo != null) {
|
|
|
+ CustomerBusinessInfo businessEntity = MapstructUtils.convert(businessBo, CustomerBusinessInfo.class);
|
|
|
+ businessEntity.setCustomerId(customerId);
|
|
|
+ customerBusinessInfoMapper.insert(businessEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 销售信息
|
|
|
+ if (salesBo != null) {
|
|
|
+ CustomerSalesInfo salesEntity = MapstructUtils.convert(salesBo, CustomerSalesInfo.class);
|
|
|
+ salesEntity.setCustomerId(customerId);
|
|
|
+ customerSalesInfoMapper.insert(salesEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 联系人列表
|
|
|
+ if (contactList != null && !contactList.isEmpty()) {
|
|
|
+ for (CustomerContactBo contactBo : contactList) {
|
|
|
+ CustomerContact contact = MapstructUtils.convert(contactBo, CustomerContact.class);
|
|
|
+ contact.setCustomerId(customerId);
|
|
|
+ customerContactMapper.insert(contact);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发票信息列表
|
|
|
+ if (invoiceList != null && !invoiceList.isEmpty()) {
|
|
|
+ for (CustomerInvoiceInfoBo invoiceBo : invoiceList) {
|
|
|
+ CustomerInvoiceInfo invoice = MapstructUtils.convert(invoiceBo, CustomerInvoiceInfo.class);
|
|
|
+ invoice.setCustomerId(customerId);
|
|
|
+ customerInvoiceInfoMapper.insert(invoice);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改客户信息
|
|
|
+ *
|
|
|
+ * @param bo 客户信息
|
|
|
+ * @return 是否修改成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean updateByBo(CustomerInfoBo bo) {
|
|
|
+ if (bo == null || bo.getId() == null || bo.getId() <= 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 更新主表
|
|
|
+ CustomerInfo entity = MapstructUtils.convert(bo, CustomerInfo.class);
|
|
|
+ validEntityBeforeSave(entity);
|
|
|
+ boolean mainUpdated = baseMapper.updateById(entity) > 0;
|
|
|
+ if (!mainUpdated) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long customerId = bo.getId();
|
|
|
+
|
|
|
+ // 2. 删除旧的关联数据
|
|
|
+ customerBusinessInfoMapper.deleteByCustomerId(customerId);
|
|
|
+ customerSalesInfoMapper.deleteByCustomerId(customerId);
|
|
|
+ customerContactMapper.deleteByCustomerId(customerId);
|
|
|
+ customerInvoiceInfoMapper.deleteByCustomerId(customerId);
|
|
|
+
|
|
|
+ // 3. 插入新的关联数据
|
|
|
+ saveAssociatedData(customerId, bo);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存客户的关联信息(不包含删除逻辑,仅插入)
|
|
|
+ */
|
|
|
+ private void saveAssociatedData(Long customerId, CustomerInfoBo bo) {
|
|
|
+ // 业务信息
|
|
|
+ if (bo.getCustomerBusinessBo() != null) {
|
|
|
+ CustomerBusinessInfo business = MapstructUtils.convert(bo.getCustomerBusinessBo(), CustomerBusinessInfo.class);
|
|
|
+ business.setCustomerId(customerId);
|
|
|
+ customerBusinessInfoMapper.insert(business);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 销售信息
|
|
|
+ if (bo.getCustomerSalesInfoBo() != null) {
|
|
|
+ CustomerSalesInfo sales = MapstructUtils.convert(bo.getCustomerSalesInfoBo(), CustomerSalesInfo.class);
|
|
|
+ sales.setCustomerId(customerId);
|
|
|
+ customerSalesInfoMapper.insert(sales);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 联系人列表
|
|
|
+ if (bo.getCustomerContactBoList() != null && !bo.getCustomerContactBoList().isEmpty()) {
|
|
|
+ for (CustomerContactBo contactBo : bo.getCustomerContactBoList()) {
|
|
|
+ CustomerContact contact = MapstructUtils.convert(contactBo, CustomerContact.class);
|
|
|
+ contact.setCustomerId(customerId);
|
|
|
+ customerContactMapper.insert(contact);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发票信息列表
|
|
|
+ if (bo.getCustomerInvoiceInfoBoList() != null && !bo.getCustomerInvoiceInfoBoList().isEmpty()) {
|
|
|
+ for (CustomerInvoiceInfoBo invoiceBo : bo.getCustomerInvoiceInfoBoList()) {
|
|
|
+ CustomerInvoiceInfo invoice = MapstructUtils.convert(invoiceBo, CustomerInvoiceInfo.class);
|
|
|
+ invoice.setCustomerId(customerId);
|
|
|
+ customerInvoiceInfoMapper.insert(invoice);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存前的数据校验
|
|
|
+ */
|
|
|
+ private void validEntityBeforeSave(CustomerInfo entity){
|
|
|
+ //TODO 做一些数据校验,如唯一约束
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验并批量删除客户信息信息
|
|
|
+ *
|
|
|
+ * @param ids 待删除的主键集合
|
|
|
+ * @param isValid 是否进行有效性校验
|
|
|
+ * @return 是否删除成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
|
|
+ if(isValid){
|
|
|
+ //TODO 做一些业务上的校验,判断是否需要校验
|
|
|
+ }
|
|
|
+ return baseMapper.deleteByIds(ids) > 0;
|
|
|
+ }
|
|
|
+}
|