|
|
@@ -0,0 +1,113 @@
|
|
|
+package cn.com.yusys.yusp.service;
|
|
|
+
|
|
|
+
|
|
|
+import cn.com.yusys.yusp.commons.util.StringUtils;
|
|
|
+import cn.com.yusys.yusp.domain.entity.AitagTagDailyAggEntity;
|
|
|
+import cn.com.yusys.yusp.domain.entity.AitagTagLogEntity;
|
|
|
+import cn.com.yusys.yusp.mapper.AitagTagDailyAggDao;
|
|
|
+import cn.com.yusys.yusp.mapper.AitagTagLogDao;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import static cn.com.yusys.yusp.config.DataDictionary.FEEDBACK_RESULT_AGREE;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class DistributedScheduledTask {
|
|
|
+
|
|
|
+
|
|
|
+ private static final String TASK_LOCK_KEY = "scheduled_task:sync_data";
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StringRedisTemplate redisTemplate;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AitagTagLogDao aitagTagLogDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AitagTagDailyAggDao aitagTagDailyAggDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 每天凌晨统计昨天打标数据
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "#{@environment.getProperty('application.scheduled.task-cron')}")
|
|
|
+ public void syncDataTask() {
|
|
|
+ log.info("智能标签统计,准备获取redis锁");
|
|
|
+ Boolean taskCron = redisTemplate.opsForValue().setIfAbsent("taskCron", "1", 4, TimeUnit.HOURS);
|
|
|
+ if(Boolean.TRUE.equals(taskCron)){
|
|
|
+ try{
|
|
|
+ log.info("获取redis锁成功,智能标签开始统计");
|
|
|
+ LambdaQueryWrapper<AitagTagDailyAggEntity> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ String yesterday = LocalDate.now().minusDays(1).toString();
|
|
|
+ queryWrapper.eq(AitagTagDailyAggEntity::getAggDate,yesterday);
|
|
|
+ int count = aitagTagDailyAggDao.selectCount(queryWrapper);
|
|
|
+ if(count == 0){
|
|
|
+ executeBusinessLogic(yesterday);
|
|
|
+ }
|
|
|
+ log.info("智能标签统计任务结束");
|
|
|
+ }finally {
|
|
|
+ log.info("删除redis锁信息");
|
|
|
+ redisTemplate.delete("taskCron");
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ log.warn("智能标签统计任务已有节点执行--本次执行跳过");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void executeBusinessLogic(String yesterday) {
|
|
|
+ List<AitagTagLogEntity> aitagTagLogEntities = aitagTagLogDao.selectByInsertTime(yesterday);
|
|
|
+ if(aitagTagLogEntities != null){
|
|
|
+ Map<String,Integer> count = new HashMap<>();
|
|
|
+ for (AitagTagLogEntity aitagTagLog:aitagTagLogEntities){
|
|
|
+ String feedback = aitagTagLog.getFeedback();
|
|
|
+ String categoryCode = aitagTagLog.getCategoryCode();
|
|
|
+ if(FEEDBACK_RESULT_AGREE.equals(feedback)){
|
|
|
+ String result = aitagTagLog.getResult();
|
|
|
+ duCount(result, count,categoryCode);
|
|
|
+ }else{
|
|
|
+ String result = aitagTagLog.getFeedbackResult();
|
|
|
+ duCount(result, count,categoryCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ AitagTagDailyAggEntity aitagTagLog = new AitagTagDailyAggEntity();
|
|
|
+ for (String key : count.keySet()){
|
|
|
+ aitagTagLog.setId(StringUtils.getUUID());
|
|
|
+ aitagTagLog.setAggDate(yesterday);
|
|
|
+ aitagTagLog.setTagCount(count.get(key));
|
|
|
+ String[] split = key.split(":");
|
|
|
+ aitagTagLog.setTagNm(split[0]);
|
|
|
+ aitagTagLog.setCategoryCode(split[1]);
|
|
|
+ aitagTagDailyAggDao.insert(aitagTagLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void duCount(String result, Map<String, Integer> count,String categoryCode) {
|
|
|
+ JSONObject resultMap = JSONObject.parseObject(result);
|
|
|
+ String labe = resultMap.getString("labe")+":"+categoryCode;
|
|
|
+ if(count.containsKey(labe)){
|
|
|
+ Integer integer = count.get(labe);
|
|
|
+ count.put(labe,integer+1);
|
|
|
+ }else{
|
|
|
+ count.put(labe,1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|