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

    <resultMap type="com.yiqi.core.domain.GoodsInfo" id="GoodsResult">
            <result property="id" column="id"/>
            <result property="goodsName" column="goods_name"/>
            <result property="goodsType" column="goods_type"/>
            <result property="goodsCategoryId" column="goods_category_id"/>
            <result property="goodsSynopsis" column="goods_synopsis"/>
            <result property="brandId" column="brand_id"/>
            <result property="goodsImg" column="goods_img"/>
            <result property="goodsImgs" column="goods_imgs"/>
            <result property="goodsCode" column="goods_code"/>
            <result property="isBalancePay" column="is_balance_pay"/>
            <result property="isCashPay" column="is_cash_pay"/>
            <result property="cashMoney" column="cash_money"/>
            <result property="isSell" column="is_sell"/>
            <result property="sort" column="sort"/>
            <result property="status" column="status"/>
            <result property="delFlag" column="del_flag"/>
            <result property="sellNum" column="sell_num"/>
            <result property="goodComment" column="good_comment"/>
            <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>

    <resultMap type="com.yiqi.core.domain.vo.weapp.WeAppGoodsInfoQueryVO" id="WeAppGoodsResult">
        <result property="id" column="id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsType" column="goods_type"/>
        <result property="goodsCategoryId" column="goods_category_id"/>
        <result property="goodsSynopsis" column="goods_synopsis"/>
        <result property="brandId" column="brand_id"/>
        <result property="goodsImg" column="goods_img"/>
        <result property="goodsImgs" column="goods_imgs"/>
        <result property="goodsCode" column="goods_code"/>
        <result property="isBalancePay" column="is_balance_pay"/>
        <result property="isCashPay" column="is_cash_pay"/>
        <result property="cashMoney" column="cash_money"/>
        <result property="isSell" column="is_sell"/>
        <result property="sellNum" column="sell_num"/>
        <result property="goodComment" column="good_comment"/>
        <result property="minSalePrice" column="min_sale_price"/>
        <result property="maxSalePrice" column="max_sale_price"/>
    </resultMap>

    <sql id="selectGoodsVo">
        select * from goods_info
    </sql>

    <select id="selectGoodsList"  resultType="com.yiqi.core.domain.vo.GoodsInfoQueryVO">
        select g.*,b.brand_name,s.source_type,s.target_id from goods_info g
        left join goods_brand as b on  b.id=g.brand_id
        left join goods_store as s on  s.goods_id = g.id
        <where>
            <if test="goodsName != null  and goodsName != ''">
                and g.goods_name like concat('%', #{goodsName}, '%')
            </if>
            <if test="goodsType != null  and goodsType != ''">
                and g.goods_type = #{goodsType}
            </if>
            <if test="goodsCategoryId != null  and goodsCategoryId != ''">
                and g.goods_category_id = #{goodsCategoryId}
            </if>
            <if test="goodsCode != null  and goodsCode != ''">
                and g.goods_code = #{goodsCode}
            </if>
            <if test="status != null  and status != ''">
                and s.status = #{status}
            </if>
            <if test="sourceType != null  and sourceType != ''">
                and s.source_type = #{sourceType}
            </if>
            <if test="targetId != null  and targetId != ''">
                and s.target_id = #{targetId}
            </if>
            and  g.del_flag = '0'
            order by sort ASC
        </where>
    </select>

    <select id="selectGoodsById" parameterType="Long"
            resultMap="GoodsResult">
            <include refid="selectGoodsVo"/>
            where id = #{id} and del_flag = '0'
    </select>

    <select id="selectGoodsByBrandIds" resultType="java.lang.Integer">
        select count(*) from goods_info  where brand_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
        and  del_flag = '0'
    </select>

    <select id="selectGoodsCategoryIds" resultType="java.lang.Integer">
        select count(*) from goods_info  where goods_category_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
        and  del_flag = '0'
    </select>

    <insert id="insertGoods"  useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="goodsName != null">goods_name,</if>
                    <if test="goodsType != null">goods_type,</if>
                    <if test="goodsCategoryId != null">goods_category_id,</if>
                    <if test="goodsSynopsis != null">goods_synopsis,</if>
                    <if test="brandId != null">brand_id,</if>
                    <if test="goodsImg != null">goods_img,</if>
                    <if test="goodsImgs != null">goods_imgs,</if>
                    <if test="goodsCode != null">goods_code,</if>
                    <if test="isCashPay != null">is_cash_pay,</if>
                    <if test="cashMoney != null">cash_money,</if>
                    <if test="isBalancePay != null">is_balance_pay,</if>
                    <if test="isSell != null">is_sell,</if>
                    <if test="sort != null">sort,</if>
                    <if test="status != null">status,</if>
                    <if test="delFlag != null">del_flag,</if>
                    <if test="sellNum != null">sell_num,</if>
                    <if test="goodComment != null">good_comment,</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="goodsName != null">#{goodsName},</if>
                    <if test="goodsType != null">#{goodsType},</if>
                    <if test="goodsCategoryId != null">#{goodsCategoryId},</if>
                    <if test="goodsSynopsis != null">#{goodsSynopsis},</if>
                    <if test="brandId != null">#{brandId},</if>
                    <if test="goodsImg != null">#{goodsImg},</if>
                    <if test="goodsImgs != null">#{goodsImgs},</if>
                    <if test="goodsCode != null">#{goodsCode},</if>
                    <if test="isCashPay != null">#{isCashPay},</if>
                    <if test="cashMoney != null">#{cashMoney},</if>
                    <if test="isBalancePay != null">#{isBalancePay},</if>
                    <if test="isSell != null">#{isSell},</if>
                    <if test="sort != null">#{sort},</if>
                    <if test="status != null">#{status},</if>
                    <if test="delFlag != null">#{delFlag},</if>
                    <if test="sellNum != null">#{sellNum},</if>
                    <if test="goodComment != null">#{goodComment},</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="updateGoods" >
        update goods_info
        <trim prefix="SET" suffixOverrides=",">
                    <if test="goodsName != null">goods_name =
                        #{goodsName},
                    </if>
                    <if test="goodsType != null">goods_type =
                        #{goodsType},
                    </if>
                    <if test="goodsCategoryId != null">goods_category_id =
                        #{goodsCategoryId},
                    </if>
                    <if test="goodsSynopsis != null">goods_synopsis =
                        #{goodsSynopsis},
                    </if>
                    <if test="brandId != null">brand_id =
                        #{brandId},
                    </if>
                    <if test="goodsImg != null">goods_img =
                        #{goodsImg},
                    </if>
                    <if test="goodsImgs != null">goods_imgs =
                        #{goodsImgs},
                    </if>
                    <if test="goodsCode != null">goods_code =
                        #{goodsCode},
                    </if>
                    <if test="isCashPay != null">is_cash_pay =
                        #{isCashPay},
                    </if>
                    <if test="cashMoney != null">cash_money =
                        #{cashMoney},
                    </if>
                    <if test="isBalancePay != null">is_balance_pay =
                        #{isBalancePay},
                    </if>
                    <if test="isSell != null">is_sell =
                        #{isSell},
                    </if>
                    <if test="sort != null">sort =
                        #{sort},
                    </if>
                    <if test="status != null">status =
                        #{status},
                    </if>
                    <if test="sellNum != null">sell_num =
                        #{sellNum},
                    </if>
                    <if test="goodComment != null">good_comment =
                        #{goodComment},
                    </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="addGoodsSaleNumber">
        update goods_info
        <trim prefix="set" suffixOverrides=",">
            <trim prefix=" `sell_num` = case " suffix="else sell_num end, ">
                <foreach collection="list" item="item">
                    WHEN id=#{item.id}
                    THEN sell_num + #{item.sellNum}
                </foreach>
            </trim>
            <trim prefix=" `virtual_sell_num` = case " suffix="else  virtual_sell_num end,">
                <foreach collection="list" item="item">
                    WHEN id=#{item.id}
                    THEN virtual_sell_num + #{item.virtualSellNum}
                </foreach>
            </trim>
        </trim>
        where
        id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>
    <update id="minusGoodsSaleNumber">
        update goods_info
        <trim prefix="set" suffixOverrides=",">
            <trim prefix=" `sell_num` = case " suffix="else sell_num end, ">
                <foreach collection="list" item="item">
                    WHEN id=#{item.id}
                    THEN sell_num - #{item.sellNum}
                </foreach>
            </trim>
            <trim prefix=" `virtual_sell_num` = case " suffix="else  virtual_sell_num end,">
                <foreach collection="list" item="item">
                    WHEN id=#{item.id}
                    THEN virtual_sell_num - #{item.virtualSellNum}
                </foreach>
            </trim>
        </trim>
        where
        id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <delete id="deleteGoodsById" parameterType="Long">
            update goods_info set del_flag = '2' where id = #{id}
        </delete>

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

    <select id="findPage" resultMap="WeAppGoodsResult" parameterType="com.yiqi.core.domain.dto.weapp.WeAppGoodsInfoQueryDTO">
        select g.id,
               g.goods_name,
               g.goods_type,
               g.goods_category_id,
               g.brand_id,
               g.goods_img,
               g.goods_imgs,
               g.goods_code,
               g.is_balance_pay,
               g.is_cash_pay,
               g.cash_money,
               g.is_sell,
               g.sell_num,
               g.good_comment,
               g.create_time,
               min(gss.sale_price) as min_sale_price,
               max(gss.sale_price) as max_sale_price
        from goods_sku as sku
                 left join goods_sku_store as gss on gss.source_type = '00' and gss.goods_sku_id = sku.id
                 left join goods_info as g on g.id = sku.goods_id
        where gss.status='0' and g.del_flag='0'
            <if test="goodsType != null and goodsType != ''">
                and g.goods_type=#{goodsType}
            </if>
            <if test="goodsCategoryId != null">
                and g.goods_category_id=#{goodsCategoryId}
            </if>
            <if test="keywords != null and keywords != ''">
                and g.goods_name like concat('%', #{keywords}, '%')
            </if>
        group by sku.goods_id
        order by g.is_sell desc, g.sort desc,g.create_time desc
        limit #{startRow},#{pageSize}
    </select>
    <select id="selectGoodsByCategoryId" resultMap="GoodsResult">
        select a.id,a.goods_name,a.goods_code,a.is_sell,a.goods_img,a.brand_id,a.goods_category_id from goods_info a
            left join goods_store b on a.id = b.goods_id
                 where a.status = '0'
                   and b.source_type = #{sourceType}
                   and b.target_id = #{orgId}
                   and a.goods_category_id = #{goodsCategoryId}
                limit #{startRow},#{pageSize}
    </select>
    <select id="getSkuInfoListByApp" resultType="com.yiqi.order.domain.vo.OrderGoodsSkuInfoVO">
        select a.id as skuId,
               b.id as goodsId, b.goods_img as goodsImg, b.goods_name as goodsName,
               c.id as goodsCategoryId, c.category_name as goodsCategoryName,
               e.spec as skuName, d.sale_price as marketPrice
        from goods_sku a
        left join goods_info b on a.goods_id = b.id
        left join goods_category c on b.goods_category_id = c.id and c.status = '0'
        left join goods_sku_store d on d.goods_sku_id = a.id and d.source_type = '00'
        left join ( select goods_sku_id, GROUP_CONCAT(CONCAT(spec_name,':',spec_value) SEPARATOR ' ') as spec
                    FROM goods_sku_spec
                    where del_flag = 0 and  goods_sku_id in
                    <foreach item="item" collection="skuIdList" open="(" separator="," close=")">#{item}
                    </foreach>
                    GROUP BY goods_sku_id
                    ) e on a.id = e.goods_sku_id
        where a.id in
        <foreach item="item" collection="skuIdList" open="(" separator="," close=")">#{item}
        </foreach>
    </select>
    <select id="getSkuInfoListByStore" resultType="com.yiqi.order.domain.vo.OrderGoodsSkuInfoVO">
        select a.id as skuId,
        b.id as goodsId, b.goods_img as goodsImg, b.goods_name as goodsName,
        c.id as goodsCategoryId, c.category_name as goodsCategoryName,
        e.spec as skuName, d.sale_price as marketPrice
        from goods_sku a
        left join goods_info b on a.goods_id = b.id
        left join goods_category c on b.goods_category_id = c.id and c.status = '0'
        left join goods_sku_store d on d.goods_sku_id = a.id and d.source_type = #{sourceType} and d.target_id = #{orgId}
        left join ( select goods_sku_id, GROUP_CONCAT(CONCAT(spec_name,':',spec_value) SEPARATOR ' ') as spec
                    FROM goods_sku_spec
                    where del_flag = 0 and  goods_sku_id in
                    <foreach item="item" collection="skuIdList" open="(" separator="," close=")">#{item}
                    </foreach>
                    GROUP BY goods_sku_id
                    ) e on a.id = e.goods_sku_id
        where a.id in
        <foreach item="item" collection="skuIdList" open="(" separator="," close=")">#{item}
        </foreach>
    </select>
</mapper>