Browse Source

渠道号——修改

zhaoxian 3 years ago
parent
commit
35dc310aca

+ 6 - 4
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/ai/service/impl/AiKuaishouAccountAutoDoServiceImpl.java

@@ -270,7 +270,8 @@ public class AiKuaishouAccountAutoDoServiceImpl implements IAiKuaishouAccountAut
             KuaishouChannel usedchannel = null;
             try {
                 KuaishouChannel channel = null;
-                Integer useChannel = Check.isNull(strategy.getUseChannel()) ? 0 : strategy.getUseChannel();//0不使用,1使用
+                //0不使用,1使用
+                Integer useChannel = Check.isNull(strategy.getUseChannel()) ? 0 : strategy.getUseChannel();
                 if (useChannel == 1) {
                     /*获取渠道号*/
                     channel = getChannel(level, accountId, appName);
@@ -298,6 +299,7 @@ public class AiKuaishouAccountAutoDoServiceImpl implements IAiKuaishouAccountAut
                                 log.error("****** 获取已创建的计划所使用的渠道号失败");
                                 return videoCnt;
                             }
+                            log.info("------因计划层级,更新为已创建的计划所使用的渠道号:{}", usedchannel.getChannelCode());
                         } else {
                             usedchannel = channel;
                         }
@@ -1813,9 +1815,9 @@ public class AiKuaishouAccountAutoDoServiceImpl implements IAiKuaishouAccountAut
             }
             campaignId = (Long) campaignCreateResult.get("campaignId");
 
-            if (!Check.isNull(channel)) {
-                channelService.callBackUpdateChannelRel("plan", channel.getChannelCode(), token.getAccountId(), campaignId, null);
-            }
+//            if (!Check.isNull(channel)) {
+//                channelService.callBackUpdateChannelRel("plan", channel.getChannelCode(), token.getAccountId(), campaignId, null);
+//            }
             record.setCampaignId(campaignId);
             aiKuaishouCampaignLevelOperationRecordService.saveOrUpdate(record);
             Integer campaignStatus = strategy.getCampaignStatus();

+ 8 - 1
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/channel/mapper/KuaishouChannelMapper.java

@@ -1,7 +1,6 @@
 package cn.com.ctop.kuaishou.modules.channel.mapper;
 
 import cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannel;
-import cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannelCreateStrategy;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@@ -51,4 +50,12 @@ public interface KuaishouChannelMapper extends BaseMapper<KuaishouChannel> {
     List<JSONObject> queryChannelList(@Param("accountId") Long accountId, @Param("appName") String appName);
 
     List<KuaishouChannel> queryChannelListByAccount(@Param("accountId") Long accountId);
+
+    List<KuaishouChannel> getAccountLevelRepeatChannel(@Param("accountId") Long accountId, @Param("appName") String appName);
+
+    List<KuaishouChannel> getAccountLevelNoRepeatChannel(@Param("accountId") Long accountId, @Param("appName") String appName);
+
+    KuaishouChannel getChannelEntity(@Param("accountId") Long accountId);
+
+    List<KuaishouChannel> queryNoAccountLevelNoRepeatChannel(@Param("usageLevel") String usageLevel, @Param("accountId") Long accountId, @Param("appName") String appName);
 }

+ 95 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/channel/mapper/xml/KuaishouChannelMapper.xml

@@ -205,14 +205,109 @@
         <if test="usageTimes > 1">
             AND is_cycle = 1
           </if>
+        <if test="appName != null">
             AND app_id in (
                 SELECT DISTINCT app_id FROM ctop_kuaishou_channel_app_info where app_name = #{appName}  and app_id is not null
             )
+        </if>
             AND t1.usage_times = #{usageTimes}
             and t1.usage_level = #{usageLevel}
             ORDER BY t1.create_time,t1.channel_number
     </select>
 
