<?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.GoodsSkuStoreMapper">

    <resultMap type="com.yiqi.core.domain.GoodsSkuStore" id="GoodsSkuStoreResult">
            <result property="id" column="id"/>
            <result property="goodsSkuId" column="goods_sku_id"/>
            <result property="sourceType" column="source_type"/>
            <result property="targetId" column="target_id"/>
            <result property="status" column="status"/>
            <result property="salePrice" column="sale_price"/>
            <result property="wholesalePrice" column="wholesale_price"/>
            <result property="purchasePrice" column="purchase_price"/>
            <result property="stock" column="stock"/>
            <result property="createBy" column="create_by"/>
            <result property="createById" column="create_by_id"/>
            <result property="createTime" column="create_time"/>
            <result property="updateBy" column="update_by"/>
            <result property="updateById" column="update_by_id"/>
            <result property="updateTime" column="update_time"/>
            <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectGoodsSkuStoreVo">
            select * from goods_sku_store
    </sql>

    <select id="selectGoodsSkuStoreList"  resultMap="GoodsSkuStoreResult">
        <include refid="selectGoodsSkuStoreVo"/>
        <where>
        </where>
    </select>

    <select id="selectGoodsSkuStoreById" parameterType="Long"
            resultMap="GoodsSkuStoreResult">
            <include refid="selectGoodsSkuStoreVo"/>
            where id = #{id}
    </select>
    <select id="getSkuStoreListBySkuId" resultMap="GoodsSkuStoreResult">
        <include refid="selectGoodsSkuStoreVo"/>
        where goods_sku_id = #{skuId}
    </select>
    <select id="getSkuStockByNow" resultMap="GoodsSkuStoreResult">
        select * from goods_sku_store where status = 0 and source_type = #{sourceType}
        <if test="orgId != null">
           and  target_id = #{orgId}
        </if>
        and `goods_sku_id` in
        <foreach collection="goodsSkuIdList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <insert id="insertGoodsSkuStore" useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_sku_store
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="goodsSkuId != null">goods_sku_id,
                    </if>
                    <if test="sourceType != null">source_type,
                    </if>
                    <if test="targetId != null">target_id,
                    </if>
                    <if test="status != null">status,
                    </if>
                    <if test="salePrice != null">sale_price,
                    </if>
                    <if test="wholesalePrice != null">wholesale_price,
                    </if>
                    <if test="purchasePrice != null">purchase_price,
                    </if>
                    <if test="stock != null">stock,
                    </if>
                    <if test="createBy != null">create_by,
                    </if>
                    <if test="createById != null">create_by_id,
                    </if>
                    <if test="createTime != null">create_time,
                    </if>
                    <if test="updateBy != null">update_by,
                    </if>
                    <if test="updateById != null">update_by_id,
                    </if>
                    <if test="updateTime != null">update_time,
                    </if>
                    <if test="remark != null">remark,
                    </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                    <if test="goodsSkuId != null">#{goodsSkuId},
                    </if>
                    <if test="sourceType != null">#{sourceType},
                    </if>
                    <if test="targetId != null">#{targetId},
                    </if>
                    <if test="status != null">#{status},
                    </if>
                    <if test="salePrice != null">#{salePrice},
                    </if>
                    <if test="wholesalePrice != null">#{wholesalePrice},
                    </if>
                    <if test="purchasePrice != null">#{purchasePrice},
                    </if>
                    <if test="stock != null">#{stock},
                    </if>
                    <if test="createBy != null">#{createBy},
                    </if>
                    <if test="createById != null">#{createById},
                    </if>
                    <if test="createTime != null">#{createTime},
                    </if>
                    <if test="updateBy != null">#{updateBy},
                    </if>
                    <if test="updateById != null">#{updateById},
                    </if>
                    <if test="updateTime != null">#{updateTime},
                    </if>
                    <if test="remark != null">#{remark},
                    </if>
        </trim>
    </insert>
    <insert id="insertSkuToAllFactory">
        insert into goods_sku_store (goods_sku_id, source_type, target_id, status, stock, create_by, create_by_id, create_time)
        select
            #{goodsSkuId} as goods_sku_id,
            '01' as source_type,
            f.id as target_id,
            '0' as status,
            #{stock} as stock,
            #{createBy} as create_by,
            #{createById} as create_by_id,
            #{createTime} as create_time
        from (
                 select id from manage_factory where del_flag = '0'
             ) as f
    </insert>
    <insert id="insertSkuToAllStore">
        insert into goods_sku_store (goods_sku_id, source_type, target_id, status, stock, create_by, create_by_id, create_time)
        select
            #{goodsSkuId} as goods_sku_id,
            '02' as source_type,
            f.id as target_id,
            '0' as status,
            #{stock} as stock,
            #{createBy} as create_by,
            #{createById} as create_by_id,
            #{createTime} as create_time
        from (
                 select id from sys_store where del_flag = '0'
             ) as f
    </insert>
    <insert id="insertFactoryToAllSku">
        insert into goods_sku_store (goods_sku_id, source_type, target_id, status, stock, create_by, create_by_id, create_time)
        select
            f.id as goods_sku_id,
            '01' as source_type,
            #{id} as target_id,
            '0' as status,
            0 as stock,
            #{createBy} as create_by,
            #{createById} as create_by_id,
            #{createTime} as create_time
        from (
                 select sku.id, store.wholesale_price
                 from goods_sku sku left join goods_sku_store store on sku.id = store.goods_sku_id
                 where sku.del_flag = '0' and store.source_type = '00'
                 ) as f
    </insert>
    <insert id="insertStoreToAllSku">
        insert into goods_sku_store (goods_sku_id, source_type, target_id, status, stock, create_by, create_by_id, create_time)
        select
            f.id as goods_sku_id,
            '02' as source_type,
            #{id} as target_id,
            '0' as status,
            0 as stock,
            #{createBy} as create_by,
            #{createById} as create_by_id,
            #{createTime} as create_time
        from (
                 select sku.id
                 from goods_sku sku left join goods_sku_store store on sku.id = store.goods_sku_id
                          where sku.del_flag = '0' and store.source_type = '00'
             ) as f
    </insert>

    <update id="updateGoodsSkuStore" >
        update goods_sku_store
        <trim prefix="SET" suffixOverrides=",">
                    <if test="goodsSkuId != null">goods_sku_id =
                        #{goodsSkuId},
                    </if>
                    <if test="sourceType != null">source_type =
                        #{sourceType},
                    </if>
                    <if test="targetId != null">target_id =
                        #{targetId},
                    </if>
                    <if test="status != null">status =
                        #{status},
                    </if>
                    <if test="salePrice != null">sale_price =
                        #{salePrice},
                    </if>
                    <if test="wholesalePrice != null">wholesale_price =
                        #{wholesalePrice},
                    </if>
                    <if test="purchasePrice != null">purchase_price =
                        #{purchasePrice},
                    </if>
                    <if test="createBy != null">create_by =
                        #{createBy},
                    </if>
                    <if test="createById != null">create_by_id =
                        #{createById},
                    </if>
                    <if test="createTime != null">create_time =
                        #{createTime},
                    </if>
                    <if test="updateBy != null">update_by =
                        #{updateBy},
                    </if>
                    <if test="updateById != null">update_by_id =
                        #{updateById},
                    </if>
                    <if test="updateTime != null">update_time =
                        #{updateTime},
                    </if>
                    <if test="remark != null">remark =
                        #{remark},
                    </if>
        </trim>
        where id = #{id}
    </update>


    <update id="addGoodsSkuStockByOrderItems">
        UPDATE goods_sku_store
        <trim prefix="set" suffixOverrides=",">
            <trim prefix=" `stock` = case " suffix="else stock end, ">
                <foreach collection="list" item="item">
                    WHEN id=#{item.id}
                    THEN stock + #{item.stock}
                </foreach>
            </trim>
            <trim prefix=" `update_by` = case " suffix="else  update_by end,">
                <foreach collection="list" item="item">
                    WHEN id=#{item.id}
                    THEN #{item.updateBy}
                </foreach>
            </trim>
            <trim prefix=" `update_by_id` = case " suffix="else  update_by_id end,">
                <foreach collection="list" item="item">
                    WHEN id=#{item.id}
                    THEN #{item.updateById}
                </foreach>
            </trim>
            <trim prefix=" `update_time` = case " suffix="else  update_time end,">
                <foreach collection="list" item="item">
                    WHEN id=#{item.id}
                    THEN #{item.updateTime}
                </foreach>
            </trim>
        </trim>
        where
        `id` in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>


    <update id="minusGoodsSkuStockByOrder">
        UPDATE goods_sku_store
        <trim prefix="set" suffixOverrides=",">
            <trim prefix=" `stock` = case " suffix="else stock end, ">
                <foreach collection="list" item="item">
                    WHEN goods_sku_id=#{item.goodsSkuId}
                    <if test="item.sourceType != null  and item.sourceType != ''">
                        and source_type = #{item.sourceType}
                    </if>
                    <if test="item.targetId != null  and item.targetId != ''">
                        and target_id = #{item.targetId}
                    </if>
                    THEN stock - #{item.stock}
                </foreach>
            </trim>
            <trim prefix=" `update_by` = case " suffix="else  update_by end,">
                <foreach collection="list" item="item">
                    WHEN goods_sku_id=#{item.goodsSkuId}
                    <if test="item.sourceType != null  and item.sourceType != ''">
                        and source_type = #{item.sourceType}
                    </if>
                    <if test="item.targetId != null  and item.targetId != ''">
                        and target_id = #{item.targetId}
                    </if>
                    THEN #{item.updateBy}
                </foreach>
            </trim>
            <trim prefix=" `update_by_id` = case " suffix="else  update_by_id end,">
                <foreach collection="list" item="item">
                    WHEN goods_sku_id=#{item.goodsSkuId}
                    <if test="item.sourceType != null  and item.sourceType != ''">
                        and source_type = #{item.sourceType}
                    </if>
                    <if test="item.targetId != null  and item.targetId != ''">
                        and target_id = #{item.targetId}
                    </if>
                    THEN #{item.updateById}
                </foreach>
            </trim>
            <trim prefix=" `update_time` = case " suffix="else  update_time end,">
                <foreach collection="list" item="item">
                    WHEN goods_sku_id=#{item.goodsSkuId}
                    <if test="item.sourceType != null  and item.sourceType != ''">
                        and source_type = #{item.sourceType}
                    </if>
                    <if test="item.targetId != null  and item.targetId != ''">
                        and target_id = #{item.targetId}
                    </if>
                    THEN #{item.updateTime}
                </foreach>
            </trim>
        </trim>
        where
        `goods_sku_id` in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item.goodsSkuId}
        </foreach>
    </update>

    <delete id="deleteGoodsSkuStoreById" parameterType="Long">
            delete from goods_sku_store where id = #{id}
        </delete>

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



</mapper>