package com.liang.service.impl;

//import com.liang.common.config.security.SecurityUtil;
//import com.liang.common.consts.SystemConst;
//import com.liang.common.utils.RedisUtil;

import com.liang.common.base.BaseResult;
import com.liang.entity.SysUserRole;
import com.liang.dao.SysUserRoleDao;
import com.liang.service.SysUserRoleService;
//import org.springframework.beans.factory.annotation.Autowired;
import com.liang.vo.XmSelectVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.*;

/**
 * (SysUserRole)表服务实现类
 *
 * @author makejava
 * @since 2023-02-19 21:37:39
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Resource
    private SysUserRoleDao sysUserRoleDao;

//    @Autowired
//    private RedisUtil redisUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUserRole queryById(Integer userId) {
        return this.sysUserRoleDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param sysUserRole 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysUserRole> queryByPage(SysUserRole sysUserRole, PageRequest pageRequest) {
        long total = this.sysUserRoleDao.count(sysUserRole);
        return new PageImpl<>(this.sysUserRoleDao.queryAllByLimit(sysUserRole, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole insert(SysUserRole sysUserRole) {
        this.sysUserRoleDao.insert(sysUserRole);
        return sysUserRole;
    }

    /**
     * 修改数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole update(SysUserRole sysUserRole) {
        this.sysUserRoleDao.update(sysUserRole);
        return this.queryById(sysUserRole.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.sysUserRoleDao.deleteById(userId) > 0;
    }

    @Override
    public Set<String> listUserRoleByUserId(Integer userId) {
//        String token = SecurityUtil.getUserKey();
//
//        Object object = redisUtil.get(SystemConst.SYSTEM_USER_ROLE + ":" + token);
//        if (null != object) {
//            return (Set<String>) object;
//        }
        Set<String> set = sysUserRoleDao.listUserRoleByUserId(userId);
//        redisUtil.set(SystemConst.SYSTEM_USER_ROLE + ":" + token, set, 1800);

        return set;
    }

    @Override
    public BaseResult doAuthorize(Integer userId, String roles) {
        // 先全部删除用户旧的角色
        int num = this.sysUserRoleDao.deleteUserRoleByUserId(userId);
        // 保存用户新赋予的角色
        if (StringUtils.isNotBlank(roles)) {
            String[] roleArr = roles.split(",");
            List<SysUserRole> list = new ArrayList<>();
            SysUserRole userRole = null;
            for (String item : roleArr) {
                userRole = new SysUserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(Integer.parseInt(item));
                list.add(userRole);
            }
            int numm = this.sysUserRoleDao.batchSaveUserRole(list);
            if (numm > 0) {
                return BaseResult.success("用户角色更新成功！");
            } else {
                return BaseResult.failure("用户角色更新失败！");
            }
        } else {
            return BaseResult.failure("角色信息为空！");
        }
    }

    /**
     * 根据用户id查询角色，给xmselect赋值
     *
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> listRoleByUserId(Integer userId) {
        List<XmSelectVo> list = this.sysUserRoleDao.listRoleByUserId(userId);
        Map<String, Object> selectMap = new HashMap<>();
        selectMap.put("data", list);
        return selectMap;
    }
}
