<?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.ruoyi.system.mapper.SysUserPostMapper">

	<resultMap type="SysUserPost" id="SysUserPostResult">
		<result property="userId"     column="user_id"      />
		<result property="postId"     column="post_id"      />
	</resultMap>

	<resultMap type="SysUser" id="SysUserResult">
		<id     property="userId"       column="user_id"      />
		<result property="deptId"       column="dept_id"      />
		<result property="userName"     column="user_name"    />
		<result property="nickName"     column="nick_name"    />
		<result property="email"        column="email"        />
		<result property="phonenumber"  column="phonenumber"  />
		<result property="sex"          column="sex"          />
		<result property="avatar"       column="avatar"       />
		<result property="password"     column="password"     />
		<result property="status"       column="status"       />
		<result property="delFlag"      column="del_flag"     />
		<result property="loginIp"      column="login_ip"     />
		<result property="loginDate"    column="login_date"   />
		<result property="createBy"     column="create_by"    />
		<result property="createTime"   column="create_time"  />
		<result property="updateBy"     column="update_by"    />
		<result property="updateTime"   column="update_time"  />
		<result property="remark"       column="remark"       />
		<result property="pmLevel"       column="pm_level"       />
		<result property="titles"       column="titles"       />
		<result property="certificates"       column="certificates"       />
		<result property="updateReason"       column="update_reason"       />
		<result property="engineerLevel"       column="engineer_level"       />
		<result property="postLevel"       column="post_level"       />
		<result property="salaryLevel"       column="salary_level"       />
		<result property="operatorLevel"    column="operator_level"    />
		<result property="entryDate"       column="entry_date"       />
		<result property="birthday"       column="birthday"       />
		<result property="age"       column="age"       />
		<result property="idCard"    column="id_card"    />
		<result property="nativePlace"    column="native_place"    />
		<result property="politicalAffiliation"    column="political_affiliation"    />
		<result property="ethnic"    column="ethnic"    />
		<result property="graduateSchool"    column="graduate_school"    />
		<result property="major"    column="major"    />
		<result property="degree"    column="degree"    />
		<result property="contractSign"    column="contract_sign"    />
		<result property="contractExpire"    column="contract_expire"    />
		<result property="homePlace"    column="home_place"    />
		<association property="dept"    javaType="SysDept"         resultMap="deptResult" />
	</resultMap>

	<resultMap id="deptResult" type="SysDept">
		<id     property="deptId"    column="dept_id"     />
		<result property="deptName"  column="dept_name"   />
	</resultMap>

	<select id="selectDriverList" resultMap="SysUserResult">
		select u.user_id, u.nick_name
		from sys_user u left join sys_user_post up on u.user_id = up.user_id
		where up.post_id = 31 and u.status = '0'
	</select>

	<select id="selectGmAssistant" parameterType="SysUser" resultMap="SysUserResult">
		select u.*, d.dept_name
		from sys_user u
		left join sys_dept d on u.dept_id = d.dept_id
		left join sys_user_post up on u.user_id = up.user_id
		left join sys_post p on up.post_id = p.post_id
		where p.post_name = "总经理助理"
		<if test="userId != null and userId != 0">
			AND u.user_id = #{userId}
		</if>
		<if test="userName != null and userName != ''">
			AND u.user_name like concat('%', #{userName}, '%')
		</if>
		<if test="nickName != null and nickName != ''">
			AND u.nick_name like concat('%', #{nickName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND u.status = #{status}
		</if>
		<if test="phonenumber != null and phonenumber != ''">
			AND u.phonenumber like concat('%', #{phonenumber}, '%')
		</if>
	</select>

	<select id="selectUserListByPostName" parameterType="String" resultMap="SysUserResult">
		select * from sys_user u
		left join sys_user_post up on u.user_id = up.user_id
		left join sys_post p on up.post_id = p.post_id
		where p.post_name = #{param}
	</select>

	<select id="selectSubLeaderByUserId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select u1.user_id, u1.nick_name, u.dept_id from
		sys_user as u, sys_user as u1 where u.user_id = #{param} and u1.dept_id = u.dept_id) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '子公司执行董事'
	</select>

	<select id="selectSubGmByUserId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select u1.user_id, u1.nick_name, u.dept_id from
		sys_user as u, sys_user as u1 where u.user_id = #{param} and u1.dept_id = u.dept_id) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '子公司总经理'
	</select>

	<select id="selectBranchLeaderByUserId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select u1.user_id, u1.nick_name, u.dept_id from
		sys_user as u, sys_user as u1 where u.user_id = #{param} and u1.dept_id = u.dept_id) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '经理'
	</select>

	<select id="selectDeptLeaderByUserId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select u1.user_id, u1.nick_name, u.dept_id from
		sys_user as u, sys_user as u1 where u.user_id = #{param} and u1.dept_id = u.dept_id) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '主任'
	</select>

	<select id="selectDeptLeadersByUserId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select u1.user_id, u1.nick_name, u.dept_id from
		sys_user as u, sys_user as u1 where u.user_id = #{param} and u1.dept_id = u.dept_id) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '主任'
	</select>

	<select id="selectViceDeptLeaderByUserId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select u1.user_id, u1.nick_name, u.dept_id from
		sys_user as u, sys_user as u1 where u.user_id = #{param} and u1.dept_id = u.dept_id) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '副主任' limit 1
	</select>

	<select id="selectSubLeaderByDeptId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select user_id, nick_name, dept_id from
		sys_user where dept_id = #{param}) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '子公司执行董事'
	</select>

	<select id="selectSubGmByDeptId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select user_id, nick_name, dept_id from
		sys_user where dept_id = #{param}) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '子公司总经理'
	</select>

	<select id="selectBranchLeaderByDeptId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select user_id, nick_name, dept_id from
		sys_user where dept_id = #{param}) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '经理'
	</select>

	<select id="selectDeptLeaderByDeptId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select user_id, nick_name, dept_id from
		sys_user where dept_id = #{param}) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '主任'
	</select>

	<select id="selectDeptLeadersByDeptId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select user_id, nick_name, dept_id from
		sys_user where dept_id = #{param}) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '主任'
	</select>

	<select id="selectViceDeptLeaderByDeptId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select user_id, nick_name, dept_id from
		sys_user where dept_id = #{param}) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '副主任' limit 1
	</select>

	<select id="selectViceDeptLeaderListByDeptId" parameterType="String" resultMap="SysUserResult">
		select r.user_id, r.nick_name from
		(select user_id, nick_name, dept_id from
		sys_user where dept_id = #{param}) as r
		left join sys_user_post as up on r.user_id = up.user_id
		left join sys_post as p on p.post_id = up.post_id
		where p.post_name = '副主任'
	</select>

	<delete id="deleteUserPostByUserId" parameterType="Long">
		delete from sys_user_post where user_id=#{userId}
	</delete>
	
	<select id="countUserPostById" resultType="Integer">
	    select count(1) from sys_user_post where post_id=#{postId}  
	</select>
	
	<delete id="deleteUserPost" parameterType="Long">
 		delete from sys_user_post where user_id in
 		<foreach collection="array" item="userId" open="(" separator="," close=")">
 			#{userId}
        </foreach> 
 	</delete>
	
	<insert id="batchUserPost">
		insert into sys_user_post(user_id, post_id) values
		<foreach item="item" index="index" collection="list" separator=",">
			(#{item.userId},#{item.postId})
		</foreach>
	</insert>
	
</mapper> 