<?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.xhkjedu.sapi.mapper.appversion.AppthirdMapper">
    <!--应用主页列表-->
    <select id="listAll" resultType="com.xhkjedu.sapi.vo.appversion.AppthirdVo">
        select appid,appname,applogo,appstate,apptype,packagename,belong
        from t_appthird where (belong=2
        <if test="schoolid!=null">
            or schoolid=#{schoolid}
        </if>
        )
        order by createtime
    </select>
    <!--获取我的应用列表-->
    <select id="listMy" resultType="com.xhkjedu.sapi.vo.appversion.AppthirdVo">
        select appid,appname,applogo,appstate,apptype,packagename,belong
        from t_appthird where createid=#{createid} and appstate=1
        order by createtime
    </select>
    <resultMap id="detailResult" type="com.xhkjedu.sapi.vo.appversion.AppthirdDetailVo">
        <result property="appid" column="appid"></result>
        <result property="appname" column="appname"></result>
        <result property="applogo" column="applogo"></result>
        <result property="comm" column="comm"></result>
        <collection property="histories" ofType="com.xhkjedu.sapi.model.appversion.TAppthirdHistory">
            <result property="apphid" column="apphid"></result>
            <result property="apphcode" column="apphcode"></result>
            <result property="apphname" column="apphname"></result>
            <result property="apphsize" column="apphsize"></result>
            <result property="apphpath" column="apphpath"></result>
            <result property="apphcomm" column="apphcomm"></result>
            <result property="createtime" column="createtime"></result>
        </collection>
    </resultMap>
    <!--应用详情-->
    <select id="findById" resultMap="detailResult">
        select a.appid,a.appname,a.applogo,a.comm,h.apphid,h.apphcode,h.apphname,h.apphsize,h.apphpath,h.apphcomm,h.createtime
        from t_appthird a left join t_appthird_history h on h.appid=a.appid
        where a.appid=#{appid} order by h.createtime desc
    </select>
    <!--上、下架-->
    <update id="updateState">
        update t_appthird set appstate=#{appstate} where appid=#{appid}
    </update>
    <!--获取最新app版本-->
    <select id="getNew" resultType="com.xhkjedu.sapi.model.appversion.TAppthirdHistory">
        select h.*,a.applogo from t_appthird a left join t_appthird_history h on a.appid = h.appid
        where a.appid=#{appid} order by h.apphcode desc,h.apphid desc limit 1
    </select>
    <!--最新应用列表-->
    <select id="listNew" resultType="com.xhkjedu.sapi.model.appversion.TAppthirdHistory">
        select ah.*,a.applogo,a.packagename,a.appname,a.appstate,a.apptype,a.belong
        from t_appthird a left join (select ah1.appid,(SELECT MAX(ah2.apphid)
        FROM t_appthird_history ah2 WHERE ah2.appid=ah1.appid AND ah2.apphcode=max(ah1.apphcode))apphid
        from t_appthird_history ah1 where (ah1.schoolid is null
        <if test="schoolid!=null">
            or ah1.schoolid=#{schoolid}
        </if>
        ) group by ah1.appid)h on h.appid=a.appid
        left join t_appthird_history ah on h.apphid=ah.apphid
        where (a.belong=2
        <if test="schoolid!=null">
            or a.schoolid=#{schoolid}
        </if>
        ) and (ah.apphid is not null or a.apptype=2)
        <if test="appstate!=null and appstate!=0">
            and a.appstate=#{appstate}
        </if>
    </select>
</mapper>
