<?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.ym.mec.biz.dal.dao.GoodsDao">

    <resultMap type="com.ym.mec.biz.dal.entity.Goods" id="Goods">
        <result column="id_" property="id"/>
        <result column="goods_category_id_" property="goodsCategoryId"/>
        <result column="goods_category_name_" property="goodsCategoryName"/>
        <result column="sn_" property="sn"/>
        <result column="name_" property="name"/>
        <result column="brand_" property="brand"/>
        <result column="specification_" property="specification"/>
        <result column="image_" property="image"/>
        <result column="stock_count_" property="stockCount"/>
        <result column="sell_count_" property="sellCount"/>
        <result column="market_price_" property="marketPrice"/>
        <result column="discount_price_" property="discountPrice"/>
        <result column="group_purchase_price_" property="groupPurchasePrice"/>
        <result column="brief_" property="brief"/>
        <result column="desc_" property="desc"/>
        <result column="is_new_" property="isNew" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="is_top_" property="isTop" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="memo_" property="memo"/>
        <result column="publish_time_" property="publishTime"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="complement_goods_id_list_" property="complementGoodsIdList"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="Goods">
		SELECT * FROM goods WHERE id_ = #{id} 
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="Goods">
		SELECT * FROM goods ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.Goods" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO goods
        (goods_category_id_,sn_,name_,brand_,specification_,image_,stock_count_,sell_count_,market_price_,discount_price_,group_purchase_price_,brief_,desc_,is_new_,is_top_,status_,memo_,publish_time_,complement_goods_id_list_,update_time_,create_time_,type_)
        VALUES(#{goodsCategoryId},#{sn},#{name},#{brand},#{specification},#{image},#{stockCount},#{sellCount},#{marketPrice},#{discountPrice},#{groupPurchasePrice},#{brief},#{desc},
        #{isNew,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{isTop,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{memo},#{publishTime},#{complementGoodsIdList},now(),now(),#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.Goods">
        UPDATE goods
        <set>
            <if test="specification != null">
                specification_ = #{specification},
            </if>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="isTop != null">
                is_top_ = #{isTop,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="sn != null">
                sn_ = #{sn},
            </if>
            <if test="marketPrice != null">
                market_price_ = #{marketPrice},
            </if>
            <if test="memo != null">
                memo_ = #{memo},
            </if>
            <if test="isNew != null">
                is_new_ = #{isNew,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="groupPurchasePrice != null">
                group_purchase_price_ = #{groupPurchasePrice},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="stockCount != null">
                stock_count_ = #{stockCount},
            </if>
            <if test="goodsCategoryId != null">
                goods_category_id_ = #{goodsCategoryId},
            </if>
            <if test="brand != null">
                brand_ = #{brand},
            </if>
            <if test="brief != null">
                brief_ = #{brief},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="discountPrice != null">
                discount_price_ = #{discountPrice},
            </if>
            <if test="sellCount != null">
                sell_count_ = #{sellCount},
            </if>
            <if test="image != null">
                image_ = #{image},
            </if>
            <if test="desc != null">
                desc_ = #{desc},
            </if>
            <if test="publishTime != null">
                publish_time_ = #{publishTime},
            </if>
            <if test="complementGoodsIdList != null">
                complement_goods_id_list_ = #{complementGoodsIdList},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM goods WHERE id_ = #{id} 
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="Goods" parameterType="map">
        SELECT g.*,gc.name_ goods_category_name_ FROM goods g
        LEFT JOIN goods_category gc ON g.goods_category_id_ = gc.id_
        <include refid="queryGoodsPageSql"/>
        ORDER BY g.id_
        <include refid="global.limit"/>
    </select>

    <sql id="queryGoodsPageSql">
        <where>
            <if test="goodsCategoryId != null">
                g.goods_category_id_ = #{goodsCategoryId}
            </if>
            <if test="isNew != null">
                g.is_new_ = #{isNew,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="isTop != null">
                g.is_top_ = #{isTop,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="status != null">
                g.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM goods g
        <include refid="queryGoodsPageSql"/>
	</select>

    <select id="findGoodsBySubId" resultMap="Goods">
        SELECT g.* FROM subject_goods_mapper sgm
        LEFT JOIN goods g ON sgm.goods_category_id_ = g.goods_category_id_
        WHERE sgm.subject_id_ = #{subjectId} AND g.type_ = #{type} and g.status_ != 0
    </select>

    <!-- 根据 -->
    <select id="findGoodsNumByCategoryId" resultType="int">
        SELECT COUNT(*) FROM goods WHERE goods_category_id_ = #{goodsCategoryId}
    </select>
    <select id="findGoodsByIds" resultMap="Goods">
        SELECT * FROM goods WHERE FIND_IN_SET(id_,#{ids})
    </select>
    <select id="findTypeGoods" resultMap="Goods">
        SELECT * FROM goods WHERE type_ = #{type}
    </select>
</mapper>
