Browse Source

解决账户清理BUG

zhouzeyu@c-top.com.cn 4 năm trước cách đây
mục cha
commit
b7298d68b8

+ 3 - 1
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/clean/mapper/KuaishouAccountCleanTemplateMapper.java

@@ -41,7 +41,9 @@ public interface KuaishouAccountCleanTemplateMapper {
     //查询所有启用的模板
     List<KuaiShouAccountCleanTemplateInfo> getTemplateListBystatus();
     //根据规则模板查询相应组信息
-    List<String> getGroupMessage(@Param("template")KuaiShouAccountCleanTemplateInfo kuaiShouAccountCleanTemplateInfo,@Param("list")List list, @Param("startDate")String startDate);
+    String getGroupMessage(@Param("template")KuaiShouAccountCleanTemplateInfo kuaiShouAccountCleanTemplateInfo,@Param("optimizationObjective")String optimizationObjective, @Param("startDate")String startDate, @Param("unitId")String unitId);
+    List<Map> getGroupByAccount( @Param("accountId")String accountId);
+
     //保存操作日志
     void saveCleamLog(KuaiShouAccountCleanLogInfo kuaiShouAccountCleanLogInfo);
 

+ 18 - 49
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/clean/mapper/xml/KuaishouAccountCleanTemplateMapper.xml

@@ -114,7 +114,7 @@
             </if>
 
             <if test="userName!=null and userName!=''">
-                and t3.user_name like concat('%',#{userName},'%')
+                and t.create_user like concat('%',#{userName},'%')
             </if>
             <if test="createId!=null and createId!=''">
                 and t.create_id=#{createId}
@@ -124,84 +124,49 @@
     </select>
 
     <select id="getTemplateListBystatus" resultMap="templateResultMap">
-       select  t.id,t.template_name,t.clean_time,t.optimization_objective,t.charge,t.conversion,t.promotion_group_status,t.perform_operations,t2.account_id  from ctop_kuaishou_account_clean_template t
+       select  t.id,t.template_name,t.clean_time,t.optimization_objective,t.charge,t.conversion,t.promotion_group_status,t.perform_operations,t2.account_id,t.create_id,t.create_user  from ctop_kuaishou_account_clean_template t
        left join  ctop_kuaishou_account_clean_center t2 on t.id=t2.template_id where t.status='0'
   </select>
 
-    <select id="getGroupMessage" resultType="java.lang.String">
+    <select id="getGroupMessage" resultType="String">
 
         SELECT t.unit_id from ctop_kuaishou_group t LEFT JOIN ctop_kuaishou_report_daily_group t2
         on t.unit_id=t2.unit_id
         where t.account_id=#{template.accountId}
         and t.group_create_time &lt;= #{startDate}
         and t2.stat_date &gt;= #{startDate}
+        and t.unit_id=#{unitId}
         <if test="template.promotionGroupStatus!=null and template.promotionGroupStatus!='' and template.promotionGroupStatus!='3'">
             and t.put_status=#{template.promotionGroupStatus}
         </if>
         group by t.unit_id
         having sum(t2.charge) &lt;= #{template.charge}
-        <foreach item="item" index="index" collection="list" open="and (" separator="or" close=")">
-
-            <if test='item=="activation"'>
+            <if test='optimizationObjective=="activation"'>
                 /* 激活数*/
-                sum(t2.activation) &lt; #{template.conversion}
+               and sum(t2.activation) &lt; #{template.conversion}
             </if>
 
-            <if test='item=="bclick"'>
-                /*行为数*/
-                sum(t2.bclick) &lt; #{template.conversion}
-            </if>
 
-            <if test='item=="formSize"'>
+            <if test='optimizationObjective=="formSize"'>
                 /*表单数*/
-                sum(t2.form_count) &lt; #{template.conversion}
+                and  sum(t2.form_count) &lt; #{template.conversion}
             </if>
 
-            <if test='item=="pay"'>
+            <if test='optimizationObjective=="pay"'>
                 /*付费数*/
-                sum(t2.event_pay) &lt; #{template.conversion}
+                and  sum(t2.event_pay) &lt; #{template.conversion}
             </if>
 
-            <if test='item=="roi"'>
-                /*首日roi*/
-                sum(t2.event_pay_first_day_roi) &lt; #{template.conversion}
-            </if>
 
-            <if test='item=="evocativeApplication"'>
+            <if test='optimizationObjective=="evocativeApplication"'>
                 /*唤起应用*/
-                sum(t2.event_app_invoked) &lt; #{template.conversion}
-            </if>
-
-            <if test='item=="effectiveClues"'>
-                /*落地页有效线索*/
-                sum(t2.event_valid_clues) &lt; #{template.conversion}
-            </if>
-
-            <if test='item=="creditApp"'>
-                /*授信数APP*/
-                sum(t2.event_credit_grant_app) &lt; #{template.conversion}
+                and sum(t2.event_app_invoked) &lt; #{template.conversion}
             </if>
 
-            <if test='item=="creditGrant"'>
-                /*落地页授信数*/
-                sum(t2.event_credit_grant_landing_page) &lt; #{template.conversion}
-            </if>
-
-            <if test='item=="finishedPieceApp"'>
-                /*完件数App*/
-                sum(t2.event_jin_jian_app) &lt; #{template.conversion}
-            </if>
-
-            <if test='item=="finishedPieceGrant"'>
-                /*落地页完件数*/
-                sum(t2.event_jin_jian_landing_page) &lt; #{template.conversion}
-            </if>
-
-            <if test='item=="registered"'>
+            <if test='optimizationObjective=="registered"'>
                 /*注册*/
-                sum(t2.event_register) &lt; #{template.conversion}
+                and sum(t2.event_register) &lt; #{template.conversion}
             </if>
-        </foreach>
 
     </select>
 
@@ -220,4 +185,8 @@
     <select id="getTemplateName" parameterType="String" resultType="int">
          select count(*) from ctop_kuaishou_account_clean_template t  where t.template_name=#{templateName}
     </select>
+
+    <select id="getGroupByAccount" resultType="Map">
+        select  t.unit_id as 'unitId',t.ocpx_action_type  as 'type' from ctop_kuaishou_group t  where t.account_id=#{accountId}
+    </select>
 </mapper>

+ 63 - 32
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/clean/service/impl/KuaiShouAccountCleanServiceImpl.java

@@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -227,14 +228,14 @@ public class KuaiShouAccountCleanServiceImpl implements KuaiShouAccountCleanServ
     @Override
     public Result getCleanUpLog(String templateId, String userId, String pageNum, String pageSize) {
         Result result = new Result();
-        List<KuaiShouAccountCleanLogInfo> cleanUpLog=new ArrayList<>();
+        List<KuaiShouAccountCleanLogInfo> cleanUpLog = new ArrayList<>();
         try {
             //查询当前登录人角色
             String roleCode = sysRoleService.getRoleCodeByUserId(userId);
             PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
             if (roleCode != null && roleCode.equals("admin")) {
                 cleanUpLog = kuaishouAccountCleanTemplateMapper.getCleanUpLog(templateId, null);
-            }else {
+            } else {
                 cleanUpLog = kuaishouAccountCleanTemplateMapper.getCleanUpLog(templateId, userId);
             }
             PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
@@ -291,12 +292,22 @@ public class KuaiShouAccountCleanServiceImpl implements KuaiShouAccountCleanServ
     public Result cleanAccount() {
         Result result = new Result();
         SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        Map optimizationObjectiveMap = new HashMap();
+        //优化目标 activation:激活数 bclick:行为数  formSize :表单数 pay:付费  roi:首日roi   evocativeApplication:唤起应用
+        // effectiveClues:有效线索 creditApp:授信-APP creditGrant:授信-落地页  finishedPieceApp:完件数-APP  finishedPieceGrant:完件数-落地页  registered:注册
+        optimizationObjectiveMap.put("180", "activation");
+        optimizationObjectiveMap.put("53", "formSize");
+        optimizationObjectiveMap.put("190", "pay");
+        optimizationObjectiveMap.put("324", "evocativeApplication");
+        optimizationObjectiveMap.put("396", "registered");
         try {
             //查询需要执行的规则模板
             List<KuaiShouAccountCleanTemplateInfo> templateListBystatus = kuaishouAccountCleanTemplateMapper.getTemplateListBystatus();
             if (!templateListBystatus.isEmpty()) {
                 templateListBystatus.stream().forEach(ob -> {
-                    if(ob.getAccountId()!=null){
+                    AtomicInteger count= new AtomicInteger();
+                    if (ob.getAccountId() != null) {
+                        StringBuilder unitIds = new StringBuilder();
                         //获取各项优化目标
                         List<String> optimizationObjectiveList = Arrays.asList(ob.getOptimizationObjective().split(","));
                         //获取清理时间范围
@@ -305,26 +316,48 @@ public class KuaiShouAccountCleanServiceImpl implements KuaiShouAccountCleanServ
                         Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
                         calendar.add(Calendar.DATE, -Integer.parseInt(cleanTime));
                         String startDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
-                        //查询符合模板规则的组
-                        List<String> groupMessage = kuaishouAccountCleanTemplateMapper.getGroupMessage(ob, optimizationObjectiveList, startDate);
                         AtomicReference<Boolean> falg = new AtomicReference<>(true);
-                        if (!groupMessage.isEmpty()) {
-                            //获取Token
-                            CtopOauthToken ctopOauthToken = ctopOauthTokenMapper.selectByAccountId(Long.parseLong(ob.getAccountId()));
-                            if (ctopOauthToken != null) {
-                                groupMessage.stream().forEach(item -> {
-                                    //修改广告组状态
-                                    Map<String, Object> resultMap = iKuaiShouUpdateService.updateUnitStatus(ctopOauthToken.getAccessToken(), Long.parseLong(ob.getAccountId()), Long.parseLong(item), Integer.parseInt(ob.getPerformOperations()), null);
-                                    Boolean resultFlag = (Boolean) resultMap.get("success");
-                                    //判断有没有失败
-                                    if (!resultFlag) {
-                                        falg.set(false);
+                        //查询改账户下所有组
+                        List<Map> groupByAccount = kuaishouAccountCleanTemplateMapper.getGroupByAccount(ob.getAccountId());
+                        if (!groupByAccount.isEmpty()) {
+                            groupByAccount.stream().forEach(obj -> {
+                                if (obj.get("unitId") != null && obj.get("type") != null) {
+                                    String unitId = Long.toString((Long) obj.get("unitId")) ;
+                                    //OCPM
+                                    String type = Integer.toString((Integer) obj.get("type")) ;
+                                    if(optimizationObjectiveMap.get(type)!=null){
+                                        String optimizationObjective= (String) optimizationObjectiveMap.get(type);
+                                        //查询组是否符合清理规则
+                                        String groupMessage = kuaishouAccountCleanTemplateMapper.getGroupMessage(ob, optimizationObjective, startDate,unitId);
+                                        if(groupMessage!=null){
+                                            count.getAndIncrement();
+                                            //符合清理规则清理
+                                            //获取Token
+                                            CtopOauthToken ctopOauthToken = ctopOauthTokenMapper.selectByAccountId(Long.parseLong(ob.getAccountId()));
+                                            if (ctopOauthToken != null) {
+                                                    //修改广告组状态
+                                                    Map<String, Object> resultMap = iKuaiShouUpdateService.updateUnitStatus(ctopOauthToken.getAccessToken(), Long.parseLong(ob.getAccountId()), Long.parseLong(unitId), Integer.parseInt(ob.getPerformOperations()), null);
+                                                    Boolean resultFlag = (Boolean) resultMap.get("success");
+                                                    //判断有没有失败
+                                                    if (resultFlag) {
+                                                        //清理成功
+                                                        unitIds.append(unitId).append(",");
+                                                        log.info("组"+unitId+"已被清理");
+                                                    }else {
+                                                        //清理失败
+                                                        log.info("组"+unitId+"清理失败:{}",resultMap.get("message"));
+                                                        falg.set(false);
+                                                    }
+                                            }
+                                        }
                                     }
-                                });
 
-                            }
+                                }
+                            });
                         }
-                        if (!groupMessage.isEmpty() && falg.get()) {
+
+
+                        if (count.get()>0 && falg.get()) {
                             //修改成功插入日志记录
                             KuaiShouAccountCleanLogInfo kuaiShouAccountCleanLogInfo = new KuaiShouAccountCleanLogInfo();
                             //设置账户id
@@ -332,18 +365,14 @@ public class KuaiShouAccountCleanServiceImpl implements KuaiShouAccountCleanServ
                             // 1-投放、2-暂停、3-删除
                             kuaiShouAccountCleanLogInfo.setOperationType(ob.getPerformOperations());
                             //设置运营
-                            Map<String, String> map = kuaishouAccountCleanTemplateMapper.getUserNameByAccountId(ob.getAccountId());
-                            String authName = "";
-                            if (map != null) {
-                                kuaiShouAccountCleanLogInfo.setUserName(map.get("userName"));
-                                authName = map.get("authName");
-                            }
+//                            Map<String, String> map = kuaishouAccountCleanTemplateMapper.getUserNameByAccountId(ob.getAccountId());
+//                            String authName = "";
+//                            if (map != null) {
+//                                kuaiShouAccountCleanLogInfo.setUserName(map.get("userName"));
+//                                authName = map.get("authName");
+//                            }
                             //设置时间
                             kuaiShouAccountCleanLogInfo.setCreateTime(sd.format(new Date()));
-                            StringBuilder unitIds = new StringBuilder();
-                            groupMessage.stream().forEach(unid -> {
-                                unitIds.append(unid).append(",");
-                            });
                             //操作类型
                             String operationType = null;
                             if (ob.getPerformOperations().equals("2")) {
@@ -353,10 +382,11 @@ public class KuaiShouAccountCleanServiceImpl implements KuaiShouAccountCleanServ
                                 operationType = "删除";
                             }
                             //执行信息
-                            String textMessage = "清理规则执行:  账户:" + ob.getAccountId() + ", 授权名称: " + authName + ",推广组:" + unitIds + "已经被清理规则" + operationType;
+                            String textMessage = "清理规则执行:  账户:" + ob.getAccountId() + ", 授权名称: " + ob.getCreateUser() + ",推广组:" + unitIds + "已经被清理规则" + operationType;
                             kuaiShouAccountCleanLogInfo.setExecutiveInformation(textMessage);
                             kuaiShouAccountCleanLogInfo.setTemplateId(ob.getId());
                             kuaiShouAccountCleanLogInfo.setCreateTemplateId(ob.getCreateId());
+                            kuaiShouAccountCleanLogInfo.setUserName(ob.getCreateUser());
                             kuaishouAccountCleanTemplateMapper.saveCleamLog(kuaiShouAccountCleanLogInfo);
                         }
                     }
@@ -370,6 +400,7 @@ public class KuaiShouAccountCleanServiceImpl implements KuaiShouAccountCleanServ
         }
         return result;
     }
+
     /**
      * 根据模板名称查询
      *
@@ -380,9 +411,9 @@ public class KuaiShouAccountCleanServiceImpl implements KuaiShouAccountCleanServ
     public Result getTemplateName(String templateName) {
         Result result = new Result();
         try {
-                int number = kuaishouAccountCleanTemplateMapper.getTemplateName(templateName.trim());
+            int number = kuaishouAccountCleanTemplateMapper.getTemplateName(templateName.trim());
             result.setResult(number);
-        }catch (Exception e){
+        } catch (Exception e) {
             log.info("查询失败:{}", e.toString());
             result.error500("查询失败");
         }