|
@@ -6,10 +6,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.qmth.sop.business.bean.params.AllocationParam;
|
|
|
import com.qmth.sop.business.bean.params.UserArchivesAllocationParam;
|
|
|
-import com.qmth.sop.business.bean.result.CrmArchivesAllocationResult;
|
|
|
-import com.qmth.sop.business.bean.result.UserArchivesAllocationResult;
|
|
|
-import com.qmth.sop.business.bean.result.UserArchivesAllocationSubTotalResult;
|
|
|
-import com.qmth.sop.business.bean.result.UserArchivesResult;
|
|
|
+import com.qmth.sop.business.bean.result.*;
|
|
|
import com.qmth.sop.business.entity.*;
|
|
|
import com.qmth.sop.business.mapper.TBUserArchivesAllocationMapper;
|
|
|
import com.qmth.sop.business.service.*;
|
|
@@ -23,11 +20,9 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Comparator;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -209,7 +204,6 @@ public class TBUserArchivesAllocationServiceImpl extends ServiceImpl<TBUserArchi
|
|
|
@Override
|
|
|
public void autoEditCrmAllocationBatch(Long serviceUnitId) {
|
|
|
List<UserArchivesAllocationResult> crmSourceList = this.baseMapper.findCrmAllocationSubTotal(serviceUnitId, null, null, null, null, null);
|
|
|
-
|
|
|
// crm按照差额倒序
|
|
|
crmSourceList = crmSourceList.stream().filter(e -> e.getUnDistributed() > 0)
|
|
|
.sorted(Comparator.comparing(UserArchivesAllocationResult::getUnDistributed).reversed())
|
|
@@ -218,6 +212,7 @@ public class TBUserArchivesAllocationServiceImpl extends ServiceImpl<TBUserArchi
|
|
|
for (UserArchivesAllocationResult crmSource : crmSourceList) {
|
|
|
String crmNo = crmSource.getCrmNo();
|
|
|
String city = crmSource.getCity();
|
|
|
+ String province = crmSource.getProvince();
|
|
|
|
|
|
// 区域协调人差额
|
|
|
Integer coordinatorUnDistributed = crmSource.getCoordinatorUnDistributed();
|
|
@@ -246,17 +241,150 @@ public class TBUserArchivesAllocationServiceImpl extends ServiceImpl<TBUserArchi
|
|
|
.filter(e -> RoleTypeEnum.ASSISTANT_ENGINEER.equals(e.getType()))
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
+ // 可分配的人力资源
|
|
|
+ List<ArchivesSourceResult> effectFree = tbUserArchivesService.findFreeEngineerSourceByType(RoleTypeEnum.EFFECT_ENGINEER)
|
|
|
+ .stream()
|
|
|
+ .filter(e -> province.equals(e.getProvince()) && city.equals(e.getCity()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<ArchivesSourceResult> assistantFree = tbUserArchivesService.findFreeEngineerSourceByType(RoleTypeEnum.ASSISTANT_ENGINEER)
|
|
|
+ .stream()
|
|
|
+ .filter(e -> province.equals(e.getProvince()) && city.equals(e.getCity()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<ArchivesSourceResult> coordinatorFree = tbUserArchivesService.findFreeCoordinatorByServiceId(serviceUnitId)
|
|
|
+ .stream()
|
|
|
+ .filter(e -> province.equals(e.getProvince()) && city.equals(e.getCity()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<AllocationParam> allocationParamList = new ArrayList<>();
|
|
|
+
|
|
|
// 先分实施工程师
|
|
|
if (effectUnDistributed > 0) {
|
|
|
- // 如果存在区域协调人,则要分配的实施工程师必须跟区域协调人处于相同的供应商
|
|
|
- if (CollectionUtils.isNotEmpty(coordinatorList)){
|
|
|
+ SysRole effectRole = sysRoleService.list(new QueryWrapper<SysRole>().lambda().eq(SysRole::getType, RoleTypeEnum.EFFECT_ENGINEER)).get(0);
|
|
|
+ Long effectRoleId = effectRole.getId();
|
|
|
+ List<Long> userIdList = new ArrayList<>();
|
|
|
+ if (CollectionUtils.isNotEmpty(coordinatorList)) {
|
|
|
+ // 实施工程师和区域协调人同供应商,实施工程师和crm客户同城市
|
|
|
CrmArchivesAllocationResult coordinator = coordinatorList.get(0);
|
|
|
Long supplierId = coordinator.getSupplierId();
|
|
|
-
|
|
|
+ effectFree = effectFree.stream()
|
|
|
+ .filter(e -> supplierId.equals(e.getSupplierId()))
|
|
|
+ .limit(effectUnDistributed)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ userIdList.addAll(effectFree.stream().map(ArchivesSourceResult::getUserId).distinct().collect(Collectors.toList()));
|
|
|
+ this.addDistributeTemp(effectList, effectFree);
|
|
|
+ } else {
|
|
|
+ // 不存在区域协调人,实施工程师和crm客户同城市,且实施工程师尽可能集中在同一个供应商中
|
|
|
+
|
|
|
+ // 供应商和实施工程师数量键值对
|
|
|
+ Map<Long, Integer> supplierMap = effectFree.stream().collect(Collectors.toMap(ArchivesSourceResult::getSupplierId, e -> 1, Integer::sum));
|
|
|
+ supplierMap = SystemConstant.sortMapByValues(supplierMap);
|
|
|
+ for (Long supplierId : supplierMap.keySet()) {
|
|
|
+ Integer count = supplierMap.get(supplierId);
|
|
|
+ effectUnDistributed = effectUnDistributed - count;
|
|
|
+ List<ArchivesSourceResult> cell;
|
|
|
+ if (effectUnDistributed > 0) {
|
|
|
+ // 该供应商的不够分配配额
|
|
|
+ cell = effectFree.stream().filter(e -> supplierId.equals(e.getSupplierId())).limit(count).collect(Collectors.toList());
|
|
|
+ userIdList.addAll(cell.stream().map(ArchivesSourceResult::getUserId).distinct().collect(Collectors.toList()));
|
|
|
+ this.addDistributeTemp(effectList, cell);
|
|
|
+ } else {
|
|
|
+ // 该供应商的足够分配配额
|
|
|
+ cell = effectFree.stream().filter(e -> supplierId.equals(e.getSupplierId())).limit(effectUnDistributed).collect(Collectors.toList());
|
|
|
+ userIdList.addAll(cell.stream().map(ArchivesSourceResult::getUserId).distinct().collect(Collectors.toList()));
|
|
|
+ this.addDistributeTemp(effectList, cell);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ AllocationParam allocationParam = new AllocationParam();
|
|
|
+ allocationParam.setRoleId(effectRoleId);
|
|
|
+ allocationParam.setUserIdList(userIdList);
|
|
|
+ allocationParamList.add(allocationParam);
|
|
|
+ }
|
|
|
+ // 再分助理工程师
|
|
|
+ if (assistantUnDistributed > 0) {
|
|
|
+ SysRole assistantRole = sysRoleService.list(new QueryWrapper<SysRole>().lambda().eq(SysRole::getType, RoleTypeEnum.ASSISTANT_ENGINEER)).get(0);
|
|
|
+ Long assistantRoleId = assistantRole.getId();
|
|
|
+ List<Long> userIdList = new ArrayList<>();
|
|
|
+ Map<Long, Integer> supplierMap = assistantFree.stream().collect(Collectors.toMap(ArchivesSourceResult::getSupplierId, e -> 1, Integer::sum));
|
|
|
+ supplierMap = SystemConstant.sortMapByValues(supplierMap);
|
|
|
+ for (Long supplierId : supplierMap.keySet()) {
|
|
|
+ Integer count = supplierMap.get(supplierId);
|
|
|
+ assistantUnDistributed = assistantUnDistributed - count;
|
|
|
+ List<ArchivesSourceResult> cell;
|
|
|
+ if (assistantUnDistributed > 0) {
|
|
|
+ // 该供应商的不够分配配额
|
|
|
+ cell = assistantFree.stream().filter(e -> supplierId.equals(e.getSupplierId())).limit(count).collect(Collectors.toList());
|
|
|
+ userIdList.addAll(cell.stream().map(ArchivesSourceResult::getUserId).distinct().collect(Collectors.toList()));
|
|
|
+ this.addDistributeTemp(assistantList, cell);
|
|
|
+ } else {
|
|
|
+ // 该供应商的足够分配配额
|
|
|
+ cell = assistantFree.stream().filter(e -> supplierId.equals(e.getSupplierId())).limit(assistantUnDistributed).collect(Collectors.toList());
|
|
|
+ userIdList.addAll(cell.stream().map(ArchivesSourceResult::getUserId).distinct().collect(Collectors.toList()));
|
|
|
+ this.addDistributeTemp(assistantList, cell);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
+ AllocationParam allocationParam = new AllocationParam();
|
|
|
+ allocationParam.setRoleId(assistantRoleId);
|
|
|
+ allocationParam.setUserIdList(userIdList);
|
|
|
+ allocationParamList.add(allocationParam);
|
|
|
}
|
|
|
|
|
|
+ // 最后分配区域协调人
|
|
|
+ Long regionUserId = null;
|
|
|
+ if (coordinatorUnDistributed > 0) {
|
|
|
+ if (CollectionUtils.isNotEmpty(effectList)) {
|
|
|
+ // 存在已经分配的实施工程师
|
|
|
+ List<Long> supplierIdList = effectList.stream().map(CrmArchivesAllocationResult::getSupplierId).distinct().collect(Collectors.toList());
|
|
|
+ if (supplierIdList.size() == 1) {
|
|
|
+ Long supplierId = supplierIdList.get(0);
|
|
|
+ List<ArchivesSourceResult> coordinator = coordinatorFree.stream()
|
|
|
+ .filter(e -> supplierId.equals(e.getSupplierId()) && e.getRemainCount() > 0)
|
|
|
+ .sorted(Comparator.comparing(ArchivesSourceResult::getRemainCount).reversed())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isNotEmpty(coordinator)) {
|
|
|
+ regionUserId = coordinator.get(0).getUserId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 不存在实施工程师
|
|
|
+ List<ArchivesSourceResult> coordinator = coordinatorFree.stream()
|
|
|
+ .filter(e -> e.getRemainCount() > 0)
|
|
|
+ .sorted(Comparator.comparing(ArchivesSourceResult::getRemainCount).reversed())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isNotEmpty(coordinator)) {
|
|
|
+ regionUserId = coordinator.get(0).getUserId();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ UserArchivesAllocationParam userArchivesAllocationParam = new UserArchivesAllocationParam();
|
|
|
+ userArchivesAllocationParam.setAllocationParams(allocationParamList);
|
|
|
+ userArchivesAllocationParam.setServiceUnitId(serviceUnitId);
|
|
|
+ userArchivesAllocationParam.setCrmNo(crmNo);
|
|
|
+ userArchivesAllocationParam.setRegionUserId(regionUserId);
|
|
|
+ this.editCrmAllocation(userArchivesAllocationParam);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理临时新增的分配名单
|
|
|
+ *
|
|
|
+ * @param distribute 已分配的
|
|
|
+ * @param insert 临时分配的
|
|
|
+ */
|
|
|
+ private void addDistributeTemp(List<CrmArchivesAllocationResult> distribute, List<ArchivesSourceResult> insert) {
|
|
|
+ distribute.addAll(insert.stream().flatMap(e -> {
|
|
|
+ CrmArchivesAllocationResult result = new CrmArchivesAllocationResult();
|
|
|
+ result.setArchivesId(e.getArchivesId());
|
|
|
+ result.setUserId(e.getUserId());
|
|
|
+ result.setArchivesProvince(e.getProvince());
|
|
|
+ result.setArchivesCity(e.getCity());
|
|
|
+ result.setSupplierId(e.getSupplierId());
|
|
|
+ return Stream.of(result);
|
|
|
+ }).collect(Collectors.toList()));
|
|
|
}
|
|
|
}
|