<?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.MemberRankOrganizationFeeMapperDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.MemberRankOrganizationFeeMapper" id="MemberRankOrganizationFeeMapper">
		<result column="id_" property="id" />
		<result column="organ_id_" property="organId" />
		<result column="member_rank_setting_id_" property="memberRankSettingId" />
		<result column="member_fee_setting_id_" property="memberFeeSettingId" />
		<result column="operator_id_" property="operatorId" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="rank_name_" property="rankName" />
		<result column="organ_name_" property="organName" />
		<association property="memberFeeSetting" javaType="com.ym.mec.biz.dal.entity.MemberFeeSetting">
			<result property="id" column="member_fee_setting_id_"/>
			<result property="currentDayFee" column="current_day_fee_"/>
			<result property="originalDayFee" column="original_day_fee_"/>
			<result property="currentMonthFee" column="current_month_fee_"/>
			<result property="originalMonthFee" column="original_month_fee_"/>
			<result property="currentHalfYearFee" column="current_half_year_fee_"/>
			<result property="originalHalfYearFee" column="original_half_year_fee_"/>
			<result property="currentYearFee" column="current_year_fee_"/>
			<result property="originalYearFee" column="original_year_fee_"/>
		</association>
	</resultMap>

	<select id="get" resultMap="MemberRankOrganizationFeeMapper" >
		SELECT mrofm.*,o.name_ organ_name_,mrs.name_ rank_name_
		FROM member_rank_organization_fee_mapper mrofm
		LEFT JOIN member_rank_setting mrs ON  mrofm.member_rank_setting_id_ = mrs.id_
		LEFT JOIN organization o ON o.id_ = mrofm.organ_id_
		WHERE mrofm.id_ = #{id}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="MemberRankOrganizationFeeMapper">
		SELECT mrofm.* FROM member_rank_organization_fee_mapper mrofm
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MemberRankOrganizationFeeMapper" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO member_rank_organization_fee_mapper (organ_id_,member_rank_setting_id_,member_fee_setting_id_,operator_id_,create_time_,update_time_)
		VALUES(#{organId},#{memberRankSettingId},#{memberFeeSettingId},#{operatorId},NOW(),NOW())
	</insert>

	<update id="update" parameterType="com.ym.mec.biz.dal.entity.MemberRankOrganizationFeeMapper">
		UPDATE member_rank_organization_fee_mapper <set>
		<if test="organId != null">
			organ_id_ = #{organId},
		</if>
		<if test="memberRankSettingId != null">
			member_rank_setting_id_ = #{memberRankSettingId},
		</if>
		<if test="memberFeeSettingId != null">
			member_fee_setting_id_ = #{memberFeeSettingId},
		</if>
		<if test="operatorId != null">
			operator_id_ = #{operatorId},
		</if>
		update_time_ = NOW()
	</set> WHERE id_ = #{id}
	</update>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="MemberRankOrganizationFeeMapper" parameterType="map">
		SELECT mrofm.*,o.name_ organ_name_,mrs.name_ rank_name_,mfs.*,mfs.id_ member_fee_setting_id_
		FROM member_rank_organization_fee_mapper mrofm
		LEFT JOIN member_rank_setting mrs ON  mrofm.member_rank_setting_id_ = mrs.id_
		LEFT JOIN member_fee_setting mfs ON mfs.id_ = mrofm.member_fee_setting_id_
		LEFT JOIN organization o ON o.id_ = mrofm.organ_id_
		<where>
			<if test="search != null and search != ''">
				AND mrs.name_ LIKE CONCAT('%',#{search},'%')
			</if>
			<if test="organId != null and organId != ''">
				AND FIND_IN_SET(mrofm.organ_id_,#{organId})
			</if>
		</where>
		ORDER BY mrofm.id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(mrofm.id_) FROM member_rank_organization_fee_mapper mrofm
		LEFT JOIN member_rank_setting mrs ON  mrofm.member_rank_setting_id_ = mrs.id_
		<where>
			<if test="search != null and search != ''">
				AND mrs.name_ LIKE CONCAT('%',#{search},'%')
			</if>
			<if test="organId != null and organId != ''">
				AND FIND_IN_SET(mrofm.organ_id_,#{organId})
			</if>
		</where>
	</select>
    <select id="findByOrganIdAndRankId" resultMap="MemberRankOrganizationFeeMapper">
		SELECT * FROM member_rank_organization_fee_mapper
		WHERE organ_id_ = #{organId} AND member_rank_setting_id_ = #{memberRankSettingId}
		LIMIT 1
	</select>
</mapper>
