<?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.CmcOutsourceMapper">
    
    <resultMap type="CmcOutsource" id="CmcOutsourceResult">
        <result property="outsourceId"    column="outsource_id"    />
        <result property="applyReason"    column="apply_reason"    />
        <result property="projectId"    column="project_id"    />
        <result property="partnerId"    column="partner_id"    />
        <result property="applier"    column="applier"    />
        <result property="applyDept"    column="apply_dept"    />
        <result property="applyTime"    column="apply_time"    />
        <result property="managerUserId"    column="manager_user_id"    />
        <result property="managerComment"    column="manager_comment"    />
        <result property="managerTime"    column="manager_time"    />
        <result property="zjlUserId"    column="zjl_user_id"    />
        <result property="zjlComment"    column="zjl_comment"    />
        <result property="zjlTime"    column="zjl_time"    />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
        <association property="applierUser"    javaType="SysUser"         resultMap="ApplierUserResult" />
        <association property="managerUser"    javaType="SysUser"         resultMap="ManagerUserResult" />
        <association property="zjlUser"    javaType="SysUser"         resultMap="ZjlUserResult" />
        <association property="dept"    javaType="SysDept"         resultMap="SysDeptResult" />
    </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>

    <resultMap type="SysUser" id="ApplierUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="applier_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="SysDept" id="SysDeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <sql id="selectCmcOutsourceVo">
        select o.outsource_id, o.apply_reason, o.project_id, p.project_number, p.project_name, o.partner_id, o.applier, u.nick_name as applier_nick_name,
               o.apply_dept, d.dept_name, o.apply_time, o.manager_user_id, u1.nick_name as manager_nick_name, o.manager_comment, o.manager_time,
               o.zjl_user_id, u2.nick_name as zjl_nick_name, o.zjl_comment, o.zjl_time from cmc_outsource as o
        left join cmc_project as p on o.project_id = p.project_id
        left join sys_user as u on u.user_id = o.applier
        left join sys_user as u1 on u1.user_id = o.manager_user_id
        left join sys_user as u2 on u2.user_id = o.zjl_user_id
        left join sys_dept as d on d.dept_id = o.apply_dept
    </sql>

    <select id="selectCmcOutsourceList" parameterType="CmcOutsource" resultMap="CmcOutsourceResult">
        <include refid="selectCmcOutsourceVo"/>
        <where>  
            <if test="applyReason != null  and applyReason != ''"> and o.apply_reason = #{applyReason}</if>
            <if test="projectId != null  and projectId != ''"> and o.project_id = #{projectId}</if>
            <if test="partnerId != null  and partnerId != ''"> and o.partner_id = #{partnerId}</if>
            <if test="applier != null "> and o.applier = #{applier}</if>
            <if test="applyDept != null "> and o.apply_dept = #{applyDept}</if>
            <if test="applyTime != null "> and o.apply_time = #{applyTime}</if>
            <if test="managerUserId != null "> and o.manager_user_id = #{managerUserId}</if>
            <if test="managerComment != null  and managerComment != ''"> and o.manager_comment = #{managerComment}</if>
            <if test="managerTime != null "> and o.manager_time = #{managerTime}</if>
            <if test="zjlUserId != null "> and o.zjl_user_id = #{zjlUserId}</if>
            <if test="zjlComment != null  and zjlComment != ''"> and o.zjl_comment = #{zjlComment}</if>
            <if test="zjlTime != null "> and o.zjl_time = #{zjlTime}</if>
        </where>
        order by o.apply_time desc
    </select>
    
    <select id="selectCmcOutsourceByOutsourceId" parameterType="String" resultMap="CmcOutsourceResult">
        <include refid="selectCmcOutsourceVo"/>
        where o.outsource_id = #{outsourceId}
    </select>
        
    <insert id="insertCmcOutsource" parameterType="CmcOutsource">
        insert into cmc_outsource
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="outsourceId != null">outsource_id,</if>
            <if test="applyReason != null">apply_reason,</if>
            <if test="projectId != null">project_id,</if>
            <if test="partnerId != null">partner_id,</if>
            <if test="applier != null">applier,</if>
            <if test="applyDept != null">apply_dept,</if>
            <if test="applyTime != null">apply_time,</if>
            <if test="managerUserId != null">manager_user_id,</if>
            <if test="managerComment != null">manager_comment,</if>
            <if test="managerTime != null">manager_time,</if>
            <if test="zjlUserId != null">zjl_user_id,</if>
            <if test="zjlComment != null">zjl_comment,</if>
            <if test="zjlTime != null">zjl_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="outsourceId != null">#{outsourceId},</if>
            <if test="applyReason != null">#{applyReason},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="partnerId != null">#{partnerId},</if>
            <if test="applier != null">#{applier},</if>
            <if test="applyDept != null">#{applyDept},</if>
            <if test="applyTime != null">#{applyTime},</if>
            <if test="managerUserId != null">#{managerUserId},</if>
            <if test="managerComment != null">#{managerComment},</if>
            <if test="managerTime != null">#{managerTime},</if>
            <if test="zjlUserId != null">#{zjlUserId},</if>
            <if test="zjlComment != null">#{zjlComment},</if>
            <if test="zjlTime != null">#{zjlTime},</if>
         </trim>
    </insert>

    <update id="updateCmcOutsource" parameterType="CmcOutsource">
        update cmc_outsource
        <trim prefix="SET" suffixOverrides=",">
            <if test="applyReason != null">apply_reason = #{applyReason},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="partnerId != null">partner_id = #{partnerId},</if>
            <if test="applier != null">applier = #{applier},</if>
            <if test="applyDept != null">apply_dept = #{applyDept},</if>
            <if test="applyTime != null">apply_time = #{applyTime},</if>
            <if test="managerUserId != null">manager_user_id = #{managerUserId},</if>
            <if test="managerComment != null">manager_comment = #{managerComment},</if>
            <if test="managerTime != null">manager_time = #{managerTime},</if>
            <if test="zjlUserId != null">zjl_user_id = #{zjlUserId},</if>
            <if test="zjlComment != null">zjl_comment = #{zjlComment},</if>
            <if test="zjlTime != null">zjl_time = #{zjlTime},</if>
        </trim>
        where outsource_id = #{outsourceId}
    </update>

    <delete id="deleteCmcOutsourceByOutsourceId" parameterType="String">
        delete from cmc_outsource where outsource_id = #{outsourceId}
    </delete>

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