|
@@ -0,0 +1,166 @@
|
|
|
+package com.qunzhixinxi.hnqz.admin.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.WmTaskSubmissionPercentRule;
|
|
|
+import com.qunzhixinxi.hnqz.admin.entity.WmScorePackage;
|
|
|
+import com.qunzhixinxi.hnqz.admin.entity.WmTask;
|
|
|
+import com.qunzhixinxi.hnqz.admin.entity.WmTaskType;
|
|
|
+import com.qunzhixinxi.hnqz.admin.enums.DelEnum;
|
|
|
+import com.qunzhixinxi.hnqz.admin.mapper.WmTaskSubmissionPercentRuleMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmTaskService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmTaskSubmissionPercentRuleService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmTaskTypeService;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.constant.enums.CommonFlag;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.util.R;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 任务提交百分比规则
|
|
|
+ *
|
|
|
+ * @author snows
|
|
|
+ * @date 2022/12/22 15:35
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class WmTaskSubmissionPercentRuleServiceImpl
|
|
|
+ extends ServiceImpl<WmTaskSubmissionPercentRuleMapper, WmTaskSubmissionPercentRule>
|
|
|
+ implements WmTaskSubmissionPercentRuleService {
|
|
|
+
|
|
|
+ private final WmTaskService wmTaskService;
|
|
|
+
|
|
|
+ private final WmTaskTypeService wmTaskTypeService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验任务百分比限制规则(达到要求的积分包,【任务配置】-【百分比限制】所选的任务总完成积分值不得大于积分包值*百分比)
|
|
|
+ *
|
|
|
+ * @param scorePackage 积分包实体
|
|
|
+ * @param deptId 部门id
|
|
|
+ * @param taskUserId 任务承接人userId
|
|
|
+ * @param checkTaskTypeList 要校验的任务类型list
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R<?> checkPercentRule(WmScorePackage scorePackage, Integer deptId, Integer taskUserId, List<String> checkTaskTypeList) {
|
|
|
+
|
|
|
+ AtomicBoolean failFlag = new AtomicBoolean(false);
|
|
|
+ AtomicReference<String> message = new AtomicReference<>("");
|
|
|
+
|
|
|
+ // 查询配置的百分比规则
|
|
|
+ List<WmTaskSubmissionPercentRule> percentRules = this.list(Wrappers.<WmTaskSubmissionPercentRule>lambdaQuery()
|
|
|
+ .in(WmTaskSubmissionPercentRule::getDeptId, 1, deptId)
|
|
|
+ .eq(WmTaskSubmissionPercentRule::getOptFlag, CommonFlag.OptFlag.OK));
|
|
|
+
|
|
|
+ // 根据deptId分组,再根据batchId分组
|
|
|
+ Map<Integer, Map<String, List<WmTaskSubmissionPercentRule>>> deptsMap = percentRules.stream()
|
|
|
+ .collect(Collectors.groupingBy(WmTaskSubmissionPercentRule::getDeptId, Collectors.groupingBy(WmTaskSubmissionPercentRule::getBatchId)));
|
|
|
+
|
|
|
+ deptsMap.forEach((dptId, batchRules) -> {
|
|
|
+ // 查询taskType
|
|
|
+ List<WmTaskType> taskTypeList = wmTaskTypeService.list(Wrappers.<WmTaskType>lambdaQuery()
|
|
|
+ .eq(WmTaskType::getTaskTypeLevel, "1")
|
|
|
+ .eq(WmTaskType::getDelFlag, "0")
|
|
|
+ .eq(WmTaskType::getEnableFlag, "0"));
|
|
|
+ // taskType通过id分组(key为parentId,value为taskTypeId的set)
|
|
|
+ Map<String, String> taskTypeIdMap = taskTypeList.stream()
|
|
|
+ .collect(Collectors.toMap(WmTaskType::getId, WmTaskType::getTaskTypeName));
|
|
|
+
|
|
|
+ batchRules.forEach((batchId, rules) -> {
|
|
|
+
|
|
|
+ // 获取该分组下所有taskTypeId
|
|
|
+ List<String> allTaskTypeIds = rules.stream()
|
|
|
+ .flatMap(percentRule -> Arrays.stream(percentRule.getRule().getTaskTypeIds()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<String> toCheckTaskTypes = null;
|
|
|
+ if (checkTaskTypeList != null) {
|
|
|
+ // 把要校验的任务 和 已配置的规则任务 做交集,得到实际需要校验的任务类型
|
|
|
+ toCheckTaskTypes = new ArrayList<>(CollUtil.intersection(allTaskTypeIds, checkTaskTypeList));
|
|
|
+ } else {
|
|
|
+ toCheckTaskTypes = allTaskTypeIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(toCheckTaskTypes)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ WmTaskSubmissionPercentRule.PercentRule rule = rules.get(0).getRule();
|
|
|
+
|
|
|
+ // 指定任务类型做的任务总积分不得大于积分包值的rule.limitPercent%(积分包值小于rule.startScore不做限制)
|
|
|
+ if (scorePackage.getScore() > rule.getStartScore()) {
|
|
|
+ LambdaQueryWrapper<WmTask> queryWrapper = Wrappers.<WmTask>lambdaQuery()
|
|
|
+ .eq(WmTask::getRealFlag, "0")
|
|
|
+ .eq(WmTask::getDelFlag, DelEnum.NOT_DEL.val())
|
|
|
+ .in(WmTask::getScorePackageId, scorePackage.getId())
|
|
|
+ .in(WmTask::getTaskTypeId, toCheckTaskTypes)
|
|
|
+ .ne(WmTask::getTaskStatus, "4");
|
|
|
+ if (taskUserId != null) {
|
|
|
+ queryWrapper.eq(WmTask::getTaskUserId, taskUserId);
|
|
|
+ }
|
|
|
+ List<WmTask> wmTaskList = wmTaskService.list(queryWrapper);
|
|
|
+
|
|
|
+ if (CollectionUtil.isNotEmpty(wmTaskList)) {
|
|
|
+ int totalScore = scorePackage.getScore();
|
|
|
+ int partScore = wmTaskList.stream().mapToInt(WmTask::getScore).sum();
|
|
|
+ if (partScore * 100.0 / totalScore > Float.parseFloat(rule.getLimitPercent())) {
|
|
|
+ failFlag.set(true);
|
|
|
+
|
|
|
+ String taskTypeNames = toCheckTaskTypes.stream().map(taskTypeIdMap::get).collect(Collectors.joining("、"));
|
|
|
+ message.set(String.format("%s任务类型的任务总积分不得大于积分包值的%s%%", taskTypeNames, rule.getLimitPercent()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ return failFlag.get() ? R.failed(null, message.get()) : R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新
|
|
|
+ *
|
|
|
+ * @param rules
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean updateRule(List<WmTaskSubmissionPercentRule> rules) {
|
|
|
+
|
|
|
+ // 先删除,后新增
|
|
|
+ String batchId = rules.get(0).getBatchId();
|
|
|
+ List<WmTaskSubmissionPercentRule> queryRules = this.list(Wrappers.<WmTaskSubmissionPercentRule>lambdaQuery()
|
|
|
+ .eq(WmTaskSubmissionPercentRule::getBatchId, batchId));
|
|
|
+ WmTaskSubmissionPercentRule percentRule = queryRules.get(0);
|
|
|
+
|
|
|
+ this.update(Wrappers.<WmTaskSubmissionPercentRule>lambdaUpdate()
|
|
|
+ .eq(WmTaskSubmissionPercentRule::getBatchId, batchId)
|
|
|
+ .set(WmTaskSubmissionPercentRule::getOptFlag, CommonFlag.OptFlag.DELETED));
|
|
|
+
|
|
|
+ List<WmTaskSubmissionPercentRule> updateRules = new ArrayList<>();
|
|
|
+ for (WmTaskSubmissionPercentRule rule : rules) {
|
|
|
+
|
|
|
+ WmTaskSubmissionPercentRule insertRule = new WmTaskSubmissionPercentRule();
|
|
|
+ insertRule.setRule(rule.getRule());
|
|
|
+ insertRule.setBatchId(percentRule.getBatchId());
|
|
|
+ insertRule.setDeptId(percentRule.getDeptId());
|
|
|
+ insertRule.setSubCategory(rule.getSubCategory());
|
|
|
+ insertRule.setOptFlag(CommonFlag.OptFlag.OK);
|
|
|
+ updateRules.add(insertRule);
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.saveBatch(updateRules);
|
|
|
+ }
|
|
|
+}
|