<?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.yiqi.core.mapper.GoodsSkuStockRecordMapper">

    <resultMap type="GoodsSkuStockRecord" id="GoodsSkuStockRecordResult">
        <result property="id" column="id"/>
        <result property="goodsSkuId" column="goods_sku_id"/>
        <result property="buyOrgId" column="buy_org_id"/>
        <result property="buySourceType" column="buy_source_type"/>
        <result property="num" column="num"/>
        <result property="isAdd" column="is_add"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="sellOrgId" column="sell_org_id"/>
        <result property="sellSourceType" column="sell_source_type"/>
        <result property="orgId" column="org_id"/>
        <result property="sourceType" column="source_type"/>
        <result property="orderId" column="order_id"/>
        <result property="orderNo" column="order_no"/>
        <result property="goodsId" column="goods_id"/>
        <result property="goodsType" column="goods_type"/>
        <result property="isRefund" column="is_refund"/>
    </resultMap>

    <sql id="selectGoodsSkuStockRecordVo">
        select id, goods_sku_id, buy_org_id, buy_source_type, org_id, source_type, num, is_add, create_by_id, create_by, create_time, update_by_id, update_by, update_time, remark, sell_org_id, sell_source_type, order_id, order_no, goods_id, goods_type, is_refund
        from goods_sku_stock_record
    </sql>

    <select id="selectGoodsSkuStockRecordList" resultType="com.yiqi.core.domain.vo.GoodsSkuStockRecordVO">
        select a.*, b.sku_name as goodsSkuName, c.goods_name as goodsName
        from goods_sku_stock_record a
            left join order_purchase_goods_sku b on a.order_id = b.order_id and a.goods_sku_id = b.sku_id
            left join goods_info c on a.goods_id = c.id
        <where>
            goods_sku_id = #{goodsSkuId}
            <if test="orgId != null ">
                and org_id = #{orgId}
            </if>
            <if test="sourceType != null ">
                and source_type = #{sourceType}
            </if>
            <if test="buyOrgId != null ">
                and buy_org_id = #{buyOrgId}
            </if>
            <if test="buySourceType != null  and buySourceType != ''">
                and buy_source_type = #{buySourceType}
            </if>
            <if test="isAdd != null  and isAdd != ''">
                and is_add = #{isAdd}
            </if>
            <if test="orderId != null ">
                and order_id = #{orderId}
            </if>
            <if test="orderNo != null  and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="goodsId != null ">
                and goods_id = #{goodsId}
            </if>
            <if test="goodsType != null  and goodsType != ''">
                and goods_type = #{goodsType}
            </if>
            <if test="isRefund != null  and isRefund != ''">
                and is_refund = #{isRefund}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectGoodsSkuStockRecordById" parameterType="Long"
            resultMap="GoodsSkuStockRecordResult">
        <include refid="selectGoodsSkuStockRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertGoodsSkuStockRecord" parameterType="GoodsSkuStockRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_sku_stock_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsSkuId != null">goods_sku_id,
            </if>
            <if test="buyOrgId != null">buy_org_id,
            </if>
            <if test="buySourceType != null">buy_source_type,
            </if>
            <if test="num != null">num,
            </if>
            <if test="isAdd != null">is_add,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
            <if test="sellOrgId != null">sell_org_id,
            </if>
            <if test="sellSourceType != null">sell_source_type,
            </if>
            <if test="orderId != null">order_id,
            </if>
            <if test="orderNo != null">order_no,
            </if>
            <if test="goodsId != null">goods_id,
            </if>
            <if test="goodsType != null">goods_type,
            </if>
            <if test="isRefund != null">is_refund,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsSkuId != null">#{goodsSkuId},
            </if>
            <if test="buyOrgId != null">#{buyOrgId},
            </if>
            <if test="buySourceType != null">#{buySourceType},
            </if>
            <if test="num != null">#{num},
            </if>
            <if test="isAdd != null">#{isAdd},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
            <if test="sellOrgId != null">#{sellOrgId},
            </if>
            <if test="sellSourceType != null">#{sellSourceType},
            </if>
            <if test="orderId != null">#{orderId},
            </if>
            <if test="orderNo != null">#{orderNo},
            </if>
            <if test="goodsId != null">#{goodsId},
            </if>
            <if test="goodsType != null">#{goodsType},
            </if>
            <if test="isRefund != null">#{isRefund},
            </if>
        </trim>
    </insert>

    <update id="updateGoodsSkuStockRecord" parameterType="GoodsSkuStockRecord">
        update goods_sku_stock_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsSkuId != null">goods_sku_id =
                #{goodsSkuId},
            </if>
            <if test="buyOrgId != null">buy_org_id =
                #{buyOrgId},
            </if>
            <if test="buySourceType != null">buy_source_type =
                #{buySourceType},
            </if>
            <if test="num != null">num =
                #{num},
            </if>
            <if test="isAdd != null">is_add =
                #{isAdd},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
            <if test="sellOrgId != null">sell_org_id =
                #{sellOrgId},
            </if>
            <if test="sellSourceType != null">sell_source_type =
                #{sellSourceType},
            </if>
            <if test="orderId != null">order_id =
                #{orderId},
            </if>
            <if test="orderNo != null">order_no =
                #{orderNo},
            </if>
            <if test="goodsId != null">goods_id =
                #{goodsId},
            </if>
            <if test="goodsType != null">goods_type =
                #{goodsType},
            </if>
            <if test="isRefund != null">is_refund =
                #{isRefund},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGoodsSkuStockRecordById" parameterType="Long">
        delete from goods_sku_stock_record where id = #{id}
    </delete>

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



</mapper>