<?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.cftech.mp.wechat.dao.MenuMapper">

	<resultMap id="resultMap" type="com.cftech.mp.wechat.model.Menu">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="source_type" property="sourceType"/>
		<result column="msg_type" property="msgType"/>
		<result column="source_content" property="sourceContent"/>
		<result column="sort" property="sort"/>
		<result column="create_time" property="createTime"/>
		<result column="update_time" property="updateTime"/>
		<result column="create_by" property="createBy"/>
		<result column="update_by" property="updateBy"/>
		<result column="del_flag" property="delFlag"/>
		<result column="accountsid" property="accountsid"/>
		<result column="parent_id" property="parentId"/>
	</resultMap>
	
	
	<resultMap type="com.cftech.mp.wechat.model.Menu" id="menuResultMap">
		<result property="id" column="id"/>
		<association property="children" column="id" select="selectChildByparent_id"/>
	</resultMap>

	<sql id="sqlWhere">
		<if test="conds!=null">
			<trim prefix="WHERE" prefixOverrides="AND|OR">
				<foreach collection="conds.conds" index="index" item="cond">
					${cond.linkType}
					<if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
					<if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
					<if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
					<if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
					<if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
					<if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
					<if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND #{cond.endValue}</if>
					<if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
					<if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
					<if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
					<if test="cond.condType == 'IN'">${cond.param} IN
						<foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
							#{item}
						</foreach>
					</if>
				</foreach>
			</trim>
		</if>
	</sql>

	<sql id="sqlColumns">
		id, name, source_type, msg_type, source_content, sort, create_time, update_time, create_by, update_by, del_flag,accountsid,parent_id
	</sql>

	<insert id="save" parameterType="com.cftech.mp.wechat.model.Menu" keyProperty="id">
		insert into wx_mp_menu
		(
		<include refid="sqlColumns"/>
		)
		values
		(
		#{id, jdbcType=BIGINT},  #{name, jdbcType=VARCHAR},  #{sourceType, jdbcType=CHAR},  #{msgType, jdbcType=CHAR},  
		#{sourceContent, jdbcType=VARCHAR},  #{sort, jdbcType=INTEGER},  now(),  now(),  
		#{createBy, jdbcType=BIGINT},  #{updateBy, jdbcType=BIGINT},  #{delFlag, jdbcType=CHAR},#{accountsid,jdbcType=BIGINT},#{parentId,jdbcType=BIGINT}
		)
	</insert>

	<select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT
		t.id, t.name, t.source_type, t.msg_type,
		t.source_content, t.sort, t.create_time, t.update_time,t.create_by,
		t.update_by, t.del_flag,t.accountsid,t.parent_id
		,article.news_item newsItem FROM wx_mp_menu t
		left join wx_mp_article article on article.media_id = t.source_content
		WHERE t.id=#{id}
	</select>

	<select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT COUNT(1)  FROM wx_mp_menu<include refid="sqlWhere" />
	</select>

	<select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
		SELECT <include refid="sqlColumns" />
		FROM wx_mp_menu t
		<include refid="sqlWhere" />
		<if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
		<if test="limit>0">limit #{offset},#{limit}</if>
	</select>
	
	<update id="updateChild" parameterType="java.util.Map">
		UPDATE wx_mp_menu set  del_flag = '1' where parent_id = ${parentId}
	</update>

	<update id="update" parameterType="com.cftech.mp.wechat.model.Menu">
		update wx_mp_menu
		<set>
			<if test="id != null">
				id = #{id, jdbcType=BIGINT},
			</if>
			<if test="name != null">
				name = #{name, jdbcType=VARCHAR},
			</if>
			<if test="sourceType != null">
				source_type = #{sourceType, jdbcType=CHAR},
			</if>
			<if test="msgType != null">
				msg_type = #{msgType, jdbcType=CHAR},
			</if>
			<if test="sourceContent != null">
				source_content = #{sourceContent, jdbcType=VARCHAR},
			</if>
			<if test="sort != null">
				sort = #{sort, jdbcType=INTEGER},
			</if>
			<if test="createTime != null">
				create_time = #{createTime, jdbcType=TIMESTAMP},
			</if>
			<if test="updateTime != null">
				update_time=now(),
			</if>
			<if test="createBy != null">
				create_by = #{createBy, jdbcType=BIGINT},
			</if>
			<if test="updateBy != null">
				update_by = #{updateBy, jdbcType=BIGINT},
			</if>
			<if test="delFlag != null">
				del_flag = #{delFlag, jdbcType=CHAR},
			</if>
			<if test="accountsid != null">
				accountsid = #{accountsid, jdbcType=BIGINT},
			</if>
			<if test="parentId != null">
				parent_id = #{parentId, jdbcType=BIGINT},
			</if>
		</set>
		where id=#{id,jdbcType=BIGINT}
	</update>

	<update id="delete" parameterType="java.lang.Long">
		update wx_mp_menu set del_flag=1 where id=#{id,jdbcType=BIGINT}
	</update>
	
	<select id="selectChildByparent_id" parameterType="String" resultType="com.cftech.mp.wechat.model.Menu">
		select u.*,m.name parentName from wx_mp_menu u
		left join wx_mp_menu m on u.parent_id=m.id
		where u.parent_id=#{parent_id} and m.del_flag='0' and u.del_flag='0'
		order by sort asc 
	</select>
	
	<select id="fetchSearchByPageSeach" parameterType="java.util.Map" resultMap="menuResultMap">
		select u.*,m.name parentName,article.news_item newsItem from wx_mp_menu u
		left join wx_mp_menu m on u.parent_id=m.id
		left join wx_mp_article article on article.media_id = u.source_content and u.msg_type=2
		<include refid="sqlWhere" />
		<if test="id!=null">
			and (u.id=#{id} or u.parent_id=#{id})
		</if>
		ORDER BY parent_id asc,sort desc
		<if test="limit>0">limit #{offset},#{limit}</if> 
	</select>
	
	<select id="fetchSearchByPageSeachCount" parameterType="java.util.Map" resultType="java.lang.Integer">
		select count(1) from wx_mp_menu u
		left join wx_mp_menu m on u.parent_id=m.id
		<include refid="sqlWhere" />
		<if test="id!=null">
			and (u.id=#{id} or u.parent_id=#{id})
		</if>
	</select>
</mapper>