<?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.lightinit.hsdataplatform.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdataplatform.entity.User">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="user_type" jdbcType="VARCHAR" property="userType" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="salt" jdbcType="VARCHAR" property="salt" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="auth_state" jdbcType="VARCHAR" property="authState" />
    <result column="auth_by" jdbcType="BIGINT" property="authBy" />
    <result column="auth_at" jdbcType="TIMESTAMP" property="authAt" />
    <result column="auth_result" jdbcType="VARCHAR" property="authResult" />
    <result column="auth_opinion" jdbcType="VARCHAR" property="authOpinion" />
    <result column="is_auth" jdbcType="TINYINT" property="isAuth" />
    <result column="is_lcoked_out" jdbcType="TINYINT" property="isLcokedOut" />
    <result column="is_buildin" jdbcType="TINYINT" property="isBuildin" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="created_by" jdbcType="BIGINT" property="createdBy" />
    <result column="deleted_at" jdbcType="TIMESTAMP" property="deletedAt" />
    <result column="deleted_by" jdbcType="BIGINT" property="deletedBy" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, user_type, username, password, salt, phone, email, auth_state, auth_by, auth_at, 
    auth_result, auth_opinion, is_auth, is_lcoked_out, is_buildin, created_at, created_by, 
    deleted_at, deleted_by
  </sql>
  <select id="selectByExample" parameterType="com.lightinit.hsdataplatform.entity.UserExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    LIMIT #{page.beginIndex},#{page.pageSize}
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from user
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdataplatform.entity.UserExample">
    delete from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdataplatform.entity.User">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into user (user_type, username, password, 
      salt, phone, email, 
      auth_state, auth_by, auth_at, 
      auth_result, auth_opinion, is_auth, 
      is_lcoked_out, is_buildin, created_at, 
      created_by, deleted_at, deleted_by
      )
    values (#{userType,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{salt,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, 
      #{authState,jdbcType=VARCHAR}, #{authBy,jdbcType=BIGINT}, #{authAt,jdbcType=TIMESTAMP}, 
      #{authResult,jdbcType=VARCHAR}, #{authOpinion,jdbcType=VARCHAR}, #{isAuth,jdbcType=TINYINT}, 
      #{isLcokedOut,jdbcType=TINYINT}, #{isBuildin,jdbcType=TINYINT}, #{createdAt,jdbcType=TIMESTAMP}, 
      #{createdBy,jdbcType=BIGINT}, #{deletedAt,jdbcType=TIMESTAMP}, #{deletedBy,jdbcType=BIGINT}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.lightinit.hsdataplatform.entity.User">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userType != null">
        user_type,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="salt != null">
        salt,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="authState != null">
        auth_state,
      </if>
      <if test="authBy != null">
        auth_by,
      </if>
      <if test="authAt != null">
        auth_at,
      </if>
      <if test="authResult != null">
        auth_result,
      </if>
      <if test="authOpinion != null">
        auth_opinion,
      </if>
      <if test="isAuth != null">
        is_auth,
      </if>
      <if test="isLcokedOut != null">
        is_lcoked_out,
      </if>
      <if test="isBuildin != null">
        is_buildin,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
      <if test="createdBy != null">
        created_by,
      </if>
      <if test="deletedAt != null">
        deleted_at,
      </if>
      <if test="deletedBy != null">
        deleted_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="userType != null">
        #{userType,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="salt != null">
        #{salt,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="authState != null">
        #{authState,jdbcType=VARCHAR},
      </if>
      <if test="authBy != null">
        #{authBy,jdbcType=BIGINT},
      </if>
      <if test="authAt != null">
        #{authAt,jdbcType=TIMESTAMP},
      </if>
      <if test="authResult != null">
        #{authResult,jdbcType=VARCHAR},
      </if>
      <if test="authOpinion != null">
        #{authOpinion,jdbcType=VARCHAR},
      </if>
      <if test="isAuth != null">
        #{isAuth,jdbcType=TINYINT},
      </if>
      <if test="isLcokedOut != null">
        #{isLcokedOut,jdbcType=TINYINT},
      </if>
      <if test="isBuildin != null">
        #{isBuildin,jdbcType=TINYINT},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        #{deletedBy,jdbcType=BIGINT},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.lightinit.hsdataplatform.entity.UserExample" resultType="java.lang.Long">
    select count(*) from user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update user
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.userType != null">
        user_type = #{record.userType,jdbcType=VARCHAR},
      </if>
      <if test="record.username != null">
        username = #{record.username,jdbcType=VARCHAR},
      </if>
      <if test="record.password != null">
        password = #{record.password,jdbcType=VARCHAR},
      </if>
      <if test="record.salt != null">
        salt = #{record.salt,jdbcType=VARCHAR},
      </if>
      <if test="record.phone != null">
        phone = #{record.phone,jdbcType=VARCHAR},
      </if>
      <if test="record.email != null">
        email = #{record.email,jdbcType=VARCHAR},
      </if>
      <if test="record.authState != null">
        auth_state = #{record.authState,jdbcType=VARCHAR},
      </if>
      <if test="record.authBy != null">
        auth_by = #{record.authBy,jdbcType=BIGINT},
      </if>
      <if test="record.authAt != null">
        auth_at = #{record.authAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.authResult != null">
        auth_result = #{record.authResult,jdbcType=VARCHAR},
      </if>
      <if test="record.authOpinion != null">
        auth_opinion = #{record.authOpinion,jdbcType=VARCHAR},
      </if>
      <if test="record.isAuth != null">
        is_auth = #{record.isAuth,jdbcType=TINYINT},
      </if>
      <if test="record.isLcokedOut != null">
        is_lcoked_out = #{record.isLcokedOut,jdbcType=TINYINT},
      </if>
      <if test="record.isBuildin != null">
        is_buildin = #{record.isBuildin,jdbcType=TINYINT},
      </if>
      <if test="record.createdAt != null">
        created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.createdBy != null">
        created_by = #{record.createdBy,jdbcType=BIGINT},
      </if>
      <if test="record.deletedAt != null">
        deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.deletedBy != null">
        deleted_by = #{record.deletedBy,jdbcType=BIGINT},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update user
    set id = #{record.id,jdbcType=BIGINT},
      user_type = #{record.userType,jdbcType=VARCHAR},
      username = #{record.username,jdbcType=VARCHAR},
      password = #{record.password,jdbcType=VARCHAR},
      salt = #{record.salt,jdbcType=VARCHAR},
      phone = #{record.phone,jdbcType=VARCHAR},
      email = #{record.email,jdbcType=VARCHAR},
      auth_state = #{record.authState,jdbcType=VARCHAR},
      auth_by = #{record.authBy,jdbcType=BIGINT},
      auth_at = #{record.authAt,jdbcType=TIMESTAMP},
      auth_result = #{record.authResult,jdbcType=VARCHAR},
      auth_opinion = #{record.authOpinion,jdbcType=VARCHAR},
      is_auth = #{record.isAuth,jdbcType=TINYINT},
      is_lcoked_out = #{record.isLcokedOut,jdbcType=TINYINT},
      is_buildin = #{record.isBuildin,jdbcType=TINYINT},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      created_by = #{record.createdBy,jdbcType=BIGINT},
      deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{record.deletedBy,jdbcType=BIGINT}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.lightinit.hsdataplatform.entity.User">
    update user
    <set>
      <if test="userType != null">
        user_type = #{userType,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="salt != null">
        salt = #{salt,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="authState != null">
        auth_state = #{authState,jdbcType=VARCHAR},
      </if>
      <if test="authBy != null">
        auth_by = #{authBy,jdbcType=BIGINT},
      </if>
      <if test="authAt != null">
        auth_at = #{authAt,jdbcType=TIMESTAMP},
      </if>
      <if test="authResult != null">
        auth_result = #{authResult,jdbcType=VARCHAR},
      </if>
      <if test="authOpinion != null">
        auth_opinion = #{authOpinion,jdbcType=VARCHAR},
      </if>
      <if test="isAuth != null">
        is_auth = #{isAuth,jdbcType=TINYINT},
      </if>
      <if test="isLcokedOut != null">
        is_lcoked_out = #{isLcokedOut,jdbcType=TINYINT},
      </if>
      <if test="isBuildin != null">
        is_buildin = #{isBuildin,jdbcType=TINYINT},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        created_by = #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        deleted_by = #{deletedBy,jdbcType=BIGINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lightinit.hsdataplatform.entity.User">
    update user
    set user_type = #{userType,jdbcType=VARCHAR},
      username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      salt = #{salt,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      auth_state = #{authState,jdbcType=VARCHAR},
      auth_by = #{authBy,jdbcType=BIGINT},
      auth_at = #{authAt,jdbcType=TIMESTAMP},
      auth_result = #{authResult,jdbcType=VARCHAR},
      auth_opinion = #{authOpinion,jdbcType=VARCHAR},
      is_auth = #{isAuth,jdbcType=TINYINT},
      is_lcoked_out = #{isLcokedOut,jdbcType=TINYINT},
      is_buildin = #{isBuildin,jdbcType=TINYINT},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      created_by = #{createdBy,jdbcType=BIGINT},
      deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{deletedBy,jdbcType=BIGINT}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="queryList" parameterType="com.lightinit.hsdataplatform.model.admin.UserCountSearchModel" resultType="com.lightinit.hsdataplatform.model.admin.UserCountListModel">
 SELECT userName,
        MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN 'APIauth' THEN IFNULL(c,0) ELSE 0 END ) 'applyCountApi',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN '数据auth' THEN IFNULL(c,0) ELSE 0 END ) 'applyCountdata',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN '微应用auth' THEN IFNULL(c,0) ELSE 0 END ) 'applyCountMicroapp',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN 'APIfollow' THEN IFNULL(c,0) ELSE 0 END ) 'followApi',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN '数据follow' THEN IFNULL(c,0) ELSE 0 END ) 'followData',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN '微应用follow' THEN IFNULL(c,0) ELSE 0 END ) 'followMicroapp'
