<?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.AppMapper">
    <insert id="insertOne">
        insert into yt_platform_user_app
        (
         app_id,
         app_name,
         user_id,
         app_type,
         detail_url,
         package_name,
         domain,
         issued_time,
         wx_app_id,
         wx_secret,
         sale_price,
         create_user_id,
         create_time,
         available
        )
        values
        (
         #{appId},
         #{appName},
         #{userId},
         #{appType},
         #{detailUrl},
         #{packageName},
         #{domain},
         #{issuedTime},
         #{wxAppId},
         #{wxSecret},
         #{salePrice},
         #{createUserId},
         #{createTime},
         #{available}
        )
    </insert>
    <insert id="addGrantRecord">
        insert into yt_app_grant_record
        (
         record_id,
         app_id,
         user_id,
         sale_price,
         grant_type,
         ratio,
         data_charge,
         grant_time,
         operator_id,
         operate_time
        )
        values
        (
         #{recordId},
         #{appId},
         #{userId},
         #{salePrice},
         #{grantType},
         #{ratio},
         #{dataCharge},
         #{grantTime},
         #{operatorId},
         #{operateTime}
        );
    </insert>
    <update id="updateById">
        update yt_platform_user_app
        <set>
            <if test="appName != null">
                app_name = #{appName},
            </if>
            <if test="userId != null">
                user_id = #{userId},
            </if>
            <if test="appType != null">
                app_type = #{appType},
            </if>
            <if test="detailUrl != null">
                detail_url = #{detailUrl},
            </if>
            <if test="packageName != null">
                package_name = #{packageName},
            </if>
            <if test="domain != null">
                domain = #{domain},
            </if>
            <if test="issuedTime != null">
                issued_time = #{issuedTime},
            </if>
            <if test="wxAppId != null">
                wx_app_id = #{wxAppId},
            </if>
            <if test="wxSecret != null">
                wx_secret = #{wxSecret},
            </if>
            <if test="updateUserId != null">
                update_user_id = #{updateUserId},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="salePrice != null">
                sale_price = #{salePrice},
            </if>
            <if test="chargeStatus != null">
                charge_status = #{chargeStatus},
            </if>
            <if test="grantType != null">
                grant_type = #{grantType},
            </if>
            <if test="available != null">
                available = #{available}
            </if>
        </set>
        where app_id = #{appId}
    </update>
    <delete id="delApp">
        update yt_platform_user_app set available = 0, update_time = now(), update_user_id = #{userId} where app_id = #{appId}
    </delete>

    <select id="getAppList" resultType="com.ytpm.middle.view.AppListVO">
        select
            pua.app_id,
            pua.app_name,
            pua.user_id,
            pua.app_type,
            pua.detail_url,
            pua.package_name,
            pua.domain,
            pua.sale_price,
            pua.grant_type,
            pua.issued_time,
            pua.wx_app_id,
            pua.wx_secret,
            me.enterprise_name,
            me.legal,
            me.concat_phone,
            me.credit_code,
            me.taku_public_key
        from yt_platform_user_app pua
        left join yt_middle_enterprise me on pua.user_id = me.user_id
        <where>
            pua.available = 1
            <if test="appType != null">
                and pua.app_type = #{appType}
            </if>
            <if test="appName != null and appName!=''">
                and pua.app_name like concat('%', #{appName} ,'%')
            </if>
        </where>
        order by
        pua.issued_time
    </select>
    <select id="getByPackName" resultType="com.ytpm.agent.model.YtPlatformUserApp">
        select
            app_id, app_name, user_id,sale_price,grant_type, app_type, detail_url, package_name, domain, issued_time, wx_app_id, wx_secret
        from yt_platform_user_app
        where package_name = #{packageName}
    </select>
    <select id="getByPrimary" resultType="com.ytpm.agent.model.YtPlatformUserApp">
        select
            app_id, app_name, user_id,sale_price,grant_type, app_type, detail_url, package_name, domain, issued_time, wx_app_id,wx_secret
        from yt_platform_user_app
        where app_id = #{appId}
    </select>
    <select id="getIssuedAppList" resultType="com.ytpm.middle.view.DropDownVO">
        select
            app_id optionVal,
            app_name optionName
        from yt_platform_user_app
        where available = 1
    </select>
    <select id="getApkList" resultType="com.ytpm.middle.view.DropDownVO">
        select
            app_id optionVal,
            app_name optionName
        from yt_app
        where superior_id = #{appId}
    </select>
    <select id="getAppListByUserIds" 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, create_time, create_user_id, update_time, update_user_id, available
        from yt_platform_user_app
        where user_id in
        <foreach collection="userIds.split(',')" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
</mapper>