package com.dxhy.admin.sys.service.impl;

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

import javax.annotation.Resource;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.admin.sys.dao.SysRoleMenuDao;
import com.dxhy.admin.sys.entity.SysRoleMenuEntity;
import com.dxhy.admin.sys.service.SysRoleMenuService;
import com.dxhy.admin.sys.service.SysTabulateDeptService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.rabbitmq.RabbitLogsSender;
import com.dxhy.common.utils.Aops;

import lombok.extern.slf4j.Slf4j;

/**
 * 角色与菜单对应关系
 *
 * @author jiaohongyang
 */
@Slf4j
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity>
    implements SysRoleMenuService {
    @Resource
    private SysTabulateDeptService sysTabulateDeptService;
    @Resource
    private RabbitLogsSender rabbitLogsSender;

    @Override
    public void saveOrUpdate(Long roleId, List<Long> menuIdList, String company) {
        if (menuIdList.size() == 0) {
            return;
        }

        // 查询所有菜单类型数据
        List<Long> menuList = baseMapper.getMenuList(menuIdList);

        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        // 先删除角色与菜单关系
        Aops.getSelf(this).delete(roleId);
        Aops.getSelf(this).insertRoleMenu(roleId, menuIdList);
        // 保存菜单需要的显示列
        if (menuList.size() > 0) {
            // 消息队列推送保存菜单数据
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("menuId", menuList);
                jsonObject.put("company", company);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            rabbitLogsSender.saveMenuTabulate(jsonObject.toString());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertRoleMenu(Long roleId, List<Long> menuIdList) {
        List<SysRoleMenuEntity> menuList = new ArrayList<>();
        // 保存角色与菜单关系
        for (Long menuId : menuIdList) {
            SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
            sysRoleMenuEntity.setMenuId(menuId);
            sysRoleMenuEntity.setRoleId(roleId);
            menuList.add(sysRoleMenuEntity);
        }
        if (menuList.size() > 0) {
            this.saveOrUpdateBatch(menuList);
        }
    }

    @Override
    public List<Long> queryMenuIdList(Long roleId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.queryMenuIdList(roleId);
    }

    @Override
    public int deleteBatch(Long roleIds) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        return Aops.getSelf(this).delete(roleIds);
    }

    @Transactional(rollbackFor = Exception.class)
    public int delete(Long roleId) {
        return baseMapper.delete(new QueryWrapper<SysRoleMenuEntity>().eq("role_id", roleId));
    }
}
