<?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.KuaishouAppPackageMapper">

    <insert id="replaceBatch">
        replace into ctop_kuaishou_app_package(
        id,
        project_id,
        app_md5,
        url,
        app_version,
        app_name,
        app_icon_url,
        image_md5,
        package_name,
        platform,
        app_privacy_url,
        use_sdk,
        track_url,
        data_status
        )
        values
        <foreach collection="appList" item="app" separator=",">
            (
            #{app.id},
            #{app.projectId},
            #{app.appMd5},
            #{app.url},
            #{app.appVersion},
            #{app.appName},
            #{app.appIconUrl},
            #{app.imageMd5},
            #{app.packageName},
            #{app.platform},
            #{app.appPrivacyUrl},
            #{app.useSdk},
            #{app.trackUrl},
            #{app.dataStatus}
            )
        </foreach>
    </insert>
    <select id="queryAppVersions" resultType="java.lang.String">
        SELECT app_version
        FROM ctop_kuaishou_app_package
        where project_id = #{projectId}
    </select>

    <select id="queryByAppVersion" resultType="java.lang.Long">
        SELECT id
        FROM ctop_kuaishou_app_package
        where project_id = #{projectId}
        <if test="appVersion!=null">
            and app_version = #{appVersion}
        </if>
        <if test="dataStatus!=null">
            and data_status = #{dataStatus}
        </if>
    </select>

    <select id="queryPageList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.id,
        t1.project_id 'projectId',
        t1.app_name 'appName',
        t1.app_version 'appVersion',
        t1.platform,
        CASE t1.platform
        WHEN 1 THEN 'Android应用下载'
        WHEN 2 THEN 'Android网页游戏'
        WHEN 3 THEN 'iOS应用下载'
        WHEN 4 THEN 'iOS网页游戏'
        END 'platformStr',
        t1.package_name 'packageName',
        DATE_FORMAT(t1.create_time,'%Y-%m-%d') 'createTime',
        t2.project_name 'projectName',
        (SELECT COUNT(1) FROM ctop_kuaishou_app_package_rel WHERE apptem_id = t1.id AND data_status = 1)'accountCount'
        FROM ctop_kuaishou_app_package t1
        LEFT JOIN ctop_project t2 on t1.project_id =t2.id
        LEFT JOIN ctop_project_member t3 ON t1.project_id = t3.project_id
        WHERE t1.data_status = 1
        <if test="userId!=null">
             AND t3.user_id = #{userId}
        </if>
        <if test="appVersion!=null">
            AND t1.app_version Like concat('%',#{appVersion},'%')
        </if>
        <if test="appName!=null">
            AND t1.app_name Like concat(concat('%',#{appName}),'%')
        </if>
        <if test="projectId!=null">
            AND t1.project_id=#{projectId}
        </if>
        <if test="platform!=null">
            AND t1.platform=#{platform}
        </if>
        GROUP BY t1.id
        order by t1.create_time desc
    </select>

    <select id="queryUnitCount" resultType="java.lang.Long">
        SELECT IFNULL(COUNT(1), 0)
        FROM ctop_kuaishou_app_group
        where app_id IN
              (SELECT app_id
               FROM ctop_kuaishou_app_package_rel
               WHERE data_status = 1
                 AND apptem_id = #{id}
              )
    </select>

    <select id="getRelatedGroups" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t2.account_id as 'accountId',
        t2.auth_name as 'authName',
        t3.unit_name as 'unitName'
        FROM ctop_kuaishou_app_group 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.app_id IN
        (SELECT DISTINCT app_id
        FROM ctop_kuaishou_app_package_rel
        WHERE data_status = 1
        AND apptem_id =#{id}
        <if test="accountId !=null">
            AND account_id = #{accountId}
        </if>
        )
        order by t1.create_time desc
    </select>

    <select id="getUrlByMd5" resultType="java.lang.String">
        SELECT app_icon_url
        FROM ctop_kuaishou_app_package
        WHERE image_md5 = #{md5} limit 1
    </select>

    <select id="getProjectAppList" resultType="cn.com.ctop.kuaishou.modules.ai.entity.KuaishouAppPackage">
        SELECT
            t1.id,
            t1.project_id,
            CONCAT(app_name,'(',CASE  WHEN platform=1 OR platform=2 then 'Android' else 'iOS' END ,')') as 'appName',
            t1.platform
        FROM ctop_kuaishou_app_package t1
                 LEFT JOIN ctop_kuaishou_app_package_rel t2 ON t1.id = t2.apptem_id
        WHERE t1.data_status = 1
          AND t1.project_id = #{projectId}
          and t2.data_status = 1
        GROUP BY t1.id
    </select>

</mapper>