+    <select id="getAccountLevelRepeatChannel" resultType="cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannel">
+        SELECT t1.id,
+               t1.channel_code,
+               t1.usage_level,
+               t1.usage_range,
+               t1.usage_times,
+               t1.is_have_item,
+               t1.channel_name,
+               t1.click_track_url,
+               t1.actionbar_click_url,
+               t1.schema_uri
+        FROM `ctop_kuaishou_channel` t1
+        LEFT JOIN ctop_kuaishou_channel_rel t2 ON t1.account_id = t2.account_id AND t1.channel_code = t2.channel_code
+        WHERE
+            t1.account_id = #{accountId}
+          AND t1.state = 1
+          AND t1.channel_state = 1
+          AND app_id is not null
+          and t1.usage_level = 'account'
+        <if test="appName != null">
+            AND app_id in (
+            SELECT DISTINCT app_id FROM ctop_kuaishou_channel_app_info where app_name = #{appName}  and app_id is not null
+            )
+        </if>
+        GROUP BY t1.channel_code
+        ORDER BY t1.create_time,t1.channel_number
+            LIMIT 100
+    </select>
+
+    <select id="getAccountLevelNoRepeatChannel" resultType="cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannel">
+        SELECT t1.id,
+               t1.channel_code,
+               t1.usage_level,
+               t1.usage_range,
+               t1.usage_times,
+               t1.is_have_item,
+               t1.channel_name,
+               t1.click_track_url,
+               t1.actionbar_click_url,
+               t1.schema_uri
+        FROM `ctop_kuaishou_channel` t1
+        WHERE
+            t1.account_id = #{accountId}
+          AND t1.state = 1
+          AND t1.channel_state = 1
+          AND app_id is not null
+          AND t1.channel_code not in (
+            SELECT channel_code FROM ctop_kuaishou_channel_rel WHERE account_id = #{accountId}
+        )
+        <if test="appName != null">
+            AND app_id in (
+            SELECT DISTINCT app_id FROM ctop_kuaishou_channel_app_info where app_name = #{appName}  and app_id is not null
+            )
+        </if>
+        ORDER BY t1.create_time,t1.channel_number
+            LIMIT 100
+    </select>
+
+    <select id="queryNoAccountLevelNoRepeatChannel" resultType="cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannel">
+        SELECT t1.id,
+               t1.channel_code,
+               t1.usage_level,
+               t1.usage_range,
+               t1.usage_times,
+               t1.is_have_item,
+               t1.channel_name,
+               t1.click_track_url,
+               t1.actionbar_click_url,
+               t1.schema_uri
+        FROM `ctop_kuaishou_channel` t1
+        WHERE  t1.account_id = #{accountId}
+          AND t1.state = 1
+          AND t1.channel_state = 1
+          AND app_id is not null
+        <if test="appName != null">
+            AND app_id in (
+            SELECT DISTINCT app_id FROM ctop_kuaishou_channel_app_info where app_name = #{appName}  and app_id is not null
+            )
+        </if>
+          AND t1.usage_times = 0
+          and t1.usage_level = #{usageLevel}
+        ORDER BY t1.create_time,t1.channel_number
+            LIMIT 100
+    </select>
+
+    <select id="getChannelEntity" resultType="cn.com.ctop.kuaishou.modules.channel.entity.KuaishouChannel">
+        SELECT *
+        FROM `ctop_kuaishou_channel`
+        WHERE account_id = #{accountId}
+          AND state = 1
+          AND channel_state = 1 LIMIT 1
+    </select>
+
     <select id="queryUsedLevel" resultType="java.lang.Integer">
         SELECT usage_times
         FROM `ctop_kuaishou_channel`

+ 76 - 32
jeecg-boot-module-system/src/main/java/cn/com/ctop/kuaishou/modules/channel/service/impl/KuaishouChannelServiceImpl.java

