<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.jeecg.modules.bytedance.advertise.mapper.MonitorBytedanceActionTrackUrlMapper">


<!-- 获取头条自动配置中的账户 以及 点击监测链接-->
    <select id="getBytedancStrategyAcctionId" resultType="org.jeecg.modules.bytedance.advertise.vo.MonitorBytedanceAdVo">
        SELECT
            stra.account_id accountId,
            stra.ad_convert_id convertId,
            stra.creative_action_track_url actionTrackUrl,
            ua.auth_name accountName,
            ua.user_id operateUserid,
            ua.user_name operateUserName
        FROM
            ctop_ai_bytedance_advertiser_strategy stra
                LEFT JOIN ctop_user_allocation ua on stra.account_id = ua.account_id
        WHERE
            stra.status = 0
        <if test="accountId !=null and accountId != ''">
            AND stra.account_id = #{accountId}
        </if>
        GROUP BY stra.account_id
        ORDER BY
            stra.create_time DESC
    </select>


    <!-- 根据账户id 查询该账户下的计划中的 点击监测链接 -->
    <select id="getBytedancPlanActionTrackUrl" resultType="org.jeecg.modules.bytedance.advertise.vo.MonitorBytedanceAdVo">
        SELECT
            DISTINCT
            action_track_url actionTrackUrl,
            id as planId,
            account_id
        FROM
            ctop_bytedance_advertise_plan
        WHERE action_track_url is NOT NULL
            and opt_status = 'AD_STATUS_ENABLE'
            and account_id = #{accountId}
          and DATE_FORMAT(start_time,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
    </select>


    <!-- 获取用户微信id -->
    <select id="getWChatIdByUserId" resultType="com.alibaba.fastjson.JSONObject">
        select t1.wexin_id,corp_id
        from ctop_corp_wexin_user t1
                 left join  sys_user t2
                            on  t1.user_id = t2.id
        where  t1.user_id = #{userId}
          and t2.`status` = 1

    </select>



    <!-- 新增第三方监测连接 -->
    <insert id="insertAccountTrackUrl">
        INSERT INTO ctop_ai_bytedance_advertiser_strategy_action_track_url (
            `account_id`,
            `action_track_url`,
            `warning_flag`,
            `create_user_id`,
            `create_time`
        )
        VALUES
        ( #{accountId}, #{actionTrackUrl}, 1, #{createUserId}, NOW());
    </insert>





    <!-- 查询 监测链接 -->
    <select id="getAccountActionTrackUrl" resultType="org.jeecg.modules.bytedance.advertise.vo.MonitorBytedanceAdVo">
        SELECT
        u.account_id,
        u.action_track_url,
        u.warning_flag,
        u.create_user_id,
        u.create_time,
        u.update_time,
        (SELECT p.project_name from ctop_project p where p.id = a.project_id) project_name,
        a.auth_name as accountName,
        a.user_id as operateUserid,
        a.user_name as operateUserName
        FROM
        ctop_ai_bytedance_advertiser_strategy_action_track_url u
        LEFT JOIN ctop_user_allocation a on u.account_id = a.account_id
        <where>
              <if test="accountId !=null and accountId != ''">
                  AND  u.account_id = #{accountId}
              </if>
              <if test="warningFlag !=null and warningFlag != ''">
                  AND  u.warning_flag = #{warningFlag}
              </if>
              <if test="operateUserIds != null and operateUserIds.size > 0">
                  AND  a.user_id in
                  <foreach item="userId" index="index" collection="operateUserIds" open="(" separator="," close=")">
                      #{userId}
                  </foreach>
              </if>
        </where>
    </select>


    <!-- 修改 第三方监测链接 -->
    <update id="updAccountActionTrackUrl">
        UPDATE ctop_ai_bytedance_advertiser_strategy_action_track_url
        SET
        <if test="actionTrackUrl != null and actionTrackUrl !=''">
            action_track_url =#{actionTrackUrl},
        </if>
        <if test="warningFlag != null and warningFlag !=''">
            warning_flag =#{warningFlag},
        </if>
        <if test="createUserId != null and createUserId !=''">
            create_user_id =#{createUserId},
        </if>
            update_time = NOW()
        WHERE account_id = #{accountId};
    </update>


</mapper>
