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

    <resultMap type="com.yiqi.core.domain.GoodsStore" id="GoodsStoreResult">
            <result property="id" column="id"/>
            <result property="sourceType" column="source_type"/>
            <result property="targetId" column="target_id"/>
            <result property="goodsId" column="goods_id"/>
            <result property="status" column="status"/>
            <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="selectGoodsStoreVo">
        select * from goods_store
    </sql>

    <select id="selectGoodsStoreList"  resultMap="GoodsStoreResult">
        <include refid="selectGoodsStoreVo"/>
        <where>
            <if test="sourceType != null  and sourceType != ''">
                and source_type = #{sourceType}
            </if>
            <if test="targetId != null ">
                and target_id = #{targetId}
            </if>
            <if test="goodsId != null ">
                and goods_id = #{goodsId}
            </if>
            <if test="status != null  and status != ''">
                and status = #{status}
            </if>
        </where>
    </select>

    <select id="selectGoodsStoreById" parameterType="Long"
            resultMap="GoodsStoreResult">
            <include refid="selectGoodsStoreVo"/>
            where id = #{id}
    </select>

    <insert id="insertGoodsStore"  useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_store
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="sourceType != null">source_type,
                    </if>
                    <if test="targetId != null">target_id,
                    </if>
                    <if test="goodsId != null">goods_id,
                    </if>
                    <if test="status != null">status,
                    </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="sourceType != null">#{sourceType},
                    </if>
                    <if test="targetId != null">#{targetId},
                    </if>
                    <if test="goodsId != null">#{goodsId},
                    </if>
                    <if test="status != null">#{status},
                    </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="insertGoodsToAllFactory" >
        insert into goods_store (source_type, target_id, goods_id, status, create_by, create_by_id, create_time)
        select
            '01' as source_type,
            f.id as target_id,
            #{goodsId} as goods_id,
            '0' as status,
            #{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="insertGoodsToAllStore" >
        insert into goods_store (source_type, target_id, goods_id, status, create_by, create_by_id, create_time)
    select
        '02' as source_type,
        f.id as target_id,
        #{goodsId} as goods_id,
        '0' as status,
        #{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="insertFactoryToAllGoods" >
        insert into goods_store (source_type, target_id, goods_id, status, create_by, create_by_id, create_time)
        select
            '01' as source_type,
            #{id} as target_id,
            f.id as goods_id,
            '0' as status,
            #{createBy} as create_by,
            #{createById} as create_by_id,
            #{createTime} as create_time
        from (
                 select id from goods_info where del_flag = '0'
             ) as f
    </insert>
    <insert id="insertStoreToAllGoods" >
        insert into goods_store (source_type, target_id, goods_id, status, create_by, create_by_id, create_time)
        select
            '01' as source_type,
            #{id} as target_id,
            f.id as goods_id,
            '0' as status,
            #{createBy} as create_by,
            #{createById} as create_by_id,
            #{createTime} as create_time
        from (
                 select id from goods_info where del_flag = '0'
             ) as f
    </insert>

    <update id="updateGoodsStore" >
        update goods_store
        <trim prefix="SET" suffixOverrides=",">
                    <if test="sourceType != null">source_type =
                        #{sourceType},
                    </if>
                    <if test="targetId != null">target_id =
                        #{targetId},
                    </if>
                    <if test="goodsId != null">goods_id =
                        #{goodsId},
                    </if>
                    <if test="status != null">status =
                        #{status},
                    </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>

        <delete id="deleteGoodsStoreById" parameterType="Long">
            delete from goods_store where id = #{id}
        </delete>

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



</mapper>