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

  <resultMap id="personCertificateMap" type="com.lutao.entity.PersonCertificate">
                  <id property="id" column="id"/>
                        <result property="personId" column="person_id"/>
                        <result property="certificate" column="certificate"/>
                        <result property="certificateType" column="certificate_type"/>
                        <result property="initialcollectionDate" column="initialcollection_date"/>
                        <result property="effectiveDate" column="effective_date"/>
                        <result property="certificateFile" column="certificate_file"/>
                        <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="platformId" column="platform_id"/>
                        <result property="platformCode" column="platform_code"/>
                        <result property="platformName" column="platform_name"/>
            </resultMap>
    <select id="getCertificateExpireWarningPage" resultType="com.lutao.cloud.oil.pojo.vo.PersonCertificateVO">

        SELECT
        ai.app_name AS appName,
        pi.oil_gas_name AS oilGasName,
        pi.operating_company AS operatingCompany,
        pi.platform_name AS platformName,
        pti.person_name AS personName,
        pc.certificate AS certificate,
        CASE
        WHEN pc.certificate_type = 8 THEN
        '五小证'
        END AS certificateType,
        pc.effective_date AS effectiveDate,
        pc.initialcollection_date AS initialCollectionDate
        FROM
        platform_person_time_info pti
        LEFT JOIN platform_info pi ON pti.platform_code = pi.platform_code
        LEFT JOIN app_info ai ON pti.app_id = ai.id
        LEFT JOIN person_certificate pc ON pti.person_id = pc.person_id
        <where>
            pi.delete_flag = '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 pc.app_id = #{param.appId}
            </if>
            <if test="param.expireFlag == 1">
                and pc.effective_date &lt; CURRENT_DATE
            </if>
            <if test="param.willExpireFlag == 1">
                and pc.effective_date BETWEEN CURRENT_DATE AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY)
            </if>
            <if test="true">
                and pc.certificate_type = 8
                and pti.data_date BETWEEN  STR_TO_DATE(CONCAT(CURRENT_DATE, ' 00:00:00'), '%Y-%m-%d %H:%i:%s') AND  STR_TO_DATE(CONCAT(CURRENT_DATE, ' 23:59:59'), '%Y-%m-%d %H:%i:%s')
            </if>
        </where>
    </select>
    <select id="getCertificateExpireWarningCount" resultType="java.lang.Long">
        SELECT
        count(*)
        FROM
        platform_person_time_info pti
        LEFT JOIN platform_info pi ON pti.platform_code = pi.platform_code
        LEFT JOIN app_info ai ON pti.app_id = ai.id
        LEFT JOIN person_certificate pc ON pti.person_id = pc.person_id
        <where>
            pi.delete_flag = '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 pc.app_id = #{param.appId}
            </if>
            <if test="param.expireFlag == 1">
                and pc.effective_date &lt; CURRENT_DATE
            </if>
            <if test="param.willExpireFlag == 1">
                and pc.effective_date BETWEEN CURRENT_DATE AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY)
            </if>
            <if test="true">
                and pc.certificate_type = 8
                and pti.data_date BETWEEN  STR_TO_DATE(CONCAT(CURRENT_DATE, ' 00:00:00'), '%Y-%m-%d %H:%i:%s') AND  STR_TO_DATE(CONCAT(CURRENT_DATE, ' 23:59:59'), '%Y-%m-%d %H:%i:%s')
            </if>
        </where>
    </select>

</mapper>
