<?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="tax_stock_count_" property="taxStockCount"/>
        <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="agree_cost_price_" property="agreeCostPrice"/>
        <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"/>
        <result column="supply_channel_" property="supplyChannel" />
        <result column="stock_type_" property="stockType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="client_show_" property="clientShow" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="stock_warning_" property="stockWarning" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

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

    <select id="lock" resultMap="Goods" useCache="false" flushCache="true">
		SELECT * FROM goods WHERE id_ = #{goodsId} LOCK IN SHARE MODE
	</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_,tax_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_,agree_cost_price_,client_show_,stock_warning_,stock_type_)
        VALUES(#{goodsCategoryId},#{sn},#{name},#{brand},#{specification},#{image},#{stockCount},#{taxStockCount},#{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},#{agreeCostPrice},
        #{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{stockWarning,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
    </insert>
    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO goods
        (goods_category_id_,name_,brand_,specification_,image_,market_price_,
        discount_price_,group_purchase_price_,brief_,desc_,update_time_,create_time_,type_,agree_cost_price_,sn_,
        stock_count_,tax_stock_count_,client_show_,stock_warning_,stock_type_)
        VALUES
        <foreach collection="list" separator="," item="goods">
            (#{goods.goodsCategoryId},#{goods.name},#{goods.brand},#{goods.specification},#{goods.image},#{goods.marketPrice},
            #{goods.discountPrice},#{goods.groupPurchasePrice},#{goods.brief},#{goods.desc},now(),now(),
            #{goods.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{goods.agreeCostPrice},#{goods.sn},
            #{goods.stockCount},#{goods.taxStockCount},#{goods.clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{goods.stockWarning,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{goods.stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
        </foreach>
    </insert>
    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.Goods">
        UPDATE goods
        <set>
            <if test="agreeCostPrice != null">
                agree_cost_price_ = #{agreeCostPrice},
            </if>
            <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="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>
            <if test="taxStockCount != null">
                tax_stock_count_ = #{taxStockCount},
            </if>
            <if test="clientShow != null">
                client_show_ = #{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="stockWarning != null">
                stock_warning_ = #{stockWarning,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="stockType != null">
                stock_type_ = #{stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
                update_time_ = NOW()
        </set>
        WHERE id_ = #{id}
    </update>

    <update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.Goods">
        <foreach collection="goodsList" item="goods" separator=";">
            UPDATE goods
            <set>
                <if test="goods.agreeCostPrice != null">
                    agree_cost_price_ = #{goods.agreeCostPrice},
                </if>
                <if test="goods.specification != null">
                    specification_ = #{goods.specification},
                </if>
                <if test="goods.status != null">
                    status_ = #{goods.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="goods.isTop != null">
                    is_top_ = #{goods.isTop,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="goods.type != null">
                    type_ = #{goods.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="goods.sn != null">
                    sn_ = #{goods.sn},
                </if>
                <if test="goods.marketPrice != null">
                    market_price_ = #{goods.marketPrice},
                </if>
                <if test="goods.memo != null">
                    memo_ = #{goods.memo},
                </if>
                <if test="goods.isNew != null">
                    is_new_ = #{goods.isNew,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="goods.groupPurchasePrice != null">
                    group_purchase_price_ = #{goods.groupPurchasePrice},
                </if>
                <if test="goods.name != null">
                    name_ = #{goods.name},
                </if>
                <if test="goods.stockCount != null">
                    stock_count_ = #{goods.stockCount},
                </if>
                <if test="goods.goodsCategoryId != null">
                    goods_category_id_ = #{goods.goodsCategoryId},
                </if>
                <if test="goods.brand != null">
                    brand_ = #{goods.brand},
                </if>
                <if test="goods.brief != null">
                    brief_ = #{goods.brief},
                </if>
                <if test="goods.discountPrice != null">
                    discount_price_ = #{goods.discountPrice},
                </if>
                <if test="goods.sellCount != null">
                    sell_count_ = #{goods.sellCount},
                </if>
                <if test="goods.image != null">
                    image_ = #{goods.image},
                </if>
                <if test="goods.desc != null">
                    desc_ = #{goods.desc},
                </if>
                <if test="goods.publishTime != null">
                    publish_time_ = #{goods.publishTime},
                </if>
                <if test="goods.complementGoodsIdList != null">
                    complement_goods_id_list_ = #{goods.complementGoodsIdList},
                </if>
                <if test="goods.taxStockCount != null">
                    tax_stock_count_ = #{goods.taxStockCount},
                </if>
                <if test="goods.clientShow != null">
                    client_show_ = #{goods.clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="goods.stockWarning != null">
                    stock_warning_ = #{goods.stockWarning,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="goods.stockType != null">
                    stock_type_ = #{goods.stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                    update_time_ = NOW()
            </set>
            WHERE id_ = #{goods.id}
        </foreach>
    </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_ DESC
        <include refid="global.limit"/>
    </select>
    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM goods g
        <include refid="queryGoodsPageSql"/>
    </select>

    <sql id="queryGoodsPageSql">
        <where>
            <if test="goodsCategoryId != null">
                AND g.goods_category_id_ = #{goodsCategoryId}
            </if>
            <if test="type != null">
                AND g.type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="isNew != null">
                AND g.is_new_ = #{isNew,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="isTop != null">
                AND g.is_top_ = #{isTop,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="status != null">
                AND g.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="groupGoods!=null and groupGoods==0">
                AND g.complement_goods_id_list_ IS NULL
            </if>
            <if test="groupGoods!=null and groupGoods==1">
                AND g.complement_goods_id_list_ IS NOT NULL
            </if>
            <if test="clientShow!=null">
                AND g.client_show_=#{clientShow}
            </if>
            <if test="search!=null and search!=''">
                AND (g.sn_=#{search} OR g.id_=#{search} OR g.name_ LIKE CONCAT('%', #{search}, '%'))
            </if>
        </where>
    </sql>

    <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} AND status_ != 0
    </select>
    <select id="getGoodies" resultMap="Goods">
        SELECT * FROM goods WHERE id_ IN
        <foreach collection="goodsIds" item="goodsId" open="(" close=")" separator=",">
            #{goodsId}
        </foreach>
    </select>

    <select id="lockGoods" resultMap="Goods" useCache="false" flushCache="true">
        SELECT * FROM goods WHERE id_ IN
        <foreach collection="goodsIds" item="goodsId" open="(" close=")" separator=",">
            #{goodsId}
        </foreach>
        FOR UPDATE
    </select>

    <select id="findBySn" resultMap="Goods">
        SELECT * FROM goods WHERE sn_ = #{sn}
    </select>

    <select id="lockBySn" resultMap="Goods">
        SELECT * FROM goods WHERE sn_ = #{sn} FOR UPDATE
    </select>

    <select id="findBySns" resultMap="Goods">
        SELECT * FROM goods WHERE sn_ IN
        <foreach collection="sns" item="sn" separator="," open="(" close=")">
            #{sn}
        </foreach>
    </select>
    <select id="getInnerRepertoryWarnName" resultType="java.lang.String">
        SELECT GROUP_CONCAT( DISTINCT name_) FROM goods WHERE stock_count_ &lt;= #{innerRepertoryWarnNum} AND stock_warning_ = 1 AND complement_goods_id_list_ IS NULL
    </select>
    <select id="getOuterRepertoryWarnName" resultType="java.lang.String">
        SELECT GROUP_CONCAT( DISTINCT name_) FROM goods WHERE tax_stock_count_ &lt;= #{outerRepertoryWarnNum} AND stock_warning_ = 1 AND complement_goods_id_list_ IS NULL
    </select>

    <select id="getWithComplementGoodsAndStatus" resultMap="Goods">
        SELECT * FROM goods WHERE status_ = #{status} AND FIND_IN_SET(#{goodsId}, complement_goods_id_list_)
    </select>
    <resultMap id="GoodsSellDtoMap" type="com.ym.mec.biz.dal.dto.GoodsSellDto">
        <result property="goodsId" column="id_"/>
        <result property="image" column="image_"/>
        <result property="goodsName" column="name_"/>
        <result property="goodsType" column="type_"/>
        <result property="goodsPrice" column="discount_price_"/>
        <result property="totalGoodsPrice" column="discount_price_"/>
    </resultMap>
    <select id="queryGoodsSellDtos" resultMap="GoodsSellDtoMap">
        SELECT id_,image_,name_,type_,discount_price_ FROM goods WHERE FIND_IN_SET(id_,#{goodsId})
    </select>

    <resultMap id="MusicGroupGoods" type="com.ym.mec.biz.dal.dto.MusicGroupGoodsAndDiscountDto" extends="Goods"/>
    <select id="getMusicGroupGoodsAndDiscount" resultMap="MusicGroupGoods">
        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.status_ != 0
        <if test="type!=null">
            AND g.type_ = #{type}
        </if>
    </select>

    <select id="getMusicGroupGoodsAndDiscountWithSubjects" resultMap="MusicGroupGoods">
        SELECT g.* FROM subject_goods_mapper sgm
        LEFT JOIN goods g ON sgm.goods_category_id_ = g.goods_category_id_
        WHERE FIND_IN_SET(sgm.subject_id_, #{subjectIds}) and g.status_ != 0
        <if test="type!=null">
            AND g.type_ = #{type}
        </if>
    </select>
    <select id="findByIdAndStatus" resultType="java.lang.Boolean">
        SELECT COUNT(0) FROM goods WHERE status_ = #{status} AND id_ = #{goodsId}
    </select>
</mapper>
