<?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="SapUserMapper">
	<insert id="saveOne">
		insert into sap_user(userId,personId,username,name,
			firstOrgId,secondOrgId,thirdOrgId,fourthOrgId,
			firstOrgName,secondOrgName,thirdOrgName,fourthOrgName,
			position,formatPosition,mobile,formatMobile,businessPhone,formatBusinessPhone,gender,
			email,formatEmail,isPrimary,delFlag,createDate,result,sortNumber) 
		values (#{userId},#{personId},#{username},#{name},
			#{firstOrgId},#{secondOrgId},#{thirdOrgId},#{fourthOrgId},
			#{firstOrgName},#{secondOrgName},#{thirdOrgName},#{fourthOrgName},
			#{position},#{formatPosition},#{mobile},#{formatMobile},#{businessPhone},#{formatBusinessPhone},#{gender},
			#{email},#{formatEmail},#{isPrimary},#{delFlag},#{createDate},#{result},#{sortNumber})
	</insert>
	
	<update id="updateOne">
		update sap_user 
		set username=#{username},name=#{name},personId=#{personId},
			firstOrgId=#{firstOrgId},secondOrgId=#{secondOrgId},
			thirdOrgId=#{thirdOrgId},fourthOrgId=#{fourthOrgId},
			firstOrgName=#{firstOrgName},secondOrgName=#{secondOrgName},
			thirdOrgName=#{thirdOrgName},fourthOrgName=#{fourthOrgName},
			position=#{position},formatPosition=#{formatPosition},wxPosition=#{wxPosition},
			mobile=#{mobile},formatMobile=#{formatMobile},
			businessPhone=#{businessPhone},formatBusinessPhone=#{formatBusinessPhone},
			gender=#{gender},email=#{email},formatEmail=#{formatEmail},
			isPrimary=#{isPrimary},result=#{result},delFlag=#{delFlag}
		where userId=#{userId}
	</update>
	
	<update id="updateDelFlag">
		update sap_user set delFlag=#{delFlag} where userId=#{userId}
	</update>
	
	<update id="updateAvatar">
		update sap_user set avatar=#{avatar} where personId=#{personId}
	</update>
	
	<update id="updateResult">
		update sap_user set result=#{result} where personId=#{personId}
	</update>
	
	<select id="selectByUserId" resultType="SapUser">
		select * from sap_user where userId=#{userId}
	</select>
	
	<select id="selectListByPersonId" resultType="SapUser">
		select * from sap_user where personId=#{personId} order by isPrimary desc
	</select>
	
	<select id="selectUnSyncList" resultType="SapUser">
		select * from sap_user where result is null and delFlag = 0
	</select>
	
	<update id="updateAllDelFlag">
		update sap_user set delFlag = #{delFlag}
	</update>
	
	<select id="selectReportUserList" resultType="SapUser">
		select * from sap_user where (result != '垃圾数据' or result is null) and delFlag = 0
	</select>
	
	<select id="selectListByName" resultType="SapUser">
		select * from sap_user 
		where result = 'SUCCESS' and isPrimary = 1 and delFlag = 0 and name like CONCAT(CONCAT('%', #{name}),'%')
		order by sortNumber ASC
	</select>
	
	<select id="selectListByResult" resultType="SapUser">
		select * from sap_user 
		where result = #{result} and isPrimary = 1 and delFlag = 0 
		order by sortNumber ASC
	</select>
	
	<select id="selectListByDelFlag" resultType="SapUser">
		select * from sap_user 
		where isPrimary = 1 and delFlag = #{delFlag} 
		order by sortNumber ASC
	</select>
	
	<select id="selectListByDepartIdAndLevel" resultType="SapUser">
		select * from sap_user 
		where result = 'SUCCESS' and isPrimary = 1 and delFlag = 0 
		<if test="level == 1">
			and firstOrgId = #{departId} and secondOrgId is null
		</if>
		<if test="level == 2">
			and secondOrgId = #{departId} and thirdOrgId is null
		</if>
		<if test="level == 3">
			and thirdOrgId = #{departId} and fourthOrgId is null
		</if>
		<if test="level == 4">
			and fourthOrgId = #{departId}
		</if>
		order by sortNumber ASC
	</select>
</mapper>