FROM (

SELECT u.`username`,r.`resource_type`,COUNT(*) c ,'auth' TYPE
 FROM  USER u ,resource_auth ra ,
 publish_info i ,
 publish_setting s ,
 resource r
 WHERE  ra.`publish_setting_id` = s.id  AND
  i.`id` = s.`publish_info_id` AND
  r.`id` = i.`resource_id`
 AND u.`id` = ra.`applier`
 AND ra.`platform_type` = "开放平台"
 GROUP BY u.`username`,r.`resource_type`

UNION
SELECT u.`username`,r.`resource_type`,COUNT(*)  c ,'follow' TYPE
FROM  USER u ,
resource_follow f ,
publish_info i ,
publish_setting s ,
resource r
WHERE  f.`publish_setting_id` = s.id
AND i.`id` = s.`publish_info_id`
AND r.`id` = i.`resource_id`
AND u.`id` = f.`follower_id`AND f.`platform_type` = "开放平台"
GROUP BY u.`username`,r.`resource_type`
)  td
GROUP BY username
    <if test="page != null" >
      LIMIT #{page.beginIndex},#{page.pageSize}
    </if>
  </select>
  <select id="queryCount" parameterType="com.lightinit.hsdataplatform.model.admin.UserCountSearchModel" resultType="java.lang.Long">
 SELECT COUNT(*) FROM(
 SELECT userName,
        MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN 'APIauth' THEN IFNULL(c,0) ELSE 0 END ) 'applyCountApi',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN '数据auth' THEN IFNULL(c,0) ELSE 0 END ) 'applyCountdata',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN '微应用auth' THEN IFNULL(c,0) ELSE 0 END ) 'applyCountMicroapp',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN 'APIfollow' THEN IFNULL(c,0) ELSE 0 END ) 'followApi',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN '数据follow' THEN IFNULL(c,0) ELSE 0 END ) 'followData',
    MAX(CASE CONCAT(RESOURCE_TYPE,TYPE) WHEN '微应用follow' THEN IFNULL(c,0) ELSE 0 END ) 'followMicroapp'
