<?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.PosInfoDao">
    <select id="searchList" parameterType="searchPos" resultType="posInfo">
        SELECT DISTINCT
            A.id                    AS id,
            A.imei                  AS imei,
            A.series                AS series,
            A.jointime              AS jointime,
            A.location              AS location,
            A.pos_state             AS posState,
            A.tips                  AS tips,
            B.name                  AS projectName
        FROM t_pos A
            LEFT OUTER JOIN t_project B ON A.project_id=B.id
        <where>
            A.project_id IN
            <foreach collection="pidList" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
            <if test='imei != null and imei != ""'>
                AND A.imei = #{imei}
            </if>
            <if test='location != null and location != ""'>
                AND A.location = #{location}
            </if>
            <if test='projectId != null and projectId != 0'>
                AND A.project_id = #{projectId}
            </if>
        </where>
        ORDER BY A.jointime DESC
    </select>

    <select id="searchDetail" parameterType="int" resultType="posInfo">
        SELECT DISTINCT
            A.id                    AS id,
            A.imei                  AS imei,
            A.sn                    AS sn,
            A.series                AS series,
            A.android_version       AS androidVersion,
            A.version               AS version,
            A.owner                 AS owner,
            A.jointime              AS jointime,
            A.bindtime              AS bindtime,
            A.location              AS location,
            A.pos_state             AS posState,
            A.tips                  AS tips,
            B.name                  AS projectName
        FROM t_pos A
            LEFT OUTER JOIN t_project B ON A.project_id=B.id
        WHERE A.id=#{id}
    </select>

    <select id="selectLastHistory" resultType="posHistory">
        SELECT
            id              AS id,
            pos_id          AS posId,
            location        AS location,
            bindtime        AS bindtime,
            unbindtime      AS unbindtime,
            created_time    AS createdTime
        FROM t_pos_history
        WHERE pos_id=#{posId} AND location=#{location} AND unbindtime IS NULL
            AND created_time = (SELECT MAX(created_time) FROM t_pos_history WHERE pos_id=#{posId})
        LIMIT 1
    </select>

    <!-- 查询网点数量-->
    <select id="searchLocationCount" parameterType="int" resultType="int">
        SELECT COUNT(DISTINCT location)
        FROM t_pos
        <where>
            location &lt;&gt; ''
            <if test='projectId != null and projectId != 0'>
                AND project_id = #{projectId}
            </if>
            <if test='projectId == null or projectId == 0'>
                AND project_id IN
                <foreach collection="pidList" index="index" item="i" open="(" separator="," close=")">
                    #{i}
                </foreach>
            </if>
        </where>
    </select>

    <!-- 已网点分组查询机具列表-->
    <select id="searchListGroupByLocation" parameterType="searchPos" resultType="posGroupInfo" >
        SELECT
            A.location              AS location,
            B.name                  AS projectName,
            COUNT(*)                AS posCount
        FROM t_pos A
        LEFT OUTER JOIN t_project B ON A.project_id=B.id
        <where>
            A.location &lt;&gt; ''
            AND A.project_id IN
            <foreach collection="pidList" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
            <if test='projectId != null and projectId != 0'>
                AND A.project_id = #{projectId}
            </if>
            <if test='location != null and location != ""'>
                AND A.location = #{location}
            </if>
        </where>
        GROUP BY A.location, B.name
    </select>

    <!-- 每个网点的机具监控列表-->
    <select id="selectPosMonitorList" parameterType="searchMonitor" resultType="monitorInfo">
        SELECT
            A.id                  AS id,
            A.report_time         AS reportTime,
            A.report_location     AS reportLocation,
            A.online_state        AS onlineState,
            B.imei                AS imei,
            B.series              AS series
        FROM
            t_pos_monitor A
            LEFT OUTER JOIN t_pos B ON A.pos_id=B.id
        <where>
            B.location &lt;&gt; ''
            <if test='imei != null and imei != ""'>
                AND B.imei = #{imei}
            </if>
            <if test='location != null and location != ""'>
                AND B.location = #{location}
            </if>
            AND B.project_id IN
            <foreach collection="pidList" index="index" item="i" open="(" separator="," close=")">
                #{i}
            </foreach>
            <if test='projectId != null and projectId != 0'>
                AND B.project_id = #{projectId}
            </if>
            <if test='onlineState != null and onlineState != 0'>
                AND A.online_state = #{onlineState}
            </if>
        </where>
        ORDER BY A.report_time DESC
    </select>

    <!-- 更新长期没有报告的POS状态为离线-->
    <update id="updateOnlineState">
        UPDATE t_pos_monitor SET online_state=2 WHERE report_time &lt; #{start}
    </update>
</mapper>

