|
@@ -0,0 +1,199 @@
|
|
|
+package cn.com.ctop.kuaishou.modules.report.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.mapper.UserAllocationMapper;
|
|
|
+import cn.com.ctop.common.module.service.IMessageTemplate;
|
|
|
+import cn.com.ctop.common.module.service.ISendMessageService;
|
|
|
+import cn.com.ctop.common.module.utils.Check;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.entity.KuaishouZombieCreative;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyCreativeMapper;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouZombieCreativeMapper;
|
|
|
+import cn.com.ctop.kuaishou.modules.report.service.IKuaishouZombieCreativeService;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xxl.job.core.enums.NoEn;
|
|
|
+import org.jeecg.common.util.DateUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 僵尸计划记录
|
|
|
+ *
|
|
|
+ * @author jeecg-boot
|
|
|
+ * @version V1.0
|
|
|
+ * @date 2020-10-14
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class KuaishouZombieCreativeServiceImpl extends ServiceImpl<KuaishouZombieCreativeMapper, KuaishouZombieCreative> implements IKuaishouZombieCreativeService {
|
|
|
+ @Autowired
|
|
|
+ private KuaishouReportDailyCreativeMapper kuaishouReportDailyCreativeMapper;
|
|
|
+ @Autowired
|
|
|
+ private KuaishouZombieCreativeMapper KuaishouZombieCreativeMapper;
|
|
|
+ @Autowired
|
|
|
+ private ISendMessageService sendMessageService;
|
|
|
+ @Autowired
|
|
|
+ private IMessageTemplate messageTemplate;
|
|
|
+ @Autowired
|
|
|
+ private UserAllocationMapper userAllocationMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 筛选僵尸计划
|
|
|
+ * 逻辑简述:
|
|
|
+ * 1、以当天时间为节点,往前取五天的数据,若创意消耗(花费)都为0,则获取其数据,并insert为待确认状态的僵尸创意。
|
|
|
+ * 2、获取前一天存储的待确认僵尸计划
|
|
|
+ * 3、步骤二和步骤一的数据作比对筛选,若两组数据都含有该创意时,并且为了保证数据的准确性,还需调用API接口获取当天实时消耗明细,
|
|
|
+ * 若消耗不为0,则表明为真正的僵尸创意,3.1更新其状态;3.2同时关停创意;3.3最后发送“关停通知”消息。
|
|
|
+ * 4、步骤二存在,步骤一不存在说明创意已经活跃,更新其状态为“2”已活跃、反之则是新的待确认僵尸创意,作insert操作
|
|
|
+ * 5、insert操作时,考虑到数据重复的问题,需先查询全部僵尸计划,作筛选比对,已有则更新,否则就新增。
|
|
|
+ * **凡insert数据皆为状态0,待确认僵尸创意,且要发送“提醒通知”。
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkZombieProject() {
|
|
|
+ //获取五天前的日期 yyyy-MM-dd
|
|
|
+ String date = DateUtils.getLastDay(DateUtils.formatDate(new Date()), NoEn.NO5.valueInt());
|
|
|
+ //查询五天内花费都为0的数据集
|
|
|
+ List<JSONObject> list = kuaishouReportDailyCreativeMapper.checkZombieProject(date);
|
|
|
+ if (Check.isNull(list)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //查询数据库中前一天存储的待确认僵尸创意集
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("record_time", DateUtils.getLastDay(DateUtils.formatDate(new Date()), NoEn.NO1.valueInt()));
|
|
|
+ map.put("status", NoEn.NO0.valueStr());
|
|
|
+ List<KuaishouZombieCreative> zombieCreatives = KuaishouZombieCreativeMapper.selectByMap(map);
|
|
|
+ if (!Check.isNull(zombieCreatives)) {
|
|
|
+ List<KuaishouZombieCreative> zombiePlans = new ArrayList<>();
|
|
|
+ List<JSONObject> zombieList = new ArrayList<>();
|
|
|
+ //通过账户和创意ID,筛选出确认的僵尸计划
|
|
|
+ zombieCreatives.forEach(zombie -> {
|
|
|
+ list.forEach(obj -> {
|
|
|
+ if (zombie.getAccountId() - obj.getInteger("accountId") == 0 && zombie.getCreativeId() - obj.getInteger("creativeId") == 0) {
|
|
|
+ QueryWrapper<KuaishouZombieCreative> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("id", zombie.getId());
|
|
|
+ zombie.setStatus(NoEn.NO1.valueStr());
|
|
|
+ //3.1更新状态
|
|
|
+ KuaishouZombieCreativeMapper.update(zombie, wrapper);
|
|
|
+ //已确认的僵尸计划集
|
|
|
+ zombiePlans.add(zombie);
|
|
|
+ zombieList.add(obj);
|
|
|
+ //TODO 3.3发送创意关停通知
|
|
|
+ sendMsg(NoEn.NO1.valueStr(), obj.getLong("accountId"), obj.getString("creativeName"));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ //过滤掉已确认的僵尸数据,剩余活跃的计划
|
|
|
+ zombieCreatives.removeAll(zombiePlans);
|
|
|
+ //更新计划状态为已活跃
|
|
|
+ zombieCreatives.forEach(zom -> {
|
|
|
+ QueryWrapper<KuaishouZombieCreative> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("id", zom.getId());
|
|
|
+ //状态 0-待确认,1-是僵尸计划,2-已活跃,不是僵尸计划
|
|
|
+ zom.setStatus(NoEn.NO2.valueStr());
|
|
|
+ KuaishouZombieCreativeMapper.update(zom, wrapper);
|
|
|
+ });
|
|
|
+ //过滤掉已确认的数据,新增新一批的僵尸计划
|
|
|
+ list.removeAll(zombieList);
|
|
|
+ insertorUpdate(list);
|
|
|
+ } else {
|
|
|
+ insertorUpdate(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 筛选僵尸计划数据,新增或更新操作
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private void insertorUpdate(List<JSONObject> list) {
|
|
|
+ //查询库中已有的全部数据
|
|
|
+ List<KuaishouZombieCreative> kuaishouZombieCreatives = KuaishouZombieCreativeMapper.selectList(null);
|
|
|
+ if (!Check.isNull(kuaishouZombieCreatives)) {
|
|
|
+ List<KuaishouZombieCreative> updateList = new ArrayList<>();
|
|
|
+ List<JSONObject> toBeDeleted = new ArrayList<>();
|
|
|
+ //待新增的和库中数据对比筛选,选出相同账户和相同创意的数据
|
|
|
+ list.forEach(obj -> {
|
|
|
+ kuaishouZombieCreatives.forEach(zom -> {
|
|
|
+ if (zom.getAccountId() - obj.getInteger("accountId") == 0 && zom.getCreativeId() - obj.getInteger("creativeId") == 0) {
|
|
|
+ updateList.add(zom);
|
|
|
+ toBeDeleted.add(obj);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ //已有的数据作更新操作
|
|
|
+ if (!Check.isNull(updateList)) {
|
|
|
+ updateList.forEach(zom -> {
|
|
|
+ QueryWrapper<KuaishouZombieCreative> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("id", zom.getId());
|
|
|
+ zom.setStatus(NoEn.NO0.valueStr());
|
|
|
+ zom.setRecordTime(DateUtils.getNowDate(DateUtils.WEB_FORMAT));
|
|
|
+ KuaishouZombieCreativeMapper.update(zom, wrapper);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //去除已有的数据,新的数据作insert
|
|
|
+ list.removeAll(toBeDeleted);
|
|
|
+ insert(list);
|
|
|
+ } else {
|
|
|
+ insert(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增待确认僵尸数据,并发送消息通知
|
|
|
+ * userAllocation
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private void insert(List<JSONObject> list) {
|
|
|
+ list.forEach(obj -> {
|
|
|
+ KuaishouZombieCreative create = new KuaishouZombieCreative();
|
|
|
+ create.setAccountId(obj.getLong("accountId"));
|
|
|
+ create.setCreativeId(obj.getInteger("creativeId"));
|
|
|
+ create.setCreativeName(obj.getString("creativeName"));
|
|
|
+ create.setRecordTime(DateUtils.getNowDate(DateUtils.WEB_FORMAT));
|
|
|
+ //状态 0-待确认,1-是僵尸计划,2-已活跃,不是僵尸计划
|
|
|
+ create.setStatus(NoEn.NO0.valueStr());
|
|
|
+ KuaishouZombieCreativeMapper.insert(create);
|
|
|
+ //TODO 发送待确认通知
|
|
|
+ sendMsg(NoEn.NO1.valueStr(), obj.getLong("accountId"), obj.getString("creativeName"));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送消息通知
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ * @author ZHAOXA
|
|
|
+ */
|
|
|
+ private void sendMsg(String type, long accountId, String creativeName) {
|
|
|
+ String userId = userAllocationMapper.queryUserIdByAccountId(accountId);
|
|
|
+ String message = "";
|
|
|
+ if (NoEn.NO1.valueStr().equals(type)) {
|
|
|
+ //提醒通知
|
|
|
+ message = messageTemplate.getZombieCreativeRemindTemplate(accountId, creativeName);
|
|
|
+ } else {
|
|
|
+ //关停通知
|
|
|
+ message = messageTemplate.getZombieCreativeRemindTemplate(accountId, creativeName);
|
|
|
+ }
|
|
|
+ sendMessageService.sendMessage(userId, message);
|
|
|
+ }
|
|
|
+}
|