<?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.tuoheng.admin.mapper.InspectionCycleMapper">

    <resultMap id="InspectionCycleResult" type="com.tuoheng.admin.entity.InspectionCycle" >
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="roadId"  column="road_id" />
        <result property="sectionId"  column="section_id" />
        <result property="airportId"    column="airport_id"    />
        <result property="droneId"    column="drone_id"    />
        <result property="airportLineId"    column="airport_line_id"    />
        <result property="airportLineLength"    column="airport_line_length"    />
        <result property="singleTime"    column="single_time"    />
        <result property="everydayTime"    column="everyday_time"    />
        <result property="cycleTime"    column="cycle_time"    />
        <result property="cycleNextTime"    column="cycle_next_time"    />
        <result property="cycleFrequency"    column="cycle_frequency"    />
        <result property="cycleStartTime"    column="cycle_start_time"    />
        <result property="cycleEndTime"    column="cycle_end_time"    />
        <result property="isEnable"    column="is_enable"    />
        <result property="note"    column="note"    />
        <result property="createUser"    column="create_user"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUser"    column="update_user"    />
        <result property="updateTime"    column="update_time"    />
        <result property="mark"    column="mark"    />
    </resultMap>

    <resultMap id="ForeignResult" type="com.tuoheng.admin.entity.InspectionCycle" extends="InspectionCycleResult">
        <result property="createUserName"    column="create_user_name"    />
    </resultMap>

    <sql id="Base_Column_List">
        t.id, t.tenant_id, t.dept_id, t.code, t.name, t.road_id, t.section_id, t.airport_id, t.airport_line_id, t.airport_line_length, t.drone_id, t.type,
        t.single_time, t.everyday_time, t.cycle_time, t.cycle_next_time, t.cycle_frequency, t.cycle_start_time, t.cycle_end_time,
        t.is_enable, t.note, t.create_user, t.create_time, t.update_user, t.update_time, t.mark
    </sql>

    <select id="selectPageList" parameterType="com.tuoheng.admin.request.cycle.QueryInspectionCyclePageListRequest" resultMap="ForeignResult">
        select <include refid="Base_Column_List"/>, u.realname as create_user_name
        from th_inspection_cycle t
        left join th_user u on u.id = t.create_user
        <where>
            <if test="1 == 1"> and t.mark = 1 </if>
            <if test="request.code != null and request.code != ''"> and t.code like concat('%', #{request.code}, '%') </if>
            <if test="request.name != null and request.name != ''"> and t.name like concat('%', #{request.name}, '%') </if>
            <if test="request.tenantId != null and request.tenantId != ''"> and t.tenant_id = #{request.tenantId} </if>
            <if test="request.airportId != null and request.airportId != 0"> and t.airport_id = #{request.airportId} </if>
            <if test="request.airportLineId != null and request.airportLineId != 0"> and t.airport_line_id = #{request.airportLineId} </if>
            <if test="request.isEnable != null"> and t.is_enable = #{request.isEnable} </if>
            <if test="request.status != null and request.status == 0">and now() &lt; t.cycle_start_time</if>
            <if test="request.status != null and request.status == 1">and now() &gt;= t.cycle_start_time and now() &lt;= t.cycle_end_time</if>
            <if test="request.status != null and request.status == 2">and now() &gt; t.cycle_end_time</if>
            <if test="request.deptIdList != null and request.deptIdList.size() > 0">
                and t.dept_id in
                <foreach item="deptId" collection="request.deptIdList" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
        order by t.create_time desc
    </select>

    <update id="updateEnable" parameterType="com.tuoheng.admin.entity.InspectionCycle">
        update th_inspection_cycle
        <trim prefix="SET" suffixOverrides=",">
            <if test="isEnable != null"> is_enable = #{isEnable}, </if>
            <if test="updateUser != null and updateUser != ''"> update_user = #{updateUser},</if>
            <if test="updateTime != null"> update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

</mapper>
