<?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.SysMusicScoreCategoriesDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.SysMusicScoreCategories" id="SysMusicScoreCategories">
		<result column="id_" property="id" />
		<result column="parent_id_" property="parentId" />
		<result column="organ_id_" property="organId" />
		<result column="enable_" property="enable" />
		<result column="name_" property="name" />
		<result column="cover_img_" property="coverImg" />
		<result column="operator_id_" property="operatorId" />
		<result column="order_" property="order" />
		<result column="organ_name_" property="organNames" />
		<result column="music_score_num_" property="musicScoreNum" />
		<result column="music_score_num_sum_" property="musicScoreNumSum" />
		<result column="sound_resource_" property="soundResource" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="SysMusicScoreCategories" >
		SELECT categ.*,
		(select count(1) from sys_music_score ms where categ.id_ = ms.music_score_categories_id_) as music_score_num_sum_
		FROM sys_music_score_categories categ WHERE categ.id_ = #{id}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="SysMusicScoreCategories">
		SELECT categ.*,
			   (select count(1) from sys_music_score ms where categ.id_ = ms.music_score_categories_id_) as music_score_num_sum_
		       FROM sys_music_score_categories categ ORDER BY categ.id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysMusicScoreCategories" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO sys_music_score_categories (parent_id_,name_,cover_img_,operator_id_,create_time_,update_time_,organ_id_,order_,sound_resource_)
		VALUES(#{parentId},#{name},#{coverImg},#{operatorId},NOW(),NOW(),#{organId},#{order},#{soundResource})
	</insert>
	<insert id="batchInsert">
		INSERT INTO sys_music_score_categories (parent_id_,name_,cover_img_,operator_id_,create_time_,update_time_,organ_id_,enable_,order_,sound_resource_)
		VALUES
		<foreach collection="categoriesList" item="bean" separator=",">
			(#{bean.parentId},#{bean.name},#{bean.coverImg},#{bean.operatorId},NOW(),NOW(),#{organId},#{enable},#{bean.order},#{bean.soundResource})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.SysMusicScoreCategories">
		UPDATE sys_music_score_categories <set>
			<if test="musicScoreNum != null">
				music_score_num_ = #{musicScoreNum},
			</if>
			<if test="order != null">
				order_ = #{order},
			</if>
			<if test="parentId != null">
				parent_id_ = #{parentId},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="enable != null">
				enable_ = #{enable},
			</if>
			<if test="operatorId != null">
				operator_id_ = #{operatorId},
			</if>
			<if test="coverImg != null">
				cover_img_ = #{coverImg},
			</if>
			<if test="name != null">
				name_ = #{name},
			</if>
			<if test="soundResource != null">
				sound_resource_ = #{soundResource},
			</if>
				update_time_ = NOW()
	</set> WHERE id_ = #{id}
	</update>
	<update id="batchUpdate">
		<foreach collection="categories" item="bean" index="index" open="" close="" separator=";">
			UPDATE sys_music_score_categories <set>
			<if test="bean.order != null">
				order_ = #{bean.order},
			</if>
			<if test="bean.operatorId != null">
				operator_id_ = #{bean.operatorId},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="enable != null">
				enable_ = #{enable},
			</if>
			<if test="bean.coverImg != null">
				cover_img_ = #{bean.coverImg},
			</if>
			<if test="bean.name != null">
				name_ = #{bean.name},
			</if>
			<if test="bean.soundResource != null">
				sound_resource_ = #{bean.soundResource},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{bean.id}
		</foreach>
	</update>
	<update id="enable">
		UPDATE sys_music_score_categories SET enable_ = #{enable} WHERE FIND_IN_SET(id_,#{categoriesIds})
	</update>

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

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SysMusicScoreCategories" parameterType="map">
		SELECT sm.*, GROUP_CONCAT(o.name_) AS organ_name_ FROM sys_music_score_categories sm
		LEFT JOIN organization o ON FIND_IN_SET(o.id_,sm.organ_id_)
		<where>
			<!-- <if test="organId != null and organId != ''">
				AND INTE_ARRAY(sm.organ_id_,#{organId})
			</if> -->
			<if test="parentId != null">
				AND sm.parent_id_ = #{parentId}
			</if>
			<if test="enable != null">
				AND sm.enable_ = #{enable}
			</if>
			<if test="search != null and search != ''">
				AND (sm.id_ = #{search} OR sm.name_ LIKE CONCAT('%',#{search},'%'))
			</if>
		</where>
		ORDER BY sm.order_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(sm.id_) FROM sys_music_score_categories sm
		<where>
			<!-- <if test="organId != null and organId != ''">
				AND INTE_ARRAY(sm.organ_id_,#{organId})
			</if> -->
			<if test="parentId != null">
				AND sm.parent_id_ = #{parentId}
			</if>
			<if test="enable != null">
				AND sm.enable_ = #{enable}
			</if>
			<if test="search != null and search != ''">
				AND sm.id_ = #{search} OR sm.name_ LIKE CONCAT('%',#{search},'%')
			</if>
		</where>
	</select>
	<select id="findByParentId" resultMap="SysMusicScoreCategories">
		SELECT sm.*,
		(select count(1) from sys_music_score sms where sms.del_flag_ = 0 and sm.id_ = sms.music_score_categories_id_) as music_score_num_sum_
        FROM sys_music_score_categories sm
		<include refid="queryTree"/>
 		ORDER BY sm.order_
	</select>
	<select id="findByCategoriesIds" resultType="java.lang.String">
		SELECT GROUP_CONCAT(id_) FROM sys_music_score_categories
		WHERE FIND_IN_SET(id_,#{categoriesIds}) OR FIND_IN_SET(parent_id_,#{categoriesIds})
	</select>
    <select id="queryOrganByIds" resultType="java.util.Map">
		SELECT sms.id_ 'key',GROUP_CONCAT(DISTINCT o.name_) 'value' FROM sys_music_score_categories sms
		LEFT JOIN organization o ON FIND_IN_SET(o.id_,sms.organ_id_)
		WHERE sms.id_ IN
		<foreach collection="categoriesIds" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
		GROUP BY sms.id_
	</select>
    <sql id="queryTree">
		<where>
			<if test="enable != null">
				AND sm.enable_ = #{enable}
			</if>
			<if test="parentId != null">
				AND sm.parent_id_ = #{parentId}
			</if>
			<if test="search != null and search != ''">
				AND (sm.id_ like concat('%',#{search},'%') or sm.name_ like concat('%',#{search},'%'))
			</if>
		</where>
	</sql>
	
    <select id="queryByIds" resultMap="SysMusicScoreCategories">
		SELECT * from sys_music_score_categories smsc where find_in_set(smsc.id_, #{ids})
	</select>
	
	<select id="queryByParentId" resultMap="SysMusicScoreCategories" >
		SELECT categ.* FROM sys_music_score_categories categ WHERE categ.id_ = #{parentId}
	</select>
</mapper>
