package com.itbaizhan.trave.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.trave.bean.RoleWithStatus;
import com.itbaizhan.trave.mapper.AdminMapper;
import com.itbaizhan.trave.mapper.RoleMapper;
import com.itbaizhan.trave.pojo.Admin;
import com.itbaizhan.trave.pojo.Permission;
import com.itbaizhan.trave.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);
    }

    //查询管理员
    public Admin findById(Integer aid){
        return adminMapper.selectById(aid);
    }

    //修改管理员
    public void update(Admin admin){
        String ordPassword = adminMapper.selectById(admin.getAid()).getPassword();
        String newPassword = encoder.encode(admin.getPassword());
        //如果新密码不等于旧密码，对新密码进行加密
        if (ordPassword != newPassword){
            admin.setPassword(encoder.encode(admin.getPassword()));
        }
        adminMapper.updateById(admin);
    }

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

    //查询用户角色情况
    public List<RoleWithStatus> findRole(Integer aid){
        //查询所有角色
        List<Role> roles = roleMapper.selectList(null);
        //查询用户拥有的所有角色ID
        List<Integer> rids = roleMapper.findRoleByAdmin(aid);
        //创建带有状态的角色集合
        List<RoleWithStatus> roleList = new ArrayList();
        //遍历所有角色
        for (Role role : roles) {
            //创建带有状态的角色
            RoleWithStatus roleWithStatus = new RoleWithStatus();
            //将角色的属性复制给带有状态的角色
            BeanUtils.copyProperties(role,roleWithStatus);
            //用户拥有该角色角色状态设置为true
            if (rids.contains(role.getRid())){
                roleWithStatus.setAdminHas(true);
            }else {//用户不拥有该角色角色状态设置为false
                roleWithStatus.setAdminHas(false);
            }
            roleList.add(roleWithStatus);
        }
        return roleList;
    }

    //修改用户角色
    public void updateRole(Integer aid,Integer[] ids){
        //删除用户所有角色
        adminMapper.deleteAdminAllRole(aid);
        //重新给用户添加角色
        for (Integer rid : ids) {
            adminMapper.addAdminRole(aid,rid);
        }
    }

    //修改用户状态
    public void updateStatus(Integer aid){
        Admin admin = adminMapper.selectById(aid);
        admin.setStatus(!admin.isStatus());//状态取反
        adminMapper.updateById(admin);
    }

    //根据名字查询管理员
    public Admin findByName(String username){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }

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


}
