<?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.CmcMaterialBuyMapper">
    
    <resultMap type="CmcMaterialBuy" id="CmcMaterialBuyResult">
        <result property="buyId"    column="buy_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="buyTime"    column="buy_time"    />
        <result property="place"    column="place"    />
        <result property="totalNum"    column="total_num"    />
        <result property="price"    column="price"    />
        <result property="amount"    column="amount"    />
        <result property="buyer"    column="buyer"    />
        <result property="remark"    column="remark"    />
        <association property="buyUser"    javaType="SysUser"         resultMap="SysUserResult" />
        <association property="material"    javaType="CmcMaterial"         resultMap="CmcMaterialResult" />
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="buy_nick_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="selectCmcMaterialBuyVo">
        select mb.buy_id, mb.material_id, mb.buy_time, mb.place, mb.total_num, mb.price, mb.amount, mb.buyer, mb.remark, u.nick_name as buy_nick_name, m.brand, m.name, m.series from cmc_material_buy as mb
        left join sys_user as u on u.user_id = mb.buyer
        left join cmc_material as m on m.material_id = mb.material_id
    </sql>

    <select id="selectCmcMaterialBuyList" parameterType="CmcMaterialBuy" resultMap="CmcMaterialBuyResult">
        <include refid="selectCmcMaterialBuyVo"/>
        <where>  
            <if test="materialId != null "> and mb.material_id = #{materialId}</if>
            <if test="buyTime != null "> and mb.buy_time = #{buyTime}</if>
            <if test="place != null  and place != ''"> and mb.place = #{place}</if>
            <if test="totalNum != null "> and mb.total_num = #{totalNum}</if>
            <if test="price != null "> and mb.price = #{price}</if>
            <if test="amount != null "> and mb.amount = #{amount}</if>
            <if test="buyer != null "> and mb.buyer = #{buyer}</if>
        </where>
        order by mb.buy_time desc
    </select>
    
    <select id="selectCmcMaterialBuyByBuyId" parameterType="Integer" resultMap="CmcMaterialBuyResult">
        <include refid="selectCmcMaterialBuyVo"/>
        where mb.buy_id = #{buyId}
    </select>
        
    <insert id="insertCmcMaterialBuy" parameterType="CmcMaterialBuy" useGeneratedKeys="true" keyProperty="buyId">
        insert into cmc_material_buy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialId != null">material_id,</if>
            <if test="buyTime != null">buy_time,</if>
            <if test="place != null">place,</if>
            <if test="totalNum != null">total_num,</if>
            <if test="price != null">price,</if>
            <if test="amount != null">amount,</if>
            <if test="buyer != null">buyer,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialId != null">#{materialId},</if>
            <if test="buyTime != null">#{buyTime},</if>
            <if test="place != null">#{place},</if>
            <if test="totalNum != null">#{totalNum},</if>
            <if test="price != null">#{price},</if>
            <if test="amount != null">#{amount},</if>
            <if test="buyer != null">#{buyer},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCmcMaterialBuy" parameterType="CmcMaterialBuy">
        update cmc_material_buy
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="buyTime != null">buy_time = #{buyTime},</if>
            <if test="place != null">place = #{place},</if>
            <if test="totalNum != null">total_num = #{totalNum},</if>
            <if test="price != null">price = #{price},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="buyer != null">buyer = #{buyer},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where buy_id = #{buyId}
    </update>

    <delete id="deleteCmcMaterialBuyByBuyId" parameterType="Integer">
        delete from cmc_material_buy where buy_id = #{buyId}
    </delete>

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