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

    <resultMap type="com.ym.mec.biz.dal.entity.MusicGroupSubjectGoodsGroup"
               id="MusicGroupSubjectGoodsGroup">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="goods_id_list_" property="goodsIdList"/>
        <result column="kit_group_purchase_type_json_" property="kitGroupPurchaseTypeJson" />
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="price_" property="price"/>
        <result column="deposit_fee_" property="depositFee"/>
        <result column="remission_course_fee_" property="remissionCourseFee"/>
        <result column="kit_group_purchase_type_json_" property="kitGroupPurchaseTypeJson"/>
        <result column="course_purchase_type_json_" property="coursePurchaseTypeJson"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert"
            parameterType="com.ym.mec.biz.dal.entity.MusicGroupSubjectGoodsGroup"
            useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO music_group_subject_goods_group
        (id_,name_,type_,music_group_id_,subject_id_,goods_id_list_,create_time_,update_time_,price_,deposit_fee_,remission_course_fee_,kit_group_purchase_type_json_,course_purchase_type_json_)
        VALUES(#{id},#{name},#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{musicGroupId},#{subjectId},#{goodsIdList},NOW(),NOW(),#{price},#{depositFee},#{remissionCourseFee},#{kitGroupPurchaseTypeJson},#{coursePurchaseTypeJson})
    </insert>

    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO music_group_subject_goods_group
        (name_,type_,music_group_id_,subject_id_,goods_id_list_,create_time_,update_time_,price_,deposit_fee_,remission_course_fee_,kit_group_purchase_type_json_,course_purchase_type_json_) VALUES
        <foreach collection="lists" item="item" index="index" separator=",">
            (#{item.name},#{item.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{musicGroupId},#{item.subjectId},#{item.goodsIdList},now(),now(),#{item.price},#{item.depositFee},#{item.remissionCourseFee},#{item.kitGroupPurchaseTypeJson},#{item.coursePurchaseTypeJson})
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update"
            parameterType="com.ym.mec.biz.dal.entity.MusicGroupSubjectGoodsGroup">
        UPDATE music_group_subject_goods_group
        <set>
            <if test="depositFee != null">
                deposit_fee_ = #{depositFee},
            </if>
            <if test="remissionCourseFee != null">
                remission_course_fee_ = #{remissionCourseFee},
            </if>
            <if test="kitGroupPurchaseTypeJson != null">
                kit_group_purchase_type_json_ = #{kitGroupPurchaseTypeJson},
            </if>
            <if test="price != null">
                price_ = #{price},
            </if>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="goodsIdList != null">
                goods_id_list_ = #{goodsIdList},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

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

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="MusicGroupSubjectGoodsGroup"
            parameterType="map">
        SELECT * FROM music_group_subject_goods_group ORDER BY id_
        <include refid="global.limit"/>
    </select>

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

    <resultMap type="com.ym.mec.biz.dal.entity.MusicGroupSubjectGoodsGroup" extends="MusicGroupSubjectGoodsGroup" id="findGoodsGroupMap">
        <collection property="goodsList" resultMap="com.ym.mec.biz.dal.dao.GoodsDao.Goods" columnPrefix="g_" />
    </resultMap>
    <select id="findGoodsGroup" resultMap="findGoodsGroupMap">
        SELECT mgs.*, g.id_ g_id_,g.name_ g_name_,g.image_ g_image_,g.brief_ g_brief_, g.group_purchase_price_ g_group_purchase_price_,
        g.specification_ g_specification_,g.brand_ g_brand,g.desc_ g_desc, g.market_price_ g_market_price_,g.complement_goods_id_list_
        g_complement_goods_id_list_
        FROM music_group_subject_goods_group mgs,goods g
        WHERE FIND_IN_SET (g.id_ ,mgs.goods_id_list_)
        AND mgs.music_group_id_ = #{musicGroupId}
        <if test="subId != null">
            AND mgs.subject_id_ = #{subId}
        </if>
    </select>

    <!-- 根据ids获取对应分组商品 -->
    <select id="findGoodsGroupByIds" resultMap="MusicGroupSubjectGoodsGroup">
        SELECT * FROM music_group_subject_goods_group WHERE FIND_IN_SET(id_,#{ids})
    </select>
    
    <select id="query" resultMap="MusicGroupSubjectGoodsGroup" parameterType="map">
        SELECT * FROM music_group_subject_goods_group WHERE music_group_id_ = #{musicGroupId} and subject_id_ = #{subjectId} and goods_id_list_ = #{goodsIdList}
    </select>
</mapper>
