package com.ytpm.middle.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ytpm.constant.StrConstant;
import com.ytpm.general.RepMessage;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.middle.dao.MiddleUserMapper;
import com.ytpm.middle.dao.PermissionMapper;
import com.ytpm.middle.dao.RoleMapper;
import com.ytpm.middle.model.YtMiddleRole;
import com.ytpm.middle.model.YtMiddleUser;
import com.ytpm.middle.param.MiddleRoleParam;
import com.ytpm.middle.service.RoleService;
import com.ytpm.middle.util.RedisUtil;
import com.ytpm.middle.view.MiddleRoleVO;
import com.ytpm.middle.view.MiddleUserInfo;
import com.ytpm.util.RedisService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private RedisUtil redisService;
    @Resource
    private MiddleUserMapper userMapper;

    /**
     * 查询角色列表
     */
    @Override
    public ResultTable<MiddleRoleVO> queryList(MiddleRoleParam param) {
        PageHelper.startPage(1, 10);
        return ResultTable.resultTableOk(new PageInfo<MiddleRoleVO>(roleMapper.queryList(param)));
    }

    /**
     * 新增角色
     */
    @Override
    public Result<String> addOne(MiddleRoleParam param) {
        YtMiddleRole old = roleMapper.selectByName(param.getRoleName());
        if(Objects.nonNull(old)){
            return Result.resultErr(RepMessage.OBJECT_ALREADY_EXIST);
        }
        YtMiddleRole middleRole = new YtMiddleRole();
        BeanUtils.copyProperties(param, middleRole);
        roleMapper.insertOne(middleRole);
        return Result.resultOk(RepMessage.SAVE_SUCCESS);
    }

    /**
     * 角色修改
     */
    @Override
    public Result<String> updateOne(MiddleRoleParam param) {
        YtMiddleRole old = roleMapper.selectPrimary(param.getRoleId());
        if(Objects.isNull(old)){
            return Result.resultErr(RepMessage.OBJECT_NOT_EXIST);
        }
        YtMiddleRole middleRole = new YtMiddleRole();
        BeanUtils.copyProperties(param, middleRole);
        roleMapper.updateById(middleRole);
        return Result.resultOk(RepMessage.MODIFY_SUCCESS);
    }

    /**
     * 删除角色ID
     */
    @Override
    public Result<String> delOne(Integer roleId, String userId) {
        YtMiddleRole old = roleMapper.selectPrimary(roleId);
        if(Objects.isNull(old)){
            return Result.resultOk(RepMessage.DELETE_SUCCESS);
        }
        YtMiddleRole middleRole = new YtMiddleRole();
        middleRole.setRoleId(roleId);
        middleRole.setAvailable(0);
        middleRole.setUpdateParam(userId);
        roleMapper.updateById(middleRole);
        return Result.resultOk(RepMessage.DELETE_SUCCESS);
    }

    /**
     * 角色授权用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> grantUsers(String userIds, Integer roleId, MiddleUserInfo userInfo) {
        YtMiddleRole old = roleMapper.selectPrimary(roleId);
        if(Objects.isNull(old)){
            return Result.resultOk(RepMessage.DELETE_SUCCESS);
        }
        roleMapper.delUserRoleById(roleId);
        roleMapper.grantRoleToUsers(roleId,userInfo.getUserId(),userIds);
        //清除redis用户信息
        redisService.del(StrConstant.ADS_USER_INFO_PRE+userInfo.getLoginName());
        return Result.resultOk(RepMessage.GRANT_SUCCESS);
    }

    /**
     * 角色分配权限
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> savePermission(Integer roleId, String permissionIds,MiddleUserInfo userInfo) {
        YtMiddleRole old = roleMapper.selectPrimary(roleId);
        if(Objects.isNull(old)){
            return Result.resultOk(RepMessage.DELETE_SUCCESS);
        }
        //清除旧权限，分配新权限
        permissionMapper.delByRoleId(roleId);
        permissionMapper.addRolePermissions(roleId,permissionIds,userInfo.getUserId());
        //清除redis用户信息
        redisService.del(StrConstant.ADS_USER_INFO_PRE+userInfo.getLoginName());
        return Result.resultOk(RepMessage.GRANT_SUCCESS);
    }
}