FROM (

SELECT u.`username`,r.`resource_type`,COUNT(*) c ,'auth' TYPE
 FROM  USER u ,resource_auth ra ,
 publish_info i ,
 publish_setting s ,
 resource r
 WHERE  ra.`publish_setting_id` = s.id  AND
  i.`id` = s.`publish_info_id` AND
  r.`id` = i.`resource_id`
 AND u.`id` = ra.`applier`
 AND ra.`platform_type` = "开放平台"
 GROUP BY u.`username`,r.`resource_type`

UNION
SELECT u.`username`,r.`resource_type`,COUNT(*)  c ,'follow' TYPE
FROM  USER u ,
resource_follow f ,
publish_info i ,
publish_setting s ,
resource r
WHERE  f.`publish_setting_id` = s.id
AND i.`id` = s.`publish_info_id`
AND r.`id` = i.`resource_id`
AND u.`id` = f.`follower_id`AND f.`platform_type` = "开放平台"
GROUP BY u.`username`,r.`resource_type`
)  td
GROUP BY username
) AS D
  </select>
  <select id="selectByuserId" parameterType="java.lang.Long" resultType="com.lightinit.hsdataplatform.model.admin.UserModel">
    SELECT u.`username`,up.`full_name`,u.`phone`,u.`user_type` FROM USER u
    left JOIN `user_personal` up ON u.id = up.`id`
    where u.id = #{id}
  </select>


  <!--s-->
  <select id="registerUserStatistics" resultType="com.lightinit.hsdataplatform.model.admin.RegisterUserStatisticsModel">
    SELECT YEAR(u.created_at) AS registeYear,COUNT(*) AS registeSum
    FROM `user` u
    GROUP BY YEAR(u.created_at)
    WITH ROLLUP
  </select>

  <!--s-->
  <select id="registerUserGraph" resultType="com.lightinit.hsdataplatform.model.admin.RegisterUserStatisticsModel">
    SELECT YEAR(u.created_at) AS registeYear,MONTH(u.created_at) AS registeMonth,COUNT(*) AS registeSum
  FROM  `user` u
  WHERE YEAR(u.created_at)=#{year}
  GROUP BY YEAR(u.created_at),MONTH(u.created_at)
  WITH ROLLUP
  </select>
</mapper>