<?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.archives.mapper.ShipArchivesMapper">

    <resultMap type="shipArchivesData" id="ShipResult">
        <id     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="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="lastStatus"    column="status"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="location"    column="location"    />
        <result property="kwh" column="kwh"/>
    </resultMap>

    <resultMap id="disposeResult" type="ShipDisposeResult">
        <id property="id" column="id"></id>
        <result property="warningId" column="warning_id"/>
        <result property="warningTime" column="warning_time"/>
        <result property="disposeType" column="dispose_type"/>
        <result property="disposeSuggestion" column="dispose_suggestion"/>
        <result property="disposePicture" column="dispose_picture"/>
        <result property="disposeUserId" column="dispose_user_id"/>
        <result property="disposeTime" column="dispose_time"/>
    </resultMap>

    <select id="selectByDevideNo" parameterType="string" resultMap="ShipResult">
        select * from ax_bd_ship_archives where devide_no=#{devideNo}
    </select>

    <update id="updateById" parameterType="shipArchivesData" >
        update ax_beidou_ship_archives
        <set>
            <if test="shipId != null and shipId != ''">ship_id = #{shipId},</if>
            <if test="shipType != null and shipType != ''">ship_type = #{shipType},</if>
            <if test="jobType != null and jobType != ''">job_type = #{jobType},</if>
            <if test="jobWay != null and jobWay != ''">job_way = #{jobWay},</if>
            <if test="ownerName != null and ownerName != ''">owner_name = #{ownerName},</if>
            <if test="ownerTelNo != null and ownerTelNo != ''">owner_tel_no = #{ownerTelNo},</if>
            <if test="shipLength != null">ship_length = #{shipLength},</if>
            <if test="shipWidth != null">ship_width = #{shipWidth},</if>
            <if test="shipMaterial != null">ship_material = #{shipMaterial},</if>
            <if test="policeStationId != null and policeStationId != ''">police_station_id = #{policeStationId},</if>
            <if test="ownerCompany != null and ownerCompany != ''">owner_company = #{ownerCompany},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="modifyTime != null">modify_time = #{modifyTime},</if>
            location_time = now(),
            <if test="ownerType != null and ownerType != ''">owner_type = #{ownerType}</if>
        </set>
        where devide_no = #{devideNo}
    </update>

    <insert id="insertShip" parameterType="shipArchivesData">
        insert into ax_beidou_ship_archives (
        <if test="shipId != null and shipId != ''">ship_id,</if>
        <if test="devideNo != null and devideNo != ''">devide_no,</if>
        <if test="shipType != null and shipType != ''">ship_type,</if>
        <if test="jobType != null and jobType != ''">job_type,</if>
        <if test="jobWay != null and jobWay != ''">job_way,</if>
        <if test="ownerName != null and ownerName != ''">owner_name,</if>
        <if test="ownerTelNo != null and ownerTelNo != ''">owner_tel_no,</if>
        <if test="shipLength != null">ship_length,</if>
        <if test="shipWidth != null">ship_width,</if>
        <if test="shipMaterial != null">ship_material,</if>
        <if test="policeStationId != null and policeStationId != ''">police_station_id,</if>
        <if test="ownerCompany != null and ownerCompany != ''">owner_company,</if>
        <if test="createTime != null">create_time,</if>
        <if test="modifyTime != null">modify_time,</if>
        location_time,
        <if test="ownerType != null and ownerType != ''">owner_type</if>
        )values(
        <if test="shipId != null and shipId != ''">#{shipId},</if>
        <if test="devideNo != null and devideNo != ''">#{devideNo},</if>
        <if test="shipType != null and shipType != ''">#{shipType},</if>
        <if test="jobType != null and jobType != ''">#{jobType},</if>
        <if test="jobWay != null and jobWay != ''">#{jobWay},</if>
        <if test="ownerName != null and ownerName != ''">#{ownerName},</if>
        <if test="ownerTelNo != null and ownerTelNo != ''">#{ownerTelNo},</if>
        <if test="shipLength != null">#{shipLength},</if>
        <if test="shipWidth != null">#{shipWidth},</if>
        <if test="shipMaterial != null">#{shipMaterial},</if>
        <if test="policeStationId != null and policeStationId != ''">#{policeStationId},</if>
        <if test="ownerCompany != null and ownerCompany != ''">#{ownerCompany},</if>
        <if test="createTime != null">#{createTime},</if>
        <if test="modifyTime != null">#{modifyTime},</if>
        now(),
        <if test="ownerType != null and ownerType != ''">#{ownerType}</if>
        )
    </insert>
    
    
    <select id="selectShipArchivesList" parameterType="shipArchivesREQ" resultMap="ShipResult">
        SELECT
        archives.*,
        ( CASE sta.is_online WHEN 'ONLINE' THEN 1 WHEN 'OFFLINE' THEN 2 WHEN 'MARK' THEN dispose_temp.dispose_type ELSE 2 END ) AS STATUS,
        IFNULL(dispose_temp.dispose_type,'0') as device_status,
        dept.pscmc AS dept_name,
        location.longitude,
        location.latitude,
        location.location,
        location.kwh
        FROM
        ax_beidou_ship_archives archives
        LEFT JOIN ax_beidou_ship_status sta ON archives.devide_no = sta.device_id
        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
        LEFT JOIN (select devide_no,max(id) as id from ax_beidou_ship_dispose GROUP BY devide_no) as dispose on dispose.devide_no = archives.devide_no
        LEFT JOIN ax_beidou_ship_dispose dispose_temp on dispose.id = dispose_temp.id
        where
            1=1
        <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="shipType != null and shipType != ''">and archives.ship_type = #{shipType}</if>
        <if test="online != null and online != ''">and sta.is_online = #{online}</if>
        <if test="inPort != null and inPort != ''">and sta.is_inport = #{inPort}</if>
        ORDER BY devide_no ASC
    </select>


    <select id="selectById" parameterType="string" resultMap="ShipResult">
        select archives.*,
        (CASE sta.is_online WHEN 'ONLINE' THEN 1  WHEN 'OFFLINE' THEN 2 WHEN 'MARK' THEN 3 END) as status,
        IFNULL(dispose.dispose_type,'0') as device_status,location.kwh as kwh,focus.focus_id as focusId,
        dept.pscmc as dept_name
        from ax_beidou_ship_archives archives
        LEFT JOIN ax_beidou_ship_status sta ON archives.devide_no = sta.device_id
        INNER JOIN ax_beidou_dept dept on archives.police_station_id = dept.pscbh
        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
        LEFT JOIN ax_beidou_ship_location location on archives.devide_no = location.device_id
        LEFT JOIN ax_beidou_focus_ship focus on archives.devide_no = focus.devide_no and focus.user_id=#{userId}
        where  archives.devide_no=#{devideNo}
    </select>

    <select id="getShipTotal" parameterType="string" resultType="shipArchivesTotal">
        select COUNT(*) as shipTotal,
            IFNULL(SUM(CASE sta.is_online WHEN 'MARK' THEN 1 ELSE 0 END ),0) as mark,
            IFNULL(SUM(CASE sta.is_online WHEN 'ONLINE' THEN 1 ELSE 0 END ),0) as onlineTotal,
            IFNULL(SUM(CASE sta.is_online WHEN 'OFFLINE' THEN 1 ELSE 0 END ),0) as offlineTotal,
            IFNULL(SUM(CASE sta.is_inport WHEN 'TRUE' THEN 1 ELSE 0 END),0) as inport,
            IFNULL(SUM(CASE sta.is_inport WHEN 'FALSE' THEN 1 ELSE 0 END),0) as offport
        from ax_beidou_ship_archives archives
            INNER JOIN ax_beidou_dept dept ON archives.police_station_id = dept.pscbh
            LEFT JOIN ax_beidou_ship_status sta ON archives.devide_no = sta.device_id
            where 1=1
            <if test="deptId != null and deptId != ''">and dept.xw_dept_id like concat('%',#{deptId},'%')</if>
    </select>


    <select id="selectDisposeById" parameterType="string" resultMap="disposeResult">
        select dis.id,dis.warning_id,dis.dispose_type,dis.dispose_suggestion,dis.dispose_picture,
         dis.dispose_user_id,dis.dispose_time,record.warning_time from ax_beidou_ship_dispose dis
        inner join ax_beidou_warning_record record on dis.warning_id = record.id
        where (record.`start` is null or record.`start` != 'false')
            <if test="warningId != null and warningId != ''">and dis.warning_id = #{warningId}</if>
            <if test="devideNo != null and devideNo != ''">and record.device_id = #{devideNo}</if>
        ORDER BY dispose_time DESC
        limit 1
    </select>


</mapper>