<?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.zcloud.mapper.datasource.integratedmanagement.GateMachineAreaMapper">

    <!--表名 -->
    <sql id="tableName">
        MKMJ_AREA
    </sql>

    <!--数据字典表名 -->
    <sql id="dicTableName">
        SYS_DICTIONARIES
    </sql>

    <!-- 字段 -->
    <sql id="Field">
        f
        .
        AREA_ID
        ,
  		f.AREA_NAME,
  		f.AREA_TYPE,
  		f.AREA_STATUS,
  		f.CORPINFO_ID,
  		f.LONGITUDE,
  		f.LATITUDE,
  		f.CREATOR,
  		f.CREATTIME,
  		f.OPERATOR,
  		f.OPERATTIME,
  		f.ISDELETE
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field2">
        AREA_ID
        ,
  		AREA_NAME,
  		AREA_TYPE,
  		AREA_STATUS,
  		CORPINFO_ID,
  		LONGITUDE,
  		LATITUDE,
  		CREATOR,
  		CREATTIME,
  		OPERATOR,
  		OPERATTIME,
  		ISDELETE
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{AREA_ID}
        ,
        #{AREA_NAME},
        #{AREA_TYPE},
        #{AREA_STATUS},
        #{CORPINFO_ID},
        #{LONGITUDE},
        #{LATITUDE},
        #{CREATOR},
        #{CREATTIME},
        #{OPERATOR},
        #{OPERATTIME},
        #{ISDELETE}
    </sql>

    <!-- 新增-->
    <insert id="save" parameterType="pd">
        insert into
        <include refid="tableName"></include>
        (
        <include refid="Field2"></include>
        ) values (
        <include refid="FieldValue"></include>
        )
    </insert>

    <!-- 修改 -->
    <update id="edit" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        <if test="AREA_NAME != null and AREA_NAME != ''">
            AREA_NAME= #{AREA_NAME},
        </if>
        <if test="AREA_TYPE != null and AREA_TYPE != ''">
            AREA_TYPE = #{AREA_TYPE},

        </if>
        <if test="AREA_STATUS != null and AREA_STATUS != ''">
            AREA_STATUS = #{AREA_STATUS},

        </if>
        <if test="LONGITUDE != null and LONGITUDE != ''">
            LONGITUDE = #{LONGITUDE},

        </if>
        <if test="LATITUDE != null and LATITUDE != ''">
            LATITUDE = #{LATITUDE},
        </if>
        <if test="LATITUDE != null and LATITUDE != ''"></if>
        OPERATOR = #{OPERATOR},
        OPERATTIME = #{OPERATTIME}
        where
        AREA_ID = #{AREA_ID}
    </update>

    <!-- 通过ID获取数据 -->
    <select id="findById" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where
        f.AREA_ID = #{AREA_ID}
    </select>

    <!-- 列表 -->
    <select id="datalistPage" parameterType="page" resultType="pd">
        SELECT
        m.*,
        IFNULL(t.GATE_COUNT,0) GATE_COUNT
        FROM
        mkmj_area m
        LEFT JOIN (SELECT AREA_ID,count(1) GATE_COUNT FROM mkmj_area_gate g WHERE ISDELETE = 0  GROUP BY g.AREA_ID) t on m.AREA_ID = t.AREA_ID
        where m.ISDELETE = 0 and m.CORPINFO_ID = #{pd.CORPINFO_ID}
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (

            m.AREA_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            <!--	根据需求自己加检索条件
       or
      字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
  -->
            )
        </if>
        order by
        m.OPERATTIME desc
    </select>
    <select id="getGateMachineCountByCid" resultType="com.zcloud.entity.PageData">
        SELECT
            g.GATE_CATEGORY,
            COUNT( g.GATE_CATEGORY ) CATEGORY_COUNT
        FROM
            mkmj_area_gate g
                LEFT JOIN mkmj_area a ON g.AREA_ID = a.AREA_ID
        WHERE
            g.ISDELETE = 0
          AND a.ISDELETE = 0
          -- 必须已经定位的 才会展示再地图上
          and g.GATE_POSITION = 0
          AND g.CORPINFO_ID = #{CORPINFO_ID}
        GROUP BY
            g.GATE_CATEGORY
    </select>
    <select id="getGateMachineList" resultType="com.zcloud.entity.PageData">
        select * from mkmj_area_gate where  ISDELETE = 0 and GATE_POSITION = 0 and CORPINFO_ID = #{CORPINFO_ID} and GATE_CATEGORY = #{GATE_CATEGORY}
    </select>
    <select id="getPeopleAndVehiclesRecord" resultType="com.zcloud.entity.PageData">
        SELECT
            ifnull(people.today_entries,0) AS people_today_entries,
            ifnull(people.today_exits,0) AS people_today_exits,
            ifnull(people.week_entries,0) AS people_week_entries,
            ifnull(people.week_exits,0) AS people_week_exits,
            ifnull(people.total_entries,0) AS people_total_entries,
            ifnull(people.total_exits,0) AS people_total_exits,
            ifnull(vehicles.today_entries,0) AS vehicle_today_entries,
            ifnull(vehicles.today_exits,0) AS vehicle_today_exits,
            ifnull(vehicles.week_entries,0) AS vehicle_week_entries,
            ifnull(vehicles.week_exits,0) AS vehicle_week_exits,
            ifnull(vehicles.total_entries,0) AS vehicle_total_entries,
            ifnull(vehicles.total_exits,0) AS vehicle_total_exits
        FROM
            (
                SELECT
                    SUM( CASE WHEN READER = '0' AND DATE ( EVENT_TIME ) = CURDATE() THEN 1 ELSE 0 END ) AS today_entries,
                    SUM( CASE WHEN READER = '1' AND DATE ( EVENT_TIME ) = CURDATE() THEN 1 ELSE 0 END ) AS today_exits,
                    SUM( CASE WHEN READER = '0' AND YEARWEEK( EVENT_TIME, 1 ) = YEARWEEK( CURDATE(), 1 ) THEN 1 ELSE 0 END ) AS week_entries,
                    SUM( CASE WHEN READER = '1' AND YEARWEEK( EVENT_TIME, 1 ) = YEARWEEK( CURDATE(), 1 ) THEN 1 ELSE 0 END ) AS week_exits,
                    SUM( CASE WHEN READER = '0' THEN 1 ELSE 0 END ) AS total_entries,
                    SUM( CASE WHEN READER = '1' THEN 1 ELSE 0 END ) AS total_exits
                FROM
                    mkmj_people_access
                WHERE
                    ISDELETE = 0
                  and CORPINFO_ID = #{CORPINFO_ID}
            ) AS people,
            (
                SELECT
                    SUM( CASE WHEN DATE ( ENTRANCE_DATE ) = CURDATE() THEN 1 ELSE 0 END ) AS today_entries,
                    SUM( CASE WHEN DATE ( LEAVE_DATE ) = CURDATE() THEN 1 ELSE 0 END ) AS today_exits,
                    SUM( CASE WHEN YEARWEEK( ENTRANCE_DATE, 1 ) = YEARWEEK( CURDATE(), 1 ) THEN 1 ELSE 0 END ) AS week_entries,
                    SUM( CASE WHEN YEARWEEK( LEAVE_DATE, 1 ) = YEARWEEK( CURDATE(), 1 ) THEN 1 ELSE 0 END ) AS week_exits,
                    SUM( CASE WHEN ENTRANCE_DATE IS NOT NULL THEN 1 ELSE 0 END ) AS total_entries,
                    SUM( CASE WHEN LEAVE_DATE IS NOT NULL THEN 1 ELSE 0 END ) AS total_exits
                FROM
                    mkmj_car_access
                WHERE
                    ISDELETE = '0'
                  and CORPINFO_ID = #{CORPINFO_ID}
            ) AS vehicles
    </select>
    <!-- 批量删除 -->
    <delete id="deleteAll" parameterType="String">
        update
        <include refid="tableName"></include>
        set
        ISDELETE = 1
        where
        AREA_ID in
        <foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
</mapper>
