350 lines
12 KiB
Java
350 lines
12 KiB
Java
package com.nis.web.service.configuration;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
import org.apache.ibatis.session.ExecutorType;
|
||
import org.apache.ibatis.session.SqlSession;
|
||
import org.apache.ibatis.session.SqlSessionFactory;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import com.nis.domain.Page;
|
||
import com.nis.domain.configuration.BaseCfg;
|
||
import com.nis.domain.configuration.BaseIpCfg;
|
||
import com.nis.domain.configuration.BaseStringCfg;
|
||
import com.nis.domain.configuration.ComplexkeywordCfg;
|
||
import com.nis.domain.configuration.MultipleCfg;
|
||
import com.nis.domain.configuration.MultipleSearchCfg;
|
||
import com.nis.domain.configuration.NumBoundaryCfg;
|
||
import com.nis.util.Constants;
|
||
import com.nis.web.dao.CrudDao;
|
||
import com.nis.web.dao.configuration.ComplexStringCfgDao;
|
||
import com.nis.web.dao.configuration.IpCfgDao;
|
||
import com.nis.web.dao.configuration.MultipleCfgDao;
|
||
import com.nis.web.dao.configuration.NumCfgDao;
|
||
import com.nis.web.dao.configuration.StringCfgDao;
|
||
import com.nis.web.service.CrudService;
|
||
import com.nis.web.service.SpringContextHolder;
|
||
|
||
/**
|
||
* 多域相关配置事务类
|
||
* @author dell
|
||
*
|
||
*/
|
||
@Service
|
||
public class MultipleCfgService extends CrudService<MultipleCfgDao,MultipleSearchCfg> {
|
||
@Autowired
|
||
protected MultipleCfgDao multipleCfgDao;
|
||
@Autowired
|
||
protected IpCfgDao ipCfgDao;
|
||
@Autowired
|
||
protected StringCfgDao stringCfgDao;
|
||
@Autowired
|
||
protected ComplexStringCfgDao complexStringCfgDao;
|
||
@Autowired
|
||
protected NumCfgDao numCfgDao;
|
||
/**
|
||
*
|
||
* addStringCfg(新增IP类配置)
|
||
* (继承ComplexkeywordCfg这个类方可使用)
|
||
* @param cfg
|
||
* @return
|
||
*int
|
||
* @exception
|
||
* @since 1.0.0
|
||
*/
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void addCfg(MultipleCfg cfg){
|
||
if(cfg!=null) {
|
||
if(cfg.getIpCfg()!=null){
|
||
for(BaseIpCfg _cfg:cfg.getIpCfg().values()){
|
||
ipCfgDao.insert(_cfg);
|
||
}
|
||
}
|
||
if(cfg.getStringCfg()!=null){
|
||
for(BaseStringCfg _cfg:cfg.getStringCfg().values()){
|
||
stringCfgDao.insert(_cfg);
|
||
}
|
||
}
|
||
if(cfg.getNumCfg()!=null){
|
||
for(NumBoundaryCfg _cfg:cfg.getNumCfg().values()){
|
||
numCfgDao.insert(_cfg);
|
||
}
|
||
}
|
||
if(cfg.getComplexCfg()!=null){
|
||
for(ComplexkeywordCfg _cfg:cfg.getComplexCfg().values()){
|
||
complexStringCfgDao.insert(_cfg);
|
||
}
|
||
}
|
||
if(cfg.getAreaCfg()!=null&&cfg.getAreaCfg().size()>0){
|
||
List<BaseIpCfg> areaCfg=new ArrayList<>();
|
||
areaCfg.addAll(cfg.getAreaCfg());
|
||
// this.saveIpBatch(areaCfg);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void updateCfg(MultipleCfg cfg,List<BaseCfg> deleteCfg,List<BaseIpCfg> addAreaCfg,List<BaseIpCfg> updateAreaCfg,List<BaseIpCfg> deleteAreaCfgs){
|
||
if(cfg!=null){
|
||
if(cfg.getIpCfg()!=null){
|
||
for(BaseIpCfg _cfg:cfg.getIpCfg().values()){
|
||
if(_cfg.getCfgId()==null){//修改配置时用户可能希望新增IP配置,此时新增的IP配置无配置ID
|
||
ipCfgDao.insert(_cfg);
|
||
}else{
|
||
ipCfgDao.update(_cfg);
|
||
}
|
||
|
||
}
|
||
}
|
||
if(cfg.getStringCfg()!=null){
|
||
for(BaseStringCfg _cfg:cfg.getStringCfg().values()){
|
||
if(_cfg.getCfgId()==null){
|
||
stringCfgDao.insert(_cfg);
|
||
}else
|
||
stringCfgDao.update(_cfg);
|
||
}
|
||
}
|
||
if(cfg.getNumCfg()!=null){
|
||
for(NumBoundaryCfg _cfg:cfg.getNumCfg().values()){
|
||
if(_cfg.getCfgId()==null){
|
||
numCfgDao.insert(_cfg);
|
||
}else
|
||
numCfgDao.updateByPrimaryKeySelective(_cfg);
|
||
}
|
||
}
|
||
if(cfg.getComplexCfg()!=null){
|
||
for(ComplexkeywordCfg _cfg:cfg.getComplexCfg().values()){
|
||
if(_cfg.getCfgId()==null){
|
||
complexStringCfgDao.insert(_cfg);
|
||
}else
|
||
complexStringCfgDao.update(_cfg);
|
||
}
|
||
}
|
||
if(deleteCfg!=null&&deleteCfg.size()>0){
|
||
for(BaseCfg _cfg:deleteCfg){
|
||
if(_cfg instanceof BaseIpCfg){
|
||
ipCfgDao.deleteByCompileId((BaseIpCfg)_cfg);
|
||
}else if(_cfg instanceof BaseStringCfg){
|
||
stringCfgDao.deleteByCompileId((BaseStringCfg)_cfg);
|
||
}else if(_cfg instanceof NumBoundaryCfg){
|
||
numCfgDao.deleteByCompileId((NumBoundaryCfg)_cfg);
|
||
}else if(_cfg instanceof ComplexkeywordCfg){
|
||
complexStringCfgDao.deleteByCompileId((ComplexkeywordCfg)_cfg);
|
||
}
|
||
}
|
||
}
|
||
if(addAreaCfg!=null&&addAreaCfg.size()>0){
|
||
// this.saveIpBatch(addAreaCfg);
|
||
}
|
||
if(updateAreaCfg!=null&&updateAreaCfg.size()>0){
|
||
this.updateIpBatch(updateAreaCfg);
|
||
}
|
||
if(deleteAreaCfgs!=null&&deleteAreaCfgs.size()>0){
|
||
this.deleteIpBatch(deleteAreaCfgs);
|
||
}
|
||
}
|
||
}
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void deleteCfg(List<BaseIpCfg> delIpCfgs,List<BaseStringCfg> delStringCfgs,List<NumBoundaryCfg> delNumCfgs,List<ComplexkeywordCfg> delComplexCfgs){
|
||
if(delIpCfgs!=null&&delIpCfgs.size()>0){
|
||
this.deleteIpBatch(delIpCfgs);
|
||
}
|
||
if(delStringCfgs!=null&&delStringCfgs.size()>0){
|
||
this.deleteStringBatch(delStringCfgs);
|
||
}
|
||
if(delNumCfgs!=null&&delNumCfgs.size()>0){
|
||
this.deleteNumBatch(delNumCfgs);
|
||
}
|
||
if(delComplexCfgs!=null&&delComplexCfgs.size()>0){
|
||
this.deleteComplexBatch(delComplexCfgs);
|
||
}
|
||
}
|
||
/**
|
||
*
|
||
* auditCfg(这里用一句话描述这个方法的作用)
|
||
* (这里描述这个方法适用条件 – 可选)
|
||
* @param cfg
|
||
* @return
|
||
*int
|
||
* @throws Exception
|
||
* @exception
|
||
* @since 1.0.0
|
||
*/
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void auditCfg(int isAudit,List<MultipleCfg> sendCfgs,List<BaseIpCfg> auduitIpCfgs,
|
||
List<BaseStringCfg> auduitStringCfgs,List<NumBoundaryCfg> auduitNumCfgs,List<ComplexkeywordCfg> auduitComplexCfgs) throws Exception{
|
||
if(Constants.AUDIT_YES==isAudit||Constants.AUDIT_NOT_YES==isAudit){
|
||
List<List<BaseCfg>> cfgs=new ArrayList<List<BaseCfg>>();
|
||
for(MultipleCfg sendCfg:sendCfgs){
|
||
List<BaseCfg> cfg=new ArrayList<>();
|
||
if(sendCfg.getIpCfg()!=null){
|
||
for(BaseIpCfg _cfg:sendCfg.getIpCfg().values()){
|
||
cfg.add(_cfg);
|
||
}
|
||
}
|
||
if(sendCfg.getStringCfg()!=null){
|
||
for(BaseStringCfg _cfg:sendCfg.getStringCfg().values()){
|
||
cfg.add(_cfg);
|
||
}
|
||
}
|
||
if(sendCfg.getNumCfg()!=null){
|
||
for(NumBoundaryCfg _cfg:sendCfg.getNumCfg().values()){
|
||
cfg.add(_cfg);
|
||
}
|
||
}
|
||
if(sendCfg.getComplexCfg()!=null){
|
||
for(ComplexkeywordCfg _cfg:sendCfg.getComplexCfg().values()){
|
||
cfg.add(_cfg);
|
||
}
|
||
}
|
||
//设置区域ip
|
||
if(sendCfg.getAreaCfg()!=null){
|
||
if(cfg.size()>0){
|
||
cfg.get(0).setAreaCfg(sendCfg.getAreaCfg());
|
||
}else{
|
||
throw new RuntimeException("发送至maat异常,没有有效的配置!");
|
||
}
|
||
}
|
||
cfgs.add(cfg);
|
||
}
|
||
// if(this.sendToMaatConvertorBatch(isAudit, cfgs)){
|
||
// if(auduitIpCfgs!=null&&auduitIpCfgs.size()>0){
|
||
// this.auditIpBatch(auduitIpCfgs);
|
||
// }
|
||
// if(auduitStringCfgs!=null&&auduitStringCfgs.size()>0){
|
||
// this.auditStringBatch(auduitStringCfgs);
|
||
// }
|
||
// if(auduitNumCfgs!=null&&auduitNumCfgs.size()>0){
|
||
// this.auditNumBatch(auduitNumCfgs);
|
||
// }
|
||
// if(auduitComplexCfgs!=null&&auduitComplexCfgs.size()>0){
|
||
// this.auditComplexBatch(auduitComplexCfgs);
|
||
// }
|
||
// }else{
|
||
// throw new RuntimeException("发送至maat失败,返回false!");
|
||
// }
|
||
}else{
|
||
if(auduitIpCfgs!=null&&auduitIpCfgs.size()>0){
|
||
this.auditIpBatch(auduitIpCfgs);
|
||
}
|
||
if(auduitStringCfgs!=null&&auduitStringCfgs.size()>0){
|
||
this.auditStringBatch(auduitStringCfgs);
|
||
}
|
||
if(auduitNumCfgs!=null&&auduitNumCfgs.size()>0){
|
||
this.auditNumBatch(auduitNumCfgs);
|
||
}
|
||
if(auduitComplexCfgs!=null&&auduitComplexCfgs.size()>0){
|
||
this.auditComplexBatch(auduitComplexCfgs);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 由于mysql不支持full join,并且考虑mysql的性能问题,这里分别从各个基础表里查询数据再拼接
|
||
*/
|
||
@Override
|
||
public Page<MultipleSearchCfg> findPage(Page<MultipleSearchCfg> page, MultipleSearchCfg entity) {
|
||
return super.findPage(page, entity);
|
||
}
|
||
private void deleteStringBatch(List<BaseStringCfg> data) {
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
try{
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
||
for(int index = 0; index < data.size();index++){
|
||
BaseStringCfg t = data.get(index);
|
||
((CrudDao<BaseStringCfg>) batchSqlSession.getMapper(StringCfgDao.class)).delete(t);
|
||
}
|
||
batchSqlSession.commit();
|
||
}finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
}
|
||
}
|
||
private void deleteComplexBatch(List<ComplexkeywordCfg> data) {
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
try{
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
||
for(int index = 0; index < data.size();index++){
|
||
ComplexkeywordCfg t = data.get(index);
|
||
((CrudDao<ComplexkeywordCfg>) batchSqlSession.getMapper(ComplexStringCfgDao.class)).delete(t);
|
||
}
|
||
batchSqlSession.commit();
|
||
}finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
}
|
||
}
|
||
private void deleteNumBatch(List<NumBoundaryCfg> data) {
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
try{
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
||
for(int index = 0; index < data.size();index++){
|
||
NumBoundaryCfg t = data.get(index);
|
||
((CrudDao<NumBoundaryCfg>) batchSqlSession.getMapper(NumCfgDao.class)).delete(t);
|
||
}
|
||
batchSqlSession.commit();
|
||
}finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
}
|
||
}
|
||
private void auditStringBatch(List<BaseStringCfg> data) {
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
try{
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
||
for(int index = 0; index < data.size();index++){
|
||
BaseStringCfg t = data.get(index);
|
||
((CrudDao<BaseStringCfg>) batchSqlSession.getMapper(StringCfgDao.class)).audit(t);
|
||
}
|
||
batchSqlSession.commit();
|
||
}finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
}
|
||
}
|
||
private void auditNumBatch(List<NumBoundaryCfg> data) {
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
try{
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
||
for(int index = 0; index < data.size();index++){
|
||
NumBoundaryCfg t = data.get(index);
|
||
((CrudDao<NumBoundaryCfg>) batchSqlSession.getMapper(NumCfgDao.class)).audit(t);
|
||
}
|
||
batchSqlSession.commit();
|
||
}finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
}
|
||
}
|
||
private void auditComplexBatch(List<ComplexkeywordCfg> data) {
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
try{
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
||
for(int index = 0; index < data.size();index++){
|
||
ComplexkeywordCfg t = data.get(index);
|
||
((CrudDao<ComplexkeywordCfg>) batchSqlSession.getMapper(ComplexStringCfgDao.class)).audit(t);
|
||
}
|
||
batchSqlSession.commit();
|
||
}finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
}
|
||
}
|
||
}
|