<?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.ruoyi.system.mapper.BusinessTripMapper">
    
    <resultMap type="BusinessTrip" id="BusinessTripResult">
        <result property="id"    column="id"    />
        <result property="type"    column="type"    />

        <result property="transportation"    column="transportation"    />
        <result property="departurePlace"    column="departure_place"    />
        <result property="destination"    column="destination"    />

        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />

        <result property="duration"    column="duration"    />

        <result property="reason"    column="reason"    />
        <result property="photo"    column="photo"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="initiatorId"    column="initiator_id"    />
        <result property="initiatorName"    column="initiator_name"    />
        <result property="absenteeName"    column="absentee_name"    />
        <result property="examinersId"    column="examiners_id"    />
        <result property="examinersName"    column="examiners_name"    />
        <result property="isPass"    column="is_pass"    />
        <result property="reject"    column="reject"    />
        <result property="submitTime"    column="submit_time"    />
    </resultMap>

    <sql id="selectBusinessTripVo">
        select id, type,transportation,departure_place,destination, start_time, end_time,duration, reason, photo, dept_id, dept_name,initiator_id,initiator_name, absentee_name, examiners_id, examiners_name, is_pass, reject, submit_time from business_trip
    </sql>

    <sql id="selectBusinessTripLbVo">
        select t.id, t.type,t.transportation,t.departure_place,t.destination, t.start_time, t.end_time,t.duration, t.reason, t.photo, t.dept_id,
               t.dept_name,t.initiator_id,t.initiator_name, t.absentee_name, t.examiners_id, t.examiners_name, t.is_pass, t.reject, t.submit_time from business_trip t
        left join business_user u on t.id = u.business_id
    </sql>

    <select id="selectBusinessTripList" parameterType="BusinessTrip" resultMap="BusinessTripResult">
        <include refid="selectBusinessTripVo"/>
        <where>  
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="transportation != null  and transportation != ''"> and transportation = #{transportation}</if>
            <if test="departurePlace != null  and departurePlace != ''"> and departure_place like concat('%', #{departurePlace}, '%')</if>
            <if test="destination != null  and destination != ''"> and destination like concat('%', #{destination}, '%')</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="reason != null  and reason != ''"> and reason = #{reason}</if>
            <if test="photo != null  and photo != ''"> and photo = #{photo}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptIds!=null and deptIds.size() >0">
                and dept_id in
                <foreach item="deptId" collection="deptIds" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="initiatorId != null "> and initiator_id = #{initiatorId}</if>
            <if test="initiatorName != null  and initiatorName != ''"> and initiator_name like concat('%', #{initiatorName}, '%')</if>
            <if test="absenteeName != null  and absenteeName != ''"> and absentee_name like concat('%', #{absenteeName}, '%')</if>
            <if test="examinersId != null "> and examiners_id = #{examinersId}</if>
            <if test="examinersName != null  and examinersName != ''"> and examiners_name like concat('%', #{examinersName}, '%')</if>
            <if test="isPass != null  and isPass != ''"> and is_pass = #{isPass}</if>
            <if test="reject != null  and reject != ''"> and reject = #{reject}</if>
            <if test="submitTime != null "> and submit_time = #{submitTime}</if>
        </where>
        order by is_pass, submit_time desc
    </select>

    <select id="selectBusinessTripListLb" parameterType="BusinessTrip" resultMap="BusinessTripResult">
        <include refid="selectBusinessTripLbVo"/>
        <where>
            <if test="userId != null "> and u.absentee_id = #{userId}</if>
            <if test="type != null  and type != ''"> and t.type = #{type}</if>
            <if test="transportation != null  and transportation != ''"> and t.transportation = #{transportation}</if>
            <if test="departurePlace != null  and departurePlace != ''"> and t.departure_place like concat('%', #{departurePlace}, '%')</if>
            <if test="destination != null  and destination != ''"> and t.destination like concat('%', #{destination}, '%')</if>
            <if test="reason != null  and reason != ''"> and t.reason = #{reason}</if>
            <if test="deptId != null "> and t.dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and t.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="initiatorId != null "> and initiator_id = #{initiatorId}</if>
            <if test="initiatorName != null  and initiatorName != ''"> and initiator_name like concat('%', #{initiatorName}, '%')</if>
            <if test="absenteeName != null  and absenteeName != ''"> and t.absentee_name like concat('%', #{absenteeName}, '%')</if>
            <if test="examinersId != null "> and t.examiners_id = #{examinersId}</if>
            <if test="examinersName != null  and examinersName != ''"> and t.examiners_name like concat('%', #{examinersName}, '%')</if>
            <if test="isPass != null  and isPass != ''"> and t.is_pass = #{isPass}</if>
            <if test="submitTime != null "> and t.submit_time = #{submitTime}</if>
            <if test="startTime != null ">
                AND date_format(t.start_time,'%y%m%d %H:%i:%s') &gt;=  date_format(#{startTime},'%y%m%d %H:%i:%s')
            </if>
            <if test="endTime != null ">
                AND date_format(t.end_time,'%y%m%d %H:%i:%s') &lt;=  date_format(#{endTime},'%y%m%d %H:%i:%s')
            </if>
        </where>
        order by t.is_pass, t.submit_time desc
    </select>
    
    <select id="selectBusinessTripById" parameterType="Long" resultMap="BusinessTripResult">
        <include refid="selectBusinessTripVo"/>
        where id = #{id}
    </select>

    <insert id="insertBusinessTrip" parameterType="BusinessTrip" useGeneratedKeys="true" keyProperty="id">
        insert into business_trip
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null">type,</if>
            <if test="transportation != null ">transportation ,</if>
            <if test="departurePlace != null ">departure_place ,</if>
            <if test="destination != null ">destination ,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="duration != null">duration,</if>
            <if test="reason != null">reason,</if>
            <if test="photo != null">photo,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="initiatorId != null ">  initiator_id ,</if>
            <if test="initiatorName != null">  initiator_name ,</if>
            <if test="absenteeName != null">absentee_name,</if>
            <if test="examinersId != null">examiners_id,</if>
            <if test="examinersName != null">examiners_name,</if>
            <if test="isPass != null">is_pass,</if>
            <if test="reject != null">reject,</if>
            <if test="submitTime != null">submit_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null">#{type},</if>
            <if test="transportation != null  ">#{transportation},</if>
            <if test="departurePlace != null ">#{departurePlace},</if>
            <if test="destination != null  "> #{destination}, </if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="duration != null">#{duration},</if>
            <if test="reason != null">#{reason},</if>
            <if test="photo != null">#{photo},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="initiatorId != null ">  #{initiatorId},</if>
            <if test="initiatorName != null  ">  #{initiatorName}, </if>
            <if test="absenteeName != null">#{absenteeName},</if>
            <if test="examinersId != null">#{examinersId},</if>
            <if test="examinersName != null">#{examinersName},</if>
            <if test="isPass != null">#{isPass},</if>
            <if test="reject != null">#{reject},</if>
            <if test="submitTime != null">#{submitTime},</if>
         </trim>
    </insert>

    <update id="updateBusinessTrip" parameterType="BusinessTrip">
        update business_trip
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="transportation != null ">  transportation = #{transportation},</if>
            <if test="departurePlace != null  ">  departure_place = #{departurePlace},</if>
            <if test="destination != null ">  destination = #{destination}, </if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="photo != null">photo = #{photo},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="initiatorId != null ">  initiator_id = #{initiatorId},</if>
            <if test="initiatorName != null ">  initiator_name= #{initiatorName},</if>
            <if test="absenteeName != null">absentee_name = #{absenteeName},</if>
            <if test="examinersId != null">examiners_id = #{examinersId},</if>
            <if test="examinersName != null">examiners_name = #{examinersName},</if>
            <if test="isPass != null">is_pass = #{isPass},</if>
            <if test="reject != null">reject = #{reject},</if>
            <if test="submitTime != null">submit_time = #{submitTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBusinessTripById" parameterType="Long">
        delete from business_trip where id = #{id}
    </delete>

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