<?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="cn.com.ctop.kuaishou.modules.ai.mapper.KuaishouLandpagePackageMapper">


    <insert id="replaceBatch">
        replace into ctop_kuaishou_landpage_package(
        id,
        project_id,
        account_id,
        landpage_name,
        site_id,
        url,
        biz_type,
        app_id,
        comps,
        landpage_create_time,
        landpage_update_time
        )
        values
        <foreach collection="landpagePackageList" item="landpage" separator=",">
            (
            #{landpage.id},
            #{landpage.projectId},
            #{landpage.accountId},
            #{landpage.landpageName},
            #{landpage.siteId},
            #{landpage.url},
            #{landpage.bizType},
            #{landpage.appId},
            #{landpage.comps},
            #{landpage.landpageCreateTime},
            #{landpage.landpageUpdateTime}
            )
        </foreach>
    </insert>

    <delete id="deleteByAccountId">
        delete
        from ctop_kuaishou_landpage_package
        where account_id = #{accountId}
    </delete>

    <select id="queryPageList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.project_id 'projectId',
        t1.account_id 'accountId',
        CONCAT(t1.site_id,'') 'siteId',
        t1.landpage_name 'landpageName',
        t2.project_name 'projectName',
        t3.auth_name 'authName',
        t1.landpage_update_time 'landpageUpdateTime',
        (SELECT COUNT(1) FROM ctop_kuaishou_landpage_package WHERE site_id = t1.site_id) as 'accountCount',
        (SELECT COUNT(1) FROM ctop_kuaishou_landpage_package_rel WHERE site_id = t1.site_id) as 'unitCount'
        FROM ctop_kuaishou_landpage_package t1
        LEFT JOIN ctop_project t2 ON t1.project_id = t2.id
        LEFT JOIN ctop_user_allocation t3 ON t1.account_id = t3.account_id
        LEFT JOIN ctop_project_member t4 ON t1.project_id = t4.project_id
        WHERE 1=1
        <if test="userId!=null">
            AND t4.user_id =#{userId}
        </if>
        <if test="projectId!=null">
            AND t1.project_id =#{projectId}
        </if>
        <if test="accountId!=null">
            AND t1.account_id =#{accountId}
        </if>
        GROUP BY t1.id
        ORDER BY t1.landpage_update_time desc
    </select>

    <select id="getRelatedAccounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t2.auth_name 'authName', (SELECT COUNT(1)
                                         FROM ctop_kuaishou_landpage_package
                                         WHERE account_id = t1.account_id) 'count'
        FROM ctop_kuaishou_landpage_package t1
                 LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
        where t1.account_id IN (
            SELECT DISTINCT account_id
            FROM ctop_kuaishou_landpage_package
            WHERE site_id = #{siteId}
        )
        GROUP BY t1.account_id
        order by t1.create_time desc
    </select>

    <select id="getRelatedGroups" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t2.auth_name 'authName', t3.unit_name 'unitName'
        FROM ctop_kuaishou_landpage_package_rel t1
        INNER JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
        INNER JOIN ctop_kuaishou_group t3 ON t1.unit_id = t3.unit_id
        where t1.site_id = #{siteId}
        <if test="accountId!=null">
            AND t1.account_id = #{accountId}
        </if>
        order by t1.create_time desc
    </select>
</mapper>