<?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.base.org.dao.PositionMapper">

	<resultMap id="resultMap" type="com.cftech.base.org.model.Position">
		<id column="id" property="id" />
		<result column="name" property="name" />
		<result column="number" property="number" />
		<result column="accounts_id" property="accountsId" />
		<result column="parent_id" property="parentId" />
		<result column="parent_ids" property="parentIds" />
		<result column="status" property="status" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
		<result column="description" property="description" />
		<result column="del_flag" property="delFlag" />
		<result column="create_by" property="createBy" />
		<result column="update_by" property="updateBy" />
	</resultMap>
	<resultMap id="resultMapVO" type="com.cftech.base.org.model.vo.PositionVO">
		<id column="id" property="id" />
		<result column="name" property="name" />
		<result column="number" property="number" />
		<result column="accounts_id" property="accountsId" />
		<result column="parent_name" property="parentName" />
		<result column="create_time" property="createTime" />
	</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, number, accounts_id, parent_id, parent_ids,
		status, create_time, update_time, description, del_flag, create_by,
		update_by
	</sql>

	<insert id="save" parameterType="com.cftech.base.org.model.Position"
		keyProperty="id">
		insert into t_position
		(
		id, name, number, accounts_id,
		parent_id, parent_ids, status, create_time, update_time, description,
		del_flag, create_by, update_by
		)
		values
		(
		#{id, jdbcType=BIGINT}, #{name,
		jdbcType=VARCHAR}, #{number, jdbcType=VARCHAR}, #{accountsId,
		jdbcType=BIGINT},
		#{parentId, jdbcType=BIGINT}, #{parentIds, jdbcType=VARCHAR}, #{status,
		jdbcType=VARCHAR}, now(),
		now(), #{description, jdbcType=VARCHAR}, #{delFlag, jdbcType=TINYINT},
		#{createBy, jdbcType=BIGINT},
		#{updateBy, jdbcType=BIGINT}
		)
	</insert>

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

	<select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT COUNT(1) FROM t_position
		<include refid="sqlWhere" />
		and instr(parent_ids,'${positionId},')>0
	</select>

	<select id="fetchSearchByPage" parameterType="java.util.Map"
		resultMap="resultMap">
		SELECT
		<include refid="sqlColumns" />
		FROM t_position t
		<include refid="sqlWhere" />
		and instr(t.parent_ids,'${positionId},')>0
		<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.base.org.model.Position">
		update t_position
		<set>
			<if test="id != null">
				id = #{id, jdbcType=BIGINT},
			</if>
			<if test="name != null">
				name = #{name, jdbcType=VARCHAR},
			</if>
			<if test="number != null">
				number = #{number, jdbcType=VARCHAR},
			</if>
			<if test="accountsId != null">
				accounts_id = #{accountsId, jdbcType=BIGINT},
			</if>
			<if test="parentId != null">
				parent_id = #{parentId, jdbcType=BIGINT},
			</if>
			<if test="parentIds != null">
				parent_ids = #{parentIds, jdbcType=VARCHAR},
			</if>
			<if test="status != null">
				status = #{status, jdbcType=VARCHAR},
			</if>
			<if test="createTime != null">
				create_time = #{createTime, jdbcType=TIMESTAMP},
			</if>
			<if test="updateTime != null">
				update_time=now(),
			</if>
			<if test="description != null">
				description = #{description, jdbcType=VARCHAR},
			</if>
			<if test="delFlag != null">
				del_flag = #{delFlag, jdbcType=TINYINT},
			</if>
			<if test="createBy != null">
				create_by = #{createBy, jdbcType=BIGINT},
			</if>
			<if test="updateBy != null">
				update_by = #{updateBy, jdbcType=BIGINT},
			</if>
		</set>
		where id=#{id,jdbcType=BIGINT}
	</update>

	<update id="delete" parameterType="java.lang.Long">
		update t_position set
		del_flag=1 where id=#{id,jdbcType=BIGINT}
	</update>

	<select id="fetchSearchVOByPage" parameterType="java.util.Map"
		resultMap="resultMapVO">
		SELECT t.id id, t.name name, t.number number, t.accounts_id
		accounts_id,
		t.create_time create_time, IFNULL(p.name,'无') parent_name
		FROM t_position t
		left join t_position p on p.id = t.parent_id
		<include refid="sqlWhere" />
		and instr(t.parent_ids,'${positionId},')>0
		<if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
		<if test="limit>0">limit #{offset},#{limit}</if>
	</select>



	<!-- 根据人员ID，查询此人所有存在人的职位信息   或者查询某职位下面的所有人 -->
	<select id="getSubordinatePostion" parameterType="java.util.Map"
		resultType="java.util.Map">
		select p.id positionId,p.name positionName,qyuser.id userId,qyuser.name userName,qyuser.wxuser_id wxUserid from t_qyuser qyuser
		left join t_position p
		on p.id = qyuser.position_id
		left join t_orgunit o on o.id =
		qyuser.org_id
		where
		instr(o.parent_ids,',${orgId},') > 0
		and
		instr(p.parent_ids,',${positionId},') > 0
		and
		<if test="searchPositionId!=null">
			p.id = #{searchPositionId}
		</if> 
		<if test="searchPositionId==null">
			p.id != #{positionId}
		</if>
		and
		qyuser.id != #{userId}
		and qyuser.del_flag = 0 and p.del_flag=0 and
		o.del_flag=0
		and qyuser.accounts_id = #{accountsId}
		<if test="name!=null and name !='' ">and p.name like '%${name}%'</if>
		<if test="searchPositionId==null">group by positionId</if>
		<if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
		<if test="limit>0">limit #{offset},#{limit}</if>
	</select>
</mapper>