<?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.system.mapper.TGoodsMapper">
    
    <resultMap type="TGoods" id="TGoodsResult">
        <result property="id"    column="id"    />
        <result property="goodsName"    column="goods_name"    />
        <result property="goodsType"    column="goods_type"    />
        <result property="goodsUnit"    column="goods_unit"    />
        <result property="goodsNum"    column="goods_num"    />
        <result property="upTime"    column="up_time"    />
        <result property="saveYear"    column="save_year"    />
        <result property="scrapNum"    column="scrap_num"    />
        <result property="leaveNum"    column="leave_num"    />
        <result property="makeTime"    column="make_time"    />
        <result property="remark"    column="remark"    />
        <result property="kufangId"    column="kufang_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTGoodsVo">
        select id, goods_name, goods_type, goods_unit, goods_num, up_time, save_year, scrap_num, leave_num, make_time, remark, kufang_id, create_by, create_time, update_by, update_time from t_goods
    </sql>

    <select id="selectTGoodsList" parameterType="TGoods" resultMap="TGoodsResult">
            select tg.id, goods_name, goods_type, goods_unit, goods_num, up_time, save_year, scrap_num, leave_num, make_time, remark, kufang_id, create_by, tg.create_time, update_by, update_time
            from t_goods tg

            <if test="roleId != null and roleId != '' and roleId > 2">
                left join t_kufang tk on tg.kufang_id = tk.id
            </if>
            <where>
                <if test="goodsName != null  and goodsName != ''"> and goods_name like '%'+#{goodsName}+'%' </if>
                <if test="goodsType != null  and goodsType != ''"> and goods_type = #{goodsType}</if>
                <if test="goodsUnit != null  and goodsUnit != ''"> and goods_unit = #{goodsUnit}</if>
                <if test="goodsNum != null "> and goods_num = #{goodsNum}</if>
                <if test="startTime != null and startTime != '' and endTime != null and endTime != '' ">
                 and up_time BETWEEN #{startTime} and #{endTime}
                 </if>
                <if test="saveYear != null "> and save_year = #{saveYear}</if>
                <if test="scrapNum != null "> and scrap_num = #{scrapNum}</if>
                <if test="leaveNum != null "> and leave_num = #{leaveNum}</if>
                <if test="makeTime != null "> and make_time = #{makeTime}</if>
                <if test="kufangId != null "> and kufang_id = #{kufangId}</if>
                <if test="roleId != null and roleId != '' and roleId > 2">
                    and tk.kf_admin = #{loginUserId}
                </if>
            </where>

    </select>
    
    <select id="selectTGoodsById" parameterType="Long" resultMap="TGoodsResult">
        <include refid="selectTGoodsVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTGoods" parameterType="TGoods" useGeneratedKeys="true" keyProperty="id">
        insert into t_goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsName != null">goods_name,</if>
            <if test="goodsType != null">goods_type,</if>
            <if test="goodsUnit != null">goods_unit,</if>
            <if test="goodsNum != null">goods_num,</if>
            <if test="upTime != null">up_time,</if>
            <if test="saveYear != null">save_year,</if>
            <if test="scrapNum != null">scrap_num,</if>
            <if test="leaveNum != null">leave_num,</if>
            <if test="makeTime != null">make_time,</if>
            <if test="remark != null">remark,</if>
            <if test="kufangId != null">kufang_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsName != null">#{goodsName},</if>
            <if test="goodsType != null">#{goodsType},</if>
            <if test="goodsUnit != null">#{goodsUnit},</if>
            <if test="goodsNum != null">#{goodsNum},</if>
            <if test="upTime != null">#{upTime},</if>
            <if test="saveYear != null">#{saveYear},</if>
            <if test="scrapNum != null">#{scrapNum},</if>
            <if test="leaveNum != null">#{leaveNum},</if>
            <if test="makeTime != null">#{makeTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="kufangId != null">#{kufangId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTGoods" parameterType="TGoods">
        update t_goods
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsName != null">goods_name = #{goodsName},</if>
            <if test="goodsType != null">goods_type = #{goodsType},</if>
            <if test="goodsUnit != null">goods_unit = #{goodsUnit},</if>
            <if test="goodsNum != null">goods_num = #{goodsNum},</if>
            <if test="upTime != null">up_time = #{upTime},</if>
            <if test="saveYear != null">save_year = #{saveYear},</if>
            <if test="scrapNum != null">scrap_num = #{scrapNum},</if>
            <if test="leaveNum != null">leave_num = #{leaveNum},</if>
            <if test="makeTime != null">make_time = #{makeTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="kufangId != null">kufang_id = #{kufangId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTGoodsById" parameterType="Long">
        delete from t_goods where id = #{id}
    </delete>

    <delete id="deleteTGoodsByIds" parameterType="String">
        delete from t_goods where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getKuFangList" resultType="java.util.Map">
        select tk.id, kf_name kfName, kf_address kfAddress, su.user_name userName from t_kufang tk
        left join sys_user su on tk.kf_admin = su.user_id
        <where>
            <if test="roleId > 2">
                and kf_admin = #{userId}
            </if>
        </where>
    </select>

    <select id="getRoleIdByUser" parameterType="Long" >
        select role_id from sys_user_role where user_id = #{userId}
    </select>
</mapper>