<?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.PhotoDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.Photo" id="Photo">
		<result column="id_" property="id" />
		<result column="photo_album_id_" property="photoAlbumId" />
		<result column="name_" property="name" />
		<result column="client_show_" property="clientShow" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="url_" property="url" />
		<result column="thumbnail_url_" property="thumbnailUrl" />
		<result column="order_" property="order" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="Photo" >
		SELECT * FROM photo_ WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="Photo">
		SELECT * FROM photo_ where tenant_id_ = #{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.Photo" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO photo_ (photo_album_id_,name_,client_show_,url_,thumbnail_url_,create_time_,update_time_,order_,tenant_id_)
		VALUES(#{photoAlbumId},#{name},#{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{url},#{thumbnailUrl},NOW(),NOW(),#{order},#{tenantId})
	</insert>
	<insert id="batchInsert">
		INSERT INTO photo_ (photo_album_id_,name_,client_show_,url_,thumbnail_url_,create_time_,update_time_,order_,tenant_id_)
		VALUES
		<foreach collection="photoList" item="photo" separator=",">
			(#{photo.photoAlbumId},#{photo.name},#{photo.clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			 #{photo.url},#{photo.thumbnailUrl},NOW(),NOW(),#{photo.order},#{photo.tenantId})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.Photo">
		UPDATE photo_ <set>
		<if test="order != null">
			order_ = #{order},
		</if>
		<if test="photoAlbumId != null">
		photo_album_id_ = #{photoAlbumId},
		</if>
		<if test="url != null">
		url_ = #{url},
		</if>
		<if test="clientShow != null">
		client_show_ = #{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		</if>
		<if test="thumbnailUrl != null">
		thumbnail_url_ = #{thumbnailUrl},
		</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="photoList" item="item" separator=";">
			UPDATE photo_ <set>
			<if test="item.order != null">
				order_ = #{item.order},
			</if>
			<if test="item.photoAlbumId != null">
				photo_album_id_ = #{item.photoAlbumId},
			</if>
			<if test="item.url != null">
				url_ = #{item.url},
			</if>
			<if test="item.clientShow != null">
				client_show_ = #{item.clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="item.thumbnailUrl != null">
				thumbnail_url_ = #{item.thumbnailUrl},
			</if>
			<if test="item.name != null">
				name_ = #{item.name},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{item.id} and tenant_id_ = #{item.tenantId}
		</foreach>
	</update>

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

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="Photo" parameterType="map">
		SELECT * FROM photo_
		<include refid="queryPageSql"/>
		ORDER BY order_ ASC,id_ DESC
		<include refid="global.limit"/>
	</select>
	<sql id="queryPageSql">
		<where>
			tenant_id_ = #{tenantId}
			<if test="photoAlbumId != null">
				AND photo_album_id_ = #{photoAlbumId}
			</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_
		<include refid="queryPageSql"/>
	</select>
</mapper>
