<?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.lutao.cloud.oil.mapper.PlatformInfoMapper">

    <resultMap id="platformInfoMap" type="com.lutao.entity.PlatformInfo">
        <id property="id" column="id"/>
        <result property="originId" column="origin_id"/>
        <result property="appId" column="app_id"/>
        <result property="platformName" column="platform_name"/>
        <result property="platformCode" column="platform_code"/>
        <result property="platformType" column="platform_type"/>
        <result property="platformLat" column="platform_lat"/>
        <result property="platformLng" column="platform_lng"/>
        <result property="lifeboatQuantity" column="lifeboat_quantity"/>
        <result property="personnelLifeboatQuantity" column="personnel_lifeboat_quantity"/>
        <result property="liferaftQuantity" column="liferaft_quantity"/>
        <result property="personnelLiferaftQuantity" column="personnel_liferaft_quantity"/>
        <result property="personnelLqQuantity" column="personnel_lq_quantity"/>
        <result property="createDate" column="create_date"/>
        <result property="createBy" column="create_by"/>
        <result property="updateDate" column="update_date"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>
    <select id="selectByPlatformNameList" resultType="com.lutao.cloud.oil.pojo.vo.PlatformInfoVO">
        SELECT pi.app_id AS appId,
        pi.platform_name AS platformName,
        pi.platform_code AS platformId
        FROM platform_info pi
        <where>
            pi.delete_flag = '0'
            <if test="platformName != null and platformName != ''">
               and pi.platform_name like CONCAT('%',#{platformName},'%')
            </if>
        </where>
    </select>
    <select id="selectMapDisplayList" resultType="com.lutao.cloud.oil.pojo.vo.MapDisplayVO">


        SELECT
        p.*,
        w.*
        FROM
        platform_info p
        LEFT JOIN ( SELECT * FROM atmosphere_monitor_info WHERE ( platform_code, create_time ) IN ( SELECT
        platform_code, MAX( create_time ) FROM atmosphere_monitor_info GROUP BY platform_code ) ) w ON
        p.platform_code = w.platform_code


        -- SELECT DISTINCT pi.platform_name AS platformName,
        -- pi.platform_lat AS platformLat,
        -- pi.platform_lng AS platformLng,
        -- ati.wind_speed AS windSpeed,
        -- ati.wind_direction AS windDirection,
        -- ati.pressure AS pressure,
        -- ati.temp AS temp
        --
        -- FROM platform_info pi
        -- LEFT JOIN atmosphere_monitor_info ati on pi.platform_code = ati.platform_code
        -- WHERE platform_lat IS NOT NULL AND platform_lng IS NOT NULL
        <where>
            p.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
               and p.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and p.app_id = #{param.appId}
            </if>
        </where>
        ORDER BY temp IS NOT NULL DESC
    </select>
    <select id="selectAppInfo" resultType="com.lutao.cloud.oil.pojo.vo.AppInfoVO">
        SELECT id       AS appId,
               app_name AS appName
        FROM app_info
    </select>
    <select id="selectByAppId" resultType="com.lutao.cloud.oil.pojo.vo.PlatformInfoVO">
        SELECT CONCAT(pi.platform_name,'-',pi.platform_code) AS platformName,
        pi.platform_code AS platformCode,
        pi.oil_gas_name AS oilGasName,
        pi.operating_company AS operatingCompany
        FROM platform_info pi
        <where>
            pi.delete_flag = '0'
            and pi.is_count = '0'
            <if test="appId != null and appId != ''">
               and pi.app_id = #{appId}
            </if>
        </where>
    </select>
    <select id="selectPlatformEquipInfo" resultType="com.lutao.cloud.oil.pojo.vo.SeaPlatformEquipmentInfoVO">
        SELECT
        platform_name AS platformName,
        oil_gas_name AS oilGasName,
        operating_company AS operatingCompany,
        IFNULL(personnel_lifeboat_quantity,0) AS lifeboatQuantity,
        IFNULL(personnel_liferaft_quantity,0) AS lifeRaftQuantity,
        personnel_quantity AS peopleLivingNumber,
        ai.app_name AS appName
        FROM platform_info pi LEFT JOIN app_info ai ON pi.app_id = ai.id
        <where>
            pi.delete_flag = '0'
            and pi.is_count = '0'
            <if test="param.platformId != null and param.platformId != ''">
               and pi.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and pi.app_id = #{param.appId}
            </if>
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
                and pi.operating_company = #{param.operatingCompany}
            </if>
            <if test="param.oilGasName != null and param.oilGasName != ''">
                and pi.oil_gas_name = #{param.oilGasName}
            </if>
        </where>
        ORDER BY pi.app_id,pi.oil_gas_name,pi.operating_company,pi.platform_name
    </select>
    <select id="getSeaPlatformPersonAndEquipmentVO"
            resultType="com.lutao.cloud.oil.pojo.vo.SeaPlatformPersonAndEquipmentVO">
        SELECT
        IFNULL(SUM( personnel_quantity ),0) AS peopleLivingNumber,
        IFNULL(SUM( personnel_lifeboat_quantity ),0) AS lifeboatQuantity,
        IFNULL(SUM( personnel_liferaft_quantity ),0) AS lifeRaftQuantity
        FROM platform_info pi
        <where>
            pi.delete_flag = '0'
            and pi.is_connected_platform = '0'
            <if test="param.platformId != null and param.platformId != ''">
               and pi.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and pi.app_id = #{param.appId}
            </if>
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
                and pi.operating_company = #{param.operatingCompany}
            </if>
            <if test="param.oilGasName != null and param.oilGasName != ''">
                and pi.oil_gas_name = #{param.oilGasName}
            </if>
        </where>
    </select>
    <select id="selectPlatformInfoCount" resultType="com.lutao.cloud.oil.pojo.vo.PlatformInfoCountVO">
        SELECT
            COUNT(*) AS platformCount,
            app_name AS appName
        FROM
            platform_info pi
                LEFT JOIN app_info ai ON pi.app_id = ai.id
        <where>
            pi.delete_flag = '0'
            <if test="param.appId != null and param.appId != ''">
               and pi.app_id = #{param.appId}
            </if>
        </where>
        GROUP BY
            ai.app_name

    </select>
</mapper>
