package com.xkrs.dao;

import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.model.vo.SysUserVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

/**
 * SysUserDao
 * @author tajochen
 */
public interface SysUserDao extends JpaRepository<SysUserEntity,Integer> {

    /**
     * 检查系统用户名是否存在
     * @param userName
     * @return
     */
    @Query(value = "SELECT COUNT(*) FROM sys_user WHERE user_name = :userName", nativeQuery = true)
    int checkUserName(@Param("userName") String userName);

    /**
     * 查找用户实体根据用户名
     * @param userName
     * @return
     */
    @Query(value = "SELECT * FROM sys_user WHERE user_name = :userName", nativeQuery = true)
    SysUserEntity selectByUserName(@Param("userName") String userName);

    /**
     * 根据用户名查询实体信息
     * @param userName
     * @return
     */
    @Query(value = "select new com.xkrs.model.vo.SysUserVo (id,userName,nickName,telephone,addTime,lastEntryTime,lastEntryIp,avatar,province,city,county,identifier,identity) " +
            "from SysUserEntity where userName = :userName")
    SysUserVo selectUserByUserName(String userName);

    /**
     * 更新用户登录信息
     * @param userName
     * @param ipAddress
     * @return
     */
    @Modifying
    @Query(value = "UPDATE sys_user SET last_entry_time = now(), last_entry_ip = :ipAddress " +
            "WHERE user_name = :userName ;", nativeQuery = true)
    int updateSysUserLogin(@Param("userName") String userName, @Param("ipAddress") String ipAddress);

    /**
     * 软删除系统用户根据用户名
     * @param userName
     * @return
     */
    @Modifying
    @Query(value = "UPDATE sys_user SET delete_flag = 1  " +
            "WHERE user_name = :userName ;", nativeQuery = true)
    int softDeleteSysUserByUserName(@Param("userName") String userName);

    /**
     * 软删除系统用户根据id
     * @param id
     * @return
     */
    @Modifying
    @Query(value = "UPDATE sys_user SET delete_flag = 1  " +
            "WHERE id = :id ;", nativeQuery = true)
    int softDeleteGeneralUserById(@Param("id") Integer id);

    /**
     * 删除系统用户（危险操作！）
     * @param userName
     * @return
     */
    @Modifying
    @Query(value = "DELETE FROM sys_user WHERE user_name = :userName ;", nativeQuery = true)
    int deleteSysUser(@Param("userName") String userName);

    /**
     * 获取所有存在的系统用户Vo
     * @return
     */
    /*@Query(value = "select new com.xkrs.model.vo.SysUserVo (u.id, u.userName, u.nickName, u.userCode, " +
            "u.telephone, u.signature," +
            "u.statusCode,u.addTime,u.lastEntryTime) from SysUserEntity u ,SysRoleEntity r," +
            "RelUserRoleEntity u_r " +
            "WHERE u.deleteFlag = 0 AND u.activeFlag = 0 AND r.id = u_r.roleId AND u.id = u_r.userId")
    List<SysUserVo> selectAllSysUser();*/

    /**
     * 通过userId修改手机号（绑定手机号）
     * @param userId
     * @param phone
     */
    @Modifying(clearAutomatically=true)
    @Query(value = "update sys_user set telephone = ?2 where id = ?1",nativeQuery = true)
    void updatePhoneByUserId(Integer userId, String phone);

    /**
     * 根据手机号查询用户信息
     * @param telephone
     * @return
     */
    SysUserEntity findByTelephone(String telephone);

    /**
     * 用户完善信息
     * @param userId
     * @param nickName
     * @param avatar
     * @param
     * @param
     * @param
     */
    @Modifying(clearAutomatically=true)
    @Query(value = "update sys_user set nick_name = ?2,avatar = ?3 where id = ?1",nativeQuery = true)
    void updateUserByUserId(Integer userId,String nickName,String avatar);

    /**
     * 通过用户昵称查询用户信息
     * @param nickName
     * @return
     */
    SysUserEntity findByNickName(String nickName);

    /**
     * 启用
     * @param userId
     */
    @Query(value = "update sys_user set active_flag = 0 where id = ?",nativeQuery = true)
    @Modifying(clearAutomatically=true)
    void updateEnable(Integer userId);

    /**
     * 禁用
     * @param userId
     */
    @Query(value = "update sys_user set active_flag = 1 where id = ?",nativeQuery = true)
    @Modifying(clearAutomatically=true)
    void updateDisable(Integer userId);

    /**
     * 管理员查看所有的用户信息
     * @return
     */
    @Query(value = "select nick_name nickname,avatar,telephone,province,city,county," +
            "identifier,identity,active_flag activeflag,last_entry_ip lastentryip," +
            "last_entry_time lastentrytime,add_time addtime from sys_user",nativeQuery = true)
    List<Map<String,Object>> selectAllUser();

    /**
     * 用户修改账号密码
     * @param userId
     * @param newPassword
     * @param userName
     */
    @Query(value = "update sys_user set password = ?2,user_name = ?3 where id = ?1",nativeQuery = true)
    @Modifying(clearAutomatically=true)
    void updatePassword(Integer userId,String newPassword,String userName);

    /**
     * 用户忘记密码
     * @param phone
     * @param password
     */
    @Query(value = "update sys_user set password = ?2 where telephone = ?1",nativeQuery = true)
    @Modifying(clearAutomatically=true)
    void updateRememberPassword(String phone,String password);

}
