<?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.younike.system.mapper.GoodsServerMapper">
    
    <resultMap type="GoodsServer" id="GoodsServerResult">
        <result property="goodsId"    column="goods_id"    />
        <result property="serverId"    column="server_id"    />
        <result property="goodsServerPrice"    column="goods_server_price"    />
        <result property="unshelveTime"    column="unshelve_time"    />
        <result property="unshelveReason"    column="unshelve_reason"    />
        <result property="sorting"    column="sorting"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
    </resultMap>

    <resultMap type="GoodsServerResultVo" id="GoodsServerListResult">
        <result property="goodsId"    column="goods_id"    />
        <result property="serverId"    column="server_id"    />
        <result property="goodsServerPrice"    column="goods_server_price"    />
        <result property="unshelveTime"    column="unshelve_time"    />
        <result property="unshelveReason"    column="unshelve_reason"    />
        <result property="sorting"    column="sorting"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
        <result property="categoryName" column="category_name"/>
        <result property="unitName" column="decode_value"/>
        <association property="goods" resultMap="com.younike.system.mapper.GoodsMapper.GoodsResult"/>
    </resultMap>

    <sql id="selectGoodsServerVo">
        select goods_id, server_id, goods_server_price, unshelve_time, unshelve_reason, sorting, del_flag, create_by, create_time, update_by, update_time, column1, column2, column3 from goods_server
    </sql>

    <select id="serverGoodsList" resultMap="GoodsServerListResult">
        select gs.*, g.*, c.category_name, sc.decode_value
        from goods_server gs
        left join goods g on gs.goods_id = g.goods_id
        left join sys_code sc on g.goods_unit = sc.decode_id and sc.code_id = '011'
        left join category c on g.cate_id = c.category_id and c.del_flag = 0
        where gs.server_id = #{serverId}
        and gs.del_flag = 0
        and g.del_flag = 0
    </select>

    <select id="selectGoodsServerList" parameterType="GoodsServer" resultMap="GoodsServerResult">
        <include refid="selectGoodsServerVo"/>
        <where>  
            <if test="serverId != null "> and server_id = #{serverId}</if>
            <if test="goodsServerPrice != null "> and goods_server_price = #{goodsServerPrice}</if>
            <if test="unshelveTime != null "> and unshelve_time = #{unshelveTime}</if>
            <if test="unshelveReason != null  and unshelveReason != ''"> and unshelve_reason = #{unshelveReason}</if>
            <if test="sorting != null "> and sorting = #{sorting}</if>
            <if test="column1 != null  and column1 != ''"> and column1 = #{column1}</if>
            <if test="column2 != null  and column2 != ''"> and column2 = #{column2}</if>
            <if test="column3 != null  and column3 != ''"> and column3 = #{column3}</if>
        </where>
    </select>
    
    <select id="selectGoodsServerByGoodsId" parameterType="Long" resultMap="GoodsServerResult">
        <include refid="selectGoodsServerVo"/>
        where goods_id = #{goodsId}
    </select>
        
    <insert id="insertGoodsServer" parameterType="GoodsServer">
        insert into goods_server
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">goods_id,</if>
            <if test="serverId != null">server_id,</if>
            <if test="goodsServerPrice != null">goods_server_price,</if>
            <if test="unshelveTime != null">unshelve_time,</if>
            <if test="unshelveReason != null">unshelve_reason,</if>
            <if test="sorting != null">sorting,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</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>
            <if test="column1 != null">column1,</if>
            <if test="column2 != null">column2,</if>
            <if test="column3 != null">column3,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">#{goodsId},</if>
            <if test="serverId != null">#{serverId},</if>
            <if test="goodsServerPrice != null">#{goodsServerPrice},</if>
            <if test="unshelveTime != null">#{unshelveTime},</if>
            <if test="unshelveReason != null">#{unshelveReason},</if>
            <if test="sorting != null">#{sorting},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</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>
            <if test="column1 != null">#{column1},</if>
            <if test="column2 != null">#{column2},</if>
            <if test="column3 != null">#{column3},</if>
         </trim>
    </insert>

    <update id="updateGoodsServer" parameterType="GoodsServer">
        update goods_server
        <trim prefix="SET" suffixOverrides=",">
            <if test="serverId != null">server_id = #{serverId},</if>
            <if test="goodsServerPrice != null">goods_server_price = #{goodsServerPrice},</if>
            <if test="unshelveTime != null">unshelve_time = #{unshelveTime},</if>
            <if test="unshelveReason != null">unshelve_reason = #{unshelveReason},</if>
            <if test="sorting != null">sorting = #{sorting},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</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>
            <if test="column1 != null">column1 = #{column1},</if>
            <if test="column2 != null">column2 = #{column2},</if>
            <if test="column3 != null">column3 = #{column3},</if>
        </trim>
        where goods_id = #{goodsId}
    </update>

    <delete id="deleteGoodsServerByGoodsId" parameterType="Long">
        delete from goods_server where goods_id = #{goodsId}
    </delete>

    <delete id="deleteGoodsServerByGoodsIds" parameterType="String">
        delete from goods_server where goods_id in 
        <foreach item="goodsId" collection="array" open="(" separator="," close=")">
            #{goodsId}
        </foreach>
    </delete>
</mapper>