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

    <resultMap type="com.yiqi.core.domain.GoodsSku" id="GoodsSkuResult">
            <result property="id" column="id"/>
            <result property="goodsId" column="goods_id"/>
            <result property="sourceType" column="source_type"/>
            <result property="status" column="status"/>
            <result property="delFlag" column="del_flag"/>
            <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="selectGoodsSkuVo">
        select * from goods_sku
    </sql>

    <select id="selectGoodsSkuInfoByHq" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select a.id as id, a.goods_id as goodsId, a.status as status,
               c.target_id as targetId, c.source_type as sourceType,
               c.sale_price as salePrice, c.wholesale_price as wholesalePrice,
               c.purchase_price as purchasePrice, c.stock as stock
        from goods_sku a
        left join goods_sku_store c on c.goods_sku_id = a.id and c.source_type = #{sourceType}
            where a.del_flag = 0
            <if test="goodsId != null  and goodsId != ''">
                and  a.goods_id = #{goodsId}
            </if>
            <if test="targetId != null  and targetId != ''">
                and c.target_id = #{targetId}
            </if>
            <if test="goodsSkuId != null  and goodsSkuId != ''">
                and c.goods_sku_id = #{goodsSkuId}
            </if>
            <if test="goodsIdList != null">
                and a.goods_id in
                <foreach item="goodsIdList" collection="array" open="(" separator="," close=")">
                    #{goodsIdList}
                </foreach>
            </if>

    </select>

    <select id="selectGoodsSkuInfoByFactory" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select a.id as id, a.goods_id as goodsId, a.status as status,
        c.target_id as targetId, c.source_type as sourceType,
        c.sale_price as salePrice,c.wholesale_price as wholesalePrice,
        d.wholesale_price as purchasePrice, c.stock as stock
        from goods_sku a
        left join goods_sku_store c on c.goods_sku_id = a.id
        left join goods_sku_store d on d.goods_sku_id = a.id and d.source_type = '00'
        where a.del_flag = 0
        <if test="goodsId != null  and goodsId != ''">
            and  a.goods_id = #{goodsId}
        </if>
        <if test="targetId != null  and targetId != ''">
            and c.target_id = #{targetId}
        </if>
        <if test="sourceType != null  and sourceType != ''">
            and c.source_type = #{sourceType}
        </if>
        <if test="goodsSkuId != null  and goodsSkuId != ''">
            and c.goods_sku_id = #{goodsSkuId}
        </if>
    </select>

    <select id="selectGoodsSkuInfoByStore" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select b.id as id, b.goods_id as goodsId, b.status as status,
        a.target_id as targetId, a.source_type as sourceType,
        a.sale_price as salePrice, a.stock as stock,
        d.wholesale_price as purchasePrice
        from goods_sku_store a
        left join goods_sku b on b.id = a.goods_sku_id
        left join sys_store c on c.id = a.target_id
        left join goods_sku_store d on a.goods_sku_id = d.goods_sku_id and d.source_type = '01' and d.target_id = c.factory_id
        where b.del_flag = 0
        <if test="goodsId != null  and goodsId != ''">
            and  b.goods_id = #{goodsId}
        </if>
        <if test="targetId != null  and targetId != ''">
            and a.target_id = #{targetId}
        </if>
        <if test="sourceType != null  and sourceType != ''">
            and a.source_type = #{sourceType}
        </if>
        <if test="goodsSkuId != null  and goodsSkuId != ''">
            and a.goods_sku_id = #{goodsSkuId}
        </if>
    </select>

    <select id="selectGoodsSkuList"  resultMap="GoodsSkuResult">
        <include refid="selectGoodsSkuVo"/>
        <where>
            <if test="goodsId != null ">
                and goods_id = #{goodsId}
            </if>
            <if test="sourceType != null  and sourceType != ''">
                and source_type = #{sourceType}
            </if>
            and  del_flag = '0'
        </where>
    </select>

    <select id="selectGoodsSkuById" parameterType="Long"
            resultMap="GoodsSkuResult">
            <include refid="selectGoodsSkuVo"/>
            where id = #{id} and  del_flag = '0'
    </select>

    <insert id="insertGoodsSku"  useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_sku
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">goods_id,
            </if>
            <if test="sourceType != null">source_type,
            </if>
            <if test="status != null">status,
            </if>
            <if test="delFlag != null">del_flag,
            </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="goodsId != null">#{goodsId},
            </if>
            <if test="sourceType != null">#{sourceType},
            </if>
            <if test="status != null">#{status},
            </if>
            <if test="delFlag != null">#{delFlag},
            </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>

    <update id="updateGoodsSku" >
        update goods_sku
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsId != null">goods_id =
                #{goodsId},
            </if>
            <if test="sourceType != null">source_type =
                #{sourceType},
            </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="deleteGoodsSkuById" parameterType="Long">
            update goods_sku set del_flag = '2' where id = #{id}
        </delete>

        <delete id="deleteGoodsSkuByIds" parameterType="String">
            update goods_sku set del_flag = '2' where id in
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
        </delete>

    <select id="selectPurchaseGoodsSkuInfoByHq" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select a.id as id, a.goods_id as goodsId, a.status as status,
        e.goods_name as goodsName, c.id as goodsSkuStoreId,
        c.purchase_price as purchasePrice, c.stock as stock
        from goods_sku a
        left join goods_info e on e.id = a.goods_id
        left join goods_sku_store c on c.goods_sku_id = a.id and c.source_type = '00'
        where a.del_flag = 0 and a.status = 0
        <if test="goodsIdList != null">
            and a.goods_id in
            <foreach item="item" collection="goodsIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

    </select>

    <select id="selectPurchaseGoodsSkuInfoByFactory" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select a.id as id, a.goods_id as goodsId, a.status as status,
        e.goods_name as goodsName, c.id as goodsSkuStoreId,
        c.wholesale_price as purchasePrice, c.stock as stock
        from goods_sku a
        left join goods_info e on e.id = a.goods_id
        left join goods_sku_store c on c.goods_sku_id = a.id and c.source_type = '00'
        where a.del_flag = 0 and a.status = 0
        <if test="goodsIdList != null">
            and a.goods_id in
            <foreach item="item" collection="goodsIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectPurchaseGoodsSkuInfoByStore" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select a.id as id, a.goods_id as goodsId, a.status as status,
        e.goods_name as goodsName, c.id as goodsSkuStoreId,
        c.wholesale_price as purchasePrice, c.stock as stock
        from goods_sku a
        left join goods_info e on e.id = a.goods_id
        left join goods_sku_store c on c.goods_sku_id = a.id and c.source_type = '00'
        where a.del_flag = 0 and a.status = 0
        <if test="goodsIdList != null">
            and a.goods_id in
            <foreach item="item" collection="goodsIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>



    <select id="selectRetailGoodsSkuInfoByHq" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select a.id as id, a.goods_id as goodsId, a.status as status,
        e.goods_name as goodsName, c.id as goodsSkuStoreId,
        c.purchase_price as purchasePrice, c.stock as stock
        from goods_sku a
        left join goods_info e on e.id = a.goods_id
        left join goods_sku_store c on c.goods_sku_id = a.id and c.source_type = '00'
        where a.del_flag = 0 and a.status = 0

        and a.goods_id = #{goodsId}


    </select>

    <select id="selectRetailGoodsSkuInfoByFactory" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select a.id as id, a.goods_id as goodsId, a.status as status,
        e.goods_name as goodsName, c.id as goodsSkuStoreId,
        c.wholesale_price as purchasePrice, c.stock as stock
        from goods_sku a
        left join goods_info e on e.id = a.goods_id
        left join goods_sku_store c on c.goods_sku_id = a.id and c.source_type = '00'
        where a.del_flag = 0 and a.status = 0
          and a.goods_id = #{goodsId}
    </select>

    <select id="selectRetailGoodsSkuInfoByStore" resultType="com.yiqi.core.domain.vo.GoodsSkuQueryVO">
        select b.id as id, b.goods_id as goodsId, b.status as status,
        e.goods_name as goodsName,e.goods_img as goodsImg, a.id as goodsSkuStoreId,
        a.sale_price as salePrice, a.stock as stock
        from goods_sku_store a
        left join goods_sku b on b.id = a.goods_sku_id
        left join goods_info e on e.id = b.goods_id
        where b.del_flag = 0 and b.status = '0' and b.goods_id = #{goodsId} and a.sale_price is not Null
        <if test="targetId != null  and targetId != ''">
            and a.target_id = #{targetId}
        </if>
        <if test="sourceType != null  and sourceType != ''">
            and a.source_type = #{sourceType}
        </if>

    </select>
    <select id="getGoodsSkuNumsByGoodsId" resultType="java.lang.Long" parameterType="java.lang.Long">
        select count(1) from goods_sku where goods_id = #{goodsId} and del_flag = 0
    </select>
</mapper>