<?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.sundot.airport.ledger.mapper.LedgerUnsafeEventMapper">

    <resultMap id="BaseResultMap" type="com.sundot.airport.ledger.domain.LedgerUnsafeEvent">
        <id property="id" column="id" />
        <result property="recordDate" column="record_date" />
        <result property="flightNo" column="flight_no" />
        <result property="deptName" column="dept_name" />
        <result property="deptId" column="dept_id" />
        <result property="teamName" column="team_name" />
        <result property="teamInternalDuty" column="team_internal_duty" />
        <result property="teamId" column="team_id" />
        <result property="groupName" column="group_name" />
        <result property="groupId" column="group_id" />
        <result property="responsibleName" column="responsible_name" />
        <result property="responsibleId" column="responsible_id" />
        <result property="eventType" column="event_type" />
        <result property="involvedItems" column="involved_items" />
        <result property="position" column="position" />
        <result property="positionId" column="position_id" />
        <result property="area" column="area" />
        <result property="areaId" column="area_id" />
        <result property="channelNo" column="channel_no" />
        <result property="image" column="image" />
        <result property="eventDesc" column="event_desc" />
        <result property="resultHandling" column="result_handling" />
        <result property="deductScore" column="deduct_score" />
        <result property="evidenceFile" column="evidence_file" />
        <result property="remark" column="remark" />
        <result property="importBatch" column="import_batch" />
        <result property="sourceType" column="source_type" />
        <result property="createBy"   column="create_by"   />
        <result property="createTime" column="create_time" />
        <result property="updateBy"   column="update_by"   />
        <result property="updateTime" column="update_time" />
        <result property="syncFlag" column="sync_flag" />
    </resultMap>

    <sql id="selectVo">
        SELECT id, record_date, flight_no, dept_name, dept_id, team_name, team_internal_duty, team_id, group_name, group_id, responsible_name, responsible_id, event_type, involved_items, position, position_id, area, area_id, channel_no, image, event_desc, result_handling, deduct_score, evidence_file, remark, import_batch, source_type, create_by, create_time, update_by, update_time, sync_flag
        FROM ledger_unsafe_event
        WHERE del_flag = '0'
    </sql>

    <select id="selectList" parameterType="com.sundot.airport.ledger.domain.LedgerUnsafeEvent" resultMap="BaseResultMap">
        <include refid="selectVo"/>
        <if test="deptName != null and deptName != ''">AND dept_name LIKE CONCAT('%', #{deptName}, '%')</if>
        <if test="teamName != null and teamName != ''">AND team_name LIKE CONCAT('%', #{teamName}, '%')</if>
        <if test="responsibleName != null and responsibleName != ''">AND responsible_name LIKE CONCAT('%', #{responsibleName}, '%')</if>
        <if test="syncFlag != null and syncFlag != ''">AND sync_flag = #{syncFlag}</if>
        <if test="params != null and params.beginTime != null and params.beginTime != ''">
            AND (record_date &gt;= #{params.beginTime} OR approve_date &gt;= #{params.beginTime} OR exam_date &gt;= #{params.beginTime})
        </if>
        <if test="params != null and params.endTime != null and params.endTime != ''">
            AND (record_date &lt;= #{params.endTime} OR approve_date &lt;= #{params.endTime} OR exam_date &lt;= #{params.endTime})
        </if>
        ORDER BY id DESC
    </select>

    <update id="updateSyncFlag">
        update ledger_unsafe_event
        set sync_flag = '1'
        where sync_flag = '0'
    </update>

    <select id="countSeizureStatsItem" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="com.sundot.airport.ledger.domain.vo.UnsafeItemVO">
        SELECT involved_items AS itemName, COUNT(*) AS itemNum
        FROM ledger_unsafe_event
        <where>
            <include refid="FILTER_CRITERIA_SQL"/>
            <!-- 过滤空物品脏数据 -->
            AND involved_items IS NOT NULL AND involved_items != ''
        </where>
        GROUP BY involved_items
        ORDER BY itemNum DESC
    </select>

    <select id="countSeizureStatsType" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="com.sundot.airport.ledger.domain.vo.UnsafeTypeVO">
        SELECT event_type AS eventType, COUNT(*) AS eventTypeNum
        FROM ledger_unsafe_event
        <where>
            <include refid="FILTER_CRITERIA_SQL"/>
            <!-- 过滤空类型脏数据 -->
            AND event_type IS NOT NULL AND event_type != ''
        </where>
        GROUP BY event_type
        ORDER BY eventTypeNum DESC
    </select>

    <select id="countSeizureStatsPost" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="com.sundot.airport.ledger.domain.vo.UnsafePositionVO">
        SELECT position_id AS positionId, position AS positionName, COUNT(*) AS positionNum
        FROM ledger_unsafe_event
        <where>
            <include refid="FILTER_CRITERIA_SQL"/>
            <!-- 过滤空岗位脏数据 -->
            AND position IS NOT NULL AND position != ''
        </where>
        GROUP BY position_id, position
        ORDER BY positionNum DESC
    </select>
    
    <sql id="FILTER_CRITERIA_SQL">
        del_flag = '0'
        <if test="startDate != null and endDate != null">
            AND record_date BETWEEN #{startDate,jdbcType=DATE} AND #{endDate,jdbcType=DATE}
        </if>
        <!-- 三级联动筛选 -->
        <if test="deptId != null">AND dept_id = #{deptId}</if>
        <if test="teamId != null">AND team_id = #{teamId}</if>
        <if test="groupId != null">AND group_id = #{groupId}</if>
    </sql>
</mapper>
