package com.wsk.travelf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsk.travelf.bean.RoleWithStatus;
import com.wsk.travelf.mapper.AdminMapper;
import com.wsk.travelf.mapper.RoleMapper;
import com.wsk.travelf.pojo.Admin;
import com.wsk.travelf.pojo.Permission;
import com.wsk.travelf.pojo.Role;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private BCryptPasswordEncoder encoder;


    //分页查询管理员
    public Page<Admin> findPage(int page,int size) {
        Page selectPage = adminMapper.selectPage(new Page(page,size),null);

        return selectPage;
    }

    //新增管理员
    public void add(Admin admin) {
        admin.setPassword(encoder.encode(admin.getPassword()));
        adminMapper.insert(admin);
    }

    //根据id查询用户
    public Admin findById(Integer aid) {
        return adminMapper.selectById(aid);
    }

    //根据id修改用户
    public void updateById(Admin admin) {
        //获取旧密码
        String oldPassword = adminMapper.selectById(admin.getAid()).getPassword();

        //判断旧密码与新密码是否相等，如不相等，则对密码进行加密
        if (!oldPassword.equals(admin.getPassword())) {
            admin.setPassword(encoder.encode(admin.getPassword()));
        }

        adminMapper.updateById(admin);
    }

    //查询用户详情
    public Admin findDesc(Integer aid) {
        return adminMapper.findDesc(aid);
    }

    //查询用户的角色情况
    public List<RoleWithStatus> findRoleWithStatus(Integer aid) {

        //查询所有角色
        List<Role> roles = roleMapper.selectList(null);
        //查询用户拥有的角色
        List<Integer> roleIds = roleMapper.findRoleIdByAdmin(aid);
        //构建带有状态的角色集合，用户拥有的状态为true，否则为false
        List<RoleWithStatus> roleWithStatusList = new ArrayList<>();
        for (Role role : roles) {
            //创建带有状态的角色
            RoleWithStatus roleWithStatus = new RoleWithStatus();
            //将对象role中的属性复制给对象roleWithStatus
            BeanUtils.copyProperties(role,roleWithStatus);

            //判断用户是否拥有该角色，如有，状态设置为true;如没有，则设置为false
            if (roleIds.contains(role.getRid())) {
                roleWithStatus.setAdminHas(true);
            } else {
                roleWithStatus.setAdminHas(false);
            }

            //将带有状态的角色对象添加到带有状态的角色对象集合中
            roleWithStatusList.add(roleWithStatus);
        }

        //返回带有状态的角色集合
        return roleWithStatusList;
    }

    //修改用户角色
    public void updateAdminRole(Integer aid,Integer rids[]) {
        //删除用户角色
        adminMapper.deleteAdminAllRoles(aid);

        //添加用户角色
        for (Integer rid : rids) {
            adminMapper.addAdminRole(aid,rid);
        }
    }

    //修改用户状态
    public void updateStatus(Integer aid) {
        //根据id查询用户
        Admin admin = adminMapper.selectById(aid);

        //状态取反
        admin.setStatus(!admin.isStatus());

        //根据id修改用户
        adminMapper.updateById(admin);
    }

    //根据用户名查询用户
    public Admin findByAdminName(String username) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);

        return adminMapper.selectOne(wrapper);
    }

    //根据用户名查询权限
    public List<Permission> findAllPermission(String username) {
        return adminMapper.findAllPermission(username);
    }
}
