<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cxyxiaomo.epp.shop.dao.GoodDao">
    <resultMap id="GoodsResultMap" type="com.cxyxiaomo.epp.common.pojo.Good">
        <id column="id" property="id"/>
        <result column="goods_name" property="goodsName"/>
        <result column="category_id" property="categoryId"/>
        <result column="brand" property="brand"/>
        <result column="gallery" property="gallery"/>
        <result column="brief" property="brief"/>
        <result column="is_on_sale" property="isOnSale"/>
        <result column="sort_order" property="sortOrder"/>
        <result column="pic_url" property="picUrl"/>
        <result column="type" property="type"/>
        <result column="unit" property="unit"/>
        <result column="counter_price" property="counterPrice"/>
        <result column="retail_price" property="retailPrice"/>
        <result column="detail" property="detail"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <!--<insert id="insert" parameterType="com.cxyxiaomo.epp.common.pojo.Good">-->
    <!--    INSERT INTO report (`user_id`, `name`, `address`, `time`, `temperature`)-->
    <!--    VALUES (#{userId}, #{name}, #{address}, #{time}, #{temperature})-->
    <!--</insert>-->
    <select id="list" resultType="com.cxyxiaomo.epp.common.pojo.Good">
        SELECT *
        FROM goods
        WHERE 1 = 1
        <if test="cateId != null">
            AND category_id = #{cateId}
        </if>
        <if test="searchText != null">
            AND goods_name LIKE concat('%',#{searchText,jdbcType=VARCHAR},'%')
        </if>
        order by `sort_order` asc
    </select>
    <select id="listOnSale" resultType="com.cxyxiaomo.epp.common.pojo.Good">
        SELECT *
        FROM goods
        WHERE is_on_sale = 1
        <if test="cateId != null">
            AND category_id = #{cateId}
        </if>
        <if test="searchText != null">
            AND goods_name LIKE concat('%',#{searchText,jdbcType=VARCHAR},'%')
        </if>
        order by `sort_order` asc
    </select>
    <select id="getById" parameterType="java.lang.Long" resultType="com.cxyxiaomo.epp.common.pojo.Good">
        SELECT *
        FROM goods
        WHERE id = #{id}
        order by `sort_order` asc
    </select>

    <select id="selectById" resultMap="GoodsResultMap">
        SELECT *
        FROM goods
        WHERE id = #{id}
    </select>

    <select id="listByIds" parameterType="java.util.List" resultType="com.cxyxiaomo.epp.common.pojo.Good">
        SELECT *
        FROM goods
        <where>
            <if test="list != null and list.size > 0">
                AND id IN
                <foreach collection="list" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            AND deleted = 0
        </where>
    </select>

    <!--<select id="selectByCategoryId" resultMap="GoodsResultMap">-->
    <!--    SELECT *-->
    <!--    FROM goods-->
    <!--    WHERE category_id = #{categoryId}-->
    <!--</select>-->

    <!--<select id="selectAll" resultMap="GoodsResultMap">-->
    <!--    SELECT *-->
    <!--    FROM goods-->
    <!--</select>-->

    <insert id="insert" parameterType="com.cxyxiaomo.epp.common.pojo.Good" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO goods (goods_name, category_id, brand, gallery, brief, is_on_sale, sort_order, pic_url, type, unit,
                           counter_price, retail_price, detail, deleted)
        VALUES (#{goodsName}, #{categoryId}, #{brand}, #{gallery}, #{brief}, #{isOnSale}, #{sortOrder}, #{picUrl},
                #{type}, #{unit}, #{counterPrice}, #{retailPrice}, #{detail}, #{deleted})
    </insert>

    <update id="update" parameterType="com.cxyxiaomo.epp.common.pojo.Good">
        UPDATE goods
        SET goods_name    = #{goodsName},
            category_id   = #{categoryId},
            brand         = #{brand},
            gallery       = #{gallery},
            brief         = #{brief},
            is_on_sale    = #{isOnSale},
            sort_order    = #{sortOrder},
            pic_url       = #{picUrl},
            type          = #{type},
            unit          = #{unit},
            counter_price = #{counterPrice},
            retail_price  = #{retailPrice},
            detail        = #{detail},
            deleted       = #{deleted}
        WHERE id = #{id}
    </update>

    <delete id="deleteById">
        DELETE
        FROM goods
        WHERE id = #{id}
    </delete>


    <insert id="addGood" parameterType="com.cxyxiaomo.epp.common.pojo.Good">
        INSERT INTO goods (goods_name, category_id, brand, gallery, brief, is_on_sale, sort_order, pic_url, type, unit,
                           counter_price, retail_price, detail)
        VALUES (#{goodsName}, #{categoryId}, #{brand}, #{gallery}, #{brief}, #{isOnSale}, #{sortOrder}, #{picUrl},
                #{type}, #{unit}, #{counterPrice}, #{retailPrice}, #{detail})
    </insert>
    <update id="updateGood" parameterType="com.cxyxiaomo.epp.common.pojo.Good">
        UPDATE goods
        <set>
            <if test="goodsName != null and goodsName != ''">goods_name = #{goodsName},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="brand != null and brand != ''">brand = #{brand},</if>
            <if test="gallery != null and gallery != ''">gallery = #{gallery},</if>
            <if test="brief != null and brief != ''">brief = #{brief},</if>
            <if test="isOnSale != null">is_on_sale = #{isOnSale},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="picUrl != null and picUrl != ''">pic_url = #{picUrl},</if>
            <if test="type != null">type = #{type},</if>
            <if test="unit != null and unit != ''">unit = #{unit},</if>
            <if test="counterPrice != null">counter_price = #{counterPrice},</if>
            <if test="retailPrice != null">retail_price = #{retailPrice},</if>
            <if test="detail != null and detail != ''">detail = #{detail},</if>
            <if test="deleted != null">deleted = #{deleted}</if>
        </set>
        WHERE id = #{id}
    </update>
    <select id="getGoodList" resultType="com.cxyxiaomo.epp.common.pojo.Good">
        select *
        from goods
        where 1 = 1
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="goodsName != null &amp;&amp; goodsName != ''">
            AND goods_name LIKE concat('%',#{goodsName,jdbcType=VARCHAR},'%')
        </if>
        <if test="categoryId != null">
            AND category_id = #{categoryId}
        </if>
        <if test="brand != null &amp;&amp; brand != ''">
            AND brand LIKE concat('%',#{brand,jdbcType=VARCHAR},'%')
        </if>
        <if test="gallery != null &amp;&amp; gallery != ''">
            AND gallery LIKE concat('%',#{gallery,jdbcType=VARCHAR},'%')
        </if>
        <if test="brief != null &amp;&amp; brief != ''">
            AND brief LIKE concat('%',#{brief,jdbcType=VARCHAR},'%')
        </if>
        <if test="isOnSale != null">
            AND is_on_sale = #{isOnSale}
        </if>
        <if test="sortOrder != null">
            AND sort_order = #{sortOrder}
        </if>
        <if test="picUrl != null &amp;&amp; picUrl != ''">
            AND pic_url LIKE concat('%',#{picUrl,jdbcType=VARCHAR},'%')
        </if>
        <if test="type != null">
            AND type = #{type}
        </if>
        <if test="unit != null &amp;&amp; unit != ''">
            AND unit LIKE concat('%',#{unit,jdbcType=VARCHAR},'%')
        </if>
        <if test="counterPrice != null">
            AND counter_price = #{counterPrice}
        </if>
        <if test="retailPrice != null">
            AND retail_price = #{retailPrice}
        </if>
    </select>

    <delete id="deleteGoodById">
        DELETE
        FROM goods
        WHERE id = #{goodId}
    </delete>
</mapper>
