<?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.inkasso.factoring.system.user.dao.UserDAO">


	<select id="getUserByOpenId" resultType="com.inkasso.factoring.system.user.model.dto.UserDTO"
			parameterType="java.lang.String">
		select
		   a.*,
		   b.open_id,
		   b.id AS "wxAccountId"
		   from  kfb_sys_user a       left join kfb_sys_user_wxaccount b on a.id = b.user_id
        where open_id = #{openId}
	</select>


	<!-- 查询所有 拥有某个资源的code 的用户集合 -->
  <select id="getByResourceCode" resultType="com.inkasso.factoring.system.user.model.dto.UserDTO"
    parameterType="java.lang.String">
				select
					a.*,
					a1.open_id
				from kfb_sys_user a
							 LEFT JOIN kfb_sys_user_wxaccount a1 on a.id = a1.user_id
							 JOIN kfb_sys_user_role b  on  a.id = b.user_id
							 JOIN kfb_sys_role_resource c on b.role_id = c.role_id
							 JOIN kfb_sys_resource d on c.resource_id = d.id
				where  d.code =  #{resourceCode}
	</select>


	<!-- 小程序管理端 用户数据统计 -->
  <select id="pageUserStatistis"
    resultType="com.inkasso.management.statistics.model.UserStatisticsVO">
		select
			a.phone,
			a.register_time "loginDate",
			b.authDate,
			c.referenceNumber,
			if(a.reference is null,0,1) AS "addUserType"
		from kfb_sys_user a
	 LEFT JOIN (
			select
				min(create_time) AS "authDate",
				user_id
			from  kfb_sys_tenant  group by  user_id) b on a.id = b.user_id
	 LEFT JOIN(
			select
				reference,
				count(*)  AS "referenceNumber"
			from  kfb_sys_user
			where reference is not null
			group by reference ) c on c.reference = a.id


	</select>
</mapper>