<?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="cn.com.taiji.beidou.ship.status.mapper.ShipStatusMapper">

    <resultMap id="shipStatusResult" type="shipStatusResult">
        <id property="devideNo" column="devide_no"/>
        <result property="isOnline" column="is_online"/>
        <result property="isStasis" column="is_stasis"/>
        <result property="longitude" column="longitude"/>
        <result property="latitude" column="latitude"/>
        <result property="location" column="location"/>
        <result property="kwh" column="kwh"/>
        <result property="lastTime" column="last_time"/>
        <result property="shipId"      column="ship_id"      />
        <result property="shipType"     column="ship_type"     />
        <result property="jobType"   column="job_type"   />
        <result property="jobWay"    column="job_way"    />
        <result property="ownerName"      column="owner_name"      />
        <result property="ownerTelNo"    column="owner_tel_no"    />
        <result property="shipLength"     column="ship_length"     />
        <result property="shipWidth"   column="ship_width"   />
        <result property="shipMaterial"    column="ship_material"    />
        <result property="policeStationId"      column="police_station_id"      />
        <result property="deptName"     column="dept_name"     />
        <result property="ownerCompany"      column="owner_company"      />
        <result property="createTime"    column="create_time"    />
        <result property="modifyTime"      column="modify_time"      />
        <result property="ownerType"    column="owner_type"    />
        <result property="onlineChangeTime"    column="online_change_time"    />
        <result property="locationTime"    column="location_time"    />
        <result property="numberDay"    column="number_day"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="status" column="status"/>
    </resultMap>

    <select id="selectById" parameterType="string" resultMap="shipStatusResult">
        select archives.*,
        (CASE st.is_online WHEN 'ONLINE' THEN 1  WHEN 'OFFLINE' THEN 2 WHEN 'MARK' THEN 3 END) as status,
        st.is_inport,location.longitude,location.latitude,location.location,st.online_change_time,
		location.kwh as kwh,
        dept.pscmc as dept_name,
        <if test="isOnline != null and isOnline != ''">
        TIMESTAMPDIFF(DAY,st.online_change_time,NOW()) as number_day
        </if>
        <if test="isStasis != null and isStasis != ''">
            TIMESTAMPDIFF(DAY,st.anchor_change_time,NOW()) as number_day
        </if>
        from ax_beidou_ship_status st
        inner join ax_beidou_ship_archives archives on st.device_id = archives.devide_no
        inner join ax_beidou_dept dept on archives.police_station_id = dept.pscbh
        LEFT JOIN ax_beidou_ship_location location on archives.devide_no = location.device_id
        where  1=1
        <if test="devideNo != null and devideNo != ''">
            and st.device_id = #{devideNo}
        </if>
        <if test="isOnline != null and isOnline != ''">
            and st.is_online = #{isOnline}
        </if>
        <if test="isStasis != null and isStasis != ''">
            and st.is_anchor = #{isStasis}
            and TIMESTAMPDIFF(MINUTE,st.anchor_change_time,NOW())>24*60*7
        </if>
    </select>

    <select id="selectShipStatusList" parameterType="shipStatusReq" resultMap="shipStatusResult">
        select archives.*,(CASE st.is_online WHEN 'ONLINE' THEN 1  WHEN 'OFFLINE' THEN 2 WHEN 'MARK' THEN 3 END) as is_online,
        st.is_inport,location.longitude,location.latitude,location.location,
        st.online_change_time ,
        location.kwh as kwh,
        dept.pscmc as dept_name,
        <if test="isOnline != null and isOnline != ''">
        TIMESTAMPDIFF(SECOND,st.online_change_time,NOW()) as order_day,
        IF(floor(TIMESTAMPDIFF(SECOND,st.online_change_time,NOW())/24/60/60)>0,concat( floor(TIMESTAMPDIFF(SECOND,st.online_change_time,NOW())/24/60/60),'天'),
        IF(floor((TIMESTAMPDIFF(SECOND,st.online_change_time,NOW())/60/60)%24)>0,concat(floor( (TIMESTAMPDIFF(SECOND,st.online_change_time,NOW())/60/60)%24),
        '小时'),IF(floor((TIMESTAMPDIFF(SECOND,st.online_change_time,NOW())/60)%60)>0,concat(floor( (TIMESTAMPDIFF(SECOND,st.online_change_time,NOW())/60)%60),'分钟'),""))) as number_day
        </if>
        <if test="isStasis != null and isStasis != ''">
            TIMESTAMPDIFF(DAY,st.anchor_change_time,NOW()) as number_day
        </if>
        from ax_beidou_ship_status st
        inner join ax_beidou_ship_archives archives on st.device_id = archives.devide_no
        inner join ax_beidou_dept dept on archives.police_station_id = dept.pscbh
        LEFT JOIN ax_beidou_ship_location location on archives.devide_no = location.device_id
        where  1=1
        <if test="isOnline != null and isOnline != ''">
            and st.is_online = #{isOnline}
        </if>
        <if test="isStasis != null and isStasis != ''">
            and st.is_anchor = #{isStasis}
            and TIMESTAMPDIFF(MINUTE,st.anchor_change_time,NOW())>24*60*7
        </if>
        <if test="shipId != null and shipId != ''">and (archives.ship_id like concat('%', #{shipId},'%') or archives.devide_no like concat('%', #{devideNo},'%'))</if>
        <if test="deptId != null and deptId != ''">and dept.xw_dept_id like concat('%',#{deptId},'%')</if>
        <if test="stasisDay == 1">
            and st.anchor_change_time BETWEEN DATE_SUB(NOW(),INTERVAL 15 DAY) AND DATE_SUB(NOW(),INTERVAL 7 DAY)
        </if>
        <if test="stasisDay == 2">
            and st.anchor_change_time BETWEEN DATE_SUB(NOW(),INTERVAL 30 DAY) AND DATE_SUB(NOW(),INTERVAL 15 DAY)
        </if>
        <if test="stasisDay == 3">
            and st.anchor_change_time &lt; DATE_SUB(NOW(),INTERVAL 30 DAY)
        </if>
        <if test="offlineDay == 1">
            and st.online_change_time BETWEEN DATE_SUB(NOW(),INTERVAL 8 DAY) AND NOW()
        </if>
        <if test="offlineDay == 2">
            and st.online_change_time BETWEEN DATE_SUB(NOW(),INTERVAL 30 DAY) AND DATE_SUB(NOW(),INTERVAL 8 DAY)
        </if>
        <if test="offlineDay == 3">
            and st.online_change_time &lt; DATE_SUB(NOW(),INTERVAL 30 DAY)
        </if>
        <if test="sortField != null and sortField != ''">ORDER BY ${sortField} </if>
        <if test="sortType != null and sortType != ''">${sortType}</if>
        <if test="sortField == null and sortField == ''">ORDER BY  st.online_change_time DESC</if>
    </select>

    <select id="stasisShipTotal" parameterType="string" resultType="stasisShipStatistics">
        select count(1) as shipTotal,
        SUM(CASE  WHEN st.anchor_change_time BETWEEN DATE_SUB(NOW(),INTERVAL 15 DAY) AND DATE_SUB(NOW(),INTERVAL 7 DAY) THEN 1 ELSE 0 END) as smallDay,
        SUM(CASE  WHEN st.anchor_change_time BETWEEN DATE_SUB(NOW(),INTERVAL 30 DAY) AND DATE_SUB(NOW(),INTERVAL 15 DAY) THEN 1 ELSE 0 END) as middleDay,
        SUM(CASE  WHEN st.anchor_change_time &lt; DATE_SUB(NOW(),INTERVAL 30 DAY) THEN 1 ELSE 0 END) as largeDay
        from ax_beidou_ship_status st
        inner join ax_beidou_ship_archives archives on st.device_id = archives.devide_no
        <if test="deptId != null and deptId != ''">
        inner join ax_beidou_dept dept on archives.police_station_id = dept.pscbh
        </if>
        where st.is_anchor='1' and TIMESTAMPDIFF(MINUTE,st.anchor_change_time,NOW())>24*60*7
        <if test="shipId != null and shipId != ''">and (archives.ship_id like concat('%', #{shipId},'%') or archives.devide_no like concat('%', #{devideNo},'%'))</if>
        <if test="deptId != null and deptId != ''">and dept.xw_dept_id like concat('%',#{deptId},'%')</if>
    </select>


    <select id="offlineShipTotal" parameterType="string" resultType="offlineShipStatistics">
        select count(1) as shipTotal,
        SUM(CASE  WHEN st.online_change_time BETWEEN DATE_SUB(NOW(),INTERVAL 8 DAY) AND NOW() THEN 1 ELSE 0 END) as smallDay,
        SUM(CASE  WHEN st.online_change_time BETWEEN DATE_SUB(NOW(),INTERVAL 30 DAY) AND DATE_SUB(NOW(),INTERVAL 8 DAY) THEN 1 ELSE 0 END) as middleDay,
        SUM(CASE  WHEN st.online_change_time &lt; DATE_SUB(NOW(),INTERVAL 30 DAY) THEN 1 ELSE 0 END) as largeDay
        from ax_beidou_ship_status st
        inner join ax_beidou_ship_archives archives on st.device_id = archives.devide_no
        <if test="deptId != null and deptId != ''">
        inner join ax_beidou_dept dept on archives.police_station_id = dept.pscbh
        </if>
        where st.is_online='OFFLINE'
        <if test="shipId != null and shipId != ''">and (archives.ship_id like concat('%', #{shipId},'%') or archives.devide_no like concat('%', #{devideNo},'%'))</if>
        <if test="deptId != null and deptId != ''">and dept.xw_dept_id like concat('%',#{deptId},'%')</if>
    </select>

    <update id="updateMark" parameterType="string">
        update ax_beidou_ship_status set is_online = #{mark} where device_id = #{devideNo}
    </update>

</mapper>