<?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="cn.cecep.talroad.mapper.PcStationBuildingStatMapper">
    <resultMap type="cn.cecep.talroad.domain.PcStationBuildingStat" id="PcStationBuildingStatResult">
        <result property="id" column="id"/>
        <result property="factoryId" column="factory_id"/>
        <result property="statisticalTime" column="statistical_time"/>
        <result property="stationBuildingId" column="station_building_id"/>
        <result property="swipesOfNumber" column="swipes_of_number"/>
        <result property="createTime" column="create_time"/>
    </resultMap>


    <sql id="selectPcStationBuildingStatVo">
        select id, factory_id, statistical_time, station_building_id, swipes_of_number, create_time
        from pc_station_building_stat
    </sql>

    <select id="selectPcStationBuildingStatList" parameterType="cn.cecep.talroad.domain.PcStationBuildingStat"
            resultMap="PcStationBuildingStatResult">
        <include refid="selectPcStationBuildingStatVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''">
                and factory_id = #{factoryId}
            </if>
            <if test="statisticalTime != null  and statisticalTime != ''">
                and statistical_time = #{statisticalTime}
            </if>
            <if test="stationBuildingId != null">
                and station_building_id = #{stationBuildingId}
            </if>
            <if test="swipesOfNumber != null">
                and swipes_of_number = #{swipesOfNumber}
            </if>
        </where>
    </select>

    <select id="selectPage" resultType="cn.cecep.talroad.vo.PcStationBuildingStatVo">
        select
            t1.*,
            t2.factory_name,
            t2.industry_permission_name,
            t2.country_code,
            t2.country_name,
            t3.station_building_code,
            t3.station_building_name
        from pc_station_building_stat t1
             left join pc_factory t2 on t1.factory_id = t2.id
             left join pc_station_building t3 on t1.station_building_id::VARCHAR = t3.id
        <where>
            <if test="query.countryCode != null and query.countryCode != ''"> and t2.country_code = #{query.countryCode}</if>
            <if test="query.factoryName != null and query.factoryName != ''"> and t2.factory_name like concat('%', #{query.factoryName}, '%')</if>
            <if test="query.industryPermissionName != null and query.industryPermissionName != ''"> and t2.industry_permission_name = #{query.industryPermissionName}</if>
            <if test="query.stationBuildingName != null and query.stationBuildingName != ''"> and t3.station_building_name like concat('%', #{query.stationBuildingName}, '%')</if>
            <if test="query.startTime != null and query.startTime != ''">
                and t1.statistical_time >= to_timestamp(#{query.startTime}, 'yyyy-mm-dd hh24:MI:SS')
            </if>
            <if test="query.endTime != null and query.endTime != ''">
                and t1.statistical_time &lt;= to_timestamp(#{query.endTime}, 'yyyy-mm-dd hh24:MI:SS')
            </if>
            <if test="query.swipesOfNumber != null and query.swipesOfNumber != '' and query.swipesOfNumber == 0">
                and t1.swipes_of_number = 0
            </if>
            <if test="query.swipesOfNumber != null and query.swipesOfNumber != '' and query.swipesOfNumber == 1">
                and t1.swipes_of_number &lt; 5
            </if>
            <if test="query.swipesOfNumber != null and query.swipesOfNumber != '' and query.swipesOfNumber == 2">
                and t1.swipes_of_number >= 5
            </if>
        </where>
        ORDER BY t2.factory_name, t1.id
    </select>
    <select id="selectStatisticsData" resultMap="PcStationBuildingStatResult">
        select
            b.factory_id ,
            a.id as station_building_id,
            count(1) as totalCount
        from
            pc_station_building_entrance_gu a
        inner join pc_station_building b on a.station_building_id = b.station_building_code
        <where>
            a.visit_time between #{startDate} and #{endDate}
        </where>
        group by b.factory_id ,a.id
    </select>

    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO pc_station_building_stat
        (id, factory_id, statistical_time, station_building_id, swipes_of_number, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.id},
            #{item.factoryId},
            now(),
            #{item.stationBuildingId},
            #{item.swipesOfNumber},
            #{item.createTime})
        </foreach>
    </insert>
</mapper>
