<?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.CloudCoachPaymentProgramDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.CloudCoachPaymentProgram" id="CloudCoachPaymentProgram">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="remark_" property="remark" />
		<result column="member_num_" property="memberNum" />
		<result column="user_num_" property="userNum" />
		<result column="payment_user_num_" property="paymentUserNum" />
		<result column="period_" property="period" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="member_rank_setting_id_" property="memberRankSettingId" />
		<result column="actual_amount_" property="actualAmount" />
		<result column="original_amount_" property="originalAmount" />
		<result column="operator_" property="operator" />
		<result column="operator_name_" property="operatorName" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<result column="audit_" property="audit" />
		<result column="audit_name_" property="auditName" />
		<result column="audit_time_" property="auditTime" />
		<result column="organ_id_" property="organId" />
		<result column="organ_name_" property="organName" />
		<result column="auto_activation_flag_" property="autoActivationFlag" />
		<result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="CloudCoachPaymentProgram" >
		SELECT * FROM cloud_coach_payment_program WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="CloudCoachPaymentProgram">
		SELECT * FROM cloud_coach_payment_program ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CloudCoachPaymentProgram" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO cloud_coach_payment_program (name_,remark_,member_num_,user_num_,
		payment_user_num_,period_,member_rank_setting_id_,actual_amount_,original_amount_,
		operator_,create_time_,update_time_,tenant_id_,memo_,status_,organ_id_,auto_activation_flag_)
		VALUES(#{name},#{remark},#{memberNum},#{userNum},#{paymentUserNum},#{period,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		#{memberRankSettingId},#{actualAmount},#{originalAmount},#{operator},
		NOW(),NOW(),#{tenantId},#{memo},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{organId},#{autoActivationFlag})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.CloudCoachPaymentProgram">
		UPDATE cloud_coach_payment_program <set>
		<if test="status != null">
			status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		</if>
		<if test="autoActivationFlag != null">
			auto_activation_flag_ = #{autoActivationFlag},
		</if>
		<if test="memo != null">
			memo_ = #{memo},
		</if>
		<if test="organId != null">
			organ_id_ = #{organId},
		</if>
		<if test="audit != null">
			audit_ = #{audit},
		</if>
		<if test="auditTime != null">
			audit_time_ = #{auditTime},
		</if>
		<if test="operator != null">
			operator_ = #{operator},
		</if>
		<if test="memberNum != null">
			member_num_ = #{memberNum},
		</if>
		<if test="tenantId != null">
			tenant_id_ = #{tenantId},
		</if>
		<if test="paymentUserNum != null">
			payment_user_num_ = #{paymentUserNum},
		</if>
		<if test="originalAmount != null">
			original_amount_ = #{originalAmount},
		</if>
		<if test="name != null">
			name_ = #{name},
		</if>
		<if test="remark != null">
			remark_ = #{remark},
		</if>
		<if test="period != null">
			period_ = #{period,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		</if>
		<if test="userNum != null">
			user_num_ = #{userNum},
		</if>
		<if test="updateTime != null">
			update_time_ = #{updateTime},
		</if>
		<if test="memberRankSettingId != null">
			member_rank_setting_id_ = #{memberRankSettingId},
		</if>
		<if test="actualAmount != null">
			actual_amount_ = #{actualAmount},
		</if>
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM cloud_coach_payment_program WHERE id_ = #{id} 
	</delete>

	<sql id="queryPageSql">
		<where>
			<if test="organId != null and organId != ''">
				AND FIND_IN_SET(ccpp.organ_id_,#{organId})
			</if>
			<if test="tenantId != null">
				AND ccpp.tenant_id_ = #{tenantId}
			</if>
			<if test="period != null and period != ''">
				AND ccpp.period_ = #{period}
			</if>
			<if test="status != null and status != ''">
				AND ccpp.status_ = #{status}
			</if>
			<if test="startTime != null and startTime != ''">
				AND DATE_FORMAT(ccpp.create_time_,"%Y-%m-%d") >= #{startTime}
			</if>
			<if test="endTime != null and endTime != ''">
				AND DATE_FORMAT(ccpp.create_time_,"%Y-%m-%d") &lt;= #{endTime}
			</if>
		</where>
	</sql>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="CloudCoachPaymentProgram" parameterType="map">
		SELECT ccpp.*,su.real_name_ operator_name_,su1.real_name_ audit_name_,o.name_ organ_name_
		FROM cloud_coach_payment_program ccpp
		LEFT JOIN organization o ON o.id_ = ccpp.organ_id_
		LEFT JOIN sys_user su ON su.id_ = ccpp.operator_
		LEFT JOIN sys_user su1 ON su1.id_ = ccpp.audit_
		<include refid="queryPageSql"/>
		ORDER BY ccpp.id_ DESC
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(DISTINCT ccpp.id_) FROM cloud_coach_payment_program ccpp
		LEFT JOIN organization o ON o.id_ = ccpp.organ_id_
		<include refid="queryPageSql"/>
	</select>
	<select id="getLock" resultMap="CloudCoachPaymentProgram">
		SELECT * FROM cloud_coach_payment_program WHERE id_ = #{id} FOR UPDATE
	</select>
</mapper>
