<?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.focus.mapper.FocusShipMapper">

    <resultMap id="focusShipDetail" type="focusShipResult">
        <id     property="focusId"      column="focus_id"      />
        <result property="userId"    column="user_id"    />
        <result property="focusType" column="focus_type"/>
        <result property="focusCreate"     column="focus_create"     />
        <result property="deptName"     column="dept_name"     />
        <result property="shipId"      column="ship_id"      />
        <result property="devideNo"    column="devide_no"    />
        <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="ownerCompany"      column="owner_company"      />
        <result property="createTime"    column="create_time"    />
        <result property="modifyTime"      column="modify_time"      />
        <result property="ownerType"    column="owner_type"    />
        <result property="lastStatus"    column="STATUS"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="location"    column="location"    />
        <result property="kwh" column="kwh"/>
    </resultMap>

    <insert id="insertFocusShip" parameterType="focusShipData">
        insert into ax_beidou_focus_ship(
            focus_id,
            <if test="devideNo != null and devideNo != ''">devide_no,</if>
            <if test="shipId != null and shipId != ''">ship_id,</if>
            <if test="focusType != null and focusType != ''">focus_type,</if>
            <if test="userId != null and userId != ''">user_id,</if>
            create_time
        )values (
            #{focusId},
            <if test="devideNo != null and devideNo != ''">#{devideNo},</if>
            <if test="shipId != null and shipId != ''">#{shipId},</if>
            <if test="focusType != null and focusType != ''">#{focusType},</if>
            <if test="userId != null and userId != ''">#{userId},</if>
            now()
        )
    </insert>

    <delete id="deleteFocusShip" parameterType="long">
        delete from ax_beidou_focus_ship where focus_id=#{focusId}
    </delete>

    <select id="selectById" parameterType="long" resultMap="focusShipDetail">
        select focus.user_id,focus.focus_id as focus_id,focus.focus_type,focus.create_time as focus_create,
            (CASE st.is_online WHEN 'ONLINE' THEN 1 WHEN 'OFFLINE' THEN 2 WHEN 'MARK' THEN 3 ELSE 1 END ) AS STATUS,
            dept.pscmc as dept_name,location.kwh,location.location,IFNULL(dispose.dispose_type,'0') as device_status,
            archives.*
            from ax_beidou_ship_archives archives
            INNER JOIN ax_beidou_focus_ship focus on archives.devide_no = focus.devide_no
            INNER JOIN ax_beidou_dept dept on archives.police_station_id = dept.pscbh
            INNER JOIN ax_beidou_ship_status st on st.device_id = focus.devide_no
            LEFT JOIN ax_beidou_ship_location location on focus.devide_no = location.device_id
            LEFT JOIN (select id,devide_no,dispose_type from ax_beidou_ship_dispose
            where devide_no=#{devideNo} ORDER BY id DESC limit 1) as dispose on dispose.devide_no = archives.devide_no
            where focus.focus_id = #{focusId}
    </select>

    <select id="selectList" parameterType="focusShipReq"  resultMap="focusShipDetail">
        select focus.user_id,focus.focus_id,focus.focus_type,focus.create_time as focus_create,dept.pscmc as dept_name,
        (CASE st.is_online WHEN 'ONLINE' THEN 1 WHEN 'OFFLINE' THEN 2 WHEN 'MARK' THEN 3 ELSE 1 END ) AS STATUS,
        location.kwh,location.location,archives.*
        from ax_beidou_ship_archives archives
        INNER JOIN ax_beidou_focus_ship focus on archives.devide_no = focus.devide_no
        INNER JOIN ax_beidou_dept dept on archives.police_station_id = dept.pscbh
        LEFT JOIN ax_beidou_ship_status st on st.device_id = focus.devide_no
        LEFT JOIN ax_beidou_ship_location location on focus.devide_no = location.device_id
        where 1=1
        <if test="shipId != null and shipId != ''">and (archives.ship_id like concat('%', #{shipId},'%') or archives.devide_no like concat('%', #{shipId},'%'))</if>
        <if test="deptId != null and deptId != ''">and dept.xw_dept_id like concat('%',#{deptId},'%')</if>
        <if test="userId != null and userId != ''">and focus.user_id=#{userId}</if>
        <if test="sortField != null and sortField != ''">ORDER BY ${sortField} </if>
        <if test="sortType != null and sortType != ''">${sortType}</if>
        <if test="sortField == null or sortField == ''">ORDER BY  focus.create_time DESC</if>
    </select>

    <select id="selectByDevideNoUserId" parameterType="string" resultType="integer">
        select count(1) from ax_beidou_focus_ship where devide_no = #{devideNo} and user_id = #{userId}
    </select>

    <select id="getShipByUserId" parameterType="string" resultType="simpleFocusShipResult">
        select focus.focus_id as focusId,archives.ship_id as shipId,focus.focus_type as focusType
        from ax_beidou_focus_ship focus
        inner join ax_beidou_ship_archives archives on archives.devide_no = focus.devide_no
        where focus.user_id = #{userId} order by focus.create_time desc
    </select>

</mapper>