|
|
@@ -0,0 +1,251 @@
|
|
|
+package com.fs.his.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fs.common.core.domain.entity.SysUser;
|
|
|
+import com.fs.common.exception.ServiceException;
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.common.utils.bean.BeanUtils;
|
|
|
+import com.fs.company.domain.Company;
|
|
|
+import com.fs.company.domain.CompanyUser;
|
|
|
+import com.fs.company.mapper.CompanyMapper;
|
|
|
+import com.fs.company.mapper.CompanyUserMapper;
|
|
|
+import com.fs.course.domain.FsUserCompanyUser;
|
|
|
+import com.fs.course.service.IFsUserCompanyUserService;
|
|
|
+import com.fs.his.domain.FsUser;
|
|
|
+import com.fs.his.domain.FsUserCompanyUserTransferTask;
|
|
|
+import com.fs.his.domain.FsUserCompanyUserTransferTaskDetail;
|
|
|
+import com.fs.his.dto.FsUserTransferImportDTO;
|
|
|
+import com.fs.his.mapper.FsUserCompanyUserTransferTaskMapper;
|
|
|
+import com.fs.his.mapper.FsUserMapper;
|
|
|
+import com.fs.his.service.IFsUserCompanyUserTransferTaskDetailService;
|
|
|
+import com.fs.his.service.IFsUserCompanyUserTransferTaskService;
|
|
|
+import com.fs.his.vo.FsUserCompanyUserTransferTaskDetailVO;
|
|
|
+import com.fs.his.vo.FsUserCompanyUserTransferTaskVO;
|
|
|
+import com.fs.system.mapper.SysDictDataMapper;
|
|
|
+import com.fs.system.vo.DictVO;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class FsUserCompanyUserTransferTaskServiceImpl extends ServiceImpl<FsUserCompanyUserTransferTaskMapper, FsUserCompanyUserTransferTask>
|
|
|
+ implements IFsUserCompanyUserTransferTaskService {
|
|
|
+
|
|
|
+ private final IFsUserCompanyUserTransferTaskDetailService transferTaskDetailService;
|
|
|
+ private final FsUserMapper userMapper;
|
|
|
+ private final CompanyMapper companyMapper;
|
|
|
+ private final CompanyUserMapper companyUserMapper;
|
|
|
+ private final IFsUserCompanyUserService userCompanyUserService;
|
|
|
+ private final SysDictDataMapper sysDictDataMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转移导入会员数据
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void transferImportData(List<FsUserTransferImportDTO> list, SysUser user) {
|
|
|
+ FsUserCompanyUserTransferTask transferTask = new FsUserCompanyUserTransferTask();
|
|
|
+ transferTask.setStatus(0);
|
|
|
+ transferTask.setCreateTime(LocalDateTime.now());
|
|
|
+ transferTask.setTotalCount(list.size());
|
|
|
+ transferTask.setSubmitUserId(user.getUserId());
|
|
|
+ transferTask.setSubmitUserName(user.getUserName());
|
|
|
+ baseMapper.insert(transferTask);
|
|
|
+
|
|
|
+ // 查询所有的销售
|
|
|
+ Set<Long> companyUserIds = list.stream()
|
|
|
+ .flatMap(d -> Stream.of(d.getOldCompanyUserId(), d.getNewCompanyUserId()))
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ List<CompanyUser> companyUsers = companyUserMapper.selectCompanyUserByIds(companyUserIds);
|
|
|
+ if (companyUsers.isEmpty()) {
|
|
|
+ throw new ServiceException("销售列表不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<Long, CompanyUser> companyUserMap = companyUsers.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .filter(cu -> cu.getUserId() != null)
|
|
|
+ .collect(Collectors.toMap(CompanyUser::getUserId, Function.identity(), (a, b) -> a));
|
|
|
+
|
|
|
+ // 查询所有的公司
|
|
|
+ Set<Long> companyIds = companyUsers.stream()
|
|
|
+ .map(CompanyUser::getCompanyId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ List<Company> companies = companyMapper.selectCompanyByIds3(companyIds);
|
|
|
+ if (companies.isEmpty()) {
|
|
|
+ throw new ServiceException("销售对应公司不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<Long, Company> companyMap = companies.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .filter(c -> c.getCompanyId() != null)
|
|
|
+ .collect(Collectors.toMap(Company::getCompanyId, Function.identity(), (a, b) -> a));
|
|
|
+
|
|
|
+
|
|
|
+ // 查询所有的用户
|
|
|
+ List<Long> userIds = list.stream().map(FsUserTransferImportDTO::getUserId).collect(Collectors.toList());
|
|
|
+ List<FsUser> users = userMapper.findUsersByIdsV2(userIds);
|
|
|
+ if (users.isEmpty()) {
|
|
|
+ throw new ServiceException("转移会员不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<Long, FsUser> userMap = users.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .filter(u -> u.getUserId() != null)
|
|
|
+ .collect(Collectors.toMap(FsUser::getUserId, Function.identity(), (a, b) -> a));
|
|
|
+
|
|
|
+ // 获取所有项目
|
|
|
+ List<DictVO> dictVOS = sysDictDataMapper.selectDictDataListByType("sys_course_project");
|
|
|
+ Map<Long, String> dictMap = dictVOS.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .filter(d -> StringUtils.isNotBlank(d.getDictValue()))
|
|
|
+ .collect(Collectors.toMap(d -> Long.valueOf(d.getDictValue()), DictVO::getDictLabel, (a, b) -> a));
|
|
|
+
|
|
|
+ List<FsUserCompanyUser> updateRelations = new ArrayList<>();
|
|
|
+ List<FsUserCompanyUserTransferTaskDetail> details = new ArrayList<>();
|
|
|
+ for (FsUserTransferImportDTO fsUserTransferImportDTO : list) {
|
|
|
+ FsUserCompanyUserTransferTaskDetail detail = new FsUserCompanyUserTransferTaskDetail();
|
|
|
+ detail.setTaskId(transferTask.getId());
|
|
|
+ detail.setCreateTime(LocalDateTime.now());
|
|
|
+
|
|
|
+ try {
|
|
|
+ boolean flag = true;
|
|
|
+ StringBuilder reason = new StringBuilder();
|
|
|
+
|
|
|
+ Long oldCompanyUserId = fsUserTransferImportDTO.getOldCompanyUserId();
|
|
|
+ CompanyUser oldCompanyUser = oldCompanyUserId == null ? null : companyUserMap.get(oldCompanyUserId);
|
|
|
+ if (oldCompanyUser != null) {
|
|
|
+ Company company = companyMap.get(oldCompanyUser.getCompanyId());
|
|
|
+ if (company != null) {
|
|
|
+ detail.setOldCompanyId(company.getCompanyId());
|
|
|
+ detail.setOldCompanyName(company.getCompanyName());
|
|
|
+ } else {
|
|
|
+ flag = false;
|
|
|
+ reason.append("原销售公司不存在[").append(oldCompanyUser.getCompanyId()).append("],");
|
|
|
+ }
|
|
|
+
|
|
|
+ detail.setOldCompanyUserId(oldCompanyUser.getUserId());
|
|
|
+ detail.setOldCompanyUserName(oldCompanyUser.getUserName());
|
|
|
+ } else {
|
|
|
+ flag = false;
|
|
|
+ reason.append("原销售不存在[").append(oldCompanyUserId).append("],");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long userId = fsUserTransferImportDTO.getUserId();
|
|
|
+ FsUser fsUser = userId == null ? null : userMap.get(userId);
|
|
|
+ if (fsUser != null) {
|
|
|
+ detail.setUserId(fsUser.getUserId());
|
|
|
+ detail.setUserName(fsUser.getNickName());
|
|
|
+ } else {
|
|
|
+ flag = false;
|
|
|
+ reason.append("会员不存在[").append(userId).append("],");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long newCompanyUserId = fsUserTransferImportDTO.getNewCompanyUserId();
|
|
|
+ CompanyUser newCompanyUser = newCompanyUserId == null ? null : companyUserMap.get(newCompanyUserId);
|
|
|
+ if (newCompanyUser != null) {
|
|
|
+ Company company = companyMap.get(newCompanyUser.getCompanyId());
|
|
|
+ if (company != null) {
|
|
|
+ detail.setNewCompanyId(company.getCompanyId());
|
|
|
+ detail.setNewCompanyName(company.getCompanyName());
|
|
|
+ } else {
|
|
|
+ flag = false;
|
|
|
+ reason.append("新归属销售公司不存在[").append(newCompanyUser.getCompanyId()).append("],");
|
|
|
+ }
|
|
|
+
|
|
|
+ detail.setNewCompanyUserId(newCompanyUser.getUserId());
|
|
|
+ detail.setNewCompanyUserName(newCompanyUser.getUserName());
|
|
|
+ } else {
|
|
|
+ flag = false;
|
|
|
+ reason.append("新归属销售不存在[").append(newCompanyUserId).append("],");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (flag) {
|
|
|
+ FsUserCompanyUser params = new FsUserCompanyUser();
|
|
|
+ params.setCompanyId(detail.getOldCompanyId());
|
|
|
+ params.setCompanyUserId(detail.getOldCompanyUserId());
|
|
|
+ params.setUserId(detail.getUserId());
|
|
|
+ List<FsUserCompanyUser> fsUserCompanyUsers = userCompanyUserService.selectFsUserCompanyUserList(params);
|
|
|
+ if (!fsUserCompanyUsers.isEmpty()) {
|
|
|
+ for (FsUserCompanyUser u : fsUserCompanyUsers) {
|
|
|
+ u.setCompanyId(detail.getNewCompanyId());
|
|
|
+ u.setCompanyUserId(detail.getNewCompanyUserId());
|
|
|
+ u.setUpdateTime(LocalDateTime.now());
|
|
|
+ updateRelations.add(u);
|
|
|
+
|
|
|
+ FsUserCompanyUserTransferTaskDetail copyDetail = new FsUserCompanyUserTransferTaskDetail();
|
|
|
+ BeanUtils.copyProperties(detail, copyDetail);
|
|
|
+ copyDetail.setFinishTime(LocalDateTime.now());
|
|
|
+ copyDetail.setStatus(1);
|
|
|
+ copyDetail.setProjectId(u.getProjectId());
|
|
|
+ copyDetail.setProjectName(dictMap.get(u.getProjectId()));
|
|
|
+ details.add(copyDetail);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ reason.append("会员[").append(detail.getUserId()).append("]与销售[").append(detail.getOldCompanyUserId()).append("]归属关系不正确,");
|
|
|
+ detail.setStatus(2);
|
|
|
+ detail.setReason(StringUtils.removeEnd(reason.toString(), ","));
|
|
|
+ detail.setFinishTime(LocalDateTime.now());
|
|
|
+ details.add(detail);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ detail.setStatus(2);
|
|
|
+ detail.setReason(StringUtils.removeEnd(reason.toString(), ","));
|
|
|
+ detail.setFinishTime(LocalDateTime.now());
|
|
|
+ details.add(detail);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ detail.setStatus(2);
|
|
|
+ detail.setReason(e.getMessage());
|
|
|
+ detail.setFinishTime(LocalDateTime.now());
|
|
|
+ details.add(detail);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!details.isEmpty()) {
|
|
|
+ transferTaskDetailService.saveBatch(details);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!updateRelations.isEmpty()) {
|
|
|
+ userCompanyUserService.updateBatchById(updateRelations);
|
|
|
+ }
|
|
|
+
|
|
|
+ transferTask.setSuccessCount((int) details.stream().filter(d -> d.getStatus() == 1).count());
|
|
|
+ transferTask.setFailCount((int) details.stream().filter(d -> d.getStatus() == 2).count());
|
|
|
+ transferTask.setStatus(1);
|
|
|
+ transferTask.setFinishTime(LocalDateTime.now());
|
|
|
+ baseMapper.updateById(transferTask);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取导入会员数据列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<FsUserCompanyUserTransferTaskVO> getAllTransferTaskList() {
|
|
|
+ return baseMapper.getAllTransferTaskList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取导入会员数据详情
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<FsUserCompanyUserTransferTaskDetailVO> getAllTransferTaskDetails(Long taskId) {
|
|
|
+ return transferTaskDetailService.getTaskDetailsByTaskId(taskId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取转移失败数据
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<FsUserTransferImportDTO> getTransferFailedData(Long taskId) {
|
|
|
+ return transferTaskDetailService.getTransferFailedData(taskId);
|
|
|
+ }
|
|
|
+}
|