<?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.xncoding.pos.dao.repository.AppInfoDao">

    <resultMap id="AppVersionInfoMap" type="AppVersionInfo">
        <result property="appName" column="appName" />
        <collection property="versions" ofType="string">
            <result column="version"/>
        </collection>
    </resultMap>
    
    <select id="selectAppVersionList" resultMap="AppVersionInfoMap">
        SELECT DISTINCT
            A.`name`    AS appName,
            A.version   AS version
        FROM t_app A
        <where>
            A.project_id IN
            <foreach collection="list" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </where>
    </select>

    <select id="selectAllAppNames" resultType="string">
        SELECT DISTINCT name FROM t_app
        <where>
            project_id IN
            <foreach collection="list" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </where>
    </select>

    <select id="selectAllVersions" resultType="string">
        SELECT DISTINCT version FROM t_app
        <where>
            project_id IN
            <foreach collection="list" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </where>
    </select>

    <select id="selectNowVersionApp" resultType="app">
        SELECT A.id AS id, A.version AS version
        FROM t_app A
        WHERE A.publishtime = (SELECT MAX(publishtime) FROM t_app WHERE application_id=#{applicationId})
        LIMIT 1
    </select>

    <select id="countUserPermission" resultType="int">
        SELECT COUNT(*)
        FROM t_project_user A
            LEFT OUTER JOIN t_project B ON A.project_id=B.id
        WHERE A.user_id=#{userId} AND B.application_id=#{applicationId}
    </select>

    <select id="selectAllLocations" resultType="string">
        SELECT DISTINCT location FROM t_pos
        <where>
            project_id IN
            <foreach collection="list" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
        </where>
    </select>

    <select id="selectPosListByLocations" resultType="pos">
        SELECT DISTINCT
            id          AS id,
            imei        AS imei,
            location    AS location
        FROM t_pos
        <where>
            <if test='locations != null'>
                location IN
                <foreach collection="locations" index="index" item="i" open="(" separator="," close=")">
                    #{i}
                </foreach>
            </if>
        </where>
    </select>

    <select id="searchList" parameterType="searchApp" resultType="appInfo" >
        SELECT DISTINCT
            A.id                    AS id,
            A.name                  AS name,
            A.version               AS version,
            A.tips                  AS tips,
            A.publishtime           AS publishtime,
            A.publish_range         AS publishRange,
            B.name                  AS projectName,
            C.username              AS operatorUsername,
            C.name                  AS operatorName
        FROM t_app A
            LEFT OUTER JOIN t_project B ON A.project_id=B.id
            LEFT OUTER JOIN t_manager C ON A.operator_id=C.id
        <where>
            A.project_id IN
            <foreach collection="pidList" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
            <if test='appName != null and appName != ""'>
                AND A.name = #{appName}
            </if>
            <if test='appVersion != null and appVersion != ""'>
                AND A.version = #{appVersion}
            </if>
            <if test='projectId != null and projectId != 0'>
                AND A.project_id = #{projectId}
            </if>
            <if test='publishRange != null and publishRange != 0'>
                AND A.publish_range = #{publishRange}
            </if>
            <if test='publishTimeStart != null and publishTimeEnd != null'>
                AND A.publishtime BETWEEN #{publishTimeStart} AND #{publishTimeEnd}
            </if>
        </where>
        ORDER BY A.publishtime DESC
    </select>

    <select id="grayList" parameterType="int" resultType="pos">
        SELECT DISTINCT
            A.id        AS id,
            A.imei      AS imei,
            A.location  AS location
        FROM t_pos A
            LEFT OUTER JOIN t_app_publish B ON A.id=B.pos_id
            LEFT OUTER JOIN t_app C ON B.app_id=C.id
        WHERE C.id=#{id}
    </select>

</mapper>

