<?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.VipGroupActivityDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.VipGroupActivity" id="VipGroupActivity">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="description_" property="description" />
		<result column="vip_group_category_id_list_" property="vipGroupCategoryIdList" />
		<result column="start_time_" property="startTime" />
		<result column="end_time_" property="endTime" />
		<result column="organ_id_" property="organId" />
		<result column="courses_start_time_" property="coursesStartTime" />
		<result column="courses_end_time_" property="coursesEndTime" />
		<result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="attribute1_" property="attribute1" />
		<result column="attribute2_" property="attribute2" />
		<result column="attribute3_" property="attribute3" />
		<result column="salary_readonly_flag_" property="salaryReadonlyFlag" />
		<result column="payment_readonly_flag_" property="paymentReadonlyFlag" />
		<result column="give_class_pay_salary_flag_" property="giveClassPaySalaryFlag" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="salary_settlement_json_" property="salarySettlementJson" />
		<result column="vip_group_category_names_" property="vipGroupCategoryNames"/>
		<result property="enable" column="enable"/>
		<result property="delFlag" column="del_flag_"/>
		<result property="onlineClassJoinGradientRewards" column="online_class_join_gradient_rewards_"/>
		<result property="offlineClassJoinGradientRewards" column="offline_class_join_gradient_rewards_"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="VipGroupActivity" >
		SELECT * FROM vip_group_activity WHERE id_ = #{id}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="VipGroupActivity">
		SELECT * FROM vip_group_activity WHERE del_flag_=0 ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.VipGroupActivity" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO vip_group_activity (id_,name_,description_,vip_group_category_id_list_,start_time_,end_time_,organ_id_,courses_start_time_,courses_end_time_,type_,attribute1_,attribute2_,attribute3_,salary_readonly_flag_,give_class_pay_salary_flag_,create_time_,update_time_,salary_settlement_json_,del_flag_,payment_readonly_flag_,online_class_join_gradient_rewards_,offline_class_join_gradient_rewards_)
		VALUES(#{id},#{name},#{description},#{vipGroupCategoryIdList},#{startTime},#{endTime},#{organId},#{coursesStartTime},#{coursesEndTime},#{type},#{attribute1},#{attribute2},#{attribute3},#{salaryReadonlyFlag},#{giveClassPaySalaryFlag},now(),now(),#{salarySettlementJson},#{delFlag},#{paymentReadonlyFlag},#{onlineClassJoinGradientRewards},#{offlineClassJoinGradientRewards})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.VipGroupActivity">
		UPDATE vip_group_activity <set>
			<if test="salaryReadonlyFlag != null">
			salary_readonly_flag_ = #{salaryReadonlyFlag},
			</if>
			<if test="organId != null">
			organ_id_ = #{organId},
			</if>
			<if test="id != null">
			id_ = #{id},
			</if>
			<if test="giveClassPaySalaryFlag != null">
			give_class_pay_salary_flag_ = #{giveClassPaySalaryFlag},
			</if>
			<if test="coursesEndTime != null">
			courses_end_time_ = #{coursesEndTime},
			</if>
			<if test="attribute1 != null">
			attribute1_ = #{attribute1},
			</if>
			<if test="endTime != null">
			end_time_ = #{endTime},
			</if>
			<if test="attribute2 != null">
			attribute2_ = #{attribute2},
			</if>
			<if test="name != null">
			name_ = #{name},
			</if>
			<if test="startTime != null">
			start_time_ = #{startTime},
			</if>
			<if test="attribute3 != null">
			attribute3_ = #{attribute3},
			</if>
			<if test="createTime != null">
			create_time_ = #{createTime},
			</if>
			<if test="vipGroupCategoryIdList != null">
			vip_group_category_id_list_ = #{vipGroupCategoryIdList},
			</if>
			<if test="updateTime != null">
			update_time_ = NOW(),
			</if>
			<if test="description != null">
			description_ = #{description},
			</if>
			<if test="salarySettlementJson != null">
			salary_settlement_json_ = #{salarySettlementJson},
			</if>
			<if test="coursesStartTime != null">
			courses_start_time_ = #{coursesStartTime},
			</if>
			<if test="type != null">
			type_ = #{type},
			</if>
			<if test="delFlag != null">
			del_flag_ = #{delFlag},
			</if>
			<if test="paymentReadonlyFlag != null">
				payment_readonly_flag_ = #{paymentReadonlyFlag},
			</if>
			<if test="onlineClassJoinGradientRewards!=null">
				online_class_join_gradient_rewards_=#{onlineClassJoinGradientRewards},
			</if>
			<if test="offlineClassJoinGradientRewards!=null">
				offline_class_join_gradient_rewards_=#{offlineClassJoinGradientRewards},
			</if>
			</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		UPDATE vip_group_activity SET del_flag_=1 WHERE id_ = #{id}
	</delete>

	<sql id="queryCondition">
		<where>
			vga.del_flag_=0
			<if test="organId != null">
				AND INTE_ARRAY(organ_id_,#{organId})
			</if>
			<if test="enable != null and enable == true">
				AND ((NOW() &lt; end_time_ AND NOW() > start_time_) OR end_time_ IS NULL)
			</if>
			<if test="enable != null and enable == false">
				AND (NOW() >= end_time_ OR NOW() &lt;= start_time_)
			</if>
			<if test="search != null and search != ''">
				AND (vga.id_=#{search} OR vga.name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="VipGroupActivity" parameterType="map">
		SELECT
		vga.*,((NOW() &lt; end_time_ AND NOW() > start_time_) OR end_time_ IS NULL) enable,
		GROUP_CONCAT(vgc.name_) vip_group_category_names_
		FROM vip_group_activity vga
		LEFT JOIN vip_group_category vgc ON FIND_IN_SET(vgc.id_,vga.vip_group_category_id_list_)
		<include refid="queryCondition"/>
		GROUP BY vga.id_
		ORDER BY id_ DESC
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM vip_group_activity vga
		<include refid="queryCondition"/>
	</select>
    <select id="findByCategory" resultMap="VipGroupActivity">
		SELECT
			*
		FROM
			vip_group_activity
		WHERE
			FIND_IN_SET( #{categoryId}, vip_group_category_id_list_ )
			AND (NOW() &lt; end_time_ OR end_time_ IS NULL)
			AND del_flag_=0
			AND INTE_ARRAY(organ_id_,#{organIds})
	</select>
    <select id="queryNamesById" resultType="java.util.Map">
		SELECT vga.id_ `key`,vga.name_ `value` FROM vip_group_activity vga WHERE FIND_IN_SET(vga.id_,#{activityIds}) AND vga.del_flag_=0
	</select>
	
    <select id="queryByIds" resultMap="VipGroupActivity">
		SELECT * FROM vip_group_activity vga WHERE FIND_IN_SET(vga.id_,#{activityIds})
	</select>
</mapper>
