<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.3//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--suppress SqlShadowingAlias -->
<mapper namespace="com.zw.platform.basic.repository.ShipInfoDao">

    <insert id="insertShipInfo">
        INSERT INTO `zw_m_ship_info` (`id`, `number`, `call_sign`, `status`, `alias`, `displacement`,
        `tonnage`, `netTonnage`, `contacts`, `telephone`, `port_of_registry`, `install_time`, `remark`, `photo_path`,
        `org_id`, `create_data_username`) VALUES
        (#{id}, #{number}, #{callSign}, #{status}, #{alias}, #{displacement}, #{tonnage}, #{netTonnage}, #{contacts},
         #{telephone}, #{portOfRegistry}, #{installTime}, #{remark}, #{photoPath}, #{orgId}, #{createDataUsername})
    </insert>

    <insert id="addByBatch">
        INSERT INTO `zw_m_ship_info` (`id`, `number`, `call_sign`, `status`, `alias`, `displacement`,
        `tonnage`, `netTonnage`, `contacts`, `telephone`, `port_of_registry`, `install_time`, `remark`, `photo_path`,
        `org_id`, `create_data_username`) VALUES
        <foreach collection="shipList" item="item" separator=",">
            (#{item.id}, #{item.number}, #{item.callSign}, #{item.status}, #{item.alias}, #{item.displacement}, #{item.tonnage},
            #{item.netTonnage}, #{item.contacts},#{item.telephone}, #{item.portOfRegistry}, #{item.installTime}, #{item.remark},
            #{item.photoPath}, #{item.orgId}, #{item.createDataUsername})
        </foreach>
    </insert>

    <update id="updateShipInfo">
        UPDATE `zw_m_ship_info`
        <set>
            <if test="number != null and number != ''">`number` = #{number},</if>
            <if test="orgId != null and orgId != ''">`org_id` = #{orgId},</if>
            `call_sign` = #{callSign},
            `status` = #{status},
            `alias` = #{alias},
            `displacement` = #{displacement},
            `tonnage` = #{tonnage},
            `netTonnage` = #{netTonnage},
            `contacts` = #{contacts},
            `telephone` = #{telephone},
            `port_of_registry` = #{portOfRegistry},
            `install_time` = #{installTime},
            `remark` = #{remark},
            `photo_path` = #{photoPath},
            `update_data_time` = #{updateDataTime},
            `update_data_username` = #{updateDataUsername}
        </set>
        WHERE `id` = #{id} AND flag = 1
    </update>

    <update id="deleteShipById">
        update zw_m_ship_info set flag = 0
        WHERE id IN
        <foreach collection="ids" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <update id="updateInstallDate">
        update zw_m_ship_info set install_time = now() where id = #{id} AND flag = 1
    </update>

    <update id="updateNumber">
        update zw_m_ship_info
        set number = #{number}
        where id = #{id}
    </update>

    <update id="updateIcon">
        UPDATE zw_m_ship_info
        SET ship_icon = #{iconId}
        WHERE flag = 1
        AND id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getByNumber" resultType="com.zw.platform.basic.domain.ShipDO">
        SELECT
            *
        FROM zw_m_ship_info
        WHERE number = BINARY #{number}
        and flag = 1
        limit 1
    </select>

    <select id="getDetailById" resultType="com.zw.platform.basic.domain.ShipDO">
        SELECT
            *
        FROM zw_m_ship_info
        WHERE id = #{id}
        AND flag = 1
    </select>

    <select id="getDetailByIds" resultType="com.zw.platform.basic.dto.ShipDTO">
        SELECT ship.id, ship.number AS `name`,ship.call_sign,ship.status,ship.alias,ship.displacement,ship.tonnage,
        ship.netTonnage,ship.contacts,ship.telephone,ship.port_of_registry,ship.install_time,ship.remark,
        ship.photo_path,ship.org_id,'3' as monitorType,
        case WHEN cofig.vehicle_id<![CDATA[<>]]>'' then 1 else 0 end as bindType,cofig.id as
        configId,cofig.device_id,device.device_number,
        cofig.sim_card_id,simcard.simcard_number as simCardNumber, simcard.real_id
        realSimCardNumber,device.device_type,device.functional_type,
        device.terminal_type_id,terminal.terminal_type,terminal.terminal_manufacturer,
        GROUP_CONCAT(DISTINCT assign.assignment_id) groupId,
        GROUP_CONCAT(DISTINCT asm.`name`) groupName,
        DATE_FORMAT(cofig.create_data_time,'%Y-%m-%d %H:%i:%S') AS bindDate, DATE_FORMAT(cofig.update_data_time,'%Y-%m-%d') AS
        updateBindDate,
        DATE_FORMAT(service.billing_date,'%Y-%m-%d') AS billing_date,DATE_FORMAT(service.expire_date,'%Y-%m-%d') AS
        expire_date, cofig.service_lifecycle_id,cofig.intercom_info_id, device.org_id
        deviceOrgId,simcard.org_id simCardOrgId,simcard.auth_code, cofig.vehicle_password,
        terminal.support_video_flag AS isVideo,device.manufacturer_id
        from zw_m_ship_info ship
        LEFT JOIN zw_m_config cofig on ship.id = cofig.vehicle_id and cofig.flag = 1
        LEFT JOIN zw_m_device_info device on cofig.device_id = device.id
        LEFT JOIN zw_m_sim_card_info simcard on cofig.sim_card_id = simcard.id
        LEFT JOIN zw_m_terminal_type terminal on device.terminal_type_id = terminal.id and terminal.flag=1
        LEFT JOIN zw_m_assignment_vehicle assign on ship.id = assign.vehicle_id and assign.flag = 1
        LEFT JOIN zw_m_assignment asm on assign.assignment_id = asm.id and asm.flag = 1
        LEFT JOIN zw_m_service_lifecycle service on cofig.service_lifecycle_id = service.id
        where ship.flag = 1
        and ship.id in
        <foreach item="item" collection="ids" separator="," open="(" close=")">
            #{item}
        </foreach>
        group by ship.id
    </select>

    <select id="getDetailByNumber" resultType="com.zw.platform.basic.dto.ShipDTO">
        SELECT ship.id, ship.number AS `name`,ship.call_sign,ship.status,ship.alias,ship.displacement,ship.tonnage,
        ship.netTonnage,ship.contacts,ship.telephone,ship.port_of_registry,ship.install_time,ship.remark,
        ship.photo_path,ship.org_id,'3' asmonitorType,
        case WHEN cofig.vehicle_id<![CDATA[<>]]>'' then 1 else 0 end as bindType,cofig.id as
        configId,cofig.device_id,device.device_number,
        cofig.sim_card_id,simcard.simcard_number as simCardNumber, simcard.real_id
        realSimCardNumber,device.device_type,device.functional_type,
        device.terminal_type_id,terminal.terminal_type,terminal.terminal_manufacturer,
        GROUP_CONCAT(DISTINCT assign.assignment_id) groupId,GROUP_CONCAT(DISTINCT asm.`name`) groupName,
        DATE_FORMAT(cofig.create_data_time,'%Y-%m-%d') AS bindDate, DATE_FORMAT(cofig.update_data_time,'%Y-%m-%d') AS
        updateBindDate,
        DATE_FORMAT(service.billing_date,'%Y-%m-%d') AS billing_date,DATE_FORMAT(service.expire_date,'%Y-%m-%d') AS
        expire_date, cofig.service_lifecycle_id,cofig.intercom_info_id, device.org_id
        deviceOrgId,simcard.org_id simCardOrgId,simcard.auth_code, cofig.vehicle_password,
        terminal.support_video_flag AS isVideo,device.manufacturer_id
        from zw_m_ship_info ship
        LEFT JOIN zw_m_config cofig on ship.id = cofig.vehicle_id and cofig.flag = 1
        LEFT JOIN zw_m_device_info device on cofig.device_id = device.id
        LEFT JOIN zw_m_sim_card_info simcard on cofig.sim_card_id = simcard.id
        LEFT JOIN zw_m_terminal_type terminal on device.terminal_type_id = terminal.id and terminal.flag=1
        LEFT JOIN zw_m_assignment_vehicle assign on ship.id = assign.vehicle_id and assign.flag = 1
        LEFT JOIN zw_m_assignment asm on assign.assignment_id = asm.id and asm.flag = 1
        LEFT JOIN zw_m_service_lifecycle service on cofig.service_lifecycle_id = service.id
        WHERE ship.flag = 1 AND ship.number = #{number} group by ship.id
    </select>

    <select id="getSortList" resultType="java.lang.String">
        SELECT id from zw_m_ship_info where flag = 1 ORDER BY create_data_time
    </select>

    <select id="getByNumbers" resultType="com.zw.platform.basic.dto.MonitorBaseDTO">
        SELECT ship.id,ship.alias,ship.org_id,ship.number AS `name`,IF(c.vehicle_id IS NULL, '0', '1') AS bindType,
        IF(c.intercom_info_id IS NULL, '0', '1') AS intercomBindType
        FROM zw_m_ship_info ship
        LEFT JOIN zw_m_config c ON ship.id = c.vehicle_id and c.flag = 1
        WHERE ship.flag = 1
        <if test="numberList != null and numberList.size() > 0">
            AND ship.number IN
            <foreach collection="numberList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getIconList" resultType="com.zw.platform.basic.domain.ShipIconDO">
        SELECT ship.id AS shipId,
               icon.ico_name AS icoName
        FROM zw_m_ship_info ship
        INNER JOIN zw_c_ico_config icon on ship.ship_icon = icon.id
        WHERE ship.flag = 1
    </select>
</mapper>