<?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.github.wxiaoqi.security.admin.mapper.AppStaffUserMapper">
    <select id="selectByBysuIdOrUsername" resultType="com.github.wxiaoqi.security.admin.entity.AppStaffUser">
   SELECT
	s.id,
	s.su_id,
	s.u_id,
	s.company,
	s.remarks,
	s.crt_time,
	s.upd_time,
	s.is_del,
	s.`status`,
	(CASE
		WHEN ld.realname IS NOT NULL THEN
			ld.realname
		WHEN ld.nickname IS NOT NULL THEN
			ld.nickname
		ELSE
			s.u_name
		END
	) AS u_name,
	(
	   CASE
		WHEN ld.username IS NOT NULL THEN
			ld.username
		ELSE
			s.phone
		END
	) AS phone
        FROM
	app_staff_user s
        LEFT JOIN (
	SELECT
		l.id,
		l.username,
		d.nickname,
		d.realname
	FROM
		app_user_login l
	LEFT JOIN app_user_detail d ON l.id = d.userid
    ) ld ON s.u_id = ld.id
   WHERE is_del = 0 and  (su_id=#{id} or su_phone=#{username})
    </select>
    <update id="updateByPositionId" parameterType="com.github.wxiaoqi.security.admin.entity.ModifiedUsers">
       update app_staff_user set  su_phone=#{phone} where  su_id=#{userid}
   </update>

    <update id="update" parameterType="com.github.wxiaoqi.security.admin.entity.ModifiedUsers">
       update app_staff_user set  u_name=#{name}, phone= #{phone} where  u_id=#{userid}
   </update>

    <select id="saveStaffUser">
        INSERT IGNORE INTO `app_staff_user`(`su_phone`,`su_id`,`u_name`,`phone`,`company`,`remarks`,`crt_time`,`is_del`,`status`)VALUES(
        #{suPhone},#{suId},#{uName},#{phone},#{company},#{remarks},#{crtTime},#{isDel},#{status}
        )
    </select>
</mapper>