Browse Source

人群包更新逻辑修改

zhaoxian 4 years ago
parent
commit
384bc077e5

+ 23 - 6
module-job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/handler/CrowdListQueryJob.java

@@ -18,6 +18,7 @@ import org.springframework.stereotype.Component;
 
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -45,12 +46,13 @@ public class CrowdListQueryJob {
             }
         }
         List<JSONObject> objs = kuaishouPopulationPackageMapper.queryListByStatus();
-        list.forEach(status -> executorService.submit(() -> {
-            for (JSONObject o : objs) {
-                Long accountId = o.getLong("account_id");
-                Long projectId = o.getLong("project_id");
-                CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
-                if (!Check.isNull(token)) {
+        objs.forEach(o -> executorService.submit(() -> {
+            Long accountId = o.getLong("account_id");
+            Long projectId = o.getLong("project_id");
+            Hashtable newIds = new Hashtable();
+            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
+            if (!Check.isNull(token)) {
+                for (Integer status : list) {
                     JSONObject jsonObject = iKuaishouPopulationService.getPopulationList(accountId, token.getAccessToken(), status);
                     if (jsonObject.getInteger("code") == 0) {
                         JSONObject data = jsonObject.getJSONObject("data");
@@ -60,12 +62,14 @@ public class CrowdListQueryJob {
                                 for (int j = 0; j < details.size(); j++) {
                                     JSONObject obj = details.getJSONObject(j);
                                     KuaishouPopulationPackageRel rel = JSONObject.parseObject(obj.toJSONString(), KuaishouPopulationPackageRel.class);
+                                    newIds.put(rel.getOrientationId(), rel.getOrientationId());
                                     if (Check.isNull(relMapper.queryByOrientationId(rel.getOrientationId(), rel.getAccountId()))) {
                                         rel.setProjectId(projectId);
                                         rel.setDataStatus(1);
                                         rel.setStatDate(DateUtils.formatDate(new Date()));
                                         relService.save(rel);
                                     } else {
+                                        rel.setDataStatus(1);
                                         relMapper.updateStatus(rel);
                                     }
                                 }
@@ -73,6 +77,19 @@ public class CrowdListQueryJob {
                         }
                     }
                 }
+                //查询现有的人群包ID集
+                List<Long> oldOrientationIds = relMapper.queryByOrientationId(null, accountId);
+                if (oldOrientationIds.size() != newIds.size()) {
+                    for (Long id : oldOrientationIds) {
+                        if (!newIds.contains(id)) {
+                            KuaishouPopulationPackageRel rel = new KuaishouPopulationPackageRel();
+                            rel.setDataStatus(0);
+                            rel.setOrientationId(id);
+                            rel.setAccountId(accountId);
+                            relMapper.updateStatus(rel);
+                        }
+                    }
+                }
             }
         }));
         XxlJobHelper.log("快手人群报表更新成功");

+ 2 - 2
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/mapper/KuaishouPopulationPackageRelMapper.java

@@ -32,9 +32,9 @@ public interface KuaishouPopulationPackageRelMapper extends BaseMapper<KuaishouP
 
     KuaishouPopulationPackageRel queryEntity(@Param("accountId") Long accountId, @Param("id") Long id);
 
-    List<JSONObject> getAccountByProjectId(@Param("projectId") Long projectId,@Param("id") String id);
+    List<JSONObject> getAccountByProjectId(@Param("projectId") Long projectId, @Param("id") String id);
 
     void updateStatus(@Param("rel") KuaishouPopulationPackageRel rel);
 
-    KuaishouPopulationPackageRel queryByOrientationId(@Param("orientationId") Long orientationId,@Param("accountId")  Long accountId);
+    List<Long> queryByOrientationId(@Param("orientationId") Long orientationId, @Param("accountId") Long accountId);
 }

+ 27 - 14
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/mapper/xml/KuaishouPopulationPackageRelMapper.xml

@@ -69,7 +69,7 @@
                 FROM ctop_kuaishou_population_package_rel t
                 where t.account_id = t1.account_id
                   AND t.data_status = 1
-                  AND (t.`status`=0 OR t.`status`=1 or t.`status`=3 or t.`status`=4)) as 'populationCount'
+                  AND (t.`status` = 0 OR t.`status` = 1 or t.`status` = 3 or t.`status` = 4)) as 'populationCount'
         FROM ctop_kuaishou_population_package_rel t1
                  LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
         where t1.pop_id = #{id}
@@ -86,14 +86,14 @@
           AND account_id = #{accountId}
     </select>
 
-    <select id="queryByOrientationId" resultType="cn.com.ctop.kuaishou.modules.ai.entity.KuaishouPopulationPackageRel">
-        SELECT pop_id,
-               account_id,
-               orientation_id
+    <select id="queryByOrientationId" resultType="java.lang.Long">
+        SELECT orientation_id
         FROM ctop_kuaishou_population_package_rel
-        WHERE orientation_id = #{orientationId}
-          AND data_status = 1
-          AND account_id = #{accountId}
+        WHERE data_status = 1
+        AND account_id = #{accountId}
+        <if test="orientationId!=null">
+            AND orientation_id = #{orientationId}
+        </if>
     </select>
 
     <insert id="replaceBatch">
@@ -164,13 +164,26 @@
 
     <update id="updateStatus">
         update
-            ctop_kuaishou_population_package_rel
-        set status      = #{rel.status},
-            cover_num   =#{rel.coverNum},
-            record_size =#{rel.recordSize},
-            match_size  =#{rel.matchSize}
+        ctop_kuaishou_population_package_rel
+        <set>
+            <if test="rel.status != null">
+                status = #{rel.status},
+            </if>
+            <if test="rel.coverNum != null">
+                cover_num = #{rel.coverNum},
+            </if>
+            <if test="rel.recordSize != null">
+                record_size = #{rel.recordSize},
+            </if>
+            <if test="rel.dataStatus != null">
+                data_status = #{rel.dataStatus},
+            </if>
+            <if test="rel.matchSize != null">
+                match_size = #{rel.matchSize},
+            </if>
+        </set>
         where orientation_id = #{rel.orientationId}
-          and account_id = #{rel.accountId}
+        and account_id = #{rel.accountId}
     </update>
 
 </mapper>

+ 24 - 7
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/ai/service/impl/KuaishouPopulationPackageServiceImpl.java

@@ -28,6 +28,7 @@ import org.springframework.stereotype.Service;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.UUID;
 
@@ -277,13 +278,14 @@ public class KuaishouPopulationPackageServiceImpl extends ServiceImpl<KuaishouPo
     @Override
     public void crowdListQuery() {
         List<JSONObject> objs = kuaishouPopulationPackageMapper.queryListByStatus();
-        for (int i = 0; i <= 7; i++) {
-            if (i != 2) {
-                for (JSONObject o : objs) {
-                    Long accountId = o.getLong("account_id");
-                    Long projectId = o.getLong("project_id");
-                    CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
-                    if (!Check.isNull(token)) {
+        for (JSONObject o : objs) {
+            Long accountId = o.getLong("account_id");
+            Long projectId = o.getLong("project_id");
+            Hashtable newIds = new Hashtable();
+            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
+            if (!Check.isNull(token)) {
+                for (int i = 0; i <= 7; i++) {
+                    if (i != 2) {
                         JSONObject jsonObject = iKuaishouPopulationService.getPopulationList(accountId, token.getAccessToken(), i);
                         if (jsonObject.getInteger("code") == 0) {
                             JSONObject data = jsonObject.getJSONObject("data");
@@ -293,12 +295,14 @@ public class KuaishouPopulationPackageServiceImpl extends ServiceImpl<KuaishouPo
                                     for (int j = 0; j < details.size(); j++) {
                                         JSONObject obj = details.getJSONObject(j);
                                         KuaishouPopulationPackageRel rel = JSONObject.parseObject(obj.toJSONString(), KuaishouPopulationPackageRel.class);
+                                        newIds.put(rel.getOrientationId(), rel.getOrientationId());
                                         if (Check.isNull(relMapper.queryByOrientationId(rel.getOrientationId(), rel.getAccountId()))) {
                                             rel.setProjectId(projectId);
                                             rel.setDataStatus(1);
                                             rel.setStatDate(DateUtils.formatDate(new Date()));
                                             relService.save(rel);
                                         } else {
+                                            rel.setDataStatus(1);
                                             relMapper.updateStatus(rel);
                                         }
                                     }
@@ -307,6 +311,19 @@ public class KuaishouPopulationPackageServiceImpl extends ServiceImpl<KuaishouPo
                         }
                     }
                 }
+                //查询现有的人群包ID集
+                List<Long> oldOrientationIds = relMapper.queryByOrientationId(null, accountId);
+                if (oldOrientationIds.size() != newIds.size()) {
+                    for (Long id : oldOrientationIds) {
+                        if (!newIds.contains(id)) {
+                            KuaishouPopulationPackageRel rel = new KuaishouPopulationPackageRel();
+                            rel.setDataStatus(0);
+                            rel.setOrientationId(id);
+                            rel.setAccountId(accountId);
+                            relMapper.updateStatus(rel);
+                        }
+                    }
+                }
             }
         }
     }