<?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.backendsys.mapper.B2c.B2cGoodMapper">

    <sql id="includeGoods">
        g.id id,
        g.id good_id,

        g.category_id category_id,
        gc.category_name category_name,
        COALESCE(g.brand_id, '') brand_id,
        COALESCE(gb.brand_name, '') brand_name,
        COALESCE(g.unit_id, '') unit_id,
        COALESCE(gu.unit_name, '') unit_name,

        g.good_name good_name,
        COALESCE(g.good_description, '') good_description,
        g.good_content good_content,
        COALESCE(g.good_thumb, '') good_thumb,
        COALESCE(g.good_images, '') good_images,
        COALESCE(g.meta_keyword, '') meta_keyword,
        COALESCE(g.meta_description, '') meta_description,
        g.status status,
        g.create_time create_time,
        g.update_time update_time
    </sql>
    <resultMap id="resultMapGoods" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="good_id" column="good_id" javaType="java.lang.Long"/>
        <result property="category_id" column="category_id" javaType="java.lang.Long"/>
        <result property="category_name" column="category_name"/>
        <result property="brand_id" column="brand_id" javaType="java.lang.Long"/>
        <result property="brand_name" column="brand_name"/>
        <result property="unit_id" column="unit_id" javaType="java.lang.Long"/>
        <result property="unit_name" column="unit_name"/>
        <result property="good_name" column="good_name" />
        <result property="good_description" column="good_description" />
        <result property="good_thumb" column="good_thumb" />
        <result property="meta_keyword" column="meta_keyword" />
        <result property="meta_description" column="meta_description" />
        <result property="status" column="status" javaType="java.lang.Integer"/>
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
        <collection select="querySpecById" property="spec_sku" javaType="java.util.List" ofType="java.util.LinkedHashMap" column="{goodId=id}">
            <id property="id" column="spec_id" />
            <result property="good_id" column="good_id" />
            <result property="spec_ids" column="spec_ids" />
            <result property="stock" column="stock" />
            <result property="price" column="price" />
            <result property="is_stock" column="is_stock" />
        </collection>
    </resultMap>

    <!-- [嵌套查询] 商品规格SKU -->
    <select id="querySpecById" resultType="java.util.LinkedHashMap">
        SELECT gss.id, gss.good_id, gss.spec_ids, gss.stock, gss.price, gss.is_stock
--          , CONCAT(gs.spec_name, ',', gs.spec_value) AS specs
        FROM b2c_goods_spec_sku gss
