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

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.admin.sys.dao.SysRoleDao;
import com.dxhy.admin.sys.entity.SysDeptEntity;
import com.dxhy.admin.sys.entity.SysRoleEntity;
import com.dxhy.admin.sys.service.*;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Aops;
import com.dxhy.common.utils.Constant;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.Query;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 角色
 *
 * @author jiaohongyang
 */
@Service("sysRoleService")
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysDeptService sysDeptService;
    @Resource
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String roleName = (String)params.get("roleName");
        String deptId = (String)params.get("deptId");
        String company = (String)params.get("company");

        IPage<SysRoleEntity> page = this.page(new Query<SysRoleEntity>().getPage(params),
            new QueryWrapper<SysRoleEntity>().like(StringUtils.isNotBlank(roleName), "role_name", roleName)
                .eq(StringUtils.isNotBlank(deptId), "dept_id", deptId)
                .eq(StringUtils.isNotBlank(company), "company", company)
                .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER)));

        for (SysRoleEntity sysRoleEntity : page.getRecords()) {
            SysDeptEntity sysDeptEntity = sysDeptService.getById(sysRoleEntity.getDeptId());
            if (sysDeptEntity != null) {
                sysRoleEntity.setDeptName(sysDeptEntity.getName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    public void saveRole(SysRoleEntity role) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        role.setCreateTime(DateUtil.date());
        role.setUpdateTime(DateUtil.date());
        Aops.getSelf(this).insertRole(role);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertRole(SysRoleEntity role) {
        role.setCreateTime(new Date());
        baseMapper.insert(role);
        // 保存角色与菜单关系
        if (role.getMenuIdList() != null && role.getMenuIdList().size() > 0) {
            sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList(), role.getCompany());
        }
    }

    @Override
    public void update(SysRoleEntity role) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        role.setUpdateTime(DateUtil.date());
        Aops.getSelf(this).updateRole(role);
        // 刷新角色缓存
        this.renovateRole(role);
    }

    public boolean renovateRole(SysRoleEntity role) {
        return sysUserService.queryUserRole(role.getRoleId());
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateRole(SysRoleEntity role) {
        baseMapper.updateById(role);
        // 更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList(), role.getCompany());
    }

    @Override

    public void deleteBatch(Long roleIds) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).deleteRole(roleIds);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(Long roleIds) {
        // 删除角色
        baseMapper.deleteBatchIds(Collections.singletonList(roleIds));
        // 删除角色与菜单关联
        sysRoleMenuService.deleteBatch(roleIds);

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

    @Override
    public List<SysRoleEntity> selectCompany(String company) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper
            .selectList(new QueryWrapper<SysRoleEntity>().eq(StringUtils.isNotBlank(company), "company", company));
    }

    @Override
    public List<SysRoleEntity> queryRoleList() {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectList(null);
    }

    @Override
    public SysRoleEntity queryRoleById(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectById(id);
    }

}
