package com.mx.cneeds.server.user.service.impl;

import com.mx.cneeds.common.annotation.DataFilter;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.pager.Query;
import com.mx.cneeds.server.dao.SysRoleDao;
import com.mx.cneeds.server.entity.SysDeptEntity;
import com.mx.cneeds.server.entity.SysRoleEntity;
import com.mx.cneeds.server.user.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {

    @Autowired
    private SysRoleFuncService sysRoleFuncService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;
    @Autowired
    private SysUserRoleService sysUserRoleService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<SysRoleEntity> queryWrapper = new QueryWrapper<>();

        String roleName = (String) params.get("roleName");
        if (roleName != null && !"".equals(roleName) ){
            queryWrapper.like("role_name", roleName);
        }

        IPage<SysRoleEntity> page = this.page(
                new Query<SysRoleEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public List<SysRoleEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRoleEntity role, List<Long> funcIdList, List<Long> deptIdList) {
        role.setCreateTime(new Date());
        this.save(role);

        //保存角色与菜单关系
        sysRoleFuncService.saveOrUpdate(role.getRoleId(), funcIdList);

        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getRoleId(), deptIdList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleEntity role, List<Long> funcIdList, List<Long> deptIdList) {
        this.updateById(role);

        //更新角色与菜单关系
        sysRoleFuncService.saveOrUpdate(role.getRoleId(), funcIdList);

        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getRoleId(), deptIdList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.removeByIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleFuncService.deleteBatch(roleIds);

        //删除角色与部门关联
        sysRoleDeptService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }

}