--             JOIN b2c_goods_spec gs ON FIND_IN_SET(gs.id, gss.spec_ids) > 0
        WHERE good_id = #{goodId}
    </select>

    <resultMap id="resultMapGoodsDetail" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="good_id" column="good_id" javaType="java.lang.Long"/>
        <result property="category_id" column="category_id" javaType="java.lang.Long"/>
        <result property="category_name" column="category_name"/>
        <result property="brand_id" column="brand_id" javaType="java.lang.Long"/>
        <result property="brand_name" column="brand_name"/>
        <result property="unit_id" column="unit_id" javaType="java.lang.Long"/>
        <result property="unit_name" column="unit_name"/>
        <result property="good_name" column="good_name" />
        <result property="good_content" column="good_content" />
        <result property="good_description" column="good_description" />
        <result property="good_thumb" column="good_thumb" />
        <result property="good_images" column="good_images" />
        <result property="meta_keyword" column="meta_keyword" />
        <result property="meta_description" column="meta_description" />
        <result property="status" column="status" javaType="java.lang.Integer"/>
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <select id="queryGoodList" resultMap="resultMapGoods">
        SELECT DISTINCT
        <include refid="includeGoods" />
        FROM b2c_goods g
            LEFT JOIN b2c_goods_category gc ON g.category_id = gc.id
            LEFT JOIN b2c_goods_brand gb ON g.brand_id = gb.id
            LEFT JOIN b2c_goods_unit gu ON g.unit_id = gu.id
        <where>
            <if test="good_name != null and good_name != ''">
                AND g.good_name LIKE CONCAT('%', #{good_name}, '%')
            </if>
            <if test="category_id != null and category_id != ''">
                AND g.category_id = #{category_id}
            </if>
            <if test="brand_id != null and brand_id != ''">
                AND g.brand_id = #{brand_id}
            </if>
            <if test="status != null and status != ''">
                AND g.status = #{status}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="queryGoodDetail" resultMap="resultMapGoodsDetail">
        SELECT
        <include refid="includeGoods" />
        FROM b2c_goods g
            LEFT JOIN b2c_goods_category gc ON g.category_id = gc.id
            LEFT JOIN b2c_goods_brand gb ON g.brand_id = gb.id
            LEFT JOIN b2c_goods_unit gu ON g.unit_id = gu.id
        WHERE g.id = #{good_id}
    </select>

    <insert id="insertGood" parameterType="com.backendsys.entity.B2c.B2cGoodDTO"
        useGeneratedKeys="true" keyProperty="good_id">
        INSERT INTO b2c_goods (category_id, good_name
        <if test="brand_id != null and brand_id != ''">, brand_id</if>
        <if test="unit_id != null and unit_id != ''">, unit_id</if>
        <if test="good_description != null and good_description != ''">, good_description</if>
        <if test="good_content != null and good_content != ''">, good_content</if>
        <if test="good_thumb != null and good_thumb != ''">, good_thumb</if>
        <if test="good_images != null and good_images != ''">, good_images</if>
        <if test="meta_keyword != null and meta_keyword != ''">, meta_keyword</if>
        <if test="meta_description != null and meta_description != ''">, meta_description</if>
        <if test="status != null and status != ''">, status</if>
        <if test="del_flag != null and del_flag != ''">, del_flag</if>
        )
        VALUES (#{category_id}, #{good_name}
        <if test="brand_id != null and brand_id != ''">, #{brand_id}</if>
        <if test="unit_id != null and unit_id != ''">, #{unit_id}</if>
        <if test="good_description != null and good_description != ''">, #{good_description}</if>
        <if test="good_content != null and good_content != ''">, #{good_content}</if>
        <if test="good_thumb != null and good_thumb != ''">, #{good_thumb}</if>
        <if test="good_images != null and good_images != ''">, #{good_images}</if>
        <if test="meta_keyword != null and meta_keyword != ''">, #{meta_keyword}</if>
        <if test="meta_description != null and meta_description != ''">, #{meta_description}</if>
        <if test="status != null and status != ''">, #{status}</if>
        <if test="del_flag != null and del_flag != ''">, #{del_flag}</if>
        )
    </insert>

    <update id="updateGood" parameterType="com.backendsys.entity.B2c.B2cGoodDTO"
        useGeneratedKeys="true" keyProperty="good_id">
        UPDATE b2c_goods
        SET category_id = #{category_id},
            good_name = #{good_name}
            <if test="brand_id != null and brand_id != ''">, brand_id = #{brand_id}</if>
            <if test="unit_id != null and unit_id != ''">, unit_id = #{unit_id}</if>
            <if test="good_description != null and good_description != ''">, good_description = #{good_description}</if>
            <if test="good_content != null and good_content != ''">, good_content = #{good_content}</if>
            <if test="good_thumb != null and good_thumb != ''">, good_thumb = #{good_thumb}</if>
            <if test="good_images != null and good_images != ''">, good_images = #{good_images}</if>
            <if test="meta_keyword != null and meta_keyword != ''">, meta_keyword = #{meta_keyword}</if>
            <if test="meta_description != null and meta_description != ''">, meta_description = #{meta_description}</if>
            <if test="status != null and status != ''">, status = #{status}</if>
            <if test="del_flag != null and del_flag != ''">, del_flag = #{del_flag}</if>
        WHERE id = #{good_id}
    </update>

    <delete id="deleteGood">
        DELETE FROM b2c_goods WHERE id = #{good_id}
    </delete>

</mapper>