<?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.ghy.goods.mapper.GoodsStandardMapper">

    <resultMap id="GoodsStandardResult" type="com.ghy.goods.domain.GoodsStandard">
        <result property="goodsStandardId" column="goods_standard_id"/>
        <result property="goodsStandardName" column="goods_standard_name"/>
        <result property="goodsId" column="goods_id"/>
        <result property="deptGoodsCategoryId" column="dept_goods_category_id"/>
        <result property="goodsPrice" column="goods_price"/>
        <result property="extMoney" column="ext_money"/>
        <result property="discountPrice" column="discount_price"/>
        <result property="groupPrice" column="group_price"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="goodsUnit" column="goods_unit"/>
        <result property="saleNum" column="sale_num"/>
        <result property="status" column="status"/>
        <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="remark" column="remark"/>

    </resultMap>

    <sql id="selectGoodsStandard">
        SELECT goods_standard_id,
               goods_standard_name,
               goods_id,
               dept_goods_category_id,
               goods_price,
               ext_money,
               discount_price,
               group_price,
               goods_num,
               goods_unit,
               create_by,
               create_time,
               sale_num,
               status,
               update_by,
               update_time,
               remark
        FROM goods_standard
    </sql>

    <select id="selectById" resultMap="GoodsStandardResult">
        <include refid="selectGoodsStandard"/>
        <where>
            <if test="goodsStandardId != null and goodsStandardId != 0">
                AND goods_standard_id = #{goodsStandardId}
            </if>
        </where>
    </select>

    <select id="selectByIds" resultMap="GoodsStandardResult">
        <include refid="selectGoodsStandard"/>
        where goods_standard_id in
        <foreach collection="collection" item="goodsStandardIds" open="(" separator="," close=")">
            #{goodsStandardIds}
        </foreach>
    </select>

    <select id="selectByGoodsId" parameterType="long" resultMap="GoodsStandardResult">
        <include refid="selectGoodsStandard"/>
        <where>
            <if test="goodsId != null ">
                AND goods_id = #{goodsId}
            </if>
        </where>
    </select>

    <select id="selectByGoodsStandardId" parameterType="long" resultMap="GoodsStandardResult">
        <include refid="selectGoodsStandard"/>
        <where>
            <if test="goodsId != null ">
                AND goods_standard_id = #{goodsId}
            </if>
        </where>
    </select>

    <insert id="batchInsert" parameterType="list">
        INSERT INTO goods_standard (
        goods_standard_name, goods_id, dept_goods_category_id, goods_price, ext_money, discount_price, group_price, goods_unit, goods_num,
        sale_num, status, remark, create_by, create_time )
        VALUES
        <foreach collection="goodsStandards" separator="," item="goodsStandard">
            (
            #{goodsStandard.goodsStandardName}, #{goodsStandard.goodsId}, #{goodsStandard.deptGoodsCategoryId}, #{goodsStandard.goodsPrice}, #{goodsStandard.extMoney}, #{goodsStandard.discountPrice},
            #{goodsStandard.groupPrice}, #{goodsStandard.goodsUnit}, #{goodsStandard.goodsNum}, 0, #{goodsStandard.status}, #{goodsStandard.remark}, #{goodsStandard.createBy}, sysdate()
            )
        </foreach>
    </insert>

    <select id="checkStore" resultMap="GoodsStandardResult">
        <include refid="selectGoodsStandard"/>
        where goods_standard_id = #{goodsStandardId}
        and goods_num >= #{num}
    </select>

    <delete id="deleteByGoodsId" parameterType="Long">
        DELETE FROM goods_standard
        WHERE goods_id = #{goodsId}
    </delete>

    <update id="updateSaleNum">
        update
            goods_standard
        set
            sale_num = #{saleNum}
        where
            goods_standard_id = #{goodsStandardId}
    </update>

    <update id="update">
        UPDATE goods_standard
        <set>
            <if test="goodsStandardName != null and goodsStandardName != ''">goods_standard_name = #{goodsStandardName},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="deptGoodsCategoryId != null and deptGoodsCategoryId != ''">dept_goods_category_id = #{deptGoodsCategoryId},</if>
            <if test="goodsPrice != null">goods_price = #{goodsPrice},</if>
            <if test="extMoney != null">ext_money = #{extMoney},</if>
            <if test="discountPrice != null">discount_price = #{discountPrice},</if>
            <if test="groupPrice != null">group_price = #{groupPrice},</if>
            <if test="goodsUnit != null">goods_unit = #{goodsUnit},</if>
            <if test="goodsNum != null">goods_num = #{goodsNum},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </set>
        WHERE goods_standard_id = #{goodsStandardId}
    </update>
</mapper>
