package com.ho.user.service.impl;


import cn.hutool.core.lang.Snowflake;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.user.entity.SysRolePermission;
import com.ho.user.mapper.SysRolePermissionMapper;
import com.ho.user.service.RolePermissionService;
import com.ho.user.vo.req.RolePermissionOperationReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    Snowflake snowflake;

    @Override
    public int removeByRoleId(Integer roleId) {
        return sysRolePermissionMapper.removeByRoleId(roleId);
    }

    @Override
    public List<Integer> getPermissionIdsByRoles(List<Integer> roleIds) {

        return sysRolePermissionMapper.getPermissionIdsByRoles(roleIds);
    }

    @Override
    public void addRolePermission(RolePermissionOperationReqVO vo) {

        Date createTime = new Date();
        List<SysRolePermission> list = new ArrayList<>();
        for (Integer permissionId:vo.getPermissionIds()){
            SysRolePermission sysRolePermission = new SysRolePermission();
            //sysRolePermission.setId(UUID.randomUUID().toString());
          //  sysRolePermission.setId(snowflake.nextIdStr());
            sysRolePermission.setCreateTime(createTime);
            sysRolePermission.setPermissionId(permissionId);
            sysRolePermission.setRoleId(vo.getRoleId());
            list.add(sysRolePermission);
        }
        sysRolePermissionMapper.removeByRoleId(vo.getRoleId());
        int count = sysRolePermissionMapper.batchRolePermission(list);
        if (count==0){
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
    }

    @Override
    public void addRolePermissions(RolePermissionOperationReqVO vo) {

        Date createTime = new Date();
        List<SysRolePermission> list = new ArrayList<>();
        for (Integer permissionId:vo.getPermissionIds()){
            SysRolePermission sysRolePermission = new SysRolePermission();
            //sysRolePermission.setId(UUID.randomUUID().toString());
            //  sysRolePermission.setId(snowflake.nextIdStr());
            sysRolePermission.setCreateTime(createTime);
            sysRolePermission.setPermissionId(permissionId);
            sysRolePermission.setRoleId(vo.getRoleId());
            list.add(sysRolePermission);
        }
        int count = sysRolePermissionMapper.batchRolePermission(list);
        if (count==0){
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
    }

    @Override
    public int removeByPermissionId(Integer permissionId) {

        return sysRolePermissionMapper.removeByPermissionId(permissionId);
    }

    @Override
    public List<Integer> getRoleIds(Integer permissionId) {

        return sysRolePermissionMapper.getRoleIds(permissionId);
    }

    @Override
    public List<Integer> getPermissionIdsByRoleId(Integer roleId) {

        return sysRolePermissionMapper.getPermissionIdsByRoleId(roleId);
    }

    @Override
    public int deleteRolePermissions(RolePermissionOperationReqVO reqVO) {
        return sysRolePermissionMapper.deleteRolePermissions(reqVO);
    }
}
