<?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.CmcBorrowMapper">
    
    <resultMap type="CmcBorrow" id="CmcBorrowResult">
        <result property="borrowId"    column="borrow_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="applyReason"    column="apply_reason"    />
        <result property="borrowUsage"    column="borrow_usage"    />
        <result property="applier"    column="applier"    />
        <result property="applyDept"    column="apply_dept"    />
        <result property="applyDate"    column="apply_date"    />
        <result property="remark"    column="remark"    />
        <result property="applyAmount"    column="apply_amount"    />
        <result property="managerAmount"    column="manager_amount"    />
        <result property="deptComment"    column="dept_comment"    />
        <result property="deptUserId"    column="dept_user_id"    />
        <result property="managerComment"    column="manager_comment"    />
        <result property="managerUserId"    column="manager_user_id"    />
        <result property="zjlUserId"    column="zjl_user_id"    />
        <result property="zjlComment"    column="zjl_comment"    />
        <result property="dszUserId"    column="dsz_user_id"    />
        <result property="dszComment"    column="dsz_comment"    />
        <result property="unionUserId"    column="union_user_id"    />
        <result property="unionComment"    column="union_comment"    />
        <result property="cwUserId"    column="cw_user_id"    />
        <result property="cwComment"    column="cw_comment"    />
        <result property="deptTime"    column="dept_time"    />
        <result property="managerTime"    column="manager_time"    />
        <result property="zjlTime"    column="zjl_time"    />
        <result property="dszTime"    column="dsz_time"    />
        <result property="lendTime"    column="lend_time"    />
        <result property="lendDocument"    column="lend_document"    />
        <result property="borrowDocument"    column="borrow_document"    />
        <result property="unionTime"    column="union_time"    />
        <association property="applierUser"    javaType="SysUser"         resultMap="ApplierUserResult" />
        <association property="deptUser"    javaType="SysUser"         resultMap="DeptUserResult" />
        <association property="managerUser"    javaType="SysUser"         resultMap="ManagerUserResult" />
        <association property="zjlUser"    javaType="SysUser"         resultMap="ZjlUserResult" />
        <association property="dszUser"    javaType="SysUser"         resultMap="DszUserResult" />
        <association property="unionUser"    javaType="SysUser"         resultMap="UnionUserResult" />
        <association property="cwUser"    javaType="SysUser"         resultMap="CwUserResult" />
        <association property="dept"    javaType="SysDept"         resultMap="SysDeptResult" />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
    </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="ZjlUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="zjl_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="UnionUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="union_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="CwUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="cw_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"    />
        <result property="undertakingDept"    column="undertaking_dept"    />
    </resultMap>

    <sql id="selectCmcBorrowVo">
        select b.borrow_id, b.project_id, p.project_number, p.project_name, p.undertaking_dept, b.apply_reason, b.applier, u.nick_name as applier_nick_name, d.dept_name, b.apply_dept, b.remark, b.borrow_usage,
               b.apply_date, b.apply_amount, b.manager_amount, b.dept_comment, b.dept_user_id, u1.nick_name as dept_nick_name, b.manager_comment, b.manager_user_id, u2.nick_name as manager_nick_name,
               b.zjl_user_id, u3.nick_name as zjl_nick_name, b.zjl_comment, b.dsz_comment,  b.union_user_id, u4.nick_name as union_nick_name, b.union_comment, b.cw_user_id, u5.nick_name as cw_nick_name,
               b.dsz_user_id, u6.nick_name as dsz_nick_name, b.cw_comment, b.dept_time, b.manager_time, b.zjl_time, b.dsz_time, b.lend_time, b.lend_document, b.borrow_document, b.union_time from cmc_borrow as b
        left join sys_user as u on u.user_id = b.applier
        left join sys_user as u1 on u1.user_id = b.dept_user_id
        left join sys_user as u2 on u2.user_id = b.manager_user_id
        left join sys_user as u3 on u3.user_id = b.zjl_user_id
        left join sys_user as u4 on u4.user_id = b.union_user_id
        left join sys_user as u5 on u5.user_id = b.cw_user_id
        left join sys_user as u6 on u6.user_id = b.dsz_user_id
        left join sys_dept as d on d.dept_id = b.apply_dept
        left join cmc_project as p on b.project_id = p.project_id
    </sql>

    <select id="selectCmcBorrowList" parameterType="CmcBorrow" resultMap="CmcBorrowResult">
        <include refid="selectCmcBorrowVo"/>
        <where>  
            <if test="projectId != null  and projectId != ''"> and b.project_id = #{projectId}</if>
            <if test="projectNumber != null  and projectNumber != ''"> and p.project_number like concat('%', #{projectNumber}, '%')</if>
            <if test="undertakingDept != null  and undertakingDept != ''"> and find_in_set(#{undertakingDept}, p.undertaking_dept)</if>
            <if test="applyReason != null  and applyReason != ''"> and b.apply_reason = #{applyReason}</if>
            <if test="borrowUsage != null  and borrowUsage != ''"> and b.borrow_usage = #{borrowUsage}</if>
            <if test="applyDept != null  and applyDept != ''"> and b.apply_dept = #{applyDept}</if>
            <if test="applier != null "> and b.applier = #{applier}</if>
            <if test="remark != null "> and b.remark = #{remark}</if>
            <if test="applyDate != null "> and b.apply_date = #{applyDate}</if>
            <if test="applyAmount != null "> and b.apply_amount = #{applyAmount}</if>
            <if test="managerAmount != null "> and b.manager_amount = #{managerAmount}</if>
            <if test="deptComment != null  and deptComment != ''"> and b.dept_comment = #{deptComment}</if>
            <if test="deptUserId != null "> and b.dept_user_id = #{deptUserId}</if>
            <if test="managerComment != null  and managerComment != ''"> and b.manager_comment = #{managerComment}</if>
            <if test="managerUserId != null "> and b.manager_user_id = #{managerUserId}</if>
            <if test="zjlUserId != null "> and b.zjl_user_id = #{zjlUserId}</if>
            <if test="zjlComment != null  and zjlComment != ''"> and b.zjl_comment = #{zjlComment}</if>
            <if test="dszUserId != null "> and b.dsz_user_id = #{dszUserId}</if>
            <if test="dszComment != null  and dszComment != ''"> and b.dsz_comment = #{dszComment}</if>
            <if test="unionUserId != null "> and b.union_user_id = #{unionUserId}</if>
            <if test="unionComment != null  and unionComment != ''"> and b.union_comment = #{unionComment}</if>
            <if test="cwUserId != null "> and b.cw_user_id = #{cwUserId}</if>
            <if test="cwComment != null  and cwComment != ''"> and b.cw_comment = #{cwComment}</if>
            <if test="deptTime != null "> and b.dept_time = #{deptTime}</if>
            <if test="managerTime != null "> and b.manager_time = #{managerTime}</if>
            <if test="zjlTime != null "> and b.zjl_time = #{zjlTime}</if>
            <if test="dszTime != null "> and b.dsz_time = #{dszTime}</if>
            <if test="lendTime != null and @com.ruoyi.common.utils.DateUtils@parseDateToStr('yyyy', lendTime) == '2000' "> and b.lend_time is not null</if>
            <if test="lendTime != null and @com.ruoyi.common.utils.DateUtils@parseDateToStr('yyyy', lendTime) != '2000' "> and YEAR(b.lend_time) = YEAR(#{lendTime})</if>
            <if test="unionTime != null "> and b.union_time = #{unionTime}</if>
        </where>
        order by b.apply_date desc
    </select>
    
    <select id="selectCmcBorrowByBorrowId" parameterType="String" resultMap="CmcBorrowResult">
        <include refid="selectCmcBorrowVo"/>
        where b.borrow_id = #{borrowId}
    </select>
        
    <insert id="insertCmcBorrow" parameterType="CmcBorrow">
        insert into cmc_borrow
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="borrowId != null">borrow_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="applyReason != null">apply_reason,</if>
            <if test="borrowUsage != null">borrow_usage,</if>
            <if test="applier != null">applier,</if>
            <if test="remark != null">remark,</if>
            <if test="applyDept != null">apply_dept,</if>
            <if test="applyDate != null">apply_date,</if>
            <if test="applyAmount != null">apply_amount,</if>
            <if test="managerAmount != null">manager_amount,</if>
            <if test="deptComment != null">dept_comment,</if>
            <if test="deptUserId != null">dept_user_id,</if>
            <if test="managerComment != null">manager_comment,</if>
            <if test="managerUserId != null">manager_user_id,</if>
            <if test="zjlUserId != null">zjl_user_id,</if>
            <if test="zjlComment != null">zjl_comment,</if>
            <if test="dszUserId != null">dsz_user_id,</if>
            <if test="dszComment != null">dsz_comment,</if>
            <if test="unionUserId != null">union_user_id,</if>
            <if test="unionComment != null">union_comment,</if>
            <if test="cwUserId != null">cw_user_id,</if>
            <if test="cwComment != null">cw_comment,</if>
            <if test="deptTime != null">dept_time,</if>
            <if test="managerTime != null">manager_time,</if>
            <if test="zjlTime != null">zjl_time,</if>
            <if test="dszTime != null">dsz_time,</if>
            <if test="lendTime != null">lend_time,</if>
            <if test="unionTime != null">union_time,</if>
            <if test="lendDocument != null">lend_document,</if>
            <if test="borrowDocument != null">borrow_document,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="borrowId != null">#{borrowId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="applyReason != null">#{applyReason},</if>
            <if test="borrowUsage != null">#{borrowUsage},</if>
            <if test="applier != null">#{applier},</if>
            <if test="remark != null">#{remark},</if>
            <if test="applyDept != null">#{applyDept},</if>
            <if test="applyDate != null">#{applyDate},</if>
            <if test="applyAmount != null">#{applyAmount},</if>
            <if test="managerAmount != null">#{managerAmount},</if>
            <if test="deptComment != null">#{deptComment},</if>
            <if test="deptUserId != null">#{deptUserId},</if>
            <if test="managerComment != null">#{managerComment},</if>
            <if test="managerUserId != null">#{managerUserId},</if>
            <if test="zjlUserId != null">#{zjlUserId},</if>
            <if test="zjlComment != null">#{zjlComment},</if>
            <if test="dszUserId != null">#{dszUserId},</if>
            <if test="dszComment != null">#{dszComment},</if>
            <if test="unionUserId != null">#{unionUserId},</if>
            <if test="unionComment != null">#{unionComment},</if>
            <if test="cwUserId != null">#{cwUserId},</if>
            <if test="cwComment != null">#{cwComment},</if>
            <if test="deptTime != null">#{deptTime},</if>
            <if test="managerTime != null">#{managerTime},</if>
            <if test="zjlTime != null">#{zjlTime},</if>
            <if test="dszTime != null">#{dszTime},</if>
            <if test="unionTime != null">#{unionTime},</if>
            <if test="lendTime != null">#{lendTime},</if>
            <if test="lendDocument != null">#{lendDocument},</if>
            <if test="borrowDocument != null">#{borrowDocument},</if>
         </trim>
    </insert>

    <update id="updateCmcBorrow" parameterType="CmcBorrow">
        update cmc_borrow
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="applyReason != null">apply_reason = #{applyReason},</if>
            <if test="borrowUsage != null">borrow_usage = #{borrowUsage},</if>
            <if test="applier != null">applier = #{applier},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="applyDept != null">apply_dept = #{applyDept},</if>
            <if test="applyDate != null">apply_date = #{applyDate},</if>
            <if test="applyAmount != null">apply_amount = #{applyAmount},</if>
            <if test="managerAmount != null">manager_amount = #{managerAmount},</if>
            <if test="deptComment != null">dept_comment = #{deptComment},</if>
            <if test="deptUserId != null">dept_user_id = #{deptUserId},</if>
            <if test="managerComment != null">manager_comment = #{managerComment},</if>
            <if test="managerUserId != null">manager_user_id = #{managerUserId},</if>
            <if test="zjlUserId != null">zjl_user_id = #{zjlUserId},</if>
            <if test="zjlComment != null">zjl_comment = #{zjlComment},</if>
            <if test="dszUserId != null">dsz_user_id = #{dszUserId},</if>
            <if test="dszComment != null">dsz_comment = #{dszComment},</if>
            <if test="unionUserId != null">union_user_id = #{unionUserId},</if>
            <if test="unionComment != null">union_comment = #{unionComment},</if>
            <if test="cwUserId != null">cw_user_id = #{cwUserId},</if>
            <if test="cwComment != null">cw_comment = #{cwComment},</if>
            <if test="deptTime != null">dept_time = #{deptTime},</if>
            <if test="managerTime != null">manager_time = #{managerTime},</if>
            <if test="zjlTime != null">zjl_time = #{zjlTime},</if>
            <if test="dszTime != null">dsz_time = #{dszTime},</if>
            <if test="unionTime != null">union_time = #{unionTime},</if>
            <if test="lendTime != null">lend_time = #{lendTime},</if>
            <if test="lendDocument != null">lend_document = #{lendDocument},</if>
            <if test="borrowDocument != null">borrow_document = #{borrowDocument},</if>
        </trim>
        where borrow_id = #{borrowId}
    </update>

    <delete id="deleteCmcBorrowByBorrowId" parameterType="String">
        delete from cmc_borrow where borrow_id = #{borrowId}
    </delete>

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