/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.mapper;

import com.ifish.bean.Tbl_User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

/**
 *
 * @author Administrator
 */
public interface Tbl_User_Mapper {

    /**
     * 根据手机号获取用户对象
     *
     * @return
     */
    @Select("select user_id,nick_name,user_password,phone_number,user_email,user_type,user_img,user_sex,login_count,update_time,login_time,create_time,login_type,remarks,address,P2PVerify_code1,P2PVerify_code2,"
            + "gwell_userID,is_register_gwell,jiguang_userID,token,longitude,latitude from tbl_user where phone_number=#{phoneNumber}")
    Tbl_User getUserByPhoneNumber(@Param("phoneNumber") String phoneNumber);

    /**
     * 根据用户邮箱获取用户对象
     *
     * @param userid
     * @return
     */
    @Select("select user_id,nick_name,user_password,phone_number,user_email,user_type,user_img,user_sex,login_count,update_time,login_time,create_time,login_type,remarks,address,P2PVerify_code1,P2PVerify_code2,"
            + "gwell_userID,is_register_gwell,jiguang_userID,token,longitude,latitude from tbl_user where user_email=#{user_email}")
    Tbl_User getUserByUserEmail(@Param("user_email") String userEmail);

    /**
     * 根据用户token获取用户对象
     *
     * @param userid
     * @return
     */
    @Select("select user_id,nick_name,user_password,phone_number,user_email,user_type,user_img,user_sex,login_count,update_time,login_time,create_time,login_type,remarks,address,P2PVerify_code1,P2PVerify_code2,"
            + "gwell_userID,is_register_gwell,jiguang_userID,token,longitude,latitude from tbl_user where token=#{token}")
    Tbl_User getUserByUserToken(@Param("token") String token);

    /**
     * 根据用户ID获取用户对象
     *
     * @param userid
     * @return
     */
    @Select("select user_id,nick_name,user_password,phone_number,user_email,user_type,user_img,user_sex,login_count,update_time,login_time,create_time,login_type,remarks,address,P2PVerify_code1,P2PVerify_code2,"
            + "gwell_userID,is_register_gwell,jiguang_userID,token,longitude,latitude from tbl_user where user_id=#{userid}")
    Tbl_User getUserByUserId(@Param("userid") String userid);

    /**
     * 每次登陆后修改登陆时间、次数和手机类型
     *
     * @param userId
     * @param loginType
     * @return
     */
    @Update("update tbl_user set login_count=if(login_count is null,1,login_count+1),login_type=#{logintype},login_time=current_timestamp() where user_id=#{userid}")
    Integer executeLoginUpdate(@Param("userid") Integer userId, @Param("logintype") String loginType);

    /**
     * 修改用户账号信息
     *
     * @param user
     * @return
     */
    @UpdateProvider(type = Tbl_User_MapperSql.class, method = "updateUser")
    Integer updateUser(@Param("user") Tbl_User user);

    /**
     * 游客注册用户
     *
     * @param user
     * @return
     */
    @InsertProvider(type = Tbl_User_MapperSql.class, method = "registerUserByTourist")
    @SelectKey(statement = "select @@IDENTITY as user_id", keyProperty = "user.userId", keyColumn = "userId", before = false, resultType = int.class)
    Integer insertUserByTourist(@Param("user") Tbl_User user);

    /**
     * 手机邮箱注册用户
     *
     * @param user
     * @return
     */
    @Insert("INSERT INTO tbl_user (`nick_name`, `user_password`, `phone_number`, `user_email`, `user_type`, `user_img`, `user_sex`, `login_count`, `update_time`, `login_time`, `create_time`, `login_type`, `remarks`, `address`, `P2PVerify_code1`, `P2PVerify_code2`, `gwell_userID`, `is_register_gwell`, `jiguang_userID`,`token`, `longitude`, `latitude`) "
            + "VALUES (#{user.nickName},#{user.userPassword},#{user.phoneNumber},#{user.userEmail},#{user.userType},#{user.userImg},#{user.userSex},#{user.loginCount},#{user.updateTime},#{user.loginTime},"
            + "#{user.createTime},#{user.loginType},#{user.remarks},#{user.address},#{user.p2pverifyCode1},#{user.p2pverifyCode2},#{user.gwellUserid},#{user.isRegisterGwell},#{user.jiguangUserid},"
            + "#{user.token},#{user.longitude},#{userlatitude})")
    @SelectKey(statement = "select @@IDENTITY as user_id", keyProperty = "user.userId", keyColumn = "userId", before = false, resultType = int.class)
    Integer insertUserByPhoneNumberAndEmail(@Param("user") Tbl_User user);
}
