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

    <select id="getPersonTodayPlatformInformationPage"
            resultType="com.lutao.cloud.oil.pojo.vo.PersonTodayPlatformInformationVO">
        SELECT pi.platform_name AS platformName,
        pti.app_id AS appId,
        pti.person_name AS personName,
        pti.person_type AS personType,
        pti.data_date AS loginDate,
        ai.app_name AS appName,
        pi.oil_gas_name AS oilGasName,
        pi.operating_company AS operationCompany
        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
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
               and pti.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 pti.app_id = #{param.appId}
            </if>
            <if test="param.name != null and param.name != ''">
                and pti.person_name like concat('%',#{param.name},'%')
            </if>

            <choose>
                <when test="param.date != null and param.date != ''">
                    and pti.data_date BETWEEN  STR_TO_DATE(CONCAT(#{param.date}, ' 00:00:00'), '%Y-%m-%d %H:%i:%s') AND  STR_TO_DATE(CONCAT(#{param.date}, ' 23:59:59'), '%Y-%m-%d %H:%i:%s')
                </when>
                <otherwise>
                    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')
                </otherwise>
            </choose>
        </where>
        ORDER BY pti.person_type,pti.app_id,pi.oil_gas_name,pi.operating_company,pi.platform_name
    </select>

    <select id="getPersonTodayPlatformInformationList"
            resultType="com.lutao.cloud.oil.pojo.vo.PersonTodayPlatformInformationVO">
        SELECT pi.platform_name AS platformName,
        pti.app_id AS appId,
        pti.person_name AS personName,
        pti.person_type AS personType,
        pti.data_date AS loginDate,
        ai.app_name AS appName,
        pi.oil_gas_name AS oilGasName,
        pi.operating_company AS operationCompany
        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
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
               and pti.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 pti.app_id = #{param.appId}
            </if>
            <if test="param.name != null and param.name != ''">
                and pti.person_name like concat('%',#{param.name},'%')
            </if>

            <choose>
                <when test="param.date != null and param.date != ''">
                    and pti.data_date BETWEEN  STR_TO_DATE(CONCAT(#{param.date}, ' 00:00:00'), '%Y-%m-%d %H:%i:%s') AND  STR_TO_DATE(CONCAT(#{param.date}, ' 23:59:59'), '%Y-%m-%d %H:%i:%s')
                </when>
                <otherwise>
                    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')
                </otherwise>
            </choose>
        </where>
        ORDER BY pti.app_id,pi.oil_gas_name,pi.operating_company,pi.platform_name,pti.person_type,pti.id
    </select>
    <select id="getPersonPlatformRankPage" resultType="com.lutao.cloud.oil.pojo.vo.PersonPlatformRankVO">
        SELECT
        any_value ( ppti.app_id ) AS appId,
        any_value ( pi.platform_name ) AS platformName,
        any_value ( pi.oil_gas_name ) AS oilGasName,
        any_value ( pi.operating_company ) AS operatingCompany,
        ppti.platform_code,
        COUNT(*) AS personNumber,
        any_value ( ai.app_name ) AS appName
        FROM
        `platform_person_time_info` ppti
        LEFT JOIN platform_info pi ON ppti.platform_code = pi.platform_code
        left join app_info ai on ppti.app_id = ai.id
        <where>
            pi.delete_flag = '0'
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
               and 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 ppti.app_id = #{param.appId}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and ppti.platform_code = #{param.platformId}
            </if>
            <choose>
                <when test="param.date != null and param.date != ''">
                    and ppti.data_date = #{param.date}
                </when>
                <otherwise>
                    and ppti.data_date = DATE(NOW())
                </otherwise>
            </choose>
        </where>
        GROUP BY
        ppti.platform_code
        ORDER BY
        personNumber DESC
    </select>
    <select id="queryRecentPeopleLineChart" resultType="com.lutao.cloud.oil.pojo.vo.RecentPeopleLineChartVO">

        SELECT
        COUNT(*) as personNumber,
        IFNULL(any_value(insum.intotal),0) as loginNumber,
        IFNULL(any_value(outsum.outtotal),0) as logoutNumber,
        ppti.data_date
        FROM
        platform_person_time_info ppti
        LEFT JOIN (
        SELECT COUNT(*) as intotal,DATE(in_time) as intime FROM person_in_info
        <where>
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
                and operating_company = #{param.operatingCompany}
            </if>
            <if test="param.oilGasName != null and param.oilGasName != ''">
                and oil_gas_name = #{param.oilGasName}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and app_id = #{param.appId}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and platform_code = #{param.platformId}
            </if>
        </where>
        GROUP BY intime
        ) insum ON ppti.data_date = insum.intime
        LEFT JOIN(
        SELECT COUNT(*) as outtotal,DATE(out_time) as outtime FROM person_out_info
        <where>
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
                and operating_company = #{param.operatingCompany}
            </if>
            <if test="param.oilGasName != null and param.oilGasName != ''">
                and oil_gas_name = #{param.oilGasName}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and app_id = #{param.appId}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and platform_code = #{param.platformId}
            </if>
        </where>
            GROUP BY outtime
        ) outsum ON ppti.data_date = outsum.outtime
        LEFT JOIN platform_info pi ON ppti.platform_code = pi.platform_code

        <where>
            pi.delete_flag = '0'
            <choose>
                <when test="param.days != null">
                    and ppti.data_date BETWEEN DATE_SUB(NOW(), INTERVAL #{param.days} DAY) AND NOW()
                </when>
                <otherwise>
                    and ppti.data_date BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
                </otherwise>
            </choose>
            <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 ppti.app_id = #{param.appId}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and ppti.platform_code = #{param.platformId}
            </if>
        </where>
        GROUP BY
        ppti.data_date
        ORDER BY
        ppti.data_date DESC

    </select>
    <select id="getPersonInInfoPlatformListToday" resultType="java.lang.String">
        SELECT platform_code FROM person_in_info
        WHERE DATE(in_time) = DATE(NOW())
    </select>
    <select id="getPersonOutInfoPlatformListToday" resultType="java.lang.String">
        SELECT platform_code FROM person_out_info
        WHERE DATE(out_time) = DATE(NOW())
    </select>
    <select id="queryPersonGoSeaClassifyStatisticsGroupByDate"
            resultType="com.lutao.cloud.oil.pojo.vo.PersonGoSeaClassifyStatisticsGroupByDateBO">
        SELECT COUNT(1) as count, ppti.person_type as personType, ppti.data_date as date
        FROM `platform_person_time_info` ppti
        LEFT JOIN platform_info pi ON ppti.platform_code = pi.platform_code
        <where>
            ppti.data_date >= CURDATE() - INTERVAL 7 DAY and pi.delete_flag = '0'
            <if test="param.appId != null and param.appId != ''">
                and ppti.app_id = #{param.appId}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and ppti.platform_code = #{param.platformId}
            </if>
        </where>
        GROUP BY ppti.person_type, ppti.data_date;
    </select>
    <select id="queryPersonOutInfoClassifyStatisticsGroupByDate"
            resultType="com.lutao.cloud.oil.pojo.vo.PersonGoSeaClassifyStatisticsGroupByDateBO">
        SELECT COUNT(1) as count, person_type as personType,DATE(out_time) as date FROM person_out_info poi
        <where>
            poi.out_time >= CURDATE() - INTERVAL 7 DAY
            <if test="param.appId != null and param.appId != ''">
                and poi.app_id = #{param.appId}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and poi.platform_code = #{param.platformId}
            </if>
        </where>
        GROUP BY poi.person_type, DATE(poi.out_time);
    </select>
    <select id="queryPersonInInfoClassifyStatisticsGroupByDate"
            resultType="com.lutao.cloud.oil.pojo.vo.PersonGoSeaClassifyStatisticsGroupByDateBO">
        SELECT COUNT(1) as count, person_type as personType,DATE(in_time) as date FROM person_in_info pii
        <where>
            pii.in_time >= CURDATE() - INTERVAL 7 DAY
            <if test="param.appId != null and param.appId != ''">
                and pii.app_id = #{param.appId}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and pii.platform_code = #{param.platformId}
            </if>
        </where>
        GROUP BY pii.person_type, DATE(pii.in_time);
    </select>

</mapper>