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

    <resultMap id="BaseResultMap" type="com.sundot.airport.ledger.domain.LedgerSecurityTest">
        <id property="id" column="id" />
        <result property="recordDate" column="record_date" />
        <result property="region" column="region" />
        <result property="channel" column="channel" />
        <result property="channelId" column="channel_id" />
        <result property="deptName" column="dept_name" />
        <result property="deptId" column="dept_id" />
        <result property="teamName" column="team_name" />
        <result property="teamId" column="team_id" />
        <result property="groupName" column="group_name" />
        <result property="groupId" column="group_id" />
        <result property="areaId" column="area_id" />
        <result property="testerName" column="tester_name" />
        <result property="testerId" column="tester_id" />
        <result property="testedName" column="tested_name" />
        <result property="testedId" column="tested_id" />
        <result property="testType" column="test_type" />
        <result property="positionId" column="position_id" />
        <result property="testItem" column="test_item" />
        <result property="testResult" column="test_result" />
        <result property="evidenceFile" column="evidence_file" />
        <result property="problemDesc" column="problem_desc" />
        <result property="testLevel" column="test_level" />
        <result property="resultHandling" column="result_handling" />
        <result property="reviewMaterial" column="review_material" />
        <result property="deductScore" column="deduct_score" />
        <result property="addScore" column="add_score" />
        <result property="scoreDimension" column="score_dimension" />
        <result property="scoreIndicator" column="score_indicator" />
        <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, region, channel, channel_id, dept_name, dept_id, team_name, team_id, group_name, group_id, area_id,
               tester_name, tester_id, tested_name, tested_id, test_type, position_id, test_item, test_result,
               evidence_file, problem_desc, test_level, result_handling, review_material,
               deduct_score, add_score, score_dimension, score_indicator,
               import_batch, source_type, create_by, create_time, update_by, update_time, sync_flag
        FROM ledger_security_test
        WHERE del_flag = '0'
    </sql>

    <select id="selectList" parameterType="com.sundot.airport.ledger.domain.LedgerSecurityTest" 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="testedName != null and testedName != ''">AND tested_name LIKE CONCAT('%', #{testedName}, '%')</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}
        </if>
        <if test="params != null and params.endTime != null and params.endTime != ''">
            AND record_date &lt;= #{params.endTime}
        </if>
        ORDER BY id DESC
    </select>

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

    <select id="securityTestItemClassification"
            parameterType="com.sundot.airport.common.dto.LedgerCommonQueryReqVO"
            resultType="com.sundot.airport.common.dto.LedgerCommonResVO">
        select problem_desc name,
        count(1) total
        from ledger_security_test
        where del_flag = '0'
        <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>
        <if test="userId != null">and tested_id = #{userId}</if>
        <if test="startDate != null and endDate != null">
            and (record_date >= #{startDate}
            and record_date <![CDATA[ < ]]> date_add(#{endDate} , interval 1 day))
        </if>
        group by problem_desc
        order by total desc
    </select>

    <select id="securityTestPassingStatus"
            parameterType="com.sundot.airport.common.dto.LedgerCommonQueryReqVO"
            resultType="com.sundot.airport.common.dto.LedgerCommonResVO">
        select test_result name,
        count(1) total
        from ledger_security_test
        where del_flag = '0'
        <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>
        <if test="userId != null">and tested_id = #{userId}</if>
        <if test="startDate != null and endDate != null">
            and (record_date >= #{startDate}
            and record_date <![CDATA[ < ]]> date_add(#{endDate} , interval 1 day))
        </if>
        group by test_result
        order by total desc
    </select>

    <select id="securityTestRegion"
            parameterType="com.sundot.airport.common.dto.LedgerCommonQueryReqVO"
            resultType="com.sundot.airport.common.dto.LedgerCommonResVO">
        select region name,
        count(1) total
        from ledger_security_test
        where del_flag = '0'
        <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>
        <if test="userId != null">and tested_id = #{userId}</if>
        <if test="startDate != null and endDate != null">
            and (record_date >= #{startDate}
            and record_date <![CDATA[ < ]]> date_add(#{endDate} , interval 1 day))
        </if>
        group by region
        order by total desc
    </select>

</mapper>