<?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.bonus.boot.manager.manager.dao.UserDao">

	<select id="count" resultType="int">
		select count(1) from sys_user t
		left join pm_organization po on po.id = t.department and po.is_active = '1'
		left join sys_role sr on sr.id = t.role and sr.is_active = '1'
		where
		t.is_active = '1'
		<if test="params.username != null and params.username != ''">
			and t.username like concat('%', #{params.username}, '%')
		</if>
		<if test="params.nickname != null and params.nickname != ''">
			and t.nickname like concat('%', #{params.nickname}, '%')
		</if>
		<if test="params.status != null and params.status != ''">
			and t.status = #{params.status}
		</if>
	</select>

	<select id="list" resultType="com.bonus.boot.manager.manager.model.SysUser">
		select t.id ,
		t.username,
		t.nickname,
		t.phone,
		t.id_card as idCard,
		t.department as departmentId,
		po.name as department,
		t.age,
		if(t.sex = '1','男','女') as combSex,
		t.status,
		t.role as roleId,
		sr.name as roleName
		from sys_user t
		left join pm_organization po on po.id = t.department and po.is_active = '1'
		left join sys_role sr on sr.id = t.role and sr.is_active = '1'
		where
		t.is_active = '1'
		<if test="params.username != null and params.username != ''">
			and t.username like concat('%', #{params.username}, '%')
		</if>
		<if test="params.nickname != null and params.nickname != ''">
			and t.nickname like concat('%', #{params.nickname}, '%')
		</if>
		<if test="params.status != null and params.status != ''">
			and t.status = #{params.status}
		</if>
        limit #{offset}, #{limit}
	</select>


	<insert id="saveUserRoles">
		insert into sys_role_user(roleId, userId) values
		<foreach collection="roleIds" item="roleId" separator=",">
			(#{roleId}, #{userId})
		</foreach>
	</insert>


	<update id="update">
		update sys_user t
		<set>
			<if test="username != null">
				username = #{username},
			</if>
			<if test="nickname != null">
				nickname = #{nickname},
			</if>
			<if test="headImgUrl != null">
				headImgUrl = #{headImgUrl},
			</if>
			<if test="phone != null">
				phone = #{phone},
			</if>
			<if test="telephone != null">
				telephone = #{telephone},
			</if>
			<if test="email != null">
				email = #{email},
			</if>
			<if test="birthday != null">
				birthday = #{birthday},
			</if>
			<if test="sex != null">
				sex = #{sex},
			</if>
			<if test="status != null">
				status = #{status},
			</if>
			updateTime = #{updateTime}
		</set>
		where t.id = #{id}
	</update>

	<select id="getMsgList" resultType="com.bonus.boot.manager.manager.model.SysUser">
		select t.id ,
		t.username,
		t.nickname,
		t.phone,
		t.id_card as idCard,
		t.department as departmentId,
		po.name as department,
		t.age,
		if(t.sex = '1','男','女') as sex,
		if(t.is_push = '1','已推送','未推送') as isPush,
		t.status,
		t.role as roleId,
		sr.name as roleName
		from sys_user t
		left join pm_organization po on po.id = t.department and po.is_active = '1'
		left join sys_role sr on sr.id = t.role and sr.is_active = '1'
		where
		t.is_active = '1'
		<if test="username != null and username != ''">
			and t.username like concat('%', #{username}, '%')
		</if>
		<if test="idCard != null and idCard != ''">
			and t.id_card like concat('%', #{idCard}, '%')
		</if>
		<if test="departmentId != null and departmentId != ''">
			and t.department = #{departmentId}
		</if>
		<if test="keyWord != null and keyWord != ''">
			and (
			    t.username like concat('%', #{keyWord}, '%') or
				t.id_card like concat('%', #{keyWord}, '%') or
				sr.name like concat('%', #{keyWord}, '%') or
				po.name like concat('%', #{keyWord}, '%') or
				t.phone like concat('%', #{keyWord}, '%')
			)
		</if>
	</select>
	<select id="getListById" resultType="com.bonus.boot.manager.manager.model.SysUser">
		select t.id ,
			   t.username,
			   t.nickname,
			   t.phone,
			   t.id_card as idCard,
			   t.department as departmentId,
			   po.name as department,
			   t.age,
			   if(t.sex = '1','男','女') as sex,
			   t.status,
			   t.role as roleId,
			   t.hospital_id as hospitalId,
			   sr.name as roleName
		from sys_user t
				 left join pm_organization po on po.id = t.department and po.is_active = '1'
				 left join sys_role sr on sr.id = t.role and sr.is_active = '1'
		where
			t.is_active = '1' and t.id= #{id}
	</select>
	<select id="getEyId" resultType="com.bonus.boot.manager.manager.model.SysUser">
		SELECT
		    username AS username,
		    phone AS phone
		FROM sys_user
		WHERE id =  #{userId};
	</select>
	<select id="getLogininfor" resultType="com.bonus.boot.manager.manager.entity.UnifyBean">
		SELECT
			'' as loginName,
			l.user_name as userName ,
			l.ip_address as loginIp,
			'健康体检' as loginUrl,
			l.create_time as loginTimes,
			l.login_result as loginStatus
		FROM `sys_login_log` l
		WHERE DATE(create_time) = #{createTime}
		ORDER BY l.create_time DESC
	</select>
	<select id="getLogs" resultType="com.bonus.boot.manager.manager.entity.UnifyBean">
		SELECT
			u.username,
			'健康体检' AS appId,
			'' AS model,
			l.module AS operFun,
			'' AS description,
			l.createTime AS createDate,
			CASE WHEN l.flag = '1' THEN '200' ELSE '500' END result
		FROM sys_logs l
				 LEFT JOIN sys_user u ON l.userId = u.id
		WHERE DATE(l.createTime) = #{createTime}
		  AND l.module NOT LIKE '%登录%'
		  AND l.module NOT LIKE '%退出%'

	</select>
	<select id="getPhysicalExamination" resultType="com.bonus.boot.manager.manager.entity.UnifyBean">
		SELECT
			phy_name AS userName,
			telep_number AS phone,
			'健康体检' AS systemName,
			CASE
				WHEN EXISTS (SELECT 1 FROM sys_user WHERE phone = telep_number AND is_active = '1') THEN '后台, 移动端'
				ELSE '移动端'
				END AS auth,
			'0' AS isBlacklist,
			'1' AS accountStatus,
			'1' AS status
		FROM pm_base_physical
		WHERE is_active = '1' AND pushStatus != 1
	</select>
	<select id="getPermissionQuery" resultType="java.lang.String">
        SELECT CASE
                   WHEN EXISTS (SELECT 1 FROM pm_base_physical WHERE telep_number = #{phone} AND is_active = '1') AND
                        EXISTS (SELECT 1 FROM sys_user WHERE phone = #{phone} AND is_active = '1') THEN '后台, 移动端'
                   WHEN EXISTS (SELECT 1 FROM sys_user WHERE phone = #{phone} AND is_active = '1') THEN '后台'
                   WHEN EXISTS (SELECT 1 FROM pm_base_physical WHERE telep_number = #{phone} AND is_active = '1')
                       THEN '移动端'
                   ELSE '不存在'
                   END AS source
        FROM pm_base_physical
        WHERE telep_number = #{phone}
          AND is_active = '1'
    </select>
	<select id="getPushByBackOfficePersonnel" resultType="com.bonus.boot.manager.manager.entity.UnifyBean">
		SELECT
			username AS userName,
			phone AS phone,
			'健康体检' AS systemName,
			CASE
				WHEN EXISTS (SELECT 1 FROM pm_base_physical WHERE phone = telep_number AND is_active = '1') THEN '后台, 移动端'
				ELSE '后台'
				END AS auth,
			'0' AS isBlacklist,
			'1' AS accountStatus,
			'1' AS status
		FROM sys_user
		WHERE is_active = '1' AND is_push != 1
	</select>
	<select id="getPermissionQuerys" resultType="java.lang.String">
		SELECT CASE
				   WHEN EXISTS (SELECT 1 FROM pm_base_physical WHERE telep_number = #{phone} AND is_active = '1') AND
						EXISTS (SELECT 1 FROM sys_user WHERE phone = #{phone} AND is_active = '1') THEN '后台, 移动端'
				   WHEN EXISTS (SELECT 1 FROM sys_user WHERE phone = #{phone} AND is_active = '1') THEN '后台'
				   WHEN EXISTS (SELECT 1 FROM pm_base_physical WHERE telep_number = #{phone} AND is_active = '1')
					   THEN '移动端'
				   ELSE '不存在'
				   END AS source
		FROM sys_user
		WHERE phone = #{phone}
		  AND is_active = '1'
	</select>

	<insert id="addInfo" keyProperty="userId" useGeneratedKeys="true">
		insert into sys_user(username,password,id_card,phone,sex,age,department,role,is_active,hospital_id)
		values (#{username},#{password},#{idCard},#{phone},#{sex},#{age},#{departmentId},#{roleId},'1',#{hospitalId})
	</insert>
	<insert id="insertroleUser" parameterType="com.bonus.boot.manager.manager.model.SysUser">
		insert into sys_role_user(userId,roleId)
		values (#{userId},#{roleId})
	</insert>

	<delete id="delById">
		update sys_user set is_active = '0' where id = #{id}
	</delete>
	<update id="resetPassword">
		update sys_user set password = #{password} where id = #{id}
	</update>
	<update id="updateInfo">
		update sys_user
		<set>
			<if test="username != null">
				username = #{username},
			</if>
			<if test="idCard != null">
				id_card = #{idCard},
			</if>
			<if test="phone != null">
				phone = #{phone},
			</if>
			<if test="sex != null">
				sex = #{sex},
			</if>
			<if test="age != null">
				age = #{age},
			</if>
			<if test="roleId != null">
				role = #{roleId},
			</if>
			<if test="departmentId != null">
				department = #{departmentId},
			</if>
			<if test="hospitalId != null">
				hospital_id = #{hospitalId},
			</if>
		</set>
		where id = #{id}
	</update>
	<update id="updateRoleUser">
		UPDATE sys_role_user
		SET
			roleId = #{roleId}
		WHERE
			userId = #{id};
	</update>
	<update id="resetPasswordForOne">
		update sys_user set password = #{password} where username = #{username}
	</update>
	<update id="updateUnify">
		UPDATE sys_user SET is_push = '0' WHERE id = #{id}
	</update>
	<update id="updateUserPushStatus">
		<foreach collection="list" item="user" separator=";">
			UPDATE pm_base_physical SET `pushStatus`= #{user.status} WHERE telep_number = #{user.phone} and is_active = '1'
		</foreach>
	</update>
	<update id="updateUserBackstagePushStatus">
		<foreach collection="list" item="user" separator=";">
			UPDATE sys_user SET `is_push`= #{user.status} WHERE phone = #{user.phone} and is_active = '1'
		</foreach>
	</update>

</mapper>