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

    <resultMap type="com.ym.mec.biz.dal.entity.ChargeTypeSubjectMapper" id="ChargeTypeSubjectMapper">
        <result column="id_" property="id"/>
        <result column="charge_type_id_" property="chargeTypeId"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="kit_group_purchase_type_" property="kitGroupPurchaseTypeEnum"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="deposit_fee_" property="depositFee"/>
        <result column="fee_" property="fee"/>
        <result column="goods_discount_rate_" property="goodsDiscountRate"/>
		<result column="tenant_id_" property="tenantId" />
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="ChargeTypeSubjectMapper">
        SELECT *
        FROM charge_type_subject_mapper where tenant_id_ = #{tenantId}
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ChargeTypeSubjectMapper" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO charge_type_subject_mapper (charge_type_id_, subject_id_, kit_group_purchase_type_, deposit_fee_,
                                                fee_, goods_discount_rate_, create_time_, update_time_,tenant_id_)
        VALUES (#{chargeTypeId}, #{subjectId},
                #{kitGroupPurchaseTypeEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{depositFee},
                #{fee}, #{goodsDiscountRate}, now(), now(),#{tenantId})
    </insert>

    <!-- 向数据库增加一条记录 -->
    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO charge_type_subject_mapper
        (charge_type_id_,subject_id_,kit_group_purchase_type_,deposit_fee_,fee_,goods_discount_rate_,create_time_,update_time_,tenant_id_)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.chargeTypeId},#{item.subjectId},#{item.kitGroupPurchaseTypeEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{item.depositFee},#{item.fee},#{item.goodsDiscountRate},now(),now(),#{item.tenantId})
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ChargeTypeSubjectMapper">
        UPDATE charge_type_subject_mapper
        <set>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="chargeTypeId != null">
                charge_type_id_ = #{chargeTypeId},
            </if>
            <if test="kitGroupPurchaseTypeEnum != null">
                kit_group_purchase_type_ =
                #{kitGroupPurchaseTypeEnum, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="depositFee != null">
                deposit_fee_ = #{depositFee},
            </if>
            <if test="fee != null">
                fee_ = #{fee},
            </if>
            <if test="goodsDiscountRate != null">
                goods_discount_rate_ = #{goodsDiscountRate},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            UPDATE charge_type_subject_mapper
            <set>
                <if test="item.subjectId != null">
                    subject_id_ = #{item.subjectId},
                </if>
                <if test="item.chargeTypeId != null">
                    charge_type_id_ = #{item.chargeTypeId},
                </if>
                <if test="item.kitGroupPurchaseTypeEnum != null">
                    kit_group_purchase_type_ =
                    #{item.kitGroupPurchaseTypeEnum, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="item.depositFee != null">
                    deposit_fee_ = #{item.depositFee},
                </if>
                <if test="item.fee != null">
                    fee_ = #{item.fee},
                </if>
                <if test="item.goodsDiscountRate != null">
                    goods_discount_rate_ = #{item.goodsDiscountRate},
                </if>
                <if test="item.updateTime != null">
                    update_time_ = NOW(),
                </if>
            </set>
            WHERE id_ = #{item.id} and tenant_id_ = #{item.tenantId}
        </foreach>
    </update>

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

    <delete id="deleteByChargeTypeId">
        DELETE
        FROM charge_type_subject_mapper
        WHERE charge_type_id_ = #{chargeTypeId}
    </delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ChargeTypeSubjectMapper" parameterType="map">
        SELECT * FROM charge_type_subject_mapper where  tenant_id_ = #{tenantId}
        	<if test="chargeTypeId != null">
        		and charge_type_id_ = #{chargeTypeId}
        	</if>
        	<if test="subjectId != null">
        		and subject_id_ = #{subjectId}
        	</if>
        ORDER BY id_ desc
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM charge_type_subject_mapper where tenant_id_ = #{tenantId}
        	<if test="chargeTypeId != null">
        		and charge_type_id_ = #{chargeTypeId}
        	</if>
        	<if test="subjectId != null">
        		and subject_id_ = #{subjectId}
        	</if>
    </select>

    <!-- 根据收费类型获取乐团乐器折扣 -->
    <select id="getSubjectDiscount" resultMap="ChargeTypeSubjectMapper">
        SELECT *
        FROM charge_type_subject_mapper
        WHERE charge_type_id_ = #{chargeTypeId}
          AND subject_id_ = #{subjectId}
    </select>

    <!-- 根据收费类型获取乐团乐器折扣 -->
    <select id="getSubjectsDiscount" resultMap="ChargeTypeSubjectMapper">
        SELECT *
        FROM charge_type_subject_mapper
        WHERE charge_type_id_ = #{chargeTypeId}
          AND FIND_IN_SET(subject_id_, #{subjectIds})
    </select>
</mapper>
