<?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.CmcMaterialGiveMapper">
    
    <resultMap type="CmcMaterialGive" id="CmcMaterialGiveResult">
        <result property="giveId"    column="give_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="giveTime"    column="give_time"    />
        <result property="accepter"    column="accepter"    />
        <result property="useDept"    column="use_dept"    />
        <result property="giveNum"    column="give_num"    />
        <result property="remark"    column="remark"    />
        <association property="acceptUser"    javaType="SysUser"         resultMap="SysUserResult" />
        <association property="dept"    javaType="SysDept"         resultMap="deptResult" />
        <association property="material"    javaType="CmcMaterial"         resultMap="CmcMaterialResult" />
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="accept_nick_name"    />
    </resultMap>

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

    <resultMap type="CmcMaterial" id="CmcMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="name"    column="name"    />
        <result property="series"    column="series"    />
        <result property="brand"    column="brand"    />
    </resultMap>

    <sql id="selectCmcMaterialGiveVo">
        select mg.give_id, mg.material_id, mg.give_time, mg.accepter, mg.use_dept, mg.give_num, mg.remark, d.dept_name, u.nick_name as accept_nick_name, m.brand, m.name, m.series from cmc_material_give as mg
        left join sys_user as u on u.user_id = mg.accepter
        left join sys_dept as d on d.dept_id = mg.use_dept
        left join cmc_material as m on m.material_id = mg.material_id
    </sql>

    <select id="selectCmcMaterialGiveList" parameterType="CmcMaterialGive" resultMap="CmcMaterialGiveResult">
        <include refid="selectCmcMaterialGiveVo"/>
        <where>  
            <if test="materialId != null "> and mg.material_id = #{materialId}</if>
            <if test="giveTime != null "> and mg.give_time = #{giveTime}</if>
            <if test="accepter != null "> and mg.accepter = #{accepter}</if>
            <if test="useDept != null "> and mg.use_dept = #{useDept}</if>
            <if test="giveNum != null "> and mg.give_num = #{giveNum}</if>
        </where>
        order by mg.give_time desc
    </select>
    
    <select id="selectCmcMaterialGiveByGiveId" parameterType="Integer" resultMap="CmcMaterialGiveResult">
        <include refid="selectCmcMaterialGiveVo"/>
        where mg.give_id = #{giveId}
    </select>
        
    <insert id="insertCmcMaterialGive" parameterType="CmcMaterialGive" useGeneratedKeys="true" keyProperty="giveId">
        insert into cmc_material_give
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialId != null">material_id,</if>
            <if test="giveTime != null">give_time,</if>
            <if test="accepter != null">accepter,</if>
            <if test="useDept != null">use_dept,</if>
            <if test="giveNum != null">give_num,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialId != null">#{materialId},</if>
            <if test="giveTime != null">#{giveTime},</if>
            <if test="accepter != null">#{accepter},</if>
            <if test="useDept != null">#{useDept},</if>
            <if test="giveNum != null">#{giveNum},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCmcMaterialGive" parameterType="CmcMaterialGive">
        update cmc_material_give
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="giveTime != null">give_time = #{giveTime},</if>
            <if test="accepter != null">accepter = #{accepter},</if>
            <if test="useDept != null">use_dept = #{useDept},</if>
            <if test="giveNum != null">give_num = #{giveNum},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where give_id = #{giveId}
    </update>

    <delete id="deleteCmcMaterialGiveByGiveId" parameterType="Integer">
        delete from cmc_material_give where give_id = #{giveId}
    </delete>

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