<?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="com.ytpm.dao.AgentAppMapper">

    <resultMap id="enabledAppMap" type="com.ytpm.agent.view.AgentEnableAppView">
        <id column="app_id" property="appId" />
        <result column="app_name" property="appName" />
        <result column="qr_code" property="qrCode" />
        <collection property="clazzList" ofType="com.ytpm.agent.view.AgentAppClassView">
            <result column="full_name" property="fullName" />
            <result column="class_desc" property="classDesc" />
        </collection>
    </resultMap>
    <select id="getEnabledList" resultMap="enabledAppMap">
        SELECT
            ya.app_id,
            ya.app_name,
            ya.user_id,
            ya.app_type,
            ya.apk_url,
            ya.qr_code,
            ya.version_code,
            ya.update_tips,
            ypu.nick_name,
            ac.full_name,
            ac.class_desc
        FROM
            yt_app ya
                LEFT JOIN yt_platform_user ypu ON ya.user_id = ypu.user_id
                LEFT JOIN yt_app_class ac ON ya.app_id = ac.app_id and ac.enabled = 1
        WHERE
            ya.enabled = 1
          AND ya.user_id = #{userId}
    </select>

    <select id="searchAppList" resultType="com.ytpm.agent.view.AgentAppView">
        SELECT
            ya.app_id,
            ya.app_key,
            ya.app_name,
            ya.superior_id,
            pua.app_name superiorName,
            ya.user_id,
            ya.app_type,
            ya.apk_url,
            ya.qr_code,
            ya.version_code,
            ya.update_tips,
            ya.enabled,
            ya.store_on_sale,
            ya.store_type,
            ya.store_url,
            ya.package_name,
            ya.domain,
            ya.category,
            ya.sub_category,
            ya.coppa,
            ya.ccpa,
            ya.screen_orientation,
            ya.ditch_name,
            ya.ditch_id,
            GROUP_CONCAT( acr.channel_id ) channel_id,
            GROUP_CONCAT( acr.channel_name ) channel_name,
            GROUP_CONCAT( acr.network_app_id ) network_app_id,
            GROUP_CONCAT( acr.network_app_name ) network_app_name,
            u.nick_name
        FROM
            yt_app ya
                JOIN yt_platform_user_app pua on ya.superior_id = pua.app_id
                LEFT JOIN yt_app_channel_relative acr ON ya.app_id = acr.app_id
                LEFT JOIN yt_platform_user u ON ya.user_id = u.user_id
        WHERE
            ya.enabled = 1 and ya.user_id = #{userId}
        <if test="appName !=null and appName != ''">
            and ya.app_name like concat('%', #{appName} ,'%')
        </if>
        <if test="appType !=null">
            and ya.app_type = #{appType}
        </if>
        GROUP BY
            ya.app_id
    </select>

    <select id="searchAppIdList" resultType="com.ytpm.agent.view.AgentAppView">
        select superior_id from yt_app  where user_id =#{userId}
    </select>
    <select id="getIssuedAppList" resultType="com.ytpm.agent.model.YtPlatformUserApp">
        select
           app_id, app_name, user_id, app_type, detail_url, package_name, domain, issued_time, wx_app_id, feign_class_path, service_name
        from yt_platform_user_app
        where user_id = #{userId}
    </select>
    <select id="getApkIdList" resultType="java.lang.String">
        select
            app_id
        from yt_app
        where user_id = #{userId}
    </select>
    <select id="selectByPrimaryKey" resultType="com.ytpm.agent.model.YtPlatformUserApp">
        select
            app_id, app_name, user_id, app_type, detail_url, package_name, domain, issued_time, wx_app_id, wx_secret, sale_price, grant_type, charge_status, feign_class_path, service_name, create_time, create_user_id, update_time, update_user_id, available
        from yt_platform_user_app
        where available = 1 and app_id = #{appId}
    </select>
</mapper>