<?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.MarketActivityDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.MarketActivity" id="MarketActivity">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="activity_ids_" property="activityIds" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="MarketActivity" >
		SELECT * FROM market_activity WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="MarketActivity">
		SELECT * FROM market_activity where tenant_id_ = #{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MarketActivity" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO market_activity (name_,activity_ids_,create_time_,update_time_,tenant_id_)
		VALUES(#{name},#{activityIds},#{createTime},#{updateTime},#{tenantId})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.MarketActivity">
		UPDATE market_activity <set>
		<if test="name != null">
		name_ = #{name},
		</if>
		<if test="activityIds != null">
		activity_ids_ = #{activityIds},
		</if>
		update_time_ = NOW()
	</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM market_activity WHERE id_ = #{id} 
	</delete>
	<sql id="queryPageSql">
		<where>
			tenant_id_ = #{tenantId}
			<if test="search != null and search != ''">
				AND (name_ LIKE CONCAT('%',#{search},'%') OR id_ = #{search} OR FIND_IN_SET(#{search},activity_ids_))
			</if>
		</where>
	</sql>
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="MarketActivity" parameterType="map">
		SELECT * FROM market_activity
		<include refid="queryPageSql"/>
		ORDER BY id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM market_activity
		<include refid="queryPageSql"/>
	</select>
	<resultMap id="ActivityDetailDto" type="com.ym.mec.biz.dal.entity.ActivityDetailDto">
		<result property="activityId" column="id_"/>
		<result property="activityName" column="name_"/>
		<result property="currentPrice" column="market_price_"/>
		<result property="originalPrice" column="original_price_"/>
		<result property="activityDesc" column="description_"/>
		<result property="categoryName" column="category_name_"/>
		<result property="categoryId" column="category_id_"/>
		<result property="courseNum" column="course_num_"/>
		<result property="startTime" column="start_time_"/>
		<result property="endTime" column="end_time_"/>
	</resultMap>
    <select id="queryActivity" resultMap="ActivityDetailDto">
		SELECT vga.id_,vga.name_,vga.description_,vga.market_price_,vgc.name_ category_name_,
		vgc.id_ category_id_,vga.min_course_num_ course_num_,vga.start_time_,vga.end_time_
		FROM market_activity ma
		LEFT JOIN vip_group_activity vga ON FIND_IN_SET(vga.id_,ma.activity_ids_)
		LEFT JOIN vip_group_category vgc ON vgc.id_ = vga.vip_group_category_id_list_
		WHERE ma.id_ = #{marketPriceId} AND FIND_IN_SET(#{organId},vga.organ_id_)
	</select>
</mapper>