@@ -949,36 +949,81 @@ public class KuaishouChannelServiceImpl extends ServiceImpl<KuaishouChannelMappe
 
     @Override
     public Result<Object> queryChannel(Long accountId, String level, String appName, String photoName) throws Exception {
-        //查询账户所有策略
-        List<Long> strategyIds = channelCreateStrategyService.queryAllIdByAccountId(accountId);
-        if (Check.isNull(strategyIds) || strategyIds.isEmpty()) {
-            return Result.error("渠道号获取失败,该账户没有创建过渠道号");
+        KuaishouChannel one = channelMapper.getChannelEntity(accountId);
+        if (Check.isNull(one)) {
+            return Result.error("未查询到渠道号");
         }
-        List<Integer> usageTimesTypes = channelMapper.queryUsedLevel(strategyIds);
-        if (Check.isNull(usageTimesTypes)) {
-            return Result.error("渠道号已无效");
-        }
-        for (Integer usageTimes : usageTimesTypes) {
-            //获取可用渠道号,筛查出一个返回给前端
-            List<KuaishouChannel> list = channelMapper.queryChannelByUsageTimes(usageTimes, level, strategyIds, appName);
-            if (Check.isNull(list)) {
-                continue;
+        //是否循环重复使用 0:否,1:是
+        Integer isCycle = one.getIsCycle();
+        if (isCycle == 0) {
+            if ("account".equals(level)) {
+                //账户层级   不可循环使用渠道号
+                List<KuaishouChannel> list = channelMapper.getAccountLevelNoRepeatChannel(accountId, appName);
+                KuaishouChannel channel = getValidChannel(list);
+                if (!Check.isNull(channel)) {
+                    return Result.ok(channel);
+                }
+            } else {
+                //获取可用渠道号,筛查出一个返回给前端
+                List<KuaishouChannel> list = channelMapper.queryNoAccountLevelNoRepeatChannel(level, accountId, appName);
+                KuaishouChannel channel = getValidChannel(list);
+                if (!Check.isNull(channel)) {
+                    return Result.ok(channel);
+                }
             }
-            for (KuaishouChannel channel : list) {
-                //1 关联单品,无单品则跳过
-                if (channel.getIsHaveItem() == 1) {
-                    List<KuaishouChannelItems> items = channelItemsRelService.queryItems(channel.getId());
-                    if (Check.isNull(items) || items.isEmpty()) {
-                        continue;
+        } else {
+            //循环使用逻辑
+            if ("account".equals(level)) {
+                //账户层级可循环使用渠道号
+                List<KuaishouChannel> list = channelMapper.getAccountLevelRepeatChannel(accountId,appName);
+                KuaishouChannel channel = getValidChannel(list);
+                if (!Check.isNull(channel)) {
+                    return Result.ok(channel);
+                }
+            } else {
+                //查询账户所有策略
+                List<Long> strategyIds = channelCreateStrategyService.queryAllIdByAccountId(accountId);
+                if (Check.isNull(strategyIds) || strategyIds.isEmpty()) {
+                    return Result.error("渠道号获取失败,该账户没有创建过渠道号");
+                }
+                List<Integer> usageTimesTypes = channelMapper.queryUsedLevel(strategyIds);
+                if (Check.isNull(usageTimesTypes)) {
+                    return Result.error("渠道号已无效");
+                }
+                for (Integer usageTimes : usageTimesTypes) {
+                    //获取可用渠道号,筛查出一个返回给前端
+                    List<KuaishouChannel> list = channelMapper.queryChannelByUsageTimes(usageTimes, level, strategyIds, appName);
+                    KuaishouChannel channel = getValidChannel(list);
+                    if (!Check.isNull(channel)) {
+                        return Result.ok(channel);
                     }
-                    channel.setItems(items);
                 }
-                return Result.ok(channel);
             }
         }
         return Result.error("查询失败");
     }
 
+    /**
+     * 筛选渠道号,给渠道号添加单品
+     */
+    private KuaishouChannel getValidChannel(List<KuaishouChannel> list) {
+        if (Check.isNull(list) || list.isEmpty()) {
+            return null;
+        }
+        for (KuaishouChannel channel : list) {
+            //1 关联单品,无单品则跳过
+            if (channel.getIsHaveItem() == 1) {
+                List<KuaishouChannelItems> items = channelItemsRelService.queryItems(channel.getId());
+                if (Check.isNull(items) || items.isEmpty()) {
+                    continue;
+                }
+                channel.setItems(items);
+            }
+            return channel;
+        }
+        return null;
+    }
+
     @Override
     public Result<Object> queryChannelLevel(JSONObject data) {
         Long accountId = data.getLong("accountId");
@@ -999,8 +1044,7 @@ public class KuaishouChannelServiceImpl extends ServiceImpl<KuaishouChannelMappe
     }
 
     @Override
-    public void callBackUpdateChannelRel(String level, String channelCode, Long accountId, Long planId, Long
-            groupId) {
+    public void callBackUpdateChannelRel(String level, String channelCode, Long accountId, Long planId, Long groupId) {
         try {
             QueryWrapper<KuaishouChannel> queryWrapper = new QueryWrapper<>();
             queryWrapper.eq("account_id", accountId);
@@ -1020,15 +1064,15 @@ public class KuaishouChannelServiceImpl extends ServiceImpl<KuaishouChannelMappe
                     rel.setUnitId(groupId);
                     rel.setUpdateTime(new Date());
                     channelRelService.save(rel);
-                    if ("plan".equals(level)) {
-                        Integer usageTimes = channelRelService.queryUsePlanCount(accountId, channelCode);
-                        one.setUsageTimes(usageTimes);
-                        this.saveOrUpdate(one);
-                    } else if ("group".equals(level)) {
-                        Integer usageTimes = channelRelService.queryUseGroupCount(accountId, channelCode);
-                        one.setUsageTimes(usageTimes);
-                        this.saveOrUpdate(one);
-                    }
+//                    if ("plan".equals(level)) {
+//                        Integer usageTimes = channelRelService.queryUsePlanCount(accountId, channelCode);
+//                        one.setUsageTimes(usageTimes);
+//                        this.saveOrUpdate(one);
+//                    } else if ("group".equals(level)) {
+                    Integer usageTimes = channelRelService.queryUseGroupCount(accountId, channelCode);
+                    one.setUsageTimes(usageTimes);
+                    this.saveOrUpdate(one);
+//                    }
                 }
             }
         } catch (Exception e) {