package com.chushang.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.core.exception.ResultException;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.security.utils.SecurityUtils;
import com.chushang.system.entity.dto.ListUserDTO;
import com.chushang.system.entity.po.SysUser;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author author
 * @since 2022-08-18
 */
public interface ISysUserService extends IService<SysUser> {

    AjaxResult listUser(ListUserDTO listUser);

    SysUser selectUserByUserName(String username);

    SysUser selectUserById(Long userId);

    void checkUserDataScope(SysUser user);

    void saveUser(SysUser user);

    void update(SysUser user);

    void deleteBatch(Long[] userIds);

    /**
     * 判断是否为超级管理员 账号
     */
    default void checkUserAllowed(SysUser sysUser){
        // 不为空, 不允许修改时, 应该是判断, 当前用户是否为超级管理员
        if (ObjectUtil.isNotNull(sysUser.getUserId()) && SysUser.isAdmin(sysUser.getUserId())){
            throw new ResultException("不允许操作超级管理员用户");
        }
    }

    default boolean updatePassword(Long userId, String oldPassword, String newPassword){
        return this.update(SysUser.builder()
                        .password(newPassword)
                        .updateBy(SecurityUtils.getUsername())
                        .build()
                ,new LambdaQueryWrapper<SysUser>()
                        .eq(SysUser::getUserId, userId)
                        .eq(SysUser::getPassword, oldPassword)
        );
    }

    default void updateUserStatus(SysUser user){
        this.update(SysUser.builder()
                        .status(user.getStatus())
                        .updateBy(user.getUpdateBy())
                .build(), new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUserId, user.getUserId()));
    }

    void insertUserAuth(Long userId, Long[] roleIds);

    AjaxResult selectAllocatedList(ListUserDTO listUser);

    AjaxResult selectUnallocatedList(ListUserDTO listUser);

    default boolean checkUsername(String username){
        return count(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, username)) > 0;
    }

    default boolean updateUserProfile(SysUser user){
        return updateById(user);
    }

    String selectUserRoleGroup(String username);

    String selectUserPostGroup(String username);

    default boolean checkDeptExistUser(Long deptId){
        return count(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getDeptId, deptId)) > 0;
    }

    void resetPwd(Long userId, String newPassword);


}
