<?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.zc.business.mapper.StatusMapper">

    <resultMap id="BaseResultMap" type="com.zc.business.domain.Status">
        <result column="id" jdbcType="BIGINT" property="id"/>
        <result column="device_no" jdbcType="VARCHAR" property="deviceNo"/>
        <result column="device_name" jdbcType="VARCHAR" property="deviceName"/>
        <result column="device_status" jdbcType="INTEGER" property="deviceStatus"/>
        <result column="device_id" jdbcType="INTEGER" property="deviceId"/>
        <result column="device_ip" jdbcType="VARCHAR" property="deviceIp"/>
        <result column="success_rate" jdbcType="VARCHAR" property="successRate"/>
        <result column="lost_rate" jdbcType="VARCHAR" property="lostRate"/>
        <result column="success_rate" jdbcType="VARCHAR" property="successRate"/>

        <result column="direction" jdbcType="VARCHAR" property="direction"/>
        <result column="manufacturer" jdbcType="VARCHAR" property="production"/>
        <result column="model" jdbcType="VARCHAR" property="model"/>
        <result column="facilities_type" jdbcType="VARCHAR" property="network"/>
        <result column="remark" jdbcType="VARCHAR" property="content"/>
        <result column="type" jdbcType="VARCHAR" property="type"/>
        <result column="use_state" jdbcType="VARCHAR" property="useState"/>
    </resultMap>



    <insert id="Add" parameterType="com.zc.business.domain.Status">
        INSERT INTO status
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="status.deviceNo != null">
                device_no,
            </if>
            <if test="status.deviceName != null">
                device_name,
            </if>
            <if test="status.deviceStatus != null">
                device_status,
            </if>
            <if test="status.deviceId != null">
                device_id,
            </if>
            <if test="status.time != null">
                time,
            </if>
            <if test="status.successRate != null">
                success_rate,
            </if>

            <if test="status.lostRate != null">
                lost_rate,
            </if>
            <if test="status.deviceIp != null">
                device_ip,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="status.deviceNo != null">
                #{status.deviceNo,jdbcType=VARCHAR},
            </if>
            <if test="status.deviceName != null">
                #{status.deviceName,jdbcType=VARCHAR},
            </if>
            <if test="status.deviceStatus != null">
                #{status.deviceStatus,jdbcType=INTEGER},
            </if>
            <if test="status.deviceId != null">
                #{status.deviceId,jdbcType=BIGINT},
            </if>
            <if test="status.time != null">
                #{status.time,jdbcType=DATE},
            </if>
            <if test="status.successRate != null">
                #{status.successRate,jdbcType=VARCHAR},
            </if>
            <if test="status.lostRate != null">
                #{status.lostRate,jdbcType=VARCHAR},
            </if>

            <if test="status.deviceIp != null">
                #{status.deviceIp,jdbcType=VARCHAR}
            </if>
        </trim>
    </insert>

    <sql id="selectStatus">
        select id, device_no, device_name, device_status, time from status
    </sql>


    <select id="export" parameterType="com.zc.business.domain.Status" resultMap="BaseResultMap">
        select s.id, s.device_no, s.device_name,s.time,
        d.device_ip,s.success_rate,s.lost_rate,e.manufacturer,e.model,d.facilities_type,d.remark,COALESCE(d.child_type,
        d.device_type) AS type,
        CASE
        WHEN d.direction = 1 THEN '菏泽方向'
        WHEN d.direction = 3 THEN '济南方向'
        ELSE '双向'
        END AS direction,
        CASE
        WHEN s.device_status = 1 THEN '在线'
        WHEN s.device_status = 0 THEN '离线'
        ELSE '设备未接入'
        END AS device_status
        from dc_device d
        LEFT JOIN status s on (s.device_id = d.id)
        LEFT JOIN dc_product e on e.id = d.product_id
        <where>
            <if test="status.time != null">
                AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}
            </if>
            <if test="status.deviceNo != null">
                AND s.device_no = #{status.deviceNo}
            </if>
            <if test="status.type != null">
                AND (d.device_type in
                <foreach item="typeItem" collection="status.types" open="(" separator="," close=")">
                    #{typeItem}
                </foreach>
                or d.child_type in
                <foreach item="typeItem" collection="status.types" open="(" separator="," close=")">
                    #{typeItem}
                </foreach>
                )
            </if>
            <if test="status.deviceId != null">
                AND s.device_id = #{status.deviceId}
            </if>
            <if test="status.useState != null and status.useState != 0">
                AND d.use_state = #{status.useState}
            </if>
            <if test="status.deviceName != null and status.deviceName != ''">
                AND s.device_name like concat('%', #{status.deviceName}, '%')
            </if>
            <if test="status.direction != null">
                AND d.direction = #{status.direction}
            </if>
        </where>
        <if test="status.orderByField != null and status.orderDirection != null">
            ORDER BY ${status.orderByField} ${status.orderDirection}
        </if>
    </select>


    <select id="listStatus" parameterType="com.zc.business.domain.Status" resultMap="BaseResultMap">
        select s.id, s.device_no, s.device_name, s.device_status,s.time, d.device_ip,s.success_rate,s.lost_rate,d.direction,e.manufacturer,e.model,d.facilities_type,d.remark,COALESCE(d.child_type, d.device_type) AS type
        from dc_device d
        LEFT JOIN status s on (s.device_id=d.id)
        LEFT JOIN  dc_product e on e.id=d.product_id
        <where>
            <if test="status.time != null">
                AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}
            </if>
            <if test="status.deviceNo != null">
                AND s.device_no = #{status.deviceNo}
            </if>
            <if test="status.type != null">
                AND (d.device_type = #{status.type} or d.child_type=#{status.type})
            </if>
            <if test="status.deviceId != null">
                AND s.device_id = #{status.deviceId}
            </if>
            <if test="status.useState != null and status.useState != 0">
                AND d.use_state = #{status.useState}
            </if>
            <if test="status.deviceName != null and status.deviceName != ''">
                AND s.device_name like concat('%', #{status.deviceName}, '%')
            </if>
            <if test="status.direction != null">
                AND d.direction = #{status.direction}
            </if>
        </where>

        <if test="status.orderByField != null and status.orderDirection != null">
            ORDER BY ${status.orderByField} ${status.orderDirection}
        </if>
    </select>


    <select id="listStatusByTypes" parameterType="com.zc.business.domain.Status" resultMap="BaseResultMap">
        select s.id, s.device_no, s.device_name, s.device_status,s.time,
        d.device_ip,s.success_rate,s.lost_rate,d.direction,e.manufacturer,e.model,d.facilities_type,d.remark,COALESCE(d.child_type,
        d.device_type) AS type
        from dc_device d
        LEFT JOIN status s on (s.device_id=d.id)
        LEFT JOIN dc_product e on e.id=d.product_id
        <where>
            <if test="status.time != null">
                AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}
            </if>
            <if test="status.deviceNo != null">
                AND s.device_no = #{status.deviceNo}
            </if>
            <if test="status.type != null">
                AND (d.device_type in
                <foreach item="typeItem" collection="status.types" open="(" separator="," close=")">
                    #{typeItem}
                </foreach>
                or d.child_type in
                <foreach item="typeItem" collection="status.types" open="(" separator="," close=")">
                    #{typeItem}
                </foreach>
                )
            </if>
            <if test="status.deviceId != null">
                AND s.device_id = #{status.deviceId}
            </if>
            <if test="status.useState != null and status.useState != 0">
                AND d.use_state = #{status.useState}
            </if>
            <if test="status.deviceName != null and status.deviceName != ''">
                AND s.device_name like concat('%', #{status.deviceName}, '%')
            </if>
            <if test="status.direction != null">
                AND d.direction = #{status.direction}
            </if>
        </where>
        <if test="status.orderByField != null and status.orderDirection != null">
            ORDER BY ${status.orderByField} ${status.orderDirection}
        </if>
<!--        <if test="status.pageNum != null and status.pageSize != null">-->
<!--            LIMIT #{status.pageSize} OFFSET #{status.pageNum}-->
<!--        </if>-->
    </select>


    <select id="getStatusList" parameterType="com.zc.business.domain.Status" resultMap="BaseResultMap">
        SELECT COALESCE
        (d.child_type,d.device_type ) AS type,
        s.time,
        ROUND( avg( s.success_rate ), 2 ) success_rate
        FROM
        status s
        LEFT JOIN dc_device d ON s.device_id = d.id
        <where>
            <if test="status.time != null">
                AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}
            </if>
            <if test="status.type != null">
                AND (d.device_type in
                <foreach item="typeItem" collection="status.types" open="(" separator="," close=")">
                    #{typeItem}
                </foreach>
                or d.child_type in
                <foreach item="typeItem" collection="status.types" open="(" separator="," close=")">
                    #{typeItem}
                </foreach>
                )
            </if>
            <if test="status.deviceId != null">
                AND s.device_id = #{status.deviceId}
            </if>
            <if test="status.useState != null and status.useState != 0">
                AND d.use_state = #{status.useState}
            </if>
            <if test="status.deviceName != null and status.deviceName != ''">
                AND s.device_name like concat('%', #{status.deviceName}, '%')
            </if>
            <if test="status.direction != null">
                AND d.direction = #{status.direction}
            </if>
            GROUP BY
            d.child_type,
            d.device_type,
            DAY (s.time)
        </where>
        <if test="status.orderByField != null and status.orderDirection != null">
            ORDER BY ${status.orderByField} ${status.orderDirection}
        </if>
    </select>


    <select id="listStatusBySection" parameterType="com.zc.business.domain.Status" resultMap="BaseResultMap">
        select s.id, s.device_no, s.device_name, s.device_status,s.time,
        d.device_ip,s.success_rate,s.lost_rate,d.direction,e.manufacturer,e.model,d.facilities_type,m.section_id,d.remark,COALESCE(d.child_type,
        d.device_type) AS type
        from dc_device d
        LEFT JOIN dc_stake_mark m on m.stake_mark=d.stake_mark and m.direction = d.direction
        LEFT JOIN dc_road_section r on r.id=m.section_id
        LEFT JOIN status s on (s.device_id=d.id)
        LEFT JOIN dc_product e on e.id=d.product_id
        <where>
            <if test="status.time != null">
                AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}
            </if>
            <if test="status.deviceNo != null">
                AND s.device_no = #{status.deviceNo}
            </if>
            <if test="status.type != null">
                AND (d.device_type = #{status.type} or d.child_type=#{status.type})
            </if>
            <if test="status.deviceId != null">
                AND s.device_id = #{status.deviceId}
            </if>
            <if test="status.useState != null and status.useState != 0">
                AND d.use_state = #{status.useState}
            </if>
            <if test="status.deviceName != null and status.deviceName != ''">
                AND s.device_name like concat('%', #{status.deviceName}, '%')
            </if>
            <if test="status.direction != null">
                AND d.direction = #{status.direction}
            </if>
        </where>
        <if test="status.orderByField != null and status.orderDirection != null">
            ORDER BY ${status.orderByField} ${status.orderDirection}
        </if>
    </select>

    <select id="deviceStatusListById" parameterType="com.zc.business.domain.Status" resultMap="BaseResultMap">
        select s.id,s.time,s.success_rate,s.lost_rate
        from status s
        <where>
            <if test="status.time != null">
                AND s.time BETWEEN #{status.startTime,jdbcType=DATE} AND #{status.time,jdbcType=DATE}
            </if>
            <if test="status.deviceId != null">
                AND s.device_id = #{status.deviceId}
            </if>
            <if test="status.deviceName != null and status.deviceName != ''">
                AND s.device_name like concat('%', #{status.deviceName}, '%')
            </if>
            <if test="status.direction != null">
                AND d.direction = #{status.direction}
            </if>
        </where>
        <if test="status.orderByField != null and status.orderDirection != null">
            ORDER BY ${status.orderByField} ${status.orderDirection}
        </if>
    </select>

</mapper>