<?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.area.dao.AreaMapper">

	<resultMap id="resultMap" type="com.cftech.mp.area.model.Area">
		<id column="id" property="id"/>
		<result column="areaid" property="areaid"/>
		<result column="areaname" property="areaname"/>
		<result column="citytype" property="citytype"/>
		<result column="createtime" property="createtime"/>
		<result column="updatetime" property="updatetime"/>
		<result column="createby" property="createby"/>
		<result column="updateby" property="updateby"/>
		<result column="delflag" property="delflag"/>
		<result column="affiliationareaid" property="affiliationareaid"/>
	</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, areaid, areaname, citytype, affiliationareaid,createtime,updatetime,createby,updateby,delflag
	</sql>

	<insert id="save" parameterType="com.cftech.mp.area.model.Area" useGeneratedKeys="true" keyProperty="id">
		insert into area
		(
		<include refid="sqlColumns" /> 
		)
		values
		(#{id,jdbcType=BIGINT},
		#{areaid, jdbcType=INTEGER},  #{areaname, jdbcType=VARCHAR},  #{citytype, jdbcType=TINYINT},  #{affiliationareaid, jdbcType=INTEGER},
		now(),now(),#{createby,jdbcType=BIGINT},#{updateby,jdbcType=BIGINT},#{delflag,jdbcType=CHAR}
		)
	</insert>

	<select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT <include refid="sqlColumns" /> FROM area t
		WHERE t.id=#{id}
	</select>

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

	<select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
		SELECT <include refid="sqlColumns" /> 
		FROM area 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="update" parameterType="com.cftech.mp.area.model.Area">
		update area
		<set>
			<if test="id != null">
				id = #{id, jdbcType=BIGINT},
			</if>
			<if test="areaid != null">
				areaid = #{areaid, jdbcType=INTEGER},
			</if>
			<if test="areaname != null">
				areaname = #{areaname, jdbcType=VARCHAR},
			</if>
			<if test="citytype != null">
				citytype = #{citytype, jdbcType=TINYINT},
			</if>
			<if test="affiliationareaid != null">
				affiliationareaid = #{affiliationareaid, jdbcType=INTEGER},
			</if>
				updatetime = now(),
			<if test="delflag != null">
				delflag = #{delflag, jdbcType=CHAR},
			</if>
			<if test="createby != null">
				createby = #{createby, jdbcType=BIGINT},
			</if>
			<if test="updateby != null">
				updateby = #{updateby, jdbcType=BIGINT},
			</if>
			<if test="affiliationareaid != null">
				affiliationareaid = #{affiliationareaid, jdbcType=INTEGER},
			</if>
		</set>
		where id=#{id,jdbcType=BIGINT}
	</update>

	<update id="delete" parameterType="java.lang.Long">
		update area set delflag = 1 where id=#{id,jdbcType=BIGINT}
	</update>
	
	<resultMap type="com.cftech.mp.area.model.AreaDto" id="permResultMap">
		<result property="code" column="areaid"/>
		<result property="name" column="areaname"/>
		<association property="sub" column="areaid" select="selectChildByparent_id"/>
	</resultMap>
	
	<select id="selectChildByparent_id" parameterType="String" resultType="com.cftech.mp.area.model.AreaDto">
		select areaid code,areaname name from area where affiliationareaid=#{areaid} and delflag='0'
	</select>
	
	<select id="areaList" resultMap="permResultMap">
		select * from area where affiliationareaid='0' and delflag='0'
	</select>

</mapper>