package com.dxhy.jxpt.baseService.config.service.impl;

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.api.model.config.sys.SysRulesDeptEntity;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Aops;
import com.dxhy.jxpt.baseService.config.dao.SysRulesDeptDao;
import com.dxhy.jxpt.baseService.config.service.SysRulesDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @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, String company) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).deleteRules(ruleId, company);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteRules(Long ruleId, String deptId) {
        baseMapper.delete(new UpdateWrapper<SysRulesDeptEntity>().eq("rule_id", ruleId).eq("company", 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);
    }
}
