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

    <resultMap id="equipmentInfoMap" type="com.lutao.entity.EquipmentInfo">
        <id property="id" column="id"/>
        <result property="originId" column="origin_id"/>
        <result property="equipName" column="equip_name"/>
        <result property="equipCode" column="equip_code"/>
        <result property="equipType" column="equip_type"/>
        <result property="inspectionType" column="inspection_type"/>
        <result property="inspectionDescription" column="inspection_description"/>
        <result property="periodicInspectionDate" column="periodic_inspection_date"/>
        <result property="yearInspectionDate" column="year_inspection_date"/>
        <result property="nextPeriodicInspectionDate" column="next_periodic_inspection_date"/>
        <result property="nextYearInspectionDate" column="next_year_inspection_date"/>
        <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"/>
        <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="selectEquipmentNameAndNumber"
            resultType="com.lutao.cloud.oil.pojo.vo.EquipmentNameAndNumberVO">
        SELECT ai.app_name AS platformName, count(*) as equipmentNumber
        FROM prof_equipment_info ei
        -- LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code
        LEFT JOIN app_info ai ON ei.app_id = ai.id
        <where>
            <if test="param.platformId != null and param.platformId != ''">
                ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
        GROUP BY ei.app_id
    </select>
    <select id="selectEquipTypePage" resultType="com.lutao.cloud.oil.pojo.vo.EquipmentTypeVO">
        SELECT ei.prof_equip_name AS equipName,
        -- ei.equip_type AS equipType,
        et.type_name AS equipType,
        CASE

        WHEN ei.inspection_type = 1 THEN
        '定检'
        WHEN ei.inspection_type = 2 THEN
        '年检'
        END AS inspectionType,
        pi.oil_gas_name AS oilGasName,
        pi.operating_company AS operatingCompany,
        pi.platform_name AS platformName,
        ai.app_name AS appName,
        ei.inspection_description AS inspectionDescription,
        ei.periodic_inspection_date AS periodicInspectionDate,
        ei.year_inspection_date AS yearInspectionDate,
        ei.next_year_inspection_date AS nextYearInspectionDate,
        ei.next_periodic_inspection_date AS nextPeriodicInspectionDate
        FROM prof_equipment_info ei
        -- LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code
        LEFT JOIN app_info ai ON ei.app_id = ai.id
        LEFT JOIN equipment_type et ON ei.prof_equip_type = et.id
        LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code

        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </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.appId != null and param.appId != ''">
                AND ei.app_id = #{param.appId}
            </if>
            <if test="param.equipType != null">
                AND et.id = #{param.equipType}
            </if>

            <if test="param.checkType != null and param.checkType == 1">
                AND ei.inspection_type like '%2%'
            </if>

            <if test="param.checkType != null and param.checkType == 2">
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.recentThreeMonthFlag != null and param.recentThreeMonthFlag == 1">
                AND next_year_inspection_date &gt; CURRENT_DATE
                AND next_year_inspection_date &lt;= DATE_ADD(CURRENT_DATE, INTERVAL 3 MONTH)
                AND ei.inspection_type LIKE '%2%'
            </if>
            <if test="param.recentThreeMonthFlag != null and param.recentThreeMonthFlag == 2">
                AND next_periodic_inspection_date &gt; CURRENT_DATE
                AND next_periodic_inspection_date &lt;= DATE_ADD(CURRENT_DATE, INTERVAL 3 MONTH)
                AND ei.inspection_type LIKE '%2%'
            </if>
            <if test="param.yearInspectFlag != null and param.yearInspectFlag == 1">
                AND CURDATE() &lt;= ei.next_year_inspection_date
                AND ei.inspection_type like '%2%'
            </if>
            <if test="param.yearInspectFlag != null and param.yearInspectFlag == 2">
                AND CURDATE() &lt;= ei.next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.willExpireFlag != null and param.willExpireFlag == 1">
                and DATEDIFF(DATE(next_year_inspection_date), CURDATE()) &lt; 30
                AND CURDATE() &lt;= next_year_inspection_date
                and ei.inspection_type like '%2%'
            </if>
            <if test="param.willExpireFlag  != null and param.willExpireFlag == 2">
                AND ABS(DATEDIFF(CURDATE(),ei.next_periodic_inspection_date)) &lt; 30
                AND CURDATE() &lt;= next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.expireFlag != null and param.expireFlag == 1">
                AND CURDATE() > ei.next_year_inspection_date
                AND ei.inspection_type like '%2%'
            </if>
            <if test="param.expireFlag != null and param.expireFlag == 2">
                AND CURDATE() > ei.next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
        </where>

    </select>
    <select id="getDistinctEquipTypeCount" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT prof_equip_name)
        FROM prof_equipment_info ei
        <where>
            <if test="param.platformId != null and param.platformId != ''">
                ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
    </select>
    <select id="getEquipmentAnnuallyInspectDistribution"
            resultType="com.lutao.cloud.oil.pojo.vo.YearInspectionDistributionVO">
        SELECT ai.app_name AS platformName, count(*) AS inspectNumber
        FROM prof_equipment_info ei
        LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code
        LEFT JOIN app_info ai ON ei.app_id = ai.id
        <where>
            ei.inspection_type LIKE '%2%'
            and ei.next_year_inspection_date &gt;= CURRENT_DATE
            and pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
        GROUP BY ei.app_id
    </select>
    <select id="getEquipmentPeriodicalInspectDistribution"
            resultType="com.lutao.cloud.oil.pojo.vo.YearInspectionDistributionVO">
        SELECT ai.app_name AS platformName, count(*) AS inspectNumber
        FROM prof_equipment_info ei
        LEFT JOIN app_info ai ON ei.app_id = ai.id
        LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code
        <where>
            ei.inspection_type LIKE '%1%'
            and pi.delete_flag = '0'
            and ei.next_periodic_inspection_date &gt;= CURRENT_DATE
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
        GROUP BY ei.app_id
    </select>
    <select id="getScreenEquipmentNameAndNumber"
            resultType="com.lutao.cloud.oil.pojo.vo.ScreenDisplayEquipmentNameAndNumber">
        SELECT ai.app_name AS equipmentName, count(*) AS equipmentCount
        FROM prof_equipment_info ei LEFT JOIN app_info ai ON ei.app_id = ai.id
        LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
        GROUP BY ei.app_id
    </select>
    <select id="getWillExpireYearInspectNumber" resultType="java.lang.Long">
        SELECT COUNT(*) FROM prof_equipment_info ei
        left join platform_info pi on ei.platform_code = pi.platform_code
        <where>
            DATEDIFF(DATE(next_year_inspection_date), CURDATE()) &lt; 30
            AND CURDATE() &lt;= next_year_inspection_date
            and ei.inspection_type like '%2%'
            and pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
    </select>
    <select id="getExpireYearInspectNumber" resultType="java.lang.Long">
        SELECT COUNT(*) FROM prof_equipment_info ei
        left join platform_info pi on ei.platform_code = pi.platform_code
        <where>
            CURDATE() > ei.next_year_inspection_date
            and ei.inspection_type like '%2%'
            and pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
    </select>
    <select id="getWillExpirePeriodicalInspect" resultType="java.lang.Long">
        SELECT COUNT(*) FROM prof_equipment_info ei
        left join platform_info pi on ei.platform_code = pi.platform_code
        <where>
            DATEDIFF(DATE(next_periodic_inspection_date), CURDATE()) &lt; 30
            AND CURDATE() &lt;= next_periodic_inspection_date
            and ei.inspection_type like '%1%'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
    </select>
    <select id="getExpirePeriodicalInspectNumber" resultType="java.lang.Long">
        SELECT COUNT(*) FROM prof_equipment_info ei
        <where>
            CURDATE() > ei.next_periodic_inspection_date
            and ei.inspection_type like '%1%'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
    </select>
    <select id="selectEquipmentTypeNameList" resultType="com.lutao.cloud.oil.pojo.vo.EquipTypeNameVO">
        SELECT id,
               type_name
        FROM equipment_type
    </select>
    <select id="getRecentThreeMonthYearInspectNumber" resultType="java.lang.Long">
        SELECT count(*)
        FROM prof_equipment_info ei
        <where>
            next_year_inspection_date &gt; CURRENT_DATE
            and next_year_inspection_date &lt;= DATE_ADD(CURRENT_DATE, INTERVAL 3 MONTH)
            and ei.inspection_type LIKE '%2%'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>

    </select>
    <select id="getRecentThreeMonthPeriodicalInspectNumber" resultType="java.lang.Long">
        SELECT count(*)
        FROM prof_equipment_info ei
        <where>
            next_periodic_inspection_date &gt; CURRENT_DATE
            and next_periodic_inspection_date &lt;= DATE_ADD(CURRENT_DATE, INTERVAL 3 MONTH)
            and ei.inspection_type LIKE '%1%'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
    </select>
    <select id="getWillExpireNumber" resultType="java.lang.Long">
        SELECT COUNT(*) FROM prof_equipment_info ei
        <where>
            DATEDIFF(DATE(next_periodic_inspection_date), CURDATE()) &lt; 30
            AND CURDATE() &lt;= next_periodic_inspection_date
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and ei.app_id = #{param.appId}
            </if>
        </where>
    </select>
    <select id="selectEquipTypeCount" resultType="java.lang.Long">
        SELECT count(1)
        FROM prof_equipment_info ei
        -- LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code
        LEFT JOIN app_info ai ON ei.app_id = ai.id
        LEFT JOIN equipment_type et ON ei.prof_equip_type = et.id
        LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code

        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </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.appId != null and param.appId != ''">
                AND ei.app_id = #{param.appId}
            </if>
            <if test="param.equipType != null">
                AND et.id = #{param.equipType}
            </if>

            <if test="param.checkType != null and param.checkType == 1">
                AND ei.inspection_type like '%2%'
            </if>

            <if test="param.checkType != null and param.checkType == 2">
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.recentThreeMonthFlag != null and param.recentThreeMonthFlag == 1">
                AND next_year_inspection_date &gt; CURRENT_DATE
                AND next_year_inspection_date &lt;= DATE_ADD(CURRENT_DATE, INTERVAL 3 MONTH)
                AND ei.inspection_type LIKE '%2%'
            </if>
            <if test="param.recentThreeMonthFlag != null and param.recentThreeMonthFlag == 2">
                AND next_periodic_inspection_date &gt; CURRENT_DATE
                AND next_periodic_inspection_date &lt;= DATE_ADD(CURRENT_DATE, INTERVAL 3 MONTH)
                AND ei.inspection_type LIKE '%2%'
            </if>
            <if test="param.yearInspectFlag != null and param.yearInspectFlag == 1">
                AND CURDATE() &lt;= ei.next_year_inspection_date
                AND ei.inspection_type like '%2%'
            </if>
            <if test="param.yearInspectFlag != null and param.yearInspectFlag == 2">
                AND CURDATE() &lt;= ei.next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.willExpireFlag != null and param.willExpireFlag == 1">
                and DATEDIFF(DATE(next_year_inspection_date), CURDATE()) &lt; 30
                AND CURDATE() &lt;= next_year_inspection_date
                and ei.inspection_type like '%2%'
            </if>
            <if test="param.willExpireFlag  != null and param.willExpireFlag == 2">
                AND ABS(DATEDIFF(CURDATE(),ei.next_periodic_inspection_date)) &lt; 30
                AND CURDATE() &lt;= next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.expireFlag != null and param.expireFlag == 1">
                AND CURDATE() > ei.next_year_inspection_date
                AND ei.inspection_type like '%2%'
            </if>
            <if test="param.expireFlag != null and param.expireFlag == 2">
                AND CURDATE() > ei.next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
        </where>
    </select>
    <select id="selectEquipTypeList" resultType="com.lutao.entity.ProfEquipmentInfo">
        SELECT *
        FROM prof_equipment_info ei
        -- LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code
        LEFT JOIN app_info ai ON ei.app_id = ai.id
        LEFT JOIN equipment_type et ON ei.prof_equip_type = et.id
        LEFT JOIN platform_info pi ON ei.platform_code = pi.platform_code

        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and ei.platform_code = #{param.platformId}
            </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.appId != null and param.appId != ''">
                AND ei.app_id = #{param.appId}
            </if>
            <if test="param.equipType != null">
                AND et.id = #{param.equipType}
            </if>

            <if test="param.checkType != null and param.checkType == 1">
                AND ei.inspection_type like '%2%'
            </if>

            <if test="param.checkType != null and param.checkType == 2">
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.recentThreeMonthFlag != null and param.recentThreeMonthFlag == 1">
                AND next_year_inspection_date &gt; CURRENT_DATE
                AND next_year_inspection_date &lt;= DATE_ADD(CURRENT_DATE, INTERVAL 3 MONTH)
                AND ei.inspection_type LIKE '%2%'
            </if>
            <if test="param.recentThreeMonthFlag != null and param.recentThreeMonthFlag == 2">
                AND next_periodic_inspection_date &gt; CURRENT_DATE
                AND next_periodic_inspection_date &lt;= DATE_ADD(CURRENT_DATE, INTERVAL 3 MONTH)
                AND ei.inspection_type LIKE '%2%'
            </if>
            <if test="param.yearInspectFlag != null and param.yearInspectFlag == 1">
                AND CURDATE() &lt;= ei.next_year_inspection_date
                AND ei.inspection_type like '%2%'
            </if>
            <if test="param.yearInspectFlag != null and param.yearInspectFlag == 2">
                AND CURDATE() &lt;= ei.next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.willExpireFlag != null and param.willExpireFlag == 1">
                and DATEDIFF(DATE(next_year_inspection_date), CURDATE()) &lt; 30
                AND CURDATE() &lt;= next_year_inspection_date
                and ei.inspection_type like '%2%'
            </if>
            <if test="param.willExpireFlag  != null and param.willExpireFlag == 2">
                AND ABS(DATEDIFF(CURDATE(),ei.next_periodic_inspection_date)) &lt; 30
                AND CURDATE() &lt;= next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
            <if test="param.expireFlag != null and param.expireFlag == 1">
                AND CURDATE() > ei.next_year_inspection_date
                AND ei.inspection_type like '%2%'
            </if>
            <if test="param.expireFlag != null and param.expireFlag == 2">
                AND CURDATE() > ei.next_periodic_inspection_date
                AND ei.inspection_type like '%1%'
            </if>
        </where>
    </select>
</mapper>
