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

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.admin.sys.dao.SysRulesDeptDao;
import com.dxhy.admin.sys.entity.SysRulesDeptEntity;
import com.dxhy.admin.sys.service.SysRulesDeptService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Aops;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@Service("sysRulesDeptService")
@Slf4j
public class SysRulesDeptServiceImpl extends ServiceImpl<SysRulesDeptDao, SysRulesDeptEntity>
    implements SysRulesDeptService {
    @Override
    public List<SysRulesDeptEntity> getRuleDept(Long ruleId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return this.baseMapper.selectList(new QueryWrapper<SysRulesDeptEntity>().eq("rule_id", ruleId));

    }

    @Override
    public void deleteBatchRules(Long ruleId, Long deptId) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).deleteRules(ruleId, deptId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteRules(Long ruleId, Long deptId) {
        baseMapper.delete(new UpdateWrapper<SysRulesDeptEntity>().eq("rule_id", ruleId).eq("dept_id", deptId));
    }

    @Override
    public void saveRulesDept(List<SysRulesDeptEntity> list) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).insertRulesDept(list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertRulesDept(List<SysRulesDeptEntity> list) {
        this.saveOrUpdateBatch(list);
    }
}
