<?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.item.mapper.SimpleDutyScheduleMapper">

    <resultMap type="SimpleDutySchedule" id="SimpleDutyScheduleResult">
        <result property="id" column="id"/>
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="departmentId" column="department_id"/>
        <result property="departmentName" column="department_name"/>
        <result property="dutyDate" column="duty_date"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="crossDay" column="cross_day"/>
        <result property="status" column="status"/>
        <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="remark" column="remark"/>
    </resultMap>

    <sql id="selectSimpleDutyScheduleVo">
        select id,
               station_id,
               station_name,
               department_id,
               department_name,
               duty_date,
               start_time,
               end_time,
               cross_day,
               status,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from simple_duty_schedule
    </sql>

    <select id="selectSimpleDutyScheduleByScheduleId" parameterType="Long" resultMap="SimpleDutyScheduleResult">
        <include refid="selectSimpleDutyScheduleVo"/>
        where id = #{id}
    </select>

    <select id="selectSimpleDutyScheduleList" parameterType="SimpleDutySchedule" resultMap="SimpleDutyScheduleResult">
        <include refid="selectSimpleDutyScheduleVo"/>
        <where>
            <if test="stationName != null  and stationName != ''">and station_name = #{stationName}</if>
            <if test="departmentName != null  and departmentName != ''">and department_name = #{departmentName}</if>
            <if test="dutyDate != null ">and duty_date = #{dutyDate}</if>
            <if test="startTime != null ">and start_time >= #{startTime}</if>
            <if test="endTime != null ">and end_time &lt;= #{endTime}</if>
            <if test="crossDay != null ">and cross_day = #{crossDay}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
        </where>
        order by duty_date asc
    </select>

    <select id="selectSimpleDutyScheduleByDate" parameterType="Date" resultMap="SimpleDutyScheduleResult">
        <include refid="selectSimpleDutyScheduleVo"/>
        where start_time &lt;= #{dutyDate} and end_time >= #{dutyDate}
    </select>

    <select id="selectSimpleDutyScheduleByStationAndDate" resultMap="SimpleDutyScheduleResult">
        <include refid="selectSimpleDutyScheduleVo"/>
        where station_id = #{stationId} and department_id = #{departmentId} and duty_date = #{dutyDate}
    </select>

    <insert id="insertSimpleDutySchedule" parameterType="SimpleDutySchedule" useGeneratedKeys="true" keyProperty="id">
        insert into simple_duty_schedule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="stationName != null and stationName != ''">station_name,</if>
            <if test="departmentId != null">department_id,</if>
            <if test="departmentName != null and departmentName != ''">department_name,</if>
            <if test="dutyDate != null">duty_date,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="crossDay != null">cross_day,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null and remark != ''">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationId != null">#{stationId},</if>
            <if test="stationName != null and stationName != ''">#{stationName},</if>
            <if test="departmentId != null">#{departmentId},</if>
            <if test="departmentName != null and departmentName != ''">#{departmentName},</if>
            <if test="dutyDate != null">#{dutyDate},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="crossDay != null">#{crossDay},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
        </trim>
    </insert>

    <update id="updateSimpleDutySchedule" parameterType="SimpleDutySchedule">
        update simple_duty_schedule
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="stationName != null and stationName != ''">station_name = #{stationName},</if>
            <if test="departmentId != null">department_id = #{departmentId},</if>
            <if test="departmentName != null and departmentName != ''">department_name = #{departmentName},</if>
            <if test="dutyDate != null">duty_date = #{dutyDate},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="crossDay != null">cross_day = #{crossDay},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSimpleDutyScheduleByScheduleId" parameterType="Long">
        delete
        from simple_duty_schedule
        where id = #{id}
    </delete>

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


    <insert id="batchInsertSimpleDutySchedule" parameterType="SimpleDutySchedule">
        insert into simple_duty_schedule
        (station_id, station_name, department_id, department_name, duty_date, start_time, end_time,
        cross_day, status, create_by, create_time, update_by, update_time, remark)
        values
        <foreach item="item" collection="list" separator=",">
            (#{item.stationId}, #{item.stationName}, #{item.departmentId}, #{item.departmentName},
            #{item.dutyDate}, #{item.startTime}, #{item.endTime}, #{item.crossDay}, #{item.status},
            #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime}, #{item.remark})
        </foreach>
    </insert>

</mapper>