<?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.HighRiskOperationMapper">

    <resultMap id="highRiskOperationMap" type="com.lutao.entity.HighRiskOperation">
        <id property="id" column="id"/>
        <result property="originId" column="origin_id"/>
        <result property="taskType" column="task_type"/>
        <result property="taskNumber" column="task_number"/>
        <result property="taskBeginTime" column="task_begin_time"/>
        <result property="taskEndTime" column="task_end_time"/>
        <result property="taskUnit" column="task_unit"/>
        <result property="majorPerson" column="major_person"/>
        <result property="expirationDate" column="expiration_date"/>
        <result property="personQualificationId" column="person_qualification_id"/>
        <result property="taskContent" column="task_content"/>
        <result property="createDate" column="create_date"/>
        <result property="createBy" column="create_by"/>
        <result property="updateDate" column="update_date"/>
        <result property="updateBy" column="update_by"/>
        <result property="pfId" column="pf_id"/>
        <result property="appId" column="app_id"/>
        <result property="platformCode" column="platform_code"/>
        <result property="platformName" column="platform_name"/>
        <result property="platformId" column="platform_id"/>
    </resultMap>
    <select id="getHighRiskWork" resultType="com.lutao.cloud.oil.pojo.vo.HighRiskWorkVO">
        SELECT
        CASE
        WHEN task_type = '1' THEN '热工作业'
        WHEN task_type = '2' THEN '受限空间作业'
        WHEN task_type = '3' THEN '大型起重作业'
        WHEN task_type = '4' THEN '钻井作业'
        WHEN task_type = '5' THEN '修井作业'
        WHEN task_type = '6' THEN '潜水作业'
        WHEN task_type = '7' THEN '电气作业'
        WHEN task_type = '8' THEN '高处（舷外、脚手架）作业'
        WHEN task_type = '9' THEN '爆炸放射性及有毒有害作业'
        WHEN task_type = '10' THEN '挖掘作业'
        WHEN task_type = '11' THEN '联合作业'
        ELSE '其他'
        END AS workType,
        COUNT( task_type ) AS workNumber,
        ROUND(
        SUM( task_number ) / COUNT( task_type )
        ) AS workPersonNumber
        FROM
        high_risk_operation hro
        <where>
            <if test="param.platformId != null and param.platformId != ''">
                hro.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and hro.app_id = #{param.appId}
            </if>
            <if test="param.date != null and param.date != ''">
                and DATE_FORMAT(#{param.date},'%Y-%m-%d') between DATE_FORMAT(hro.task_begin_time,'%Y-%m-%d') and
                DATE_FORMAT(hro.task_end_time,'%Y-%m-%d')
            </if>
            <if test="param.startDate != null">
                and hro.task_end_time &gt;= #{param.startDate}
            </if>
            <if test="param.endDate != null">
                and hro.task_begin_time &lt;= #{param.endDate}
            </if>
        </where>
        GROUP BY workType
        ORDER BY workNumber DESC;
    </select>
    <select id="getHighRiskArea" resultType="com.lutao.cloud.oil.pojo.vo.HighRiskAreaVO">
        SELECT
        pi.platform_name AS areaName,
        COUNT(*) AS workNumber,
        MAX( pi.oil_gas_name ) AS oilGasName,
        MAX( pi.operating_company ) AS operationCompany,
        MAX( ai.app_name ) AS appName
        FROM
        high_risk_operation hro
        LEFT JOIN platform_info pi ON hro.platform_code = pi.platform_code
        LEFT JOIN app_info ai ON hro.app_id = ai.id
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and hro.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and hro.app_id = #{param.appId}
            </if>
            <if test="param.date != null and param.date != ''">
                and DATE_FORMAT(#{param.date},'%Y-%m-%d') between DATE_FORMAT(hro.task_begin_time,'%Y-%m-%d') and
                DATE_FORMAT(hro.task_end_time,'%Y-%m-%d')
            </if>
            <if test="param.endDate != null">
                and hro.task_begin_time &lt;= #{param.endDate}
            </if>
            <if test="param.startDate != null">
                and hro.task_end_time &gt;= #{param.startDate}
            </if>
        </where>
        GROUP BY pi.platform_name
        ORDER BY workNumber DESC
    </select>
    <select id="getHighRiskOperationListToday" resultType="com.lutao.entity.HighRiskOperation">
        select * from high_risk_operation hro
        <where>
            <if test="param.platformId != null and param.platformId != ''">
                hro.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and hro.app_id = #{param.appId}
            </if>
            <if test="true">
                and DATE_FORMAT(NOW(),'%Y-%m-%d') between DATE_FORMAT(hro.task_begin_time,'%Y-%m-%d') and
                DATE_FORMAT(hro.task_end_time,'%Y-%m-%d')
            </if>


        </where>
    </select>
    <select id="getHighRiskCountByType" resultType="com.lutao.cloud.oil.pojo.vo.HighRiskCountByTypeVO">
        SELECT CASE
        WHEN
        task_type = '1' THEN
        '热工作业'
        WHEN task_type = '2' THEN
        '受限空间作业'
        WHEN task_type = '3' THEN
        '大型起重作业'
        WHEN task_type = '4' THEN
        '钻井作业'
        WHEN task_type = '5' THEN
        '修井作业'
        WHEN task_type = '6' THEN
        '潜水作业'
        WHEN task_type = '7' THEN
        '电气作业'
        WHEN task_type = '8' THEN
        '高处（舷外、脚手架）作业'
        WHEN task_type = '9' THEN
        '爆炸放射性及有毒有害作业'
        WHEN task_type = '10' THEN
        '挖掘作业'
        WHEN task_type = '11' THEN
        '联合作业'
        ELSE '其他'
        END AS workType,
        COUNT(task_type) AS workNumber
        FROM high_risk_operation hro
        LEFT JOIN platform_info pi ON hro.platform_code = pi.platform_code
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and hro.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and hro.app_id = #{param.appId}
            </if>
            <if test="true">

                and hro.task_begin_time &lt;= #{param.endDate}
                and hro.task_end_time &gt;= #{param.startDate}
            </if>
        </where>
        GROUP BY workType
        ORDER BY workNumber DESC;
    </select>
    <select id="highRiskWorkTypeByPlatformBar"
            resultType="com.lutao.cloud.oil.pojo.vo.HighRiskCountByPlatformVO">
        SELECT
        pi.platform_name AS platformName,
        COUNT(*) AS workNumber
        FROM
        high_risk_operation hro
        LEFT JOIN platform_info pi ON hro.platform_code = pi.platform_code

        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
               and hro.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
               and hro.app_id = #{param.appId}
            </if>
            <if test="true">

                and hro.task_begin_time &lt;= #{param.endDate}
                and hro.task_end_time &gt;= #{param.startDate}
            </if>
        </where>
        GROUP BY
        platformName
        ORDER BY
        workNumber DESC
        LIMIT #{param.limit}
    </select>
    <select id="getHighRiskOperationsDetail"
            resultType="com.lutao.cloud.oil.pojo.vo.HighRiskOperationsDetailVO">
        SELECT
        CASE
        WHEN hro.task_type = '1' THEN '热工作业'
        WHEN hro.task_type = '2' THEN '受限空间作业'
        WHEN hro.task_type = '3' THEN '大型起重作业'
        WHEN hro.task_type = '4' THEN '钻井作业'
        WHEN hro.task_type = '5' THEN '修井作业'
        WHEN hro.task_type = '6' THEN '潜水作业'
        WHEN hro.task_type = '7' THEN '电气作业'
        WHEN hro.task_type = '8' THEN '高处（舷外、脚手架）作业'
        WHEN hro.task_type = '9' THEN '爆炸放射性及有毒有害作业'
        WHEN hro.task_type = '10' THEN '挖掘作业'
        WHEN hro.task_type = '11' THEN '联合作业'
        ELSE '其他'
        END AS workType,
            hro.task_number AS workNumber,
            hro.task_begin_time AS taskBeginTime,
            hro.task_end_time AS taskEndTime,
            hro.task_content AS taskContent,
            ai.app_name,
            pi.platform_name AS areaName,
            pi.oil_gas_name AS oilGasName,
            operating_company AS operationCompany
        FROM
            high_risk_operation hro
                LEFT JOIN platform_info pi ON hro.platform_code = pi.platform_code
                LEFT JOIN app_info ai ON hro.app_id = ai.id
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
               and hro.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and hro.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>
            <if test="param.startTime != null">
                and hro.task_begin_time &gt;= #{param.startTime}
            </if>
            <if test="param.endTime != null">
                and hro.task_begin_time &lt;= #{param.endTime}
            </if>
        </where>
        ORDER BY
            hro.task_end_time DESC
    </select>
    <select id="getWorkCountByPlatform" resultType="java.lang.Long">
        select count(*) from high_risk_operation hro
        left join platform_info pi on hro.platform_code = pi.platform_code
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and hro.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and hro.app_id = #{param.appId}
            </if>
            and hro.task_begin_time &lt;= #{param.endDate}
            and hro.task_end_time &gt;= #{param.startDate}
        </where>
    </select>
</mapper>
