<?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.check.mapper.CheckTaskMapper">

    <resultMap type="CheckTask" id="CheckTaskResult">
        <result property="tenantId" column="tenant_id"/>
        <result property="revision" column="revision"/>
        <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="taskCode" column="task_code"/>
        <result property="taskName" column="task_name"/>
        <result property="checkStartTime" column="check_start_time"/>
        <result property="status" column="status"/>
        <result property="checkLevel" column="check_level"/>
        <result property="checkedLevel" column="checked_level"/>
        <result property="checkResult" column="check_result"/>
        <result property="checkCategory" column="check_category"/>
        <result property="id" column="id"/>
        <result property="description" column="description"/>
        <result property="checkEndTime" column="check_end_time"/>
        <result property="isRegularTasks" column="is_regular_tasks"/>
        <result property="ruleType" column="rule_type"/>
        <result property="statusDesc" column="status_desc"/>
        <result property="checkLevelDesc" column="check_level_desc"/>
        <result property="checkedLevelDesc" column="checked_level_desc"/>
        <result property="checkResultDesc" column="check_result_desc"/>
        <result property="checkCategoryDesc" column="check_category_desc"/>
        <result property="ruleTypeDesc" column="rule_type_desc"/>
        <result property="remark" column="remark"/>
        <result property="isSelfCheck" column="is_self_check"/>
        <result property="ruleTypeNum" column="rule_type_num"/>
        <result property="createId" column="create_id"/>
        <result property="updateId" column="update_id"/>
        <result property="selfCheckDeptId" column="self_check_dept_id"/>
        <result property="selfCheckDeptName" column="self_check_dept_name"/>
    </resultMap>

    <sql id="selectCheckTaskVo">
        select tenant_id,
               revision,
               create_by,
               create_time,
               update_by,
               update_time,
               task_code,
               task_name,
               check_start_time,
               status,
               check_level,
               checked_level,
               check_result,
               check_category,
               id,
               description,
               check_end_time,
               is_regular_tasks,
               rule_type,
               status_desc,
               check_level_desc,
               checked_level_desc,
               check_result_desc,
               check_category_desc,
               rule_type_desc,
               remark,
               is_self_check,
               rule_type_num,
               create_id,
               update_id,
               self_check_dept_id,
               self_check_dept_name
        from check_task
    </sql>

    <select id="selectCheckTaskList" parameterType="CheckTask" resultMap="CheckTaskResult">
        <include refid="selectCheckTaskVo"/>
        <where>
            <if test="tenantId != null  and tenantId != ''">and tenant_id = #{tenantId}</if>
            <if test="revision != null ">and revision = #{revision}</if>
            <if test="taskCode != null  and taskCode != ''">and task_code = #{taskCode}</if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <if test="checkStartTime != null ">and check_start_time = #{checkStartTime}</if>
            <choose>
                <when test="status == 'PENDING_EXECUTION'">
                    and current_date() <![CDATA[<]]> check_start_time
                </when>
                <when test="status == 'COMPLETED'">
                    and current_date() <![CDATA[>]]> check_end_time
                </when>
                <when test="status == 'IN_PROGRESS'">
                    and current_date() <![CDATA[>=]]> check_start_time
                    and current_date() <![CDATA[<=]]> check_end_time
                </when>
                <otherwise>

                </otherwise>
            </choose>
            <if test="checkLevel != null  and checkLevel != ''">and check_level = #{checkLevel}</if>
            <if test="checkedLevel != null  and checkedLevel != ''">and checked_level = #{checkedLevel}</if>
            <if test="checkResult != null  and checkResult != ''">and check_result = #{checkResult}</if>
            <if test="checkCategory != null  and checkCategory != ''">and check_category = #{checkCategory}</if>
            <if test="description != null  and description != ''">and description = #{description}</if>
            <if test="checkEndTime != null ">and check_end_time = #{checkEndTime}</if>
            <if test="isRegularTasks != null ">and is_regular_tasks = #{isRegularTasks}</if>
            <if test="ruleType != null  and ruleType != ''">and rule_type = #{ruleType}</if>
            <if test="statusDesc != null  and statusDesc != ''">and status_desc = #{statusDesc}</if>
            <if test="checkLevelDesc != null  and checkLevelDesc != ''">and check_level_desc = #{checkLevelDesc}</if>
            <if test="checkedLevelDesc != null  and checkedLevelDesc != ''">and checked_level_desc =
                #{checkedLevelDesc}
            </if>
            <if test="checkResultDesc != null  and checkResultDesc != ''">and check_result_desc = #{checkResultDesc}
            </if>
            <if test="checkCategoryDesc != null  and checkCategoryDesc != ''">and check_category_desc =
                #{checkCategoryDesc}
            </if>
            <if test="ruleTypeDesc != null  and ruleTypeDesc != ''">and rule_type_desc = #{ruleTypeDesc}</if>
            <if test="isSelfCheck != null ">and is_self_check = #{isSelfCheck}</if>
            <if test="ruleTypeNum != null ">and rule_type_num = #{ruleTypeNum}</if>
            <if test="createId != null ">and create_id = #{createId}</if>
            <if test="updateId != null ">and update_id = #{updateId}</if>
            <if test="selfCheckDeptId != null ">and (is_self_check = 0 or self_check_dept_id = #{selfCheckDeptId})</if>
            <if test="selfCheckDeptName != null  and selfCheckDeptName != ''">and self_check_dept_name like concat('%',
                #{selfCheckDeptName}, '%')
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectCheckTaskById" parameterType="Long" resultMap="CheckTaskResult">
        <include refid="selectCheckTaskVo"/>
        where id = #{id}
    </select>

    <insert id="insertCheckTask" parameterType="CheckTask" useGeneratedKeys="true" keyProperty="id">
        insert into check_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">tenant_id,</if>
            <if test="revision != null">revision,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="taskCode != null and taskCode != ''">task_code,</if>
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="checkStartTime != null">check_start_time,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="checkLevel != null and checkLevel != ''">check_level,</if>
            <if test="checkedLevel != null and checkedLevel != ''">checked_level,</if>
            <if test="checkResult != null">check_result,</if>
            <if test="checkCategory != null">check_category,</if>
            <if test="description != null">description,</if>
            <if test="checkEndTime != null">check_end_time,</if>
            <if test="isRegularTasks != null">is_regular_tasks,</if>
            <if test="ruleType != null">rule_type,</if>
            <if test="statusDesc != null and statusDesc != ''">status_desc,</if>
            <if test="checkLevelDesc != null and checkLevelDesc != ''">check_level_desc,</if>
            <if test="checkedLevelDesc != null and checkedLevelDesc != ''">checked_level_desc,</if>
            <if test="checkResultDesc != null">check_result_desc,</if>
            <if test="checkCategoryDesc != null">check_category_desc,</if>
            <if test="ruleTypeDesc != null">rule_type_desc,</if>
            <if test="remark != null">remark,</if>
            <if test="isSelfCheck != null">is_self_check,</if>
            <if test="ruleTypeNum != null">rule_type_num,</if>
            <if test="createId != null">create_id,</if>
            <if test="updateId != null">update_id,</if>
            <if test="selfCheckDeptId != null">self_check_dept_id,</if>
            <if test="selfCheckDeptName != null">self_check_dept_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">#{tenantId},</if>
            <if test="revision != null">#{revision},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="taskCode != null and taskCode != ''">#{taskCode},</if>
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="checkStartTime != null">#{checkStartTime},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="checkLevel != null and checkLevel != ''">#{checkLevel},</if>
            <if test="checkedLevel != null and checkedLevel != ''">#{checkedLevel},</if>
            <if test="checkResult != null">#{checkResult},</if>
            <if test="checkCategory != null">#{checkCategory},</if>
            <if test="description != null">#{description},</if>
            <if test="checkEndTime != null">#{checkEndTime},</if>
            <if test="isRegularTasks != null">#{isRegularTasks},</if>
            <if test="ruleType != null">#{ruleType},</if>
            <if test="statusDesc != null and statusDesc != ''">#{statusDesc},</if>
            <if test="checkLevelDesc != null and checkLevelDesc != ''">#{checkLevelDesc},</if>
            <if test="checkedLevelDesc != null and checkedLevelDesc != ''">#{checkedLevelDesc},</if>
            <if test="checkResultDesc != null">#{checkResultDesc},</if>
            <if test="checkCategoryDesc != null">#{checkCategoryDesc},</if>
            <if test="ruleTypeDesc != null">#{ruleTypeDesc},</if>
            <if test="remark != null">#{remark},</if>
            <if test="isSelfCheck != null">#{isSelfCheck},</if>
            <if test="ruleTypeNum != null">#{ruleTypeNum},</if>
            <if test="createId != null">#{createId},</if>
            <if test="updateId != null">#{updateId},</if>
            <if test="selfCheckDeptId != null">#{selfCheckDeptId},</if>
            <if test="selfCheckDeptName != null">#{selfCheckDeptName},</if>
        </trim>
    </insert>

    <update id="updateCheckTask" parameterType="CheckTask">
        update check_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="taskCode != null and taskCode != ''">task_code = #{taskCode},</if>
            <if test="taskName != null and taskName != ''">task_name = #{taskName},</if>
            <if test="checkStartTime != null">check_start_time = #{checkStartTime},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="checkLevel != null and checkLevel != ''">check_level = #{checkLevel},</if>
            <if test="checkedLevel != null and checkedLevel != ''">checked_level = #{checkedLevel},</if>
            <if test="checkResult != null">check_result = #{checkResult},</if>
            <if test="checkCategory != null">check_category = #{checkCategory},</if>
            <if test="description != null">description = #{description},</if>
            <if test="checkEndTime != null">check_end_time = #{checkEndTime},</if>
            <if test="isRegularTasks != null">is_regular_tasks = #{isRegularTasks},</if>
            <if test="ruleType != null">rule_type = #{ruleType},</if>
            <if test="statusDesc != null and statusDesc != ''">status_desc = #{statusDesc},</if>
            <if test="checkLevelDesc != null and checkLevelDesc != ''">check_level_desc = #{checkLevelDesc},</if>
            <if test="checkedLevelDesc != null and checkedLevelDesc != ''">checked_level_desc = #{checkedLevelDesc},
            </if>
            <if test="checkResultDesc != null">check_result_desc = #{checkResultDesc},</if>
            <if test="checkCategoryDesc != null">check_category_desc = #{checkCategoryDesc},</if>
            <if test="ruleTypeDesc != null">rule_type_desc = #{ruleTypeDesc},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="isSelfCheck != null">is_self_check = #{isSelfCheck},</if>
            <if test="ruleTypeNum != null">rule_type_num = #{ruleTypeNum},</if>
            <if test="createId != null">create_id = #{createId},</if>
            <if test="updateId != null">update_id = #{updateId},</if>
            <if test="selfCheckDeptId != null">self_check_dept_id = #{selfCheckDeptId},</if>
            <if test="selfCheckDeptName != null">self_check_dept_name = #{selfCheckDeptName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCheckTaskById" parameterType="Long">
        delete
        from check_task
        where id = #{id}
    </delete>

    <delete id="deleteCheckTaskByIds" parameterType="String">
        delete from check_task where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectCheckTaskListWeb" parameterType="CheckTask" resultMap="CheckTaskResult">
        <include refid="selectCheckTaskVo"/>
        <where>
            <if test="taskCode != null  and taskCode != ''">and task_code = #{taskCode}</if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <choose>
                <when test="status == 'PENDING_EXECUTION'">
                    and current_date() <![CDATA[<]]> check_start_time
                </when>
                <when test="status == 'COMPLETED'">
                    and current_date() <![CDATA[>]]> check_end_time
                </when>
                <when test="status == 'IN_PROGRESS'">
                    and current_date() <![CDATA[>=]]> check_start_time
                    and current_date() <![CDATA[<=]]> check_end_time
                </when>
                <otherwise>

                </otherwise>
            </choose>
            <if test="checkedLevel != null  and checkedLevel != ''">and checked_level = #{checkedLevel}</if>
            and ((is_self_check = 0 and check_level in ('DEPARTMENT_LEVEL', 'TEAM_LEVEL'))
            or (is_self_check = 1 and self_check_dept_id = #{selfCheckDeptId}))
        </where>
        order by create_time desc
    </select>

    <select id="selectCheckTaskListBrigadeWeb" parameterType="CheckTask" resultMap="CheckTaskResult">
        <include refid="selectCheckTaskVo"/>
        <where>
            <if test="taskCode != null  and taskCode != ''">and task_code = #{taskCode}</if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <choose>
                <when test="status == 'PENDING_EXECUTION'">
                    and current_date() <![CDATA[<]]> check_start_time
                </when>
                <when test="status == 'COMPLETED'">
                    and current_date() <![CDATA[>]]> check_end_time
                </when>
                <when test="status == 'IN_PROGRESS'">
                    and current_date() <![CDATA[>=]]> check_start_time
                    and current_date() <![CDATA[<=]]> check_end_time
                </when>
                <otherwise>

                </otherwise>
            </choose>
            <if test="checkedLevel != null  and checkedLevel != ''">and checked_level = #{checkedLevel}</if>
            and ((is_self_check = 0 and check_level in ('BRIGADE_LEVEL','DEPARTMENT_LEVEL', 'TEAM_LEVEL'))
            or (is_self_check = 1 and self_check_dept_id = #{selfCheckDeptId}))
        </where>
        order by create_time desc
    </select>

</mapper>