|
|
@@ -1,14 +1,311 @@
|
|
|
package com.gubersail.admin.fc.customer.impl;
|
|
|
|
|
|
+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.StringUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.gubersail.admin.corp.mapper.CorpsAddrMapper;
|
|
|
+import com.gubersail.admin.corp.mapper.CorpsAttnMapper;
|
|
|
+import com.gubersail.admin.corp.mapper.CorpsFilesMapper;
|
|
|
+import com.gubersail.admin.corp.mapper.CorpsTypeDescMapper;
|
|
|
+import com.gubersail.admin.corp.service.ICorpsTypeService;
|
|
|
import com.gubersail.admin.fc.customer.mapper.FcCustomerDescMapper;
|
|
|
import com.gubersail.admin.fc.customer.service.FcCustomerDescService;
|
|
|
+import com.gubersail.dealer.admin.api.corps.entity.*;
|
|
|
import com.gubersail.dealer.admin.api.fc.customer.entity.FcCustomerDesc;
|
|
|
+import com.gubersail.dealer.admin.api.fc.customer.vo.FcCustomerDescVo;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springblade.core.secure.utils.AuthUtil;
|
|
|
+import org.springblade.core.secure.utils.SecureUtil;
|
|
|
+import org.springblade.core.tool.api.R;
|
|
|
+import org.springblade.system.feign.ISysClient;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author Rain
|
|
|
*/
|
|
|
@Service
|
|
|
+@AllArgsConstructor
|
|
|
public class FcCustomerDescServiceImpl extends ServiceImpl<FcCustomerDescMapper, FcCustomerDesc> implements FcCustomerDescService {
|
|
|
+
|
|
|
+ private final ICorpsTypeService corpsTypeService;
|
|
|
+
|
|
|
+ private final ISysClient sysClient;
|
|
|
+
|
|
|
+ private final CorpsAttnMapper corpsAttnMapper;
|
|
|
+
|
|
|
+ private final CorpsAddrMapper corpsAddrMapper;
|
|
|
+
|
|
|
+ private final CorpsFilesMapper corpsFilesMapper;
|
|
|
+
|
|
|
+ private final CorpsTypeDescMapper corpsTypeDescMapper;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<FcCustomerDescVo> selectCorpsDescPage(IPage<FcCustomerDescVo> page, FcCustomerDescVo corpsDesc) {
|
|
|
+ if (StringUtils.isNotBlank(corpsDesc.getCorpsTypeId())) {
|
|
|
+ Long corpsTypeId = Long.parseLong(corpsDesc.getCorpsTypeId());
|
|
|
+ List<Long> corpsTypeIdList = new ArrayList<>();
|
|
|
+ corpsTypeService.selectChildById(corpsTypeId, corpsTypeIdList);
|
|
|
+ corpsTypeIdList.add(corpsTypeId);
|
|
|
+ corpsDesc.setTypeList(corpsTypeIdList);
|
|
|
+ } else if (StringUtils.isNotBlank(corpsDesc.getCorpsTypeName())) {
|
|
|
+ LambdaQueryWrapper<PjCorpsType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ lambdaQueryWrapper.eq(PjCorpsType::getCname, corpsDesc.getCorpsTypeName());
|
|
|
+ lambdaQueryWrapper.eq(PjCorpsType::getIsDeleted, 0);
|
|
|
+ lambdaQueryWrapper.eq(PjCorpsType::getTenantId, SecureUtil.getTenantId());
|
|
|
+ List<PjCorpsType> corpsTypeList = corpsTypeService.list(lambdaQueryWrapper);
|
|
|
+ if (!CollectionUtils.isEmpty(corpsTypeList)) {
|
|
|
+ List<Long> corpsTypeIdList = new ArrayList<>();
|
|
|
+ corpsTypeList.forEach(item -> {
|
|
|
+ corpsTypeService.selectChildById(item.getId(), corpsTypeIdList);
|
|
|
+ corpsTypeIdList.add(item.getId());
|
|
|
+ });
|
|
|
+ corpsDesc.setTypeList(corpsTypeIdList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ corpsDesc.setTenantId(AuthUtil.getTenantId());
|
|
|
+ corpsDesc.setIsDeleted(0);
|
|
|
+ corpsDesc.setStrTenantId(Arrays.asList(AuthUtil.getDeptId().split(",")));
|
|
|
+ corpsDesc.setSalesCompanyId(Long.parseLong(AuthUtil.getDeptId()));
|
|
|
+ return page.setRecords(baseMapper.selectCorpsDescPage(page, corpsDesc));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R submit(FcCustomerDescVo corpsDesc) {
|
|
|
+ Long userId = SecureUtil.getUserId();
|
|
|
+ Date date = new Date();
|
|
|
+ List<FcCustomerDesc> pjCorpsDesc = baseMapper.selectList(new LambdaQueryWrapper<FcCustomerDesc>()
|
|
|
+ .eq(FcCustomerDesc::getTenantId, AuthUtil.getTenantId())
|
|
|
+ .eq(FcCustomerDesc::getIsDeleted, 0)
|
|
|
+ .eq(FcCustomerDesc::getSalesCompanyId, AuthUtil.getTenantId())
|
|
|
+ .eq(FcCustomerDesc::getCname, corpsDesc.getCname()));
|
|
|
+ if (corpsDesc.getId() == null) {
|
|
|
+ if (!pjCorpsDesc.isEmpty()) {
|
|
|
+ throw new RuntimeException("客户" + corpsDesc.getCname() + "已存在,请勿重复添加!");
|
|
|
+ }
|
|
|
+ corpsDesc.setTenantId(SecureUtil.getTenantId());
|
|
|
+ corpsDesc.setCreateDept(Long.valueOf(SecureUtil.getDeptId()));
|
|
|
+ corpsDesc.setCreateTime(date);
|
|
|
+ corpsDesc.setCreateUser(userId);
|
|
|
+ corpsDesc.setSalesCompanyId(Long.valueOf(AuthUtil.getDeptId()));
|
|
|
+ corpsDesc.setSalesCompanyName(sysClient.getDept(Long.valueOf(AuthUtil.getDeptId())).getData().getFullName());
|
|
|
+ corpsDesc.setEnableOrNot(1);
|
|
|
+ baseMapper.insert(corpsDesc);
|
|
|
+ } else {
|
|
|
+ List<Long> ids = pjCorpsDesc.stream().map(FcCustomerDesc::getId).collect(Collectors.toList());
|
|
|
+ if (!pjCorpsDesc.isEmpty() && !ids.contains(corpsDesc.getId())) {
|
|
|
+ throw new RuntimeException("客户" + corpsDesc.getCname() + "已存在,请勿重复添加!");
|
|
|
+ }
|
|
|
+ corpsDesc.setTenantId(SecureUtil.getTenantId());
|
|
|
+ corpsDesc.setUpdateTime(date);
|
|
|
+ corpsDesc.setCreateDept(Long.valueOf(SecureUtil.getDeptId()));
|
|
|
+ corpsDesc.setUpdateUser(AuthUtil.getUserId());
|
|
|
+ baseMapper.updateById(corpsDesc);
|
|
|
+ }
|
|
|
+ Long pId = corpsDesc.getId();
|
|
|
+ String tenantId = corpsDesc.getTenantId();
|
|
|
+ //保存客户联系人信息
|
|
|
+ R saveOrUpdateAttn = this.saveOrUpdateAttn(corpsDesc, userId, pId, tenantId, date);
|
|
|
+ if (saveOrUpdateAttn.getCode() != 200) {
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ return saveOrUpdateAttn;
|
|
|
+ }
|
|
|
+
|
|
|
+ //保存客户地址信息
|
|
|
+ this.saveOrUpdateAddr(corpsDesc, userId, pId, tenantId, date);
|
|
|
+
|
|
|
+ //保存客户文件信息
|
|
|
+ this.saveOrUpdateFiles(corpsDesc, userId, pId, tenantId, date);
|
|
|
+
|
|
|
+ //保存客户与类别对应关系
|
|
|
+ this.saveOrUpdateTypeDesc(corpsDesc, userId, pId, date, tenantId);
|
|
|
+
|
|
|
+ return R.data(corpsDesc);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FcCustomerDescVo getMessageById(FcCustomerDesc corpsDesc) {
|
|
|
+ FcCustomerDescVo detail = new FcCustomerDescVo();
|
|
|
+ FcCustomerDesc desc = baseMapper.selectById(corpsDesc.getId());
|
|
|
+ if (ObjectUtils.isEmpty(desc)) {
|
|
|
+ return detail;
|
|
|
+ }
|
|
|
+ BeanUtils.copyProperties(desc, detail);
|
|
|
+ //获取客户联系人
|
|
|
+ detail.setCorpsAttnList(corpsAttnMapper.selectList(new QueryWrapper<PjCorpsAttn>().eq("pid", corpsDesc.getId()).eq("is_deleted", 0)));
|
|
|
+ //获取客户地址
|
|
|
+ detail.setCorpsAddrList(corpsAddrMapper.selectList(new QueryWrapper<PjCorpsAddr>().eq("pid", corpsDesc.getId()).eq("is_deleted", 0)));
|
|
|
+ //获取客户附件
|
|
|
+ detail.setCorpsFilesList(corpsFilesMapper.selectList(new QueryWrapper<PjCorpsFiles>().eq("pid", corpsDesc.getId()).eq("is_deleted", 0)));
|
|
|
+
|
|
|
+ //获取客户类别
|
|
|
+ List<String> longs = corpsTypeDescMapper.selectTypeId(corpsDesc.getId());
|
|
|
+ detail.setCorpsTypeId(String.join(",", longs));
|
|
|
+ StringBuilder corpsTypeName = new StringBuilder();
|
|
|
+ if (!ObjectUtils.isEmpty(longs)) {
|
|
|
+ for (String id : longs) {
|
|
|
+ PjCorpsType corpsType = corpsTypeService.getById(Long.parseLong(id));
|
|
|
+ String name = ObjectUtils.isEmpty(corpsType) ? "" : corpsType.getCname();
|
|
|
+ corpsTypeName.append(name).append(",");
|
|
|
+ }
|
|
|
+ String corpsName = corpsTypeName.substring(0, corpsTypeName.length() - 1);
|
|
|
+ detail.setCorpsTypeName(corpsName);
|
|
|
+ } else {
|
|
|
+ detail.setCorpsTypeName("");
|
|
|
+ }
|
|
|
+ return detail;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存客户联系人信息
|
|
|
+ *
|
|
|
+ * @param corpsDesc 客户信息
|
|
|
+ * @param userId 登录人id
|
|
|
+ * @param pId 客户详情表id
|
|
|
+ * @param tenantId 租户id
|
|
|
+ * @param date 创建时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public R saveOrUpdateAttn(FcCustomerDescVo corpsDesc, Long userId, Long pId, String tenantId, Date date) {
|
|
|
+ if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(corpsDesc.getCorpsAttnList())) {
|
|
|
+ String tel = corpsDesc.getCorpsAttnList().stream().map(PjCorpsAttn::getTel).filter(Objects::nonNull)
|
|
|
+ .distinct().collect(Collectors.joining(","));
|
|
|
+ List<PjCorpsAttn> pjCorpsAttnList = corpsAttnMapper.selectList(new LambdaQueryWrapper<PjCorpsAttn>()
|
|
|
+ .eq(PjCorpsAttn::getIsDeleted, 0)
|
|
|
+ .eq(PjCorpsAttn::getTenantId, AuthUtil.getTenantId())
|
|
|
+ .apply("find_in_set(tel,'" + tel + "')"));
|
|
|
+ for (PjCorpsAttn corpsAttn : corpsDesc.getCorpsAttnList()) {
|
|
|
+ corpsAttn.setPid(pId);
|
|
|
+ List<PjCorpsAttn> pjCorpsAttns = pjCorpsAttnList.stream().filter(e -> e.getTel().equals(corpsAttn.getTel()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (corpsAttn.getId() == null) {
|
|
|
+ if (!pjCorpsAttns.isEmpty()) {
|
|
|
+ throw new RuntimeException("手机号:" + corpsAttn.getTel() + "已存在,请勿重复添加");
|
|
|
+ }
|
|
|
+ corpsAttn.setTenantId(tenantId);
|
|
|
+ corpsAttn.setCreateTime(date);
|
|
|
+ corpsAttn.setCreateUser(userId);
|
|
|
+ corpsAttnMapper.insert(corpsAttn);
|
|
|
+ } else {
|
|
|
+ List<Long> ids = pjCorpsAttns.stream().map(PjCorpsAttn::getId).collect(Collectors.toList());
|
|
|
+ if (!pjCorpsAttns.isEmpty() && !ids.contains(corpsAttn.getId())) {
|
|
|
+ throw new RuntimeException("手机号" + corpsAttn.getTel() + "已存在,请勿重复添加!");
|
|
|
+ }
|
|
|
+ corpsAttn.setTenantId(tenantId);
|
|
|
+ corpsAttn.setUpdateTime(date);
|
|
|
+ corpsAttn.setUpdateUser(userId);
|
|
|
+ corpsAttnMapper.updateById(corpsAttn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return R.success("success");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存客户文件信息
|
|
|
+ *
|
|
|
+ * @param corpsDesc 客户信息
|
|
|
+ * @param userId 登录人id
|
|
|
+ * @param pId 客户详情表id
|
|
|
+ * @param tenantId 租户id
|
|
|
+ * @param date 创建时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void saveOrUpdateFiles(FcCustomerDescVo corpsDesc, Long userId, Long pId, String tenantId, Date date) {
|
|
|
+ if (!CollectionUtils.isEmpty(corpsDesc.getCorpsFilesList())) {
|
|
|
+ corpsDesc.getCorpsFilesList().forEach(files -> {
|
|
|
+ files.setPid(pId);
|
|
|
+ files.setTenantId(tenantId);
|
|
|
+ if (files.getId() == null) {
|
|
|
+ files.setCreateTime(date);
|
|
|
+ files.setCreateUser(userId);
|
|
|
+ corpsFilesMapper.insert(files);
|
|
|
+ } else {
|
|
|
+ files.setUpdateTime(date);
|
|
|
+ files.setUpdateUser(userId);
|
|
|
+ corpsFilesMapper.updateById(files);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存客户地址信息
|
|
|
+ *
|
|
|
+ * @param corpsDesc 客户信息
|
|
|
+ * @param userId 登录人id
|
|
|
+ * @param pId 客户详情表id
|
|
|
+ * @param tenantId 租户id
|
|
|
+ * @param date 创建时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void saveOrUpdateAddr(FcCustomerDescVo corpsDesc, Long userId, Long pId, String tenantId, Date date) {
|
|
|
+ if (!CollectionUtils.isEmpty(corpsDesc.getCorpsAddrList())) {
|
|
|
+ long count = corpsDesc.getCorpsAddrList().stream().filter(e -> "1".equals(e.getDefaultAddres())).count();
|
|
|
+ if (count != 1) {
|
|
|
+ throw new RuntimeException("默认地址唯一");
|
|
|
+ }
|
|
|
+ corpsDesc.getCorpsAddrList().forEach(addr -> {
|
|
|
+ addr.setPid(pId);
|
|
|
+ addr.setTenantId(tenantId);
|
|
|
+ if (addr.getId() == null) {
|
|
|
+ addr.setCreateTime(date);
|
|
|
+ addr.setCreateUser(userId);
|
|
|
+ corpsAddrMapper.insert(addr);
|
|
|
+ } else {
|
|
|
+ addr.setUpdateTime(date);
|
|
|
+ addr.setUpdateUser(userId);
|
|
|
+ corpsAddrMapper.updateById(addr);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保护客户与类别对应信息
|
|
|
+ *
|
|
|
+ * @param corpsDesc 客户信息
|
|
|
+ * @param userId 登录人id
|
|
|
+ * @param pId 客户详情表id
|
|
|
+ * @param date 创建时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void saveOrUpdateTypeDesc(FcCustomerDescVo corpsDesc, Long userId, Long pId, Date date, String tenantId) {
|
|
|
+ if (StringUtils.isNotBlank(corpsDesc.getCorpsTypeId())) {
|
|
|
+ corpsTypeDescMapper.delete(new QueryWrapper<PjCorpsTypeDesc>().eq("corp_id", corpsDesc.getId()));
|
|
|
+ List<String> list = Arrays.asList(corpsDesc.getCorpsTypeId().split(","));
|
|
|
+ list.forEach(item -> {
|
|
|
+ PjCorpsTypeDesc typeDesc = corpsTypeDescMapper.selectOne(new QueryWrapper<PjCorpsTypeDesc>().eq("corp_id", pId)
|
|
|
+ .eq("corp_type_id", item));
|
|
|
+ if (typeDesc != null) {
|
|
|
+ typeDesc.setCorpTypeId(Long.valueOf(item));
|
|
|
+ typeDesc.setCorpId(corpsDesc.getId());
|
|
|
+ typeDesc.setUpdateTime(date);
|
|
|
+ typeDesc.setUpdateUser(userId);
|
|
|
+ corpsTypeDescMapper.updateById(typeDesc);
|
|
|
+ } else {
|
|
|
+ PjCorpsTypeDesc corpsTypeDesc = new PjCorpsTypeDesc();
|
|
|
+ corpsTypeDesc.setCorpId(corpsDesc.getId());
|
|
|
+ corpsTypeDesc.setCorpTypeId(Long.valueOf(item));
|
|
|
+ corpsTypeDesc.setCreateUser(userId);
|
|
|
+ corpsTypeDesc.setCreateTime(date);
|
|
|
+ corpsTypeDesc.setTenantId(tenantId);
|
|
|
+ corpsTypeDescMapper.insert(corpsTypeDesc);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|