|
@@ -8,6 +8,7 @@ import cn.hutool.core.lang.tree.TreeUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.text.CharSequenceUtil;
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
@@ -34,18 +35,23 @@ import com.qunzhixinxi.hnqz.admin.service.SysPlanPkgService;
|
|
|
import com.qunzhixinxi.hnqz.admin.service.SysRoleService;
|
|
|
import com.qunzhixinxi.hnqz.admin.service.WmDaDrugEntDrugtableService;
|
|
|
import com.qunzhixinxi.hnqz.admin.service.WmScorePackageService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmTaskService;
|
|
|
import com.qunzhixinxi.hnqz.admin.service.WmTaskTypeService;
|
|
|
import com.qunzhixinxi.hnqz.common.core.constant.CommonConstants;
|
|
|
import com.qunzhixinxi.hnqz.common.core.exception.BizException;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.util.R;
|
|
|
import com.qunzhixinxi.hnqz.common.security.service.HnqzUser;
|
|
|
+import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -70,6 +76,7 @@ public class SysImplementPlanManager {
|
|
|
private final SysPlanPkgService planPkgService;
|
|
|
private final WmScorePackageService scorePackageService;
|
|
|
private final WmTaskTypeService taskTypeService;
|
|
|
+ private final RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
/**
|
|
|
* 创建计划
|
|
@@ -618,72 +625,95 @@ public class SysImplementPlanManager {
|
|
|
*/
|
|
|
public List<Map<String, String>> createBatch(List<SysImplementPlanDTO.OnBatchCreate> resource, HnqzUser user) {
|
|
|
|
|
|
- // 获取商品列表
|
|
|
+
|
|
|
+ // 加锁防止多处理
|
|
|
Integer deptId = user.getDeptId();
|
|
|
- List<Map<String, String>> drugs = drugEntDrugtableService.listDrugByDeptId(Collections.singletonList(deptId));
|
|
|
-
|
|
|
- // 获取代理商
|
|
|
- Map<String, SysDept> deptMap = deptService.list(Wrappers.<SysDept>lambdaQuery()
|
|
|
- .eq(SysDept::getDelFlag, CommonConstants.STATUS_NORMAL)
|
|
|
- .eq(SysDept::getLevel, 3)).stream()
|
|
|
- .collect(Collectors.toMap(SysDept::getName, Function.identity(), (v1, v2) -> v1));
|
|
|
-
|
|
|
- // 校验表单信息
|
|
|
- List<Map<String, String>> errorMap = checkDataOnBatchCreate(resource, drugs, deptMap, deptId);
|
|
|
- if (CollUtil.isNotEmpty(errorMap)) {
|
|
|
- log.info("错误信息:{}", errorMap);
|
|
|
- return errorMap;
|
|
|
- }
|
|
|
+ String lock = "lock:plan:batch:" + deptId;
|
|
|
+ boolean locked = false;
|
|
|
|
|
|
- // 拼装信息
|
|
|
- String username = user.getUsername();
|
|
|
- LocalDateTime now = LocalDateTime.now();
|
|
|
- List<SysImplementPlan> collect = resource.stream().map(item -> {
|
|
|
- SysImplementPlan plan = new SysImplementPlan();
|
|
|
- plan.setPlanName(item.getPlanName());
|
|
|
- UpmsType.ImplementPlanType pt = "年度".equals(item.getPlanType()) ? UpmsType.ImplementPlanType.ANN : UpmsType.ImplementPlanType.QRT;
|
|
|
- plan.setPlanType(pt);
|
|
|
- plan.setParentId(0);
|
|
|
- plan.setPlanState(UpmsState.ImplementPlanState.INIT);
|
|
|
- Long score = Long.valueOf(item.getPlanScore());
|
|
|
- plan.setAvailScore(score);
|
|
|
- plan.setPlanScore(score);
|
|
|
- Optional<String> skuId = drugs.stream().filter(drug -> StrUtil.equals(item.getSkuName(), drug.get("drug_name"))).map(drug -> drug.get("drug_id")).findFirst();
|
|
|
- plan.setSkuId(Integer.valueOf(skuId.get()));
|
|
|
- plan.setConsignorId(deptId);
|
|
|
- SysDept dept = deptMap.get(item.getConsignee());
|
|
|
- plan.setConsigneeId(dept.getDeptId());
|
|
|
-
|
|
|
- LocalDate issue;
|
|
|
- LocalDate expiry;
|
|
|
-
|
|
|
- if (UpmsType.ImplementPlanType.ANN.equals(pt)) {
|
|
|
- issue = LocalDate.of(Integer.parseInt(item.getYear()), 1, 1);
|
|
|
- expiry = issue.plusYears(1).minusDays(1);
|
|
|
- } else {
|
|
|
- int qtr = Integer.parseInt(item.getQuarter());
|
|
|
- int month = qtr * 3 - 2;
|
|
|
- issue = LocalDate.of(Integer.parseInt(item.getYear()), month, 1);
|
|
|
- expiry = issue.plusMonths(3).minusDays(1);
|
|
|
+ try {
|
|
|
+
|
|
|
+ locked = Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lock, IdUtil.fastUUID(), 10, TimeUnit.MINUTES));
|
|
|
+
|
|
|
+ if (!locked) {
|
|
|
+ throw new BizException("有在途的操作,请稍后");
|
|
|
}
|
|
|
|
|
|
- plan.setPlanIssue(issue);
|
|
|
- plan.setPlanExpiry(expiry);
|
|
|
+ log.info("批量操作加锁:{} =====================================", lock);
|
|
|
|
|
|
- plan.setLocFlag(LockEnum.UN_LOCK);
|
|
|
- plan.setDelFlag(DelEnum.NOT_DEL);
|
|
|
- plan.setCreateBy(username);
|
|
|
- plan.setUpdateBy(username);
|
|
|
- plan.setCreateTime(now);
|
|
|
- plan.setUpdateTime(now);
|
|
|
+ // 获取商品列表
|
|
|
+ List<Map<String, String>> drugs = drugEntDrugtableService.listDrugByDeptId(Collections.singletonList(deptId));
|
|
|
|
|
|
- return plan;
|
|
|
- }).collect(Collectors.toList());
|
|
|
+ // 获取代理商
|
|
|
+ Map<String, SysDept> deptMap = deptService.list(Wrappers.<SysDept>lambdaQuery()
|
|
|
+ .eq(SysDept::getDelFlag, CommonConstants.STATUS_NORMAL)
|
|
|
+ .eq(SysDept::getLevel, 3)).stream()
|
|
|
+ .collect(Collectors.toMap(SysDept::getName, Function.identity(), (v1, v2) -> v1));
|
|
|
+
|
|
|
+ // 校验表单信息
|
|
|
+ List<Map<String, String>> errorMap = checkDataOnBatchCreate(resource, drugs, deptMap, deptId);
|
|
|
+ if (CollUtil.isNotEmpty(errorMap)) {
|
|
|
+ log.info("错误信息:{}", errorMap);
|
|
|
+ return errorMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 拼装信息
|
|
|
+ String username = user.getUsername();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ List<SysImplementPlan> collect = resource.stream().map(item -> {
|
|
|
+ SysImplementPlan plan = new SysImplementPlan();
|
|
|
+ plan.setPlanName(item.getPlanName());
|
|
|
+ UpmsType.ImplementPlanType pt = "年度".equals(item.getPlanType()) ? UpmsType.ImplementPlanType.ANN : UpmsType.ImplementPlanType.QRT;
|
|
|
+ plan.setPlanType(pt);
|
|
|
+ plan.setParentId(0);
|
|
|
+ plan.setPlanState(UpmsState.ImplementPlanState.INIT);
|
|
|
+ Long score = Long.valueOf(item.getPlanScore());
|
|
|
+ plan.setAvailScore(score);
|
|
|
+ plan.setPlanScore(score);
|
|
|
+ Optional<String> skuId = drugs.stream().filter(drug -> StrUtil.equals(item.getSkuName(), drug.get("drug_name"))).map(drug -> drug.get("drug_id")).findFirst();
|
|
|
+ plan.setSkuId(Integer.valueOf(skuId.get()));
|
|
|
+ plan.setConsignorId(deptId);
|
|
|
+ SysDept dept = deptMap.get(item.getConsignee());
|
|
|
+ plan.setConsigneeId(dept.getDeptId());
|
|
|
+
|
|
|
+ LocalDate issue;
|
|
|
+ LocalDate expiry;
|
|
|
+
|
|
|
+ if (UpmsType.ImplementPlanType.ANN.equals(pt)) {
|
|
|
+ issue = LocalDate.of(Integer.parseInt(item.getYear()), 1, 1);
|
|
|
+ expiry = issue.plusYears(1).minusDays(1);
|
|
|
+ } else {
|
|
|
+ int qtr = Integer.parseInt(item.getQuarter());
|
|
|
+ int month = qtr * 3 - 2;
|
|
|
+ issue = LocalDate.of(Integer.parseInt(item.getYear()), month, 1);
|
|
|
+ expiry = issue.plusMonths(3).minusDays(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ plan.setPlanIssue(issue);
|
|
|
+ plan.setPlanExpiry(expiry);
|
|
|
+
|
|
|
+ plan.setLocFlag(LockEnum.UN_LOCK);
|
|
|
+ plan.setDelFlag(DelEnum.NOT_DEL);
|
|
|
+ plan.setCreateBy(username);
|
|
|
+ plan.setUpdateBy(username);
|
|
|
+ plan.setCreateTime(now);
|
|
|
+ plan.setUpdateTime(now);
|
|
|
+
|
|
|
+ return plan;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
- implementPlanService.saveBatch(collect);
|
|
|
+ implementPlanService.saveBatch(collect);
|
|
|
+
|
|
|
+ return Collections.emptyList();
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ if (locked) {
|
|
|
+ log.info("批量操作解锁:{} =====================================", lock);
|
|
|
+ redisTemplate.delete(lock);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
/**
|