<?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.PhotoAlbumDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.PhotoAlbum" id="PhotoAlbum">
		<result column="id_" property="id" />
		<result column="music_group_id_" property="musicGroupId" />
		<result column="name_" property="name" />
		<result column="client_show_" property="clientShow" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="type_" property="type" />
		<result column="cover_url_" property="coverUrl" />
		<result column="cover_thumbnail_url_" property="coverThumbnailUrl" />
		<result column="order_" property="order" />
		<result column="time_line_" property="timeLine" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="PhotoAlbum" >
		SELECT * FROM photo_album_ WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="PhotoAlbum">
		SELECT * FROM photo_album_ where tenant_id_ = #{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.PhotoAlbum" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO photo_album_ (music_group_id_,name_,client_show_,type_,cover_url_,cover_thumbnail_url_,create_time_,update_time_,order_,time_line_,tenant_id_)
		VALUES(#{musicGroupId},#{name},#{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		       #{type},#{coverUrl},#{coverThumbnailUrl},NOW(),NOW(),#{order},#{timeLine},#{tenantId})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.PhotoAlbum">
		UPDATE photo_album_ <set>
		<if test="timeLine != null">
			time_line_ = #{timeLine},
		</if>
		<if test="coverUrl != null">
			cover_url_ = #{coverUrl},
		</if>
		<if test="order != null">
			order_ = #{order},
		</if>
		<if test="coverThumbnailUrl != null">
			cover_thumbnail_url_ = #{coverThumbnailUrl},
		</if>
		<if test="clientShow != null">
			client_show_ = #{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		</if>
		<if test="musicGroupId != null">
			music_group_id_ = #{musicGroupId},
		</if>
		<if test="type != null">
			type_ = #{type},
		</if>
		<if test="name != null">
			name_ = #{name},
		</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	<update id="batchUpdate">
		<foreach collection="photoAlbumList" item="album" separator=";">
			UPDATE photo_album_ <set>
			<if test="album.timeLine != null">
				time_line_ = #{album.timeLine},
			</if>
			<if test="album.coverUrl != null">
				cover_url_ = #{album.coverUrl},
			</if>
			<if test="album.order != null">
				order_ = #{album.order},
			</if>
			<if test="album.coverThumbnailUrl != null">
				cover_thumbnail_url_ = #{album.coverThumbnailUrl},
			</if>
			<if test="album.clientShow != null">
				client_show_ = #{album.clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="album.musicGroupId != null">
				music_group_id_ = #{album.musicGroupId},
			</if>
			<if test="album.type != null">
				type_ = #{album.type},
			</if>
			<if test="album.name != null">
				name_ = #{album.name},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{album.id} and tenant_id_ = #{album.tenantId}
		</foreach>
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM photo_album_ WHERE id_ = #{id} 
	</delete>
    <delete id="deleteByIds">
		DELETE FROM photo_album_ WHERE FIND_IN_SET(id_,#{ids})
	</delete>

    <!-- 分页查询 -->
	<select id="queryPage" resultMap="PhotoAlbum" parameterType="map">
		SELECT * FROM photo_album_
		<include refid="queryPageSql"/>
		ORDER BY order_ ASC,id_ DESC
		<include refid="global.limit"/>
	</select>
	<sql id="queryPageSql">
		<where>
			tenant_id_ = #{tenantId}
			<if test="timeLine != null and timeLine != ''">
				AND DATE_FORMAT(time_line_,"%Y%m") = #{timeLine}
			</if>
			<if test="year != null and year != ''">
				AND DATE_FORMAT(time_line_,"%Y") = #{year}
			</if>
			<if test="type != null and type != ''">
				AND type_ = #{type}
			</if>
			<if test="musicGroupId != null and musicGroupId != ''">
				AND music_group_id_ = #{musicGroupId}
			</if>
			<if test="clientShow != null">
				AND client_show_ = #{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
		</where>
	</sql>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(id_) FROM photo_album_
		<include refid="queryPageSql"/>
	</select>
	<select id="countTotalNumByAlbumIds" resultType="java.util.Map">
		SELECT photo_album_id_ 'key',COUNT(id_) 'value' FROM photo_
		WHERE photo_album_id_ IN
		<foreach collection="albumIds" separator="," item="albumId" close=")" open="(">
			#{albumId}
		</foreach>
		<if test="clientShow != null">
			AND client_show_ = #{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		</if>
		GROUP BY photo_album_id_
	</select>
	<select id="findByTypeAndGroupId" resultMap="PhotoAlbum">
		SELECT * FROM photo_album_ WHERE music_group_id_ = #{musicGroupId} AND type_ = #{type} LIMIT 1
	</select>
</mapper>
