<?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="net.mingsoft.cms.dao.ICategoryDao">

	<resultMap id="resultMap" type="net.mingsoft.cms.entity.CategoryEntity">
			<id column="id" property="id" /><!--编号 -->
				<result column="category_title" property="categoryTitle" /><!--栏目管理名称 -->
				<result column="category_pinyin" property="categoryPinyin" /><!--栏目管理名称 -->
				<result column="category_id" property="categoryId" /><!--所属栏目 -->
				<result column="category_type" property="categoryType" /><!--栏目管理属性 -->
				<result column="category_sort" property="categorySort" /><!--自定义顺序 -->
				<result column="category_list_url" property="categoryListUrl" /><!--列表模板 -->
				<result column="category_url" property="categoryUrl" /><!--内容模板 -->
				<result column="category_keyword" property="categoryKeyword" /><!--栏目管理关键字 -->
				<result column="category_descrip" property="categoryDescrip" /><!--栏目管理描述 -->
				<result column="category_img" property="categoryImg" /><!--缩略图 -->
				<result column="category_diy_url" property="categoryDiyUrl" /><!--自定义链接 -->
				<result column="mdiy_model_id" property="mdiyModelId" /><!--栏目管理的内容模型id -->
				<result column="category_datetime" property="categoryDatetime" /><!--类别发布时间 -->
				<result column="category_manager_id" property="categoryManagerId" /><!--发布用户id -->
				<result column="app_id" property="appId" /><!--应用编号 -->
				<result column="dict_id" property="dictId" /><!--字典对应编号 -->
				<result column="category_flag" property="categoryFlag" /><!--栏目属性 -->
				<result column="category_path" property="categoryPath" /><!--栏目路径 -->
				<result column="category_parent_id" property="categoryParentId" /><!--父类型编号 -->
				<result column="create_by" property="createBy" /><!--创建人 -->
				<result column="create_date" property="createDate" /><!--创建时间 -->
				<result column="update_by" property="updateBy" /><!--修改人 -->
				<result column="update_date" property="updateDate" /><!--修改时间 -->
				<result column="del" property="del" /><!--删除标记 -->
	</resultMap>

	<!--保存-->
	<insert id="saveEntity" useGeneratedKeys="true" keyProperty="id"
			parameterType="net.mingsoft.cms.entity.CategoryEntity" >
		insert into cms_category
		<trim prefix="(" suffix=")" suffixOverrides=",">
				<if test="categoryTitle != null and categoryTitle != ''">category_title,</if>
				<if test="categoryPinyin != null and categoryPinyin != ''">category_pinyin,</if>
				<if test="categoryId != null and categoryId != ''">category_id,</if>
				<if test="categoryType != null and categoryType != ''">category_type,</if>
				<if test="categorySort != null">category_sort,</if>
				<if test="categoryListUrl != null and categoryListUrl != ''">category_list_url,</if>
				<if test="categoryUrl != null and categoryUrl != ''">category_url,</if>
				<if test="categoryKeyword != null and categoryKeyword != ''">category_keyword,</if>
				<if test="categoryDescrip != null and categoryDescrip != ''">category_descrip,</if>
				<if test="categoryImg != null and categoryImg != ''">category_img,</if>
				<if test="categoryDiyUrl != null and categoryDiyUrl != ''">category_diy_url,</if>
				<if test="mdiyModelId != null and mdiyModelId != ''">mdiy_model_id,</if>
				<if test="categoryDatetime != null">category_datetime,</if>
				<if test="categoryManagerId != null">category_manager_id,</if>
				<if test="dictId != null">dict_id,</if>
				<if test="categoryFlag != null">category_flag,</if>
				<if test="categoryPath != null and categoryPath != ''">category_path,</if>
				<if test="categoryParentId != null and categoryParentId != ''">category_parent_id,</if>
				<if test="createBy &gt; 0">create_by,</if>
				<if test="createDate != null">create_date,</if>
				<if test="updateBy &gt; 0">update_by,</if>
				<if test="updateDate != null">update_date,</if>
				<if test="del != null">del,</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
				<if test="categoryTitle != null and categoryTitle != ''">#{categoryTitle},</if>
				<if test="categoryPinyin != null and categoryPinyin != ''">#{categoryPinyin},</if>
				<if test="categoryId != null and categoryId != ''">#{categoryId},</if>
				<if test="categoryType != null and categoryType != ''">#{categoryType},</if>
				<if test="categorySort != null">#{categorySort},</if>
				<if test="categoryListUrl != null and categoryListUrl != ''">#{categoryListUrl},</if>
				<if test="categoryUrl != null and categoryUrl != ''">#{categoryUrl},</if>
				<if test="categoryKeyword != null and categoryKeyword != ''">#{categoryKeyword},</if>
				<if test="categoryDescrip != null and categoryDescrip != ''">#{categoryDescrip},</if>
				<if test="categoryImg != null and categoryImg != ''">#{categoryImg},</if>
				<if test="categoryDiyUrl != null and categoryDiyUrl != ''">#{categoryDiyUrl},</if>
				<if test="mdiyModelId != null and mdiyModelId != ''">#{mdiyModelId},</if>
				<if test="categoryDatetime != null">#{categoryDatetime},</if>
				<if test="categoryManagerId != null">#{categoryManagerId},</if>
				<if test="dictId != null">#{dictId},</if>
				<if test="categoryFlag != null ">#{categoryFlag},</if>
				<if test="categoryPath != null and categoryPath != ''">#{categoryPath},</if>
				<if test="categoryParentId != null and categoryParentId != ''">#{categoryParentId},</if>
				<if test="createBy &gt; 0">#{createBy},</if>
				<if test="createDate != null">#{createDate},</if>
				<if test="updateBy &gt; 0">#{updateBy},</if>
				<if test="updateDate != null">#{updateDate},</if>
				<if test="del != null">#{del},</if>
		</trim>
	</insert>

		<!--更新-->
		<update id="updateEntity" parameterType="net.mingsoft.cms.entity.CategoryEntity">
			update cms_category
			<set>
				<if test="categoryTitle != null and categoryTitle != ''">category_title=#{categoryTitle},</if>
				<if test="categoryPinyin != null and categoryPinyin != ''">category_pinyin=#{categoryPinyin},</if>
				category_id=#{categoryId},
				category_parent_id=#{categoryParentId},
				<if test="categoryType != null and categoryType != ''">category_type=#{categoryType},</if>
				<if test="categorySort != null">category_sort=#{categorySort},</if>
				<if test="categoryListUrl != null and categoryListUrl != ''">category_list_url=#{categoryListUrl},</if>
				<if test="categoryUrl != null and categoryUrl != ''">category_url=#{categoryUrl},</if>
				<if test="categoryKeyword != null ">category_keyword=#{categoryKeyword},</if>
				<if test="categoryDescrip != null ">category_descrip=#{categoryDescrip},</if>
				<if test="categoryImg != null and categoryImg != ''">category_img=#{categoryImg},</if>
				<if test="categoryDiyUrl != null">category_diy_url=#{categoryDiyUrl},</if>
				<if test="mdiyModelId != null and mdiyModelId != ''">mdiy_model_id=#{mdiyModelId},</if>
				<if test="categoryDatetime != null">category_datetime=#{categoryDatetime},</if>
				<if test="categoryManagerId != null">category_manager_id=#{categoryManagerId},</if>
				<if test="dictId != null">dict_id=#{dictId},</if>
				<if test="categoryFlag != null ">category_flag=#{categoryFlag},</if>
				<if test="categoryPath != null and categoryPath != ''">category_path=#{categoryPath},</if>
				<if test="createBy &gt; 0">create_by=#{createBy},</if>
				<if test="createDate != null">create_date=#{createDate},</if>
				<if test="updateBy &gt; 0">update_by=#{updateBy},</if>
				<if test="updateDate != null">update_date=#{updateDate},</if>
				<if test="del != null">del=#{del},</if>
			</set>
			where id = #{id}
		</update>

	<!--根据id获取-->
		<select id="getEntity" resultMap="resultMap" parameterType="int">
			select * from cms_category where id=#{id}
		</select>

		<!--根据实体获取-->
		<select id="getByEntity" resultMap="resultMap" parameterType="net.mingsoft.cms.entity.CategoryEntity">
			select * from cms_category
			<where>
				<if test="categoryTitle != null and categoryTitle != ''">and category_title=#{categoryTitle}</if>
				<if test="categoryPinyin != null and categoryPinyin != ''">and category_pinyin=#{categoryPinyin}</if>
				<if test="categoryId != null and categoryId != ''">and category_id=#{categoryId}</if>
				<if test="categoryType != null and categoryType != ''">and category_type=#{categoryType}</if>
				<if test="categorySort != null"> and category_sort=#{categorySort} </if>
				<if test="categoryListUrl != null and categoryListUrl != ''">and category_list_url=#{categoryListUrl}</if>
				<if test="categoryUrl != null and categoryUrl != ''">and category_url=#{categoryUrl}</if>
				<if test="categoryKeyword != null and categoryKeyword != ''">and category_keyword=#{categoryKeyword}</if>
				<if test="categoryDescrip != null and categoryDescrip != ''">and category_descrip=#{categoryDescrip}</if>
				<if test="categoryImg != null and categoryImg != ''">and category_img=#{categoryImg}</if>
				<if test="categoryDiyUrl != null and categoryDiyUrl != ''">and category_diy_url=#{categoryDiyUrl}</if>
				<if test="mdiyModelId != null and mdiyModelId != ''">and mdiy_model_id=#{mdiyModelId}</if>
				<if test="categoryDatetime != null"> and category_datetime=#{categoryDatetime} </if>
				<if test="categoryManagerId != null"> and category_manager_id=#{categoryManagerId} </if>
				<if test="dictId != null"> and dict_id=#{dictId} </if>
				<if test="categoryFlag != null and categoryFlag != ''">and category_flag=#{categoryFlag}</if>
				<if test="categoryPath != null and categoryPath != ''">and category_path=#{categoryPath}</if>
				<if test="categoryParentId != null and categoryParentId != ''">and category_parent_id=#{categoryParentId}</if>
				<if test="createBy &gt; 0"> and create_by=#{createBy} </if>
				<if test="createDate != null"> and create_date=#{createDate} </if>
				<if test="updateBy &gt; 0"> and update_by=#{updateBy} </if>
				<if test="updateDate != null"> and update_date=#{updateDate} </if>
				<if test="del != null"> and del=#{del} </if>
			</where>
			limit 0,1
		</select>
	<sql id="queryWhereCategoryId" databaseId="mysql">
		find_in_set('${id}',CATEGORY_PARENT_ID)
	</sql>
	<sql id="queryWhereCategoryId" databaseId="oracle" >
		instr(','||'${id}'||',', ','||CATEGORY_PARENT_ID||',')>0
	</sql>
	<sql id="queryWhereCategoryId" databaseId="sqlServer">
		CHARINDEX(','+'${id}'+',' , ','+CATEGORY_PARENT_ID +',')>0
	</sql>

	<!-- 模糊查询开始 -->
	<select id="queryChildren" resultMap="resultMap">
		select * from cms_category
		<where>

			<if test="dictId &gt; 0">
				and dict_id=#{dictId}
			</if>
			and
			(
			<if test="categoryParentId != null and categoryParentId!=''">
				find_in_set(#{categoryParentId},CATEGORY_PARENT_ID)
			</if>
			<if test="categoryParentId == null or  categoryParentId ==''">
				<include refid="queryWhereCategoryId"></include>
			</if>
			<if test="id != null">
				or id=#{id}
			</if>
			)
			and del=0
		</where>
	</select>


		<!--删除-->
		<delete id="deleteEntity" parameterType="int">
			delete from cms_category  where id=#{id}
		</delete>

		<!--批量删除-->
		<delete id="delete" >
			delete from cms_category
			<where>
				id  in <foreach collection="ids" item="item" index="index"
										 open="(" separator="," close=")">#{item}</foreach>
			</where>
		</delete>
		<!--查询全部-->
		<select id="queryAll" resultMap="resultMap">
			select * from cms_category order by id desc
		</select>
	<!--条件查询-->
	<select id="query" resultMap="resultMap">
		select * from cms_category
		<where>
			<if test="categoryTitle != null and categoryTitle != ''"> and category_title=#{categoryTitle}</if>
			<if test="categoryPinyin != null and categoryPinyin != ''">and category_pinyin=#{categoryPinyin}</if>
			<if test="categoryId != null and categoryId != ''"> and category_id=#{categoryId}</if>
			<if test="categoryType != null and categoryType != ''"> and category_type=#{categoryType}</if>
			<if test="categorySort != null"> and category_sort=#{categorySort} </if>
			<if test="categoryListUrl != null and categoryListUrl != ''"> and category_list_url=#{categoryListUrl}</if>
			<if test="categoryUrl != null and categoryUrl != ''"> and category_url=#{categoryUrl}</if>
			<if test="categoryKeyword != null and categoryKeyword != ''"> and category_keyword=#{categoryKeyword}</if>
			<if test="categoryDescrip != null and categoryDescrip != ''"> and category_descrip=#{categoryDescrip}</if>
			<if test="categoryImg != null and categoryImg != ''"> and category_img=#{categoryImg}</if>
			<if test="categoryDiyUrl != null and categoryDiyUrl != ''"> and category_diy_url=#{categoryDiyUrl}</if>
			<if test="mdiyModelId != null and mdiyModelId != ''"> and mdiy_model_id=#{mdiyModelId}</if>
			<if test="categoryDatetime != null"> and category_datetime=#{categoryDatetime} </if>
			<if test="categoryManagerId != null"> and category_manager_id=#{categoryManagerId} </if>
			<if test="dictId != null"> and dict_id=#{dictId} </if>
			<if test="categoryFlag != null and categoryFlag != ''"> and category_flag=#{categoryFlag}</if>
			<if test="categoryPath != null and categoryPath != ''"> and category_path=#{categoryPath}</if>
			<if test="categoryParentId != null and categoryParentId != ''"> and  find_in_set(#{categoryParentId},category_parent_id)</if>
			<if test="createBy &gt; 0"> and create_by=#{createBy} </if>
			<if test="createDate != null"> and create_date=#{createDate} </if>
			<if test="updateBy &gt; 0"> and update_by=#{updateBy} </if>
			<if test="updateDate != null"> and update_date=#{updateDate} </if>
			<if test="del != null"> and del=#{del} </if>
			<include refid="net.mingsoft.base.dao.IBaseDao.sqlWhere"></include>
		</where>
	</select>

</mapper>
