<?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.fjs.scenic.mapper.user.UserStatisticalMapper">
  <resultMap id="BaseResultMap" type="com.fjs.scenic.entity.user.UserStatistical">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="user_id" jdbcType="INTEGER" property="userId" />
    <result column="statiscal_permissions_id" jdbcType="INTEGER" property="statiscalPermissionsId" />
    <result column="secret_key" jdbcType="VARCHAR" property="secretKey" />
  </resultMap>
  <sql id="Base_Column_List">
    id, user_id, statiscal_permissions_id, secret_key
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user_statistical
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user_statistical
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.fjs.scenic.entity.user.UserStatistical">
    insert into user_statistical (id, user_id, statiscal_permissions_id,
    secret_key)
    values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{statiscalPermissionsId,jdbcType=INTEGER},
    #{secretKey,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.fjs.scenic.entity.user.UserStatistical">
    insert into user_statistical
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="userId != null">
        user_id,
      </if>
      <if test="statiscalPermissionsId != null">
        statiscal_permissions_id,
      </if>
      <if test="secretKey != null">
        secret_key,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userId != null">
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="statiscalPermissionsId != null">
        #{statiscalPermissionsId,jdbcType=INTEGER},
      </if>
      <if test="secretKey != null">
        #{secretKey,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.fjs.scenic.entity.user.UserStatistical">
    update user_statistical
    <set>
      <if test="userId != null">
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="statiscalPermissionsId != null">
        statiscal_permissions_id = #{statiscalPermissionsId,jdbcType=INTEGER},
      </if>
      <if test="secretKey != null">
        secret_key = #{secretKey,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.fjs.scenic.entity.user.UserStatistical">
    update user_statistical
    set user_id = #{userId,jdbcType=INTEGER},
      statiscal_permissions_id = #{statiscalPermissionsId,jdbcType=INTEGER},
      secret_key = #{secretKey,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <!--自定义-->
  <delete id="deleteByUserId" parameterType="java.lang.Integer">
    delete from user_statistical
    where user_id = #{id,jdbcType=INTEGER}
  </delete>

  <insert id="inserts" parameterType="java.util.List">
    insert into user_statistical (id, user_id, statiscal_permissions_id,
    secret_key)
    values
      <foreach collection="list" item="question" separator=",">
        (#{question.id,jdbcType=INTEGER}, #{question.userId,jdbcType=INTEGER},
        #{question.statiscalPermissionsId,jdbcType=INTEGER},
        #{question.secretKey,jdbcType=VARCHAR})
      </foreach>
  </insert>

  <select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user_statistical
    where user_id = #{id,jdbcType=INTEGER}
  </select>

  <select id="selectForPage" parameterType="com.fjs.scenic.entity.user.UserStatistical"
          resultType="java.util.HashMap">
    SELECT us.user_id,u.head_pic,u.nick_name FROM user_statistical us LEFT JOIN `user` u ON us.user_id = u.id
    GROUP BY us.user_id
  </select>

  <delete id="delByOpenid" parameterType="java.lang.String">
    delete us.* from user_statistical us LEFT JOIN `user` u ON us.user_id = u.id
    where u.openid = #{openid,jdbcType=INTEGER}
  </delete>

  <select id="verifyPermission" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    SELECT sp.* FROM statistical_permissions sp LEFT JOIN user_statistical us ON sp.id = us.statiscal_permissions_id
    WHERE sp.id = #{pressId,jdbcType=INTEGER}
    AND us.user_id = #{userID,jdbcType=INTEGER}
  </select>

  <!--修改密码 （通过userid）-->
  <update id="updateSecretKeyByUserId">
    update user_statistical SET secret_key = #{newKey,jdbcType=VARCHAR}
    where user_id = #{userId,jdbcType=INTEGER}
  </update>


</mapper>