<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~      Copyright (c) 2018-2025, hnqz All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the pig4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: hnqz
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qunzhixinxi.hnqz.admin.mapper.SysUserMapper">
	<resultMap id="userResultMap" type="com.qunzhixinxi.hnqz.admin.api.entity.SysUser">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="salt" property="salt"/>
		<result column="phone" property="phone"/>
		<result column="id_card_number" property="idCardNumber"/>
		<result column="userSign" property="user_sign"/>
		<result column="avatar" property="avatar"/>
		<result column="wx_openid" property="wxOpenid"/>
		<result column="qq_openid" property="qqOpenid"/>
		<result column="create_time" property="createTime"/>
		<result column="update_time" property="updateTime"/>
		<result column="lock_flag" property="lockFlag"/>
		<result column="del_flag" property="delFlag"/>
		<result column="dept_id" property="deptId"/>
		<result column="tenant_id" property="tenantId"/>
		<result column="dept_name" property="deptName"/>
		<result column="realname" property="realname"/>
		<result column="province" property="province"/>
		<result column="city" property="city"/>
		<result column="area" property="area"/>
		<result column="address" property="address"/>
		<result column="temp1" property="temp1"/>
		<result column="temp2" property="temp2"/>
		<result column="temp3" property="temp3"/>
		<result column="temp4" property="temp4"/>
		<result column="temp5" property="temp5"/>
		<result column="w1" property="w1"/>
		<result column="plat_id" property="platId"/>
		<result column="drug_ent_id" property="drugEntId"/>
		<result column="cert_status" property="certStatus"/>
		<result column="rlj_cert_status" property="rljCertStatus"/>
		<result column="cert_remark" property="certRemark"/>
		<result column="province_code" property="provinceCode"/>
		<result column="city_code" property="cityCode"/>
		<result column="user_type" property="userType"/>
		<result column="bank_card_number" property="bankCardNumber"/>
		<result column="bank_phone" property="bankPhone"/>
		<result column="tax_helper_agreement_signature" property="taxHelperAgreementSignature"/>
	</resultMap>

	<!-- 通用查询映射结果 -->
	<resultMap id="baseResultMap" type="com.qunzhixinxi.hnqz.admin.api.vo.UserVO">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="salt" property="salt"/>
		<result column="phone" property="phone"/>
		<result column="id_card_number" property="idCardNumber"/>
		<result column="userSign" property="user_sign"/>
		<result column="avatar" property="avatar"/>
		<result column="wx_openid" property="wxOpenid"/>
		<result column="qq_openid" property="qqOpenid"/>
		<result column="ucreate_time" property="createTime"/>
		<result column="uupdate_time" property="updateTime"/>
		<result column="lock_flag" property="lockFlag"/>
		<result column="udel_flag" property="delFlag"/>
		<result column="dept_id" property="deptId"/>
		<result column="tenant_id" property="tenantId"/>
		<result column="dept_name" property="deptName"/>
		<result column="realname" property="realname"/>
		<result column="province" property="province"/>
		<result column="city" property="city"/>
		<result column="area" property="area"/>
		<result column="address" property="address"/>
		<result column="temp1" property="temp1"/>
		<result column="temp2" property="temp2"/>
		<result column="temp3" property="temp3"/>
		<result column="temp4" property="temp4"/>
		<result column="temp5" property="temp5"/>
		<result column="w1" property="w1"/>
		<result column="plat_id" property="platId"/>
		<result column="drug_ent_id" property="drugEntId"/>
		<result column="cert_status" property="certStatus"/>
		<result column="rlj_cert_status" property="rljCertStatus"/>
		<result column="cert_remark" property="certRemark"/>
		<result column="province_code" property="provinceCode"/>
		<result column="city_code" property="cityCode"/>
		<result column="user_type" property="userType"/>
		<result column="bank_phone" property="bankPhone"/>
		<result column="bank_card_number" property="bankCardNumber"/>
		<collection property="roleList" ofType="com.qunzhixinxi.hnqz.admin.api.entity.SysRole"
					select="com.qunzhixinxi.hnqz.admin.mapper.SysRoleMapper.listRolesByUserId" column="user_id">
		</collection>
	</resultMap>

	<!-- userVo结果集 -->
	<resultMap id="userVoResultMap" type="com.qunzhixinxi.hnqz.admin.api.vo.UserVO">
		<id column="user_id" property="userId"/>
		<result column="username" property="username"/>
		<result column="password" property="password"/>
		<result column="salt" property="salt"/>
		<result column="phone" property="phone"/>
		<result column="id_card_number" property="idCardNumber"/>
		<result column="avatar" property="avatar"/>
		<result column="wx_openid" property="wxOpenid"/>
		<result column="qq_openid" property="qqOpenid"/>
		<result column="create_time" property="createTime"/>
		<result column="update_time" property="updateTime"/>
		<result column="lock_flag" property="lockFlag"/>
		<result column="del_flag" property="delFlag"/>
		<result column="dept_id" property="deptId"/>
		<result column="tenant_id" property="tenantId"/>
		<result column="dept_name" property="deptName"/>
		<result column="realname" property="realname"/>
		<result column="province" property="province"/>
		<result column="city" property="city"/>
		<result column="area" property="area"/>
		<result column="address" property="address"/>
		<result column="temp1" property="temp1"/>
		<result column="temp2" property="temp2"/>
		<result column="temp3" property="temp3"/>
		<result column="temp4" property="temp4"/>
		<result column="temp5" property="temp5"/>
		<result column="plat_id" property="platId"/>
		<result column="w1" property="w1"/>
		<result column="drug_ent_id" property="drugEntId"/>
		<result column="cert_status" property="certStatus"/>
		<result column="rlj_cert_status" property="rljCertStatus"/>
		<result column="cert_remark" property="certRemark"/>
		<result column="province_code" property="provinceCode"/>
		<result column="city_code" property="cityCode"/>
		<result column="user_type" property="userType"/>
		<result column="bank_phone" property="bankPhone"/>
		<result column="bank_card_number" property="bankCardNumber"/>
		<collection property="roleList" ofType="com.qunzhixinxi.hnqz.admin.api.entity.SysRole">
			<id column="role_id" property="roleId"/>
			<result column="role_name" property="roleName"/>
			<result column="role_code" property="roleCode"/>
			<result column="role_desc" property="roleDesc"/>
			<result column="rcreate_time" property="createTime"/>
			<result column="rupdate_time" property="updateTime"/>
		</collection>
	</resultMap>



	<sql id="userRoleSql">
		distinct
		u.user_id,
		u.username,
		u.password,
		u.salt,
		u.phone,
		u.id_card_number,
		u.user_type,
		u.bank_card_number,
		u.avatar,
		u.wx_openid,
		u.qq_openid,
		u.dept_id,
		u.del_flag,
		u.lock_flag,
		u.tenant_id,
		u.create_time  ucreate_time,
		u.update_time  uupdate_time,
		r.role_id,
		r.role_name,
		r.role_code,
		r.role_desc,
		r.create_time  rcreate_time,
		r.update_time  rupdate_time,
		u.realname,
		u.province,
		u.city,
		u.province_code,
		u.city_code,
		u.area,
		u.address,
		u.temp1,
		u.temp2,
		u.temp3,
		u.temp4,
		u.temp5,
		u.plat_id,
		u.drug_ent_id,
		u.cert_status,
		u.rlj_cert_status,
		u.cert_remark,
		u.w1,
		r.role_id,
		r.role_name,
		r.role_code,
		r.role_desc,
		r.create_time  rcreate_time,
		r.update_time  rupdate_time
	</sql>

	<sql id="userRoleDeptSql">
		distinct
		u.user_id,
		u.username,
		u.password,
		u.salt,
		u.phone,
		u.id_card_number,
		u.user_type,
		u.bank_card_number,
		u.avatar,
		u.wx_openid,
		u.qq_openid,
		u.del_flag,
		u.lock_flag,
		u.tenant_id,
		u.create_time  ucreate_time,
		u.update_time  uupdate_time,
		r.role_id,
		r.role_name,
		r.role_code,
		r.role_desc,
		r.create_time  rcreate_time,
		r.update_time  rupdate_time,
		u.realname,
		u.province,
		u.city,
		u.province_code,
		u.city_code,
		u.area,
		u.address,
		u.temp1,
		u.temp2,
		u.temp3,
		u.temp4,
		u.temp5,
		u.plat_id,
		u.drug_ent_id,
		u.cert_status,
		u.rlj_cert_status,
		u.cert_remark,
		u.w1,
		r.role_id,
		r.role_name,
		r.role_code,
		r.role_desc,
		r.create_time  rcreate_time,
		r.update_time  rupdate_time,
		d.name  dept_name,
		d.dept_id
    </sql>

	<select id="selectByDeptId" resultMap="userVoResultMap">
		SELECT
		*
		FROM
		sys_user  u
		LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
		LEFT JOIN sys_user_role  ur ON ur.user_id = u.user_id
		LEFT JOIN sys_role  r ON r.role_id = ur.role_id
		LEFT JOIN sys_dept_sub s ON s.dept_id = u.dept_id and s.enable_flag = '1'
		where
		u.del_flag = 0
		<if test="query.username != null and query.username !=''">
			AND u.username like CONCAT('%',#{query.username},'%')
		</if>
		<if test="query.realname != null and query.realname !=''">
			AND u.realname like CONCAT('%',#{query.realname},'%')
		</if>
		<if test="query.deptId != null and query.deptId != ''">
			AND u.dept_id = #{query.deptId}
		</if>
		<if test="query.certStatus != null and query.certStatus != ''">
			<choose>
				<when test="query.certStatus == 10">
					AND (u.cert_status = #{query.certStatus} OR u.cert_status IS NULL)
				</when>
				<otherwise>
					AND u.cert_status = #{query.certStatus}
				</otherwise>
			</choose>
		</if>
		<if test="query.rljCertStatus != null and query.rljCertStatus != ''">
			<choose>
				<when test="query.rljCertStatus == 4">
					AND (u.rlj_cert_status = #{query.rljCertStatus} OR u.rlj_cert_status IS NULL)
				</when>
				<otherwise>
					AND u.rlj_cert_status = #{query.rljCertStatus}
				</otherwise>
			</choose>
		</if>
		<if test="query.subType != null and query.subType != ''">
			AND s.subject_type = #{query.subType}
		</if>
		<if test="query.lockFlag != null and query.lockFlag != ''">
			AND u.lock_flag = #{query.lockFlag}
		</if>
		<if test="query.role != null and query.role.size()>0">
			AND r.role_id in (
			<foreach collection="query.role" item="pid" index="index" separator=",">
				#{pid}
			</foreach>
			)
		</if>
	</select>

	<select id="selectByPhone" resultMap="userVoResultMap">
		SELECT
		*
		FROM
		sys_user  u
		LEFT JOIN sys_user_role  ur ON ur.user_id = u.user_id
		LEFT JOIN sys_role  r ON r.role_id = ur.role_id
		where
		u.del_flag = 0
		<if test="query.username != null and query.username !=''">
			AND u.username = #{query.username}
		</if>
		<if test="query.role != null and query.role.size()>0">
			AND r.role_id in (
			<foreach collection="query.role" item="pid" index="index" separator=",">
				#{pid}
			</foreach>
			)
		</if>
	</select>

	<select id="getUserVoByUsername" resultMap="userVoResultMap">
		SELECT
			<include refid="userRoleSql"/>
		FROM
		sys_user  u
		LEFT JOIN sys_user_role  ur ON ur.user_id = u.user_id
		LEFT JOIN sys_role  r ON r.role_id = ur.role_id
			WHERE u.username = #{username} and r.role_id not in (5,6)
			and u.lock_flag=0
	</select>

	<select id="getUserVoById" resultMap="userVoResultMap">
        SELECT
	   		<include refid="userRoleDeptSql"/>
        FROM
            sys_user  u
            LEFT JOIN sys_user_role  ur ON ur.user_id = u.user_id
            LEFT JOIN sys_role  r ON r.role_id = ur.role_id
            LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
        WHERE
           u.user_id = #{id}
           and u.lock_flag=0
    </select>

	<select id="getByIdIgnore" resultMap="userResultMap">
		SELECT * FROM sys_user  where user_id = #{id}
	</select>

	<select id="selectByUserId" resultMap="userVoResultMap">
		SELECT
		*
		FROM
		sys_user  u
		LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
		LEFT JOIN sys_user_role  ur ON ur.user_id = u.user_id
		LEFT JOIN sys_role  r ON r.role_id = ur.role_id
		where
		u.del_flag = 0
		<if test="query.username != null and query.username !=''">
			AND u.username like CONCAT('%',#{query.username},'%')
		</if>
		<if test="query.realname != null and query.realname !=''">
			AND u.realname like CONCAT('%',#{query.realname},'%')
		</if>
		<if test="query.userId != null and query.userId != ''">
			AND u.user_id = #{query.userId}
		</if>
		<if test="query.lockFlag != null and query.lockFlag != ''">
			AND u.lock_flag = #{query.lockFlag}
		</if>
		<if test="query.role != null and query.role.size()>0">
			AND r.role_id in (
			<foreach collection="query.role" item="pid" index="index" separator=",">
				#{pid}
			</foreach>
			)
		</if>
	</select>

	<select id="selectUserByName" resultMap="userResultMap">
		SELECT
		*
		FROM
		sys_user
		where
		username = #{username}
		and
		dept_id = #{deptId}
	</select>

	<select id="selectUserById" resultMap="userResultMap">
		SELECT
		*
		FROM
		sys_user
		where
		dept_id = #{deptId}
	</select>

	<select id="getUserVosPage" resultMap="baseResultMap">
		SELECT
		distinct
		u.user_id,
		u.username,
		u.password,
		u.id_card_number,
		u.user_type,
		u.salt,
		u.phone,
		u.avatar,
		u.wx_openid,
		u.qq_openid,
		u.dept_id,
		u.create_time  ucreate_time,
		u.update_time  uupdate_time,
		u.del_flag,
		u.lock_flag,
		u.tenant_id,
		d.name  dept_name,
		u.realname,
		u.province,
		u.city,
		u.province_code,
		u.city_code,
		u.area,
		u.address,
		u.temp1,
		u.temp2,
		u.temp3,
		u.temp4,
		u.temp5,
		u.w1,
		u.plat_id,
		u.drug_ent_id,
		u.cert_status,
		u.rlj_cert_status,
		u.cert_remark,
		u.bank_card_number
		FROM
		sys_user  u
		LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
		LEFT JOIN sys_user_role  ur ON ur.user_id = u.user_id
		LEFT JOIN sys_role  r ON r.role_id = ur.role_id
		LEFT JOIN sys_dept_sub s ON s.dept_id = u.dept_id and s.enable_flag = '1'
		<where>
			u.del_flag = '0'
			<if test="query.username != null and query.username != ''">
				AND u.username LIKE CONCAT('%',#{query.username},'%')
			</if>
			<if test="query.realname != null and query.realname != ''">
				AND u.realname LIKE CONCAT('%',#{query.realname},'%')
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND u.dept_id = #{query.deptId}
			</if>
			<if test="query.certStatus != null and query.certStatus != ''">
				<choose>
					<when test="query.certStatus == 10">
						AND (u.cert_status = #{query.certStatus} OR u.cert_status IS NULL)
					</when>
					<otherwise>
						AND u.cert_status = #{query.certStatus}
					</otherwise>
				</choose>
			</if>
			<if test="query.rljCertStatus != null and query.rljCertStatus != ''">
				<choose>
					<when test="query.rljCertStatus == 4">
						AND (u.rlj_cert_status = #{query.rljCertStatus} OR u.rlj_cert_status IS NULL)
					</when>
					<otherwise>
						AND u.rlj_cert_status = #{query.rljCertStatus}
					</otherwise>
				</choose>
			</if>
			<if test="query.subType != null and query.subType != ''">
				AND s.subject_type = #{query.subType}
			</if>
			<if test="query.role != null and query.role.size()>0">
				AND r.role_id in (
				<foreach collection="query.role" item="pid" index="index" separator=",">
					#{pid}
				</foreach>
				)
			</if>

			<if test="query.platId != null and query.platId != ''">
				AND u.plat_id =#{query.platId}
			</if>
			<if test="query.lockFlag != null and query.lockFlag != ''">
				AND u.lock_flag =#{query.lockFlag}
			</if>
			AND <![CDATA[ u.username<>'admin']]>
		</where>
		ORDER BY u.create_time DESC
	</select>

	<select id="getUserVosPageNoLock" resultMap="baseResultMap">
		SELECT
		distinct
		u.user_id,
		u.username,
		u.password,
		u.id_card_number,
		u.user_type,
		u.salt,
		u.phone,
		u.avatar,
		u.wx_openid,
		u.qq_openid,
		u.dept_id,
		u.create_time  ucreate_time,
		u.update_time  uupdate_time,
		u.del_flag,
		u.lock_flag,
		u.tenant_id,
		d.name  dept_name,
		u.realname,
		u.province,
		u.city,
		u.area,
		u.province_code,
		u.city_code,
		u.address,
		u.temp1,
		u.temp2,
		u.temp3,
		u.temp4,
		u.temp5,
		u.w1,
		u.plat_id,
		u.drug_ent_id,
		u.cert_status,
		u.rlj_cert_status,
		u.cert_remark,
		u.bank_card_number
		FROM
		sys_user  u
		LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
		LEFT JOIN sys_user_role  ur ON ur.user_id = u.user_id
		LEFT JOIN sys_role  r ON r.role_id = ur.role_id
		<where>
			u.del_flag = '0'
			<if test="query.username != null and query.username != ''">
				AND u.username LIKE CONCAT('%',#{query.username},'%')
			</if>
			<if test="query.realname != null and query.realname != ''">
				AND u.realname LIKE CONCAT('%',#{query.realname},'%')
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND u.dept_id = #{query.deptId}
			</if>
			<if test="query.role != null and query.role.size()>0">
				AND r.role_id in (
				<foreach collection="query.role" item="pid" index="index" separator=",">
					#{pid}
				</foreach>
				)
			</if>

			<if test="query.platId != null and query.platId != ''">
				AND u.plat_id =#{query.platId}
			</if>
			AND <![CDATA[ u.username<>'admin']]>
		</where>
		ORDER BY u.create_time DESC
	</select>


	<select id="getUserVoByPlatId" resultMap="baseResultMap">
		SELECT
		distinct
		u.user_id,
		u.username,
		u.password,
		u.id_card_number,
		u.user_type,
		u.user_sign,
		u.salt,
		u.phone,
		u.avatar,
		u.wx_openid,
		u.qq_openid,
		u.dept_id,
		u.create_time  ucreate_time,
		u.update_time  uupdate_time,
		u.del_flag,
		u.lock_flag,
		u.tenant_id,
		d.name  dept_name,
		u.realname,
		u.province,
		u.city,
		u.province_code,
		u.city_code,
		u.area,
		u.address,
		u.temp1,
		u.temp2,
		u.temp3,
		u.temp4,
		u.temp5,
		u.w1,
		u.plat_id,
		u.drug_ent_id,
		u.cert_status,
		u.rlj_cert_status,
		u.cert_remark,
		u.bank_card_number,
	  	u.bank_phone,
		u.tax_helper_agreement_signature
		FROM
		sys_user  u
		LEFT JOIN sys_dept  d ON d.dept_id = u.dept_id
		LEFT JOIN sys_user_role  ur ON ur.user_id = u.user_id
		LEFT JOIN sys_role  r ON r.role_id = ur.role_id
		<where>
			u.del_flag = '0'
			and u.lock_flag=0
			<if test="query.username != null and query.username != ''">
				AND u.username LIKE CONCAT('%',#{query.username},'%')
			</if>
			<if test="query.realname != null and query.realname != ''">
				AND u.realname LIKE CONCAT('%',#{query.realname},'%')
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND u.dept_id = #{query.deptId}
			</if>
			<if test="query.role != null and query.role.size()>0">
				AND r.role_id in (
				<foreach collection="query.role" item="pid" index="index" separator=",">
					#{pid}
				</foreach>
				)
			</if>

			<if test="query.platId != null and query.platId != ''">
				AND u.plat_id =#{query.platId}
			</if>

			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND u.drug_ent_id =#{query.drugEntId}
			</if>

		</where>
		ORDER BY u.create_time DESC
	</select>

	<!-- 根据部门id和角色id查询 -->
	<select id="selectByDeptIdAndRoleId" resultMap="userResultMap">
		SELECT
			u.user_id,
			u.username,
			u.phone,
			u.avatar,
			u.dept_id,
			u.create_time,
			u.update_time,
			r.role_id,
			u.realname
		FROM
			sys_user u
				LEFT JOIN sys_user_role r ON r.user_id = u.user_id
		WHERE
			u.dept_id = #{deptId}
		  <if test="realNameOrPhone != null and realNameOrPhone != ''">
		  	AND (u.username LIKE CONCAT('%',#{realNameOrPhone},'%') OR u.realname LIKE CONCAT('%',#{realNameOrPhone},'%'))
		  </if>
		  AND r.role_id IN (
			<foreach collection="roleList" item="roleId" index="index" separator=",">
				#{roleId}
			</foreach>
			)
	</select>

	<update id="updateByPrimaryKeySelective" parameterType="com.qunzhixinxi.hnqz.admin.api.entity.SysUser" >
		update sys_user
		<set >
			<if test="delFlag != null" >
				del_flag = #{delFlag,jdbcType=VARCHAR},
			</if>
		</set>
		where user_id = #{userId,jdbcType=BIGINT}
	</update>


	<select id="listUserDetails" resultMap="baseResultMap">
		SELECT u.user_id,
			   u.realname,
			   u.username,
			   u.id_card_number,
			   u.tenant_id,
			   u.dept_id,
			   u.bank_phone,
		       us.bank_card_number,
			   us.gig_type,
			   us.cert_status
		FROM sys_user u
				 LEFT JOIN sys_user_sub us on us.user_id = u.user_id
		<where>
			u.user_id in
			<foreach collection="userIds" index="idx" item="userId" open="(" close=")" separator=",">
				#{userId}
			</foreach>
		</where>

	</select>
</mapper>
