<?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.middle.dao.ApkMapper">
    <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,
        pua.service_name,
        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,
        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>
            <if test="channelName !=null and channelName != ''">
                and ya.ditch_name like concat('%', #{channelName} ,'%')
            </if>
            <if test="appName !=null and appName != ''">
                and ya.app_name like concat('%', #{appName} ,'%')
            </if>
            <if test="appType !=null">
                and ya.app_type = #{appType}
            </if>
            <if test="superiorId !=null and superiorId != ''">
                and ya.superior_id = #{superiorId}
            </if>
        </where>
        GROUP BY
        ya.app_id
        ORDER BY ya.apk_url
    </select>
    <select id="countApk" resultType="java.lang.Integer">
        select
            count(app_id)
        from yt_app
        where apk_url is not null
    </select>
    <select id="getAppIds" resultType="java.lang.String">
        select
            group_concat(app_id)
        from yt_app
        where apk_url is not null
    </select>
    <select id="queryAll" resultType="com.ytpm.agent.view.AgentAppView">
        select
            a.app_id,
            a.app_key,
            a.app_name,
            a.user_id,
            a.app_type,
            a.apk_url,
            a.qr_code,
            a.version_code,
            a.update_tips,
            a.enabled,
            a.store_on_sale,
            a.store_type,
            a.store_url,
            a.package_name,
            a.domain,
            a.category,
            a.sub_category,
            a.coppa,
            a.screen_orientation,
            a.ccpa,
            a.feign_path,
            a.ditch_id,
            a.ditch_name,
            a.superior_id,
            pua.service_name
        from yt_app a
        left join yt_platform_user_app pua on a.superior_id = pua.app_id
        where a.enabled = 1
    </select>
    <select id="queryBySuperiorId" resultType="com.ytpm.agent.view.AgentAppView">
        select
            app_id, app_key, app_name, user_id, app_type, apk_url, qr_code, version_code, update_tips, enabled, store_on_sale, store_type, store_url, package_name, domain, category, sub_category, coppa, screen_orientation, ccpa, feign_path, ditch_id, ditch_name, superior_id
        from yt_app
        where enabled = 1 and superior_id = #{superiorId}
    </select>
    <select id="queryServeAndApp" resultType="com.ytpm.agent.view.FeignServeAppView">
        select
            pua.service_name,
            group_concat(a.app_id) appIds
        from yt_platform_user_app pua
        left join yt_app a on pua.app_id = a.superior_id and a.apk_url is not null
        where pua.service_name is not null and pua.user_id is not null
        group by pua.service_name
    </select>
    <select id="queryByAppId" resultType="com.ytpm.agent.view.AgentAppView">
        select
            app_id, app_key, app_name, user_id, app_type, apk_url, qr_code, version_code, update_tips, enabled, store_on_sale, store_type, store_url, package_name, domain, category, sub_category, coppa, screen_orientation, ccpa, feign_path, ditch_id, ditch_name, superior_id
        from yt_app
        where app_id = #{appId}
    </select>

    <insert id="insertOne">
        insert into yt_app
        (
            app_id,
            app_key,
            app_name,
            user_id,
            app_type,
            apk_url,
            qr_code,
            version_code,
            update_tips,
            store_on_sale,
            store_type,
            package_name,
            domain,
            category,
            sub_category,
            coppa,
            screen_orientation,
            ccpa,
            store_url,
            ditch_id,
            ditch_name,
            superior_id,
            enabled
        )
        values
            (
                #{appId},
                #{appKey},
                #{appName},
                #{userId},
                #{appType},
                #{apkUrl},
                #{qrCode},
                #{versionCode},
                #{updateTips},
                #{storeOnSale},
                #{storeType},
                #{packageName},
                #{domain},
                #{category},
                #{subCategory},
                #{coppa},
                #{screenOrientation},
                #{ccpa},
                #{storeUrl},
                #{ditchId},
                #{ditchName},
                #{superiorId},
                #{enabled}
            )
    </insert>
    <update id="updateOne">
        update yt_app
        <set>
            <if test="appKey != null">
                app_key = #{appKey},
            </if>
            <if test="appName != null">
                app_name = #{appName},
            </if>
            <if test="appType != null">
                app_type = #{appType},
            </if>
            <if test="versionCode != null">
                version_code = #{versionCode},
            </if>
            <if test="updateTips != null">
                update_tips = #{updateTips},
            </if>
            <if test="enabled != null">
                enabled = #{enabled},
            </if>
            <if test="storeOnSale != null">
                store_on_sale = #{storeOnSale},
            </if>
            <if test="storeType != null">
                store_type = #{storeType},
            </if>
            <if test="storeUrl != null">
                store_url = #{storeUrl},
            </if>
            <if test="packageName != null">
                package_name = #{packageName},
            </if>
            <if test="domain != null">
                domain = #{domain},
            </if>
            <if test="category != null">
                category = #{category},
            </if>
            <if test="subCategory != null">
                sub_category = #{subCategory},
            </if>
            <if test="coppa != null">
                coppa = #{coppa},
            </if>
            <if test="screenOrientation != null">
                screen_orientation = #{screenOrientation},
            </if>
            <if test="ccpa != null">
                ccpa = #{ccpa},
            </if>
            <if test=" apkUrl != null">
                apk_url = #{apkUrl},
            </if>
            <if test="qrCode != null">
                qr_code = #{qrCode},
            </if>
            <if test="ditchId != null">
                ditch_id = #{ditchId},
            </if>
            <if test="ditchName != null">
                ditch_name = #{ditchName},
            </if>
            <if test="superiorId != null">
                superior_id = #{superiorId}
            </if>
        </set>
        where app_id = #{appId}
    </update>
</mapper>