|
|
@@ -0,0 +1,713 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.common.StateGuard;
|
|
|
+import com.train.entity.TrainCreditLedger;
|
|
|
+import com.train.entity.TrainEvalRecord;
|
|
|
+import com.train.entity.TrainPractitionerProfile;
|
|
|
+import com.train.entity.TrainPromotionRecord;
|
|
|
+import com.train.entity.TrainPromotionRule;
|
|
|
+import com.train.entity.TrainServiceOrder;
|
|
|
+import com.train.mapper.TrainCreditLedgerMapper;
|
|
|
+import com.train.mapper.TrainEvalRecordMapper;
|
|
|
+import com.train.mapper.TrainPractitionerProfileMapper;
|
|
|
+import com.train.mapper.TrainPromotionRecordMapper;
|
|
|
+import com.train.mapper.TrainPromotionRuleMapper;
|
|
|
+import com.train.mapper.TrainServiceOrderMapper;
|
|
|
+import com.train.service.BadgeService;
|
|
|
+import com.train.service.PractitionerService;
|
|
|
+import com.train.service.PromotionService;
|
|
|
+import com.train.service.RoleGuard;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 晋升域实现。
|
|
|
+ *
|
|
|
+ * 关键口径:
|
|
|
+ * - conditions JSON 为唯一执行口径(多条件 AND 判定);metric/operator/value 仅展示冗余(保存时一致性写)
|
|
|
+ * - 状态机:达标 → promoted(同事务:记录 + profile.auth_level + 勋章 lec_tX);未达标 → evaluated + pass=0 + unmet;
|
|
|
+ * 机构否决 → rejected(StateGuard 终裁;重新发起=新记录)
|
|
|
+ * - 认证升级原子性:self.evaluateAndRecord 经 @Lazy 自注入代理调用,保证单条 @Transactional 生效
|
|
|
+ * - metric 取值口径:serve_count/order_count=已确认工单数;rating=profile.rating*100(0-500);
|
|
|
+ * theory_hours/total_hours/credit_hours=台账 effective 分钟聚合;exam_score=最近一次培训考核成绩
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class PromotionServiceImpl implements PromotionService {
|
|
|
+
|
|
|
+ private static final String LV_NONE = "none";
|
|
|
+ private static final List<String> LV_ORDER = Arrays.asList("none", "T1", "T2", "T3", "T4");
|
|
|
+
|
|
|
+ private static final String PR_EVALUATED = "evaluated";
|
|
|
+ private static final String PR_PROMOTED = "promoted";
|
|
|
+ private static final String PR_REJECTED = "rejected";
|
|
|
+
|
|
|
+ private static final String ST_EFFECTIVE = "effective";
|
|
|
+ private static final String CAT_THEORY = "theory";
|
|
|
+ private static final String ET_TRAINING = "training";
|
|
|
+ private static final String ST_VALID = "valid";
|
|
|
+ private static final String O_CONFIRMED = "confirmed";
|
|
|
+ private static final String BADGE_PREFIX = "lec_t";
|
|
|
+
|
|
|
+ private static final Set<String> ALLOWED_METRICS = new HashSet<>(Arrays.asList(
|
|
|
+ "serve_count", "order_count", "rating", "theory_hours", "total_hours", "credit_hours", "exam_score"));
|
|
|
+ private static final Set<String> ALLOWED_OPS = new HashSet<>(Arrays.asList(
|
|
|
+ ">=", ">", "==", "<=", "<", "ge", "gt", "eq", "le", "lt"));
|
|
|
+
|
|
|
+ private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TrainPromotionRuleMapper trainPromotionRuleMapper;
|
|
|
+ @Resource
|
|
|
+ private TrainPromotionRecordMapper trainPromotionRecordMapper;
|
|
|
+ @Resource
|
|
|
+ private TrainPractitionerProfileMapper trainPractitionerProfileMapper;
|
|
|
+ @Resource
|
|
|
+ private TrainServiceOrderMapper trainServiceOrderMapper;
|
|
|
+ @Resource
|
|
|
+ private TrainCreditLedgerMapper trainCreditLedgerMapper;
|
|
|
+ @Resource
|
|
|
+ private TrainEvalRecordMapper trainEvalRecordMapper;
|
|
|
+ @Resource
|
|
|
+ private PractitionerService practitionerService;
|
|
|
+ @Resource
|
|
|
+ private BadgeService badgeService;
|
|
|
+ @Resource
|
|
|
+ private RoleGuard roleGuard;
|
|
|
+ /** 自注入代理:绕过 self-invocation,使 evaluateAndRecord 的 @Transactional 生效(@Lazy 避免初始化循环) */
|
|
|
+ @Lazy
|
|
|
+ @Resource
|
|
|
+ private PromotionService self;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> apply(Long userId, String authLevel) {
|
|
|
+ if (userId == null || authLevel == null || authLevel.trim().isEmpty()) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ String lv = authLevel.trim().toUpperCase();
|
|
|
+ int target = levelIndex(lv);
|
|
|
+ if (target < 1) {
|
|
|
+ return Result.error("目标认证等级不正确");
|
|
|
+ }
|
|
|
+ TrainPractitionerProfile p = practitionerService.ensureProfile(userId);
|
|
|
+ if (p == null) {
|
|
|
+ return Result.error("档案不存在");
|
|
|
+ }
|
|
|
+ int cur = levelIndex(normLevel(p.getAuthLevel()));
|
|
|
+ if (cur >= target) {
|
|
|
+ return Result.success(promptMap("当前已是该认证等级", lv));
|
|
|
+ }
|
|
|
+ TrainPromotionRule rule = enabledRule(lv);
|
|
|
+ if (rule == null) {
|
|
|
+ return Result.error("未配置该等级的晋升规则");
|
|
|
+ }
|
|
|
+ return self.evaluateAndRecord(userId, rule.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Result<Map<String, Object>> evaluateAndRecord(Long userId, Long ruleId) {
|
|
|
+ if (userId == null || ruleId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ TrainPromotionRule rule = trainPromotionRuleMapper.selectById(ruleId);
|
|
|
+ if (rule == null || (rule.getDeleted() != null && rule.getDeleted() == 1)) {
|
|
|
+ return Result.error("规则不存在");
|
|
|
+ }
|
|
|
+ // 并发幂等:目标等级 ≤ 当前等级 → 不重复晋升(不落记录)
|
|
|
+ TrainPractitionerProfile p = selectProfileByUserId(userId);
|
|
|
+ int cur = levelIndex(normLevel(p == null ? null : p.getAuthLevel()));
|
|
|
+ int target = levelIndex(rule.getAuthLevel());
|
|
|
+ if (p != null && cur >= target) {
|
|
|
+ return Result.success(promptMap("当前已是该认证等级", rule.getAuthLevel()));
|
|
|
+ }
|
|
|
+ // conditions 逐条 AND 判定
|
|
|
+ List<Map<String, Object>> conds = parseConditions(rule.getConditions());
|
|
|
+ if (conds == null) {
|
|
|
+ return Result.error("规则达标条件配置错误");
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> unmet = new ArrayList<>();
|
|
|
+ for (Map<String, Object> c : conds) {
|
|
|
+ String metric = c == null ? null : str(c.get("metric"));
|
|
|
+ String op = normOp(c == null ? null : str(c.get("op")));
|
|
|
+ Long value = toLong(c == null ? null : c.get("value"));
|
|
|
+ if (metric == null || op == null || value == null) {
|
|
|
+ return Result.error("规则达标条件配置错误");
|
|
|
+ }
|
|
|
+ long current = metricValue(userId, metric);
|
|
|
+ if (!compare(current, op, value.longValue())) {
|
|
|
+ Map<String, Object> u = new HashMap<>();
|
|
|
+ u.put("metric", metric);
|
|
|
+ u.put("label", metricLabel(metric));
|
|
|
+ u.put("current", current);
|
|
|
+ u.put("threshold", value);
|
|
|
+ unmet.add(u);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Date now = new Date();
|
|
|
+ TrainPromotionRecord rec = new TrainPromotionRecord();
|
|
|
+ rec.setUserId(userId);
|
|
|
+ rec.setAuthLevel(rule.getAuthLevel());
|
|
|
+ rec.setRuleId(rule.getId());
|
|
|
+ rec.setAppliedAt(now);
|
|
|
+ rec.setDeleted(0);
|
|
|
+ if (unmet.isEmpty()) {
|
|
|
+ // 达标 → 同事务真实晋升(记录 + profile.auth_level + 勋章)
|
|
|
+ rec.setPass(1);
|
|
|
+ rec.setStatus(PR_PROMOTED);
|
|
|
+ rec.setPromotedAt(now);
|
|
|
+ trainPromotionRecordMapper.insert(rec);
|
|
|
+ TrainPractitionerProfile pp = p != null ? p : practitionerService.ensureProfile(userId);
|
|
|
+ if (pp != null) {
|
|
|
+ pp.setAuthLevel(rule.getAuthLevel());
|
|
|
+ pp.setUpdatedAt(now);
|
|
|
+ trainPractitionerProfileMapper.updateById(pp);
|
|
|
+ }
|
|
|
+ badgeService.grantBadge(userId, BADGE_PREFIX + rule.getAuthLevel(), 0L, "讲师认证晋升 " + rule.getAuthLevel());
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("eligible", true);
|
|
|
+ data.put("promoted", true);
|
|
|
+ data.put("authLevel", rule.getAuthLevel());
|
|
|
+ data.put("unmet", new ArrayList<>());
|
|
|
+ data.put("recordId", rec.getId());
|
|
|
+ return Result.success(data);
|
|
|
+ } else {
|
|
|
+ // 未达标 → evaluated 记录缺口(不发生状态迁移)
|
|
|
+ rec.setPass(0);
|
|
|
+ rec.setStatus(PR_EVALUATED);
|
|
|
+ rec.setUnmet(writeJson(unmet));
|
|
|
+ trainPromotionRecordMapper.insert(rec);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("eligible", false);
|
|
|
+ data.put("promoted", false);
|
|
|
+ data.put("authLevel", rule.getAuthLevel());
|
|
|
+ data.put("unmet", unmet);
|
|
|
+ data.put("recordId", rec.getId());
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> unlockCheck(Long userId, Map<String, Object> body) {
|
|
|
+ if (userId == null) {
|
|
|
+ return Result.error("需要登录");
|
|
|
+ }
|
|
|
+ TrainPractitionerProfile p = selectProfileByUserId(userId);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ if (p == null) {
|
|
|
+ data.put("allowed", false);
|
|
|
+ data.put("reason", "档案不存在");
|
|
|
+ data.put("authLevel", null);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+ data.put("authLevel", p.getAuthLevel());
|
|
|
+ if (p.getStatus() == null || !"approved".equals(p.getStatus())) {
|
|
|
+ data.put("allowed", false);
|
|
|
+ data.put("reason", "资质未生效");
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+ if (levelIndex(normLevel(p.getAuthLevel())) < 1) {
|
|
|
+ data.put("allowed", false);
|
|
|
+ data.put("reason", "未认证");
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+ data.put("allowed", true);
|
|
|
+ data.put("reason", "资质已生效,认证等级 " + p.getAuthLevel());
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> records(Long userId, Integer page, Integer size) {
|
|
|
+ if (userId == null) {
|
|
|
+ return Result.error("需要登录");
|
|
|
+ }
|
|
|
+ int p = page == null || page < 1 ? 1 : page;
|
|
|
+ int s = size == null || size < 1 ? 20 : Math.min(size, 100);
|
|
|
+ int offset = (p - 1) * s;
|
|
|
+ LambdaQueryWrapper<TrainPromotionRecord> q = new LambdaQueryWrapper<TrainPromotionRecord>()
|
|
|
+ .eq(TrainPromotionRecord::getUserId, userId)
|
|
|
+ .eq(TrainPromotionRecord::getDeleted, 0)
|
|
|
+ .orderByDesc(TrainPromotionRecord::getAppliedAt);
|
|
|
+ Long total = trainPromotionRecordMapper.selectCount(q);
|
|
|
+ List<TrainPromotionRecord> list = trainPromotionRecordMapper.selectList(q.last("LIMIT " + offset + "," + s));
|
|
|
+ List<Map<String, Object>> rows = new ArrayList<>();
|
|
|
+ for (TrainPromotionRecord r : list) {
|
|
|
+ rows.add(recordVO(r));
|
|
|
+ }
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("total", total == null ? 0 : total);
|
|
|
+ data.put("page", p);
|
|
|
+ data.put("size", s);
|
|
|
+ data.put("list", rows);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> saveRule(Boolean isAdmin, Map<String, Object> body) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ if (body == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ String name = str(body.get("name"));
|
|
|
+ String authLevel = body.get("authLevel") == null ? null : str(body.get("authLevel")).toUpperCase();
|
|
|
+ if (name == null || name.isEmpty()) {
|
|
|
+ return Result.error("规则名称必填");
|
|
|
+ }
|
|
|
+ if (levelIndex(authLevel) < 1) {
|
|
|
+ return Result.error("目标认证等级不正确");
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> conds = normalizeConditions(body.get("conditions"));
|
|
|
+ if (conds == null || conds.isEmpty()) {
|
|
|
+ return Result.error("达标条件不能为空");
|
|
|
+ }
|
|
|
+ TrainPromotionRule rule = new TrainPromotionRule();
|
|
|
+ rule.setName(name);
|
|
|
+ rule.setAuthLevel(authLevel);
|
|
|
+ rule.setConditions(writeJson(conds));
|
|
|
+ // 一致性写:单条件回填展示冗余列;多条件置 NULL(语义=「多条件,看 conditions」)
|
|
|
+ if (conds.size() == 1) {
|
|
|
+ Map<String, Object> only = conds.get(0);
|
|
|
+ rule.setMetric(str(only.get("metric")));
|
|
|
+ rule.setOperator(str(only.get("op")));
|
|
|
+ rule.setValue(toLong(only.get("value")) == null ? null : toLong(only.get("value")).intValue());
|
|
|
+ }
|
|
|
+ Object en = body.get("enabled");
|
|
|
+ int enabledFlag = (en == null) ? 1 : (parseBool(en) ? 1 : 0); // 缺省启用
|
|
|
+ rule.setEnabled(enabledFlag);
|
|
|
+ rule.setVersion(1);
|
|
|
+ rule.setDeleted(0);
|
|
|
+ trainPromotionRuleMapper.insert(rule);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("ruleId", rule.getId());
|
|
|
+ data.put("authLevel", authLevel);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> ruleList(Boolean isAdmin, Integer page, Integer size) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ int p = page == null || page < 1 ? 1 : page;
|
|
|
+ int s = size == null || size < 1 ? 20 : Math.min(size, 100);
|
|
|
+ int offset = (p - 1) * s;
|
|
|
+ LambdaQueryWrapper<TrainPromotionRule> q = new LambdaQueryWrapper<TrainPromotionRule>()
|
|
|
+ .eq(TrainPromotionRule::getDeleted, 0)
|
|
|
+ .orderByAsc(TrainPromotionRule::getAuthLevel)
|
|
|
+ .orderByDesc(TrainPromotionRule::getVersion);
|
|
|
+ Long total = trainPromotionRuleMapper.selectCount(q);
|
|
|
+ List<TrainPromotionRule> list = trainPromotionRuleMapper.selectList(q.last("LIMIT " + offset + "," + s));
|
|
|
+ List<Map<String, Object>> rows = new ArrayList<>();
|
|
|
+ for (TrainPromotionRule r : list) {
|
|
|
+ rows.add(ruleVO(r));
|
|
|
+ }
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("total", total == null ? 0 : total);
|
|
|
+ data.put("page", p);
|
|
|
+ data.put("size", s);
|
|
|
+ data.put("list", rows);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> ruleEnable(Boolean isAdmin, Long ruleId, Boolean enabled) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ if (ruleId == null || enabled == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ TrainPromotionRule rule = trainPromotionRuleMapper.selectById(ruleId);
|
|
|
+ if (rule == null) {
|
|
|
+ return Result.error("规则不存在");
|
|
|
+ }
|
|
|
+ rule.setEnabled(Boolean.TRUE.equals(enabled) ? 1 : 0);
|
|
|
+ rule.setUpdatedAt(new Date());
|
|
|
+ trainPromotionRuleMapper.updateById(rule);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("ruleId", ruleId);
|
|
|
+ data.put("enabled", rule.getEnabled());
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> rejectRecord(Boolean isAdmin, Long recordId, String reason) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ if (recordId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ TrainPromotionRecord rec = trainPromotionRecordMapper.selectById(recordId);
|
|
|
+ if (rec == null || (rec.getDeleted() != null && rec.getDeleted() == 1)) {
|
|
|
+ return Result.error("晋升记录不存在");
|
|
|
+ }
|
|
|
+ String err = StateGuard.checkPromotion(rec.getStatus(), PR_REJECTED);
|
|
|
+ if (err != null) {
|
|
|
+ return Result.error(err);
|
|
|
+ }
|
|
|
+ rec.setStatus(PR_REJECTED);
|
|
|
+ rec.setUpdatedAt(new Date());
|
|
|
+ trainPromotionRecordMapper.updateById(rec);
|
|
|
+ log.info("晋升记录机构否决: recordId={}, userId={}, authLevel={}, reason={}",
|
|
|
+ recordId, rec.getUserId(), rec.getAuthLevel(), reason);
|
|
|
+ return Result.success(recordVO(rec));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> scanAndGrant(Boolean isAdmin) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ int processed = 0;
|
|
|
+ int granted = 0;
|
|
|
+ List<TrainPractitionerProfile> profiles = trainPractitionerProfileMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<TrainPractitionerProfile>()
|
|
|
+ .eq(TrainPractitionerProfile::getDeleted, 0));
|
|
|
+ List<TrainPromotionRule> rules = trainPromotionRuleMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<TrainPromotionRule>()
|
|
|
+ .eq(TrainPromotionRule::getEnabled, 1)
|
|
|
+ .eq(TrainPromotionRule::getDeleted, 0)
|
|
|
+ .orderByAsc(TrainPromotionRule::getId));
|
|
|
+ for (TrainPractitionerProfile p : profiles) {
|
|
|
+ int cur = levelIndex(normLevel(p.getAuthLevel()));
|
|
|
+ for (TrainPromotionRule r : rules) {
|
|
|
+ int target = levelIndex(r.getAuthLevel());
|
|
|
+ if (target <= cur) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ processed++;
|
|
|
+ try {
|
|
|
+ Result<Map<String, Object>> res = self.evaluateAndRecord(p.getUserId(), r.getId());
|
|
|
+ if (res != null && res.getData() != null
|
|
|
+ && Boolean.TRUE.equals(res.getData().get("promoted"))) {
|
|
|
+ granted++;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("晋升评定异常跳过: userId={}, ruleId={}, err={}", p.getUserId(), r.getId(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("晋升周期评定完成: processed={}, granted={}", processed, granted);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("processed", processed);
|
|
|
+ data.put("granted", granted);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ============ 内部工具 ============
|
|
|
+
|
|
|
+ private TrainPromotionRule enabledRule(String authLevel) {
|
|
|
+ return trainPromotionRuleMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<TrainPromotionRule>()
|
|
|
+ .eq(TrainPromotionRule::getAuthLevel, authLevel)
|
|
|
+ .eq(TrainPromotionRule::getEnabled, 1)
|
|
|
+ .eq(TrainPromotionRule::getDeleted, 0)
|
|
|
+ .orderByDesc(TrainPromotionRule::getVersion)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private TrainPractitionerProfile selectProfileByUserId(Long userId) {
|
|
|
+ return trainPractitionerProfileMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<TrainPractitionerProfile>()
|
|
|
+ .eq(TrainPractitionerProfile::getUserId, userId)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 指标当前值(数值口径:serve_count=确认工单数;rating=评分*100 0-500;学时=effective 分钟) */
|
|
|
+ private long metricValue(Long userId, String metric) {
|
|
|
+ if (userId == null) {
|
|
|
+ return -1L;
|
|
|
+ }
|
|
|
+ if ("serve_count".equals(metric) || "order_count".equals(metric)) {
|
|
|
+ Long c = trainServiceOrderMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<TrainServiceOrder>()
|
|
|
+ .eq(TrainServiceOrder::getWorkerId, userId)
|
|
|
+ .eq(TrainServiceOrder::getStatus, O_CONFIRMED));
|
|
|
+ return c == null ? 0 : c;
|
|
|
+ }
|
|
|
+ if ("rating".equals(metric)) {
|
|
|
+ TrainPractitionerProfile p = selectProfileByUserId(userId);
|
|
|
+ double rating = p == null || p.getRating() == null ? 0 : p.getRating();
|
|
|
+ return Math.round(rating * 100); // 0-500
|
|
|
+ }
|
|
|
+ if ("theory_hours".equals(metric)) {
|
|
|
+ return sumLedger(userId, CAT_THEORY);
|
|
|
+ }
|
|
|
+ if ("total_hours".equals(metric) || "credit_hours".equals(metric)) {
|
|
|
+ return sumLedger(userId, null);
|
|
|
+ }
|
|
|
+ if ("exam_score".equals(metric)) {
|
|
|
+ TrainEvalRecord e = trainEvalRecordMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<TrainEvalRecord>()
|
|
|
+ .eq(TrainEvalRecord::getUserId, userId)
|
|
|
+ .eq(TrainEvalRecord::getEvalType, ET_TRAINING)
|
|
|
+ .eq(TrainEvalRecord::getStatus, ST_VALID)
|
|
|
+ .eq(TrainEvalRecord::getDeleted, 0)
|
|
|
+ .orderByDesc(TrainEvalRecord::getCreatedAt)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ return e == null || e.getScore() == null ? 0 : e.getScore();
|
|
|
+ }
|
|
|
+ return -1L;
|
|
|
+ }
|
|
|
+
|
|
|
+ private long sumLedger(Long userId, String category) {
|
|
|
+ LambdaQueryWrapper<TrainCreditLedger> q = new LambdaQueryWrapper<TrainCreditLedger>()
|
|
|
+ .eq(TrainCreditLedger::getUserId, userId)
|
|
|
+ .eq(TrainCreditLedger::getStatus, ST_EFFECTIVE)
|
|
|
+ .eq(TrainCreditLedger::getDeleted, 0);
|
|
|
+ if (category != null) {
|
|
|
+ q.eq(TrainCreditLedger::getSourceCategory, category);
|
|
|
+ }
|
|
|
+ List<TrainCreditLedger> list = trainCreditLedgerMapper.selectList(q);
|
|
|
+ long sum = 0;
|
|
|
+ for (TrainCreditLedger l : list) {
|
|
|
+ sum += l.getHours() == null ? 0 : l.getHours();
|
|
|
+ }
|
|
|
+ return sum;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean compare(long current, String op, long value) {
|
|
|
+ switch (op) {
|
|
|
+ case ">=":
|
|
|
+ return current >= value;
|
|
|
+ case ">":
|
|
|
+ return current > value;
|
|
|
+ case "==":
|
|
|
+ return current == value;
|
|
|
+ case "<=":
|
|
|
+ return current <= value;
|
|
|
+ case "<":
|
|
|
+ return current < value;
|
|
|
+ default:
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 遍历校验并归一 conditions(op 归一为符号) */
|
|
|
+ private List<Map<String, Object>> normalizeConditions(Object obj) {
|
|
|
+ List<Map<String, Object>> conds;
|
|
|
+ try {
|
|
|
+ if (obj instanceof String) {
|
|
|
+ conds = objectMapper.readValue((String) obj, new TypeReference<List<Map<String, Object>>>() {
|
|
|
+ });
|
|
|
+ } else if (obj instanceof List) {
|
|
|
+ conds = new ArrayList<>();
|
|
|
+ for (Object o : (List<?>) obj) {
|
|
|
+ if (o instanceof Map) {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> m = (Map<String, Object>) o;
|
|
|
+ conds.add(m);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (conds == null || conds.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> norm = new ArrayList<>();
|
|
|
+ for (Map<String, Object> c : conds) {
|
|
|
+ String metric = str(c.get("metric"));
|
|
|
+ String op = normOp(str(c.get("op")));
|
|
|
+ Long value = toLong(c.get("value"));
|
|
|
+ if (metric == null || !ALLOWED_METRICS.contains(metric) || op == null || value == null || value < 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Map<String, Object> one = new HashMap<>();
|
|
|
+ one.put("metric", metric);
|
|
|
+ one.put("op", op);
|
|
|
+ one.put("value", value);
|
|
|
+ norm.add(one);
|
|
|
+ }
|
|
|
+ return norm;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Map<String, Object>> parseConditions(String json) {
|
|
|
+ if (json == null || json.trim().isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String writeJson(Object o) {
|
|
|
+ try {
|
|
|
+ return objectMapper.writeValueAsString(o);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** op 规整为符号比较符(兼容 ge/gt/eq/le/lt) */
|
|
|
+ private String normOp(String op) {
|
|
|
+ if (op == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String o = op.trim();
|
|
|
+ if (">=".equals(o) || "ge".equalsIgnoreCase(o)) {
|
|
|
+ return ">=";
|
|
|
+ }
|
|
|
+ if (">".equals(o) || "gt".equalsIgnoreCase(o)) {
|
|
|
+ return ">";
|
|
|
+ }
|
|
|
+ if ("==".equals(o) || "eq".equalsIgnoreCase(o)) {
|
|
|
+ return "==";
|
|
|
+ }
|
|
|
+ if ("<=".equals(o) || "le".equalsIgnoreCase(o)) {
|
|
|
+ return "<=";
|
|
|
+ }
|
|
|
+ if ("<".equals(o) || "lt".equalsIgnoreCase(o)) {
|
|
|
+ return "<";
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String metricLabel(String metric) {
|
|
|
+ switch (metric) {
|
|
|
+ case "serve_count":
|
|
|
+ case "order_count":
|
|
|
+ return "接单量";
|
|
|
+ case "rating":
|
|
|
+ return "综合评分";
|
|
|
+ case "theory_hours":
|
|
|
+ return "理论学时";
|
|
|
+ case "total_hours":
|
|
|
+ case "credit_hours":
|
|
|
+ return "总学时";
|
|
|
+ case "exam_score":
|
|
|
+ return "考核成绩";
|
|
|
+ default:
|
|
|
+ return metric;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int levelIndex(String lv) {
|
|
|
+ int idx = LV_ORDER.indexOf(lv);
|
|
|
+ return idx < 0 ? -1 : idx;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String normLevel(String lv) {
|
|
|
+ if (lv == null || lv.trim().isEmpty()) {
|
|
|
+ return LV_NONE;
|
|
|
+ }
|
|
|
+ String u = lv.trim().toUpperCase();
|
|
|
+ return LV_ORDER.contains(u) ? u : LV_NONE;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> promptMap(String message, String authLevel) {
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("eligible", false);
|
|
|
+ data.put("promoted", false);
|
|
|
+ data.put("authLevel", authLevel);
|
|
|
+ data.put("message", message);
|
|
|
+ data.put("unmet", new ArrayList<>());
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> recordVO(TrainPromotionRecord r) {
|
|
|
+ Map<String, Object> m = new HashMap<>();
|
|
|
+ m.put("recordId", r.getId());
|
|
|
+ m.put("id", r.getId()); // 兼容键:履历项 id(与 recordId 同值)
|
|
|
+ m.put("userId", r.getUserId());
|
|
|
+ m.put("authLevel", r.getAuthLevel());
|
|
|
+ m.put("title", (r.getAuthLevel() == null ? "" : r.getAuthLevel()) + " 讲师认证"); // 兼容键:展示标题
|
|
|
+ m.put("ruleId", r.getRuleId());
|
|
|
+ m.put("pass", r.getPass() != null && r.getPass() == 1);
|
|
|
+ m.put("unmet", parseUnmet(r.getUnmet()));
|
|
|
+ m.put("status", r.getStatus());
|
|
|
+ m.put("time", r.getAppliedAt()); // 兼容键:ISO 时间(与 appliedAt 同值)
|
|
|
+ m.put("desc", r.getStatus()); // 兼容键:状态文案(值域同 status)
|
|
|
+ m.put("appliedAt", r.getAppliedAt());
|
|
|
+ m.put("promotedAt", r.getPromotedAt());
|
|
|
+ m.put("createdAt", r.getCreatedAt());
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Map<String, Object>> parseUnmet(String json) {
|
|
|
+ if (json == null || json.trim().isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> ruleVO(TrainPromotionRule r) {
|
|
|
+ Map<String, Object> m = new HashMap<>();
|
|
|
+ m.put("ruleId", r.getId());
|
|
|
+ m.put("name", r.getName());
|
|
|
+ m.put("authLevel", r.getAuthLevel());
|
|
|
+ m.put("metric", r.getMetric());
|
|
|
+ m.put("operator", r.getOperator());
|
|
|
+ m.put("value", r.getValue());
|
|
|
+ m.put("conditions", parseConditions(r.getConditions()));
|
|
|
+ m.put("enabled", r.getEnabled() != null && r.getEnabled() == 1);
|
|
|
+ m.put("version", r.getVersion());
|
|
|
+ m.put("createdAt", r.getCreatedAt());
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long toLong(Object v) {
|
|
|
+ if (v == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String s = v.toString().trim();
|
|
|
+ if (s.startsWith("+")) {
|
|
|
+ s = s.substring(1);
|
|
|
+ }
|
|
|
+ return Long.valueOf(s);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String str(Object v) {
|
|
|
+ return v == null ? null : v.toString().trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean parseBool(Object v) {
|
|
|
+ if (v == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (v instanceof Boolean) {
|
|
|
+ return (Boolean) v;
|
|
|
+ }
|
|
|
+ String s = v.toString().trim().toLowerCase();
|
|
|
+ return "true".equals(s) || "1".equals(s) || "yes".equals(s);
|
|
|
+ }
|
|
|
+}
|