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

    <resultMap id="BaseResultMap" type="com.sundot.airport.ledger.domain.LedgerPatrolInspection">
        <id property="id" column="id" />
        <result property="recordDate"           column="record_date"           />
        <result property="deptName"             column="dept_name"             />
        <result property="teamName"             column="team_name"             />
        <result property="groupName"            column="group_name"            />
        <result property="inspectorName"        column="inspector_name"        />
        <result property="inspectedName"        column="inspected_name"        />
        <result property="patrolType"           column="patrol_type"           />
        <result property="patrolItem"           column="patrol_item"           />
        <result property="problemDesc"          column="problem_desc"          />
        <result property="location"             column="location"              />
        <result property="channelNo"            column="channel_no"            />
        <result property="resultHandling"       column="result_handling"       />
        <result property="deductScore"          column="deduct_score"          />
        <result property="scoreDimension"       column="score_dimension"       />
        <result property="scoreIndicator"       column="score_indicator"       />
        <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"             />
        <!-- 新增字段 -->
        <result property="patrolTimePeriod"     column="patrol_time_period"    />
        <result property="hasProblem"           column="has_problem"           />
        <result property="rectificationStatus"  column="rectification_status"  />
        <result property="evidenceMaterial"     column="evidence_material"     />
        <result property="responsibleLeader"    column="responsible_leader"    />
        <result property="qualityOfficer"       column="quality_officer"       />
        <result property="teamLeader"           column="team_leader"           />
        <result property="reporter"             column="reporter"              />
        <result property="teamInternalDuty"     column="team_internal_duty"    />
        <result property="reportTime"           column="report_time"           />
        <result property="sendToRelevant"       column="send_to_relevant"      />
        <!-- ID字段 -->
        <result property="inspectedId"          column="inspected_id"          />
        <result property="deptId"               column="dept_id"               />
        <result property="teamId"               column="team_id"               />
        <result property="groupId"              column="group_id"              />
        <result property="positionId"           column="position_id"           />
        <result property="areaId"               column="area_id"               />
    </resultMap>

    <sql id="selectVo">
        SELECT id, record_date, dept_name, team_name, group_name, inspector_name, inspected_name, patrol_type, patrol_item, problem_desc, location, channel_no, result_handling, deduct_score, score_dimension, score_indicator, evidence_file, remark, import_batch, source_type, create_by, create_time, update_by, update_time, sync_flag,
               patrol_time_period, has_problem, rectification_status, evidence_material, responsible_leader, quality_officer, team_leader, reporter, team_internal_duty, report_time, send_to_relevant,
               inspected_id, dept_id, team_id, group_id, position_id, area_id
        FROM ledger_patrol_inspection
        WHERE del_flag = '0'
    </sql>

    <select id="selectList" parameterType="com.sundot.airport.ledger.domain.LedgerPatrolInspection" 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="inspectorName != null and inspectorName != ''">AND inspector_name LIKE CONCAT('%', #{inspectorName}, '%')</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 record_date DESC, id DESC
    </select>

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

    <select id="countByInspectedNameAndDateRange" resultType="int">
        SELECT COUNT(*)
        FROM ledger_patrol_inspection
        WHERE del_flag = '0'
          AND inspected_name = #{inspectedName}
          <if test="beginTime != null and beginTime != ''">
              AND record_date &gt;= STR_TO_DATE(#{beginTime}, '%Y-%m-%d')
          </if>
          <if test="endTime != null and endTime != ''">
              AND record_date &lt;= STR_TO_DATE(#{endTime}, '%Y-%m-%d')
          </if>
    </select>

</mapper>