<?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.oa.mapper.CmcCarApprovalMapper">

    <resultMap type="CmcCarApproval" id="CmcCarApprovalResult">
        <result property="carApplyId"    column="car_apply_id"    />
        <result property="applier"    column="applier"    />
        <result property="useDept"    column="use_dept"    />
        <result property="cars"    column="cars"    />
        <result property="drivers"    column="drivers"    />
        <result property="projectId"    column="project_id"    />
        <result property="applyReason"    column="apply_reason"    />
        <result property="passengers"    column="passengers"    />
        <result property="applyDate"    column="apply_date"    />
        <result property="beginDate"    column="begin_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="days"    column="days"    />
        <result property="deptUserId"    column="dept_user_id"    />
        <result property="deptComment"    column="dept_comment"    />
        <result property="managerUserId"    column="manager_user_id"    />
        <result property="managerComment"    column="manager_comment"    />
        <result property="carUsage"    column="car_usage"    />
        <result property="unionUserId"    column="union_user_id"    />
        <result property="unionComment"    column="union_comment"    />
        <result property="gmUserId"    column="gm_user_id"    />
        <result property="gmComment"    column="gm_comment"    />
        <result property="dispatcher"    column="dispatcher"    />
        <result property="dispatchComment"    column="dispatch_comment"    />
        <result property="estimateCost"    column="estimate_cost"    />
        <result property="deptTime"    column="dept_time"    />
        <result property="managerTime"    column="manager_time"    />
        <result property="unionTime"    column="union_time"    />
        <result property="gmTime"    column="gm_time"    />
        <result property="dszTime"    column="dsz_time"    />
        <result property="dispatchTime"    column="dispatch_time"    />
        <association property="applierUser"    javaType="SysUser"         resultMap="ApplierUserResult" />
        <association property="dept"    javaType="SysDept"         resultMap="SysDeptResult" />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
        <association property="deptUser"    javaType="SysUser"         resultMap="DeptUserResult" />
        <association property="managerUser"    javaType="SysUser"         resultMap="ManagerUserResult" />
        <association property="unionUser"    javaType="SysUser"         resultMap="UnionUserResult" />
        <association property="gmUser"    javaType="SysUser"         resultMap="GmUserResult" />
        <association property="dszUser"    javaType="SysUser"         resultMap="DszUserResult" />
        <association property="dispatchUser"    javaType="SysUser"         resultMap="DispatchUserResult" />
    </resultMap>

    <resultMap type="SysUser" id="ApplierUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="applier_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="DeptUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="dept_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="ManagerUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="manager_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="UnionUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="union_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="GmUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="gm_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="DszUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="dsz_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="DispatchUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="dispatch_nick_name"    />
    </resultMap>

    <resultMap type="SysDept" id="SysDeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <resultMap type="CmcProject" id="CmcProjectResult">
        <result property="projectId"    column="project_id"    />
        <result property="projectNumber"    column="project_number"    />
        <result property="projectName"    column="project_name"    />
    </resultMap>

    <sql id="selectCmcCarApprovalVo">
        select ca.car_apply_id, ca.applier, u.nick_name as applier_nick_name, ca.use_dept, d.dept_name, ca.cars, ca.drivers, ca.project_id, p.project_number, p.project_name,
               ca.apply_reason, ca.passengers, ca.apply_date, ca.begin_date, ca.end_date, ca.days, ca.dept_user_id, u1.nick_name as dept_nick_name, ca.dept_comment, ca.manager_user_id,
               u2.nick_name as manager_nick_name, ca.manager_comment, ca.car_usage, ca.union_user_id, u3.nick_name as union_nick_name, ca.union_comment, ca.gm_user_id, u4.nick_name as gm_nick_name,
               ca.dsz_user_id, u5.nick_name as dsz_nick_name, ca.gm_comment, ca.dsz_comment, ca.dispatcher, u6.nick_name as dispatch_nick_name, ca.dispatch_comment, ca.estimate_cost, ca.dept_time,
               ca.manager_time, ca.union_time, ca.gm_time, ca.dsz_time, ca.dispatch_time from cmc_car_approval as ca
            left join sys_user as u on u.user_id = ca.applier
            left join sys_user as u1 on u1.user_id = ca.dept_user_id
            left join sys_user as u2 on u2.user_id = ca.manager_user_id
            left join sys_user as u3 on u3.user_id = ca.union_user_id
            left join sys_user as u4 on u4.user_id = ca.gm_user_id
            left join sys_user as u5 on u5.user_id = ca.dsz_user_id
            left join sys_user as u6 on u6.user_id = ca.dispatcher
            left join sys_dept as d on d.dept_id = ca.use_dept
            left join cmc_project as p on ca.project_id = p.project_id
    </sql>

    <select id="selectCmcCarApprovalList" parameterType="CmcCarApproval" resultMap="CmcCarApprovalResult">
        <include refid="selectCmcCarApprovalVo"/>
        <where>
            <if test="applier != null "> and ca.applier = #{applier}</if>
            <if test="useDept != null "> and ca.use_dept = #{useDept}</if>
            <if test="cars != null  and cars != ''"> and find_in_set(#{cars}, ca.cars)</if>
            <if test="drivers != null  and drivers != ''"> and find_in_set(#{drivers}, ca.drivers})</if>
            <if test="projectId != null  and projectId != ''"> and ca.project_id = #{projectId}</if>
            <if test="applyReason != null  and applyReason != ''"> and ca.apply_reason = #{applyReason}</if>
            <if test="passengers != null "> and ca.passengers = #{passengers}</if>
            <if test="applyDate != null "> and ca.apply_date = #{beginDate}</if>
            <if test="beginDate != null "> and ca.begin_date = #{beginDate}</if>
            <if test="endDate != null "> and ca.end_date = #{endDate}</if>
            <if test="days != null "> and ca.days = #{days}</if>
            <if test="deptUserId != null "> and ca.dept_user_id = #{deptUserId}</if>
            <if test="deptComment != null  and deptComment != ''"> and ca.dept_comment = #{deptComment}</if>
            <if test="managerUserId != null "> and ca.manager_user_id = #{managerUserId}</if>
            <if test="managerComment != null  and managerComment != ''"> and ca.manager_comment = #{managerComment}</if>
            <if test="carUsage != null  and carUsage != ''"> and ca.car_usage = #{carUsage}</if>
            <if test="unionUserId != null "> and ca.union_user_id = #{unionUserId}</if>
            <if test="unionComment != null  and unionComment != ''"> and ca.union_comment = #{unionComment}</if>
            <if test="gmUserId != null "> and ca.gm_user_id = #{gmUserId}</if>
            <if test="gmComment != null  and gmComment != ''"> and ca.gm_comment = #{gmComment}</if>
            <if test="dispatcher != null "> and ca.dispatcher = #{dispatcher}</if>
            <if test="dispatchComment != null  and dispatchComment != ''"> and ca.dispatch_comment = #{dispatchComment}</if>
            <if test="estimateCost != null "> and ca.estimate_cost = #{estimateCost}</if>
        </where>
        order by ca.apply_date asc
    </select>

    <select id="selectCmcCarApprovalByCarApplyId" parameterType="String" resultMap="CmcCarApprovalResult">
        <include refid="selectCmcCarApprovalVo"/>
        where car_apply_id = #{carApplyId}
    </select>

    <insert id="insertCmcCarApproval" parameterType="CmcCarApproval">
        insert into cmc_car_approval
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="carApplyId != null">car_apply_id,</if>
            <if test="applier != null">applier,</if>
            <if test="useDept != null">use_dept,</if>
            <if test="cars != null">cars,</if>
            <if test="drivers != null">drivers,</if>
            <if test="projectId != null">project_id,</if>
            <if test="applyReason != null">apply_reason,</if>
            <if test="passengers != null">passengers,</if>
            <if test="applyDate != null">apply_date,</if>
            <if test="beginDate != null">begin_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="days != null">days,</if>
            <if test="deptUserId != null">dept_user_id,</if>
            <if test="deptComment != null">dept_comment,</if>
            <if test="managerUserId != null">manager_user_id,</if>
            <if test="managerComment != null">manager_comment,</if>
            <if test="carUsage != null">car_usage,</if>
            <if test="unionUserId != null">union_user_id,</if>
            <if test="unionComment != null">union_comment,</if>
            <if test="gmUserId != null">gm_user_id,</if>
            <if test="gmComment != null">gm_comment,</if>
            <if test="dszUserId != null">dsz_user_id,</if>
            <if test="dszComment != null">dsz_comment,</if>
            <if test="dispatcher != null">dispatcher,</if>
            <if test="dispatchComment != null">dispatch_comment,</if>
            <if test="estimateCost != null">estimate_cost,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="carApplyId != null">#{carApplyId},</if>
            <if test="applier != null">#{applier},</if>
            <if test="useDept != null">#{useDept},</if>
            <if test="cars != null">#{cars},</if>
            <if test="drivers != null">#{drivers},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="applyReason != null">#{applyReason},</if>
            <if test="passengers != null">#{passengers},</if>
            <if test="applyDate != null">#{applyDate},</if>
            <if test="beginDate != null">#{beginDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="days != null">#{days},</if>
            <if test="deptUserId != null">#{deptUserId},</if>
            <if test="deptComment != null">#{deptComment},</if>
            <if test="managerUserId != null">#{managerUserId},</if>
            <if test="managerComment != null">#{managerComment},</if>
            <if test="carUsage != null">#{carUsage},</if>
            <if test="unionUserId != null">#{unionUserId},</if>
            <if test="unionComment != null">#{unionComment},</if>
            <if test="gmUserId != null">#{gmUserId},</if>
            <if test="gmComment != null">#{gmComment},</if>
            <if test="dszUserId != null">#{dszUserId},</if>
            <if test="dszComment != null">#{dszComment},</if>
            <if test="dispatcher != null">#{dispatcher},</if>
            <if test="dispatchComment != null">#{dispatchComment},</if>
            <if test="estimateCost != null">#{estimateCost},</if>
        </trim>
    </insert>

    <update id="updateCmcCarApproval" parameterType="CmcCarApproval">
        update cmc_car_approval
        <trim prefix="SET" suffixOverrides=",">
            <if test="applier != null">applier = #{applier},</if>
            <if test="useDept != null">use_dept = #{useDept},</if>
            <if test="cars != null">cars = #{cars},</if>
            <if test="drivers != null">drivers = #{drivers},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="applyReason != null">apply_reason = #{applyReason},</if>
            <if test="passengers != null">passengers = #{passengers},</if>
            <if test="applyDate != null">apply_date = #{applyDate},</if>
            <if test="beginDate != null">begin_date = #{beginDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="days != null">days = #{days},</if>
            <if test="deptUserId != null">dept_user_id = #{deptUserId},</if>
            <if test="deptComment != null">dept_comment = #{deptComment},</if>
            <if test="managerUserId != null">manager_user_id = #{managerUserId},</if>
            <if test="managerComment != null">manager_comment = #{managerComment},</if>
            <if test="carUsage != null">car_usage = #{carUsage},</if>
            <if test="unionUserId != null">union_user_id = #{unionUserId},</if>
            <if test="unionComment != null">union_comment = #{unionComment},</if>
            <if test="gmUserId != null">gm_user_id = #{gmUserId},</if>
            <if test="gmComment != null">gm_comment = #{gmComment},</if>
            <if test="dszUserId != null">dsz_user_id = #{dszUserId},</if>
            <if test="dszComment != null">dsz_comment = #{dszComment},</if>
            <if test="dispatcher != null">dispatcher = #{dispatcher},</if>
            <if test="dispatchComment != null">dispatch_comment = #{dispatchComment},</if>
            <if test="estimateCost != null">estimate_cost = #{estimateCost},</if>
            <if test="deptTime != null ">dept_time = #{deptTime},</if>
            <if test="managerTime != null ">manager_time = #{managerTime},</if>
            <if test="unionTime != null ">union_time = #{unionTime},</if>
            <if test="gmTime != null ">gm_time = #{gmTime},</if>
            <if test="dszTime != null ">dsz_time = #{dszTime}</if>
            <if test="dispatchTime != null ">dispatch_time = #{dispatchTime}</if>
        </trim>
        where car_apply_id = #{carApplyId}
    </update>

    <delete id="deleteCmcCarApprovalByCarApplyId" parameterType="String">
        delete from cmc_car_approval where car_apply_id = #{carApplyId}
    </delete>

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