304 lines
10 KiB
Java
304 lines
10 KiB
Java
package com.nis.web.service.basics;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Arrays;
|
||
import java.util.Collection;
|
||
import java.util.Collections;
|
||
import java.util.Date;
|
||
import java.util.HashMap;
|
||
import java.util.HashSet;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Set;
|
||
|
||
import com.nis.web.dao.basics.*;
|
||
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.basics.CommonGroupInfo;
|
||
import com.nis.domain.basics.DomainCommCfg;
|
||
import com.nis.domain.basics.IpCommCfg;
|
||
import com.nis.domain.basics.PolicyGroupInfo;
|
||
import com.nis.domain.basics.ProtectionListInfo;
|
||
import com.nis.domain.basics.ScriberIdCommCfg;
|
||
import com.nis.domain.basics.UrlCommCfg;
|
||
import com.nis.exceptions.MaatConvertException;
|
||
import com.nis.util.CacheUtils;
|
||
import com.nis.util.ConfigServiceUtil;
|
||
import com.nis.util.Constants;
|
||
import com.nis.util.StringUtil;
|
||
import com.nis.web.dao.basics.CommonGroupManageDao;
|
||
import com.nis.web.dao.basics.DomainCommGroupDao;
|
||
import com.nis.web.dao.basics.IpCommGroupCfgDao;
|
||
import com.nis.web.dao.basics.ScriberIdCommGroupDao;
|
||
import com.nis.web.dao.basics.UrlCommGroupDao;
|
||
import com.nis.web.security.UserUtils;
|
||
import com.nis.web.service.BaseService;
|
||
|
||
@Service
|
||
public class CommonGroupManageService extends BaseService{
|
||
|
||
@Autowired
|
||
private CommonGroupManageDao commonGroupManageDao;
|
||
|
||
@Autowired
|
||
private IpCommGroupCfgDao ipCommGroupCfgDao; // 校验分组时使用
|
||
@Autowired
|
||
private UrlCommGroupDao urlCommGroupDao;
|
||
@Autowired
|
||
private DomainCommGroupDao domainCommGroupDao;
|
||
@Autowired
|
||
private ScriberIdCommGroupDao scriberIdCommGroupDao;
|
||
@Autowired
|
||
private PolicyGroupInfoDao policyGroupInfoDao;
|
||
public Page<CommonGroupInfo> findCommonGroupInfoList(Page<CommonGroupInfo> page, CommonGroupInfo entity) {
|
||
entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"r"));
|
||
entity.setPage(page);
|
||
List<CommonGroupInfo> list=commonGroupManageDao.findCommonGroupInfoList(entity);
|
||
page.setList(list);
|
||
return page;
|
||
}
|
||
|
||
public CommonGroupInfo getById(int id) {
|
||
CommonGroupInfo groupInfo=commonGroupManageDao.getById(id);
|
||
return groupInfo;
|
||
}
|
||
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void saveOrUpdate(CommonGroupInfo entity) throws MaatConvertException{
|
||
entity.setIsValid(1);
|
||
entity.setGroupName(entity.getGroupName().toUpperCase());
|
||
if(entity.getGroupId()==null){ // 新增
|
||
Date createTime=new Date();
|
||
entity.setCreatorId(UserUtils.getUser().getId());
|
||
entity.setCreateTime(createTime);
|
||
|
||
// 获取GroupId
|
||
int serviceGroupId=0;
|
||
List<Integer> groupIds= ConfigServiceUtil.getId(2,1);
|
||
if(groupIds.size()>0) {
|
||
serviceGroupId=groupIds.get(0).intValue();
|
||
}else {
|
||
throw new MaatConvertException("Get group id failed");
|
||
}
|
||
entity.setServiceGroupId(serviceGroupId);
|
||
commonGroupManageDao.insert(entity);
|
||
//this.updatePolicyGroupDict(entity.getGroupType()+"");
|
||
}else{ // 修改
|
||
Date editTime=new Date();
|
||
entity.setEditorId(UserUtils.getUser().getId());
|
||
entity.setEditTime(editTime);
|
||
commonGroupManageDao.update(entity);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void deldete(String ids, int isValid, String groupTypes){
|
||
CommonGroupInfo entity=new CommonGroupInfo();
|
||
Date editTime=new Date();
|
||
entity.setEditorId(UserUtils.getUser().getId());
|
||
entity.setEditTime(editTime);
|
||
entity.setIsValid(isValid);
|
||
if(!StringUtil.isEmpty(ids)){
|
||
for (String id : ids.split(",")) {
|
||
if(!StringUtil.isEmpty(id)){
|
||
entity.setGroupId(Integer.parseInt(id));
|
||
commonGroupManageDao.update(entity);
|
||
}
|
||
}
|
||
}
|
||
List<PolicyGroupInfo> policyGroupInfos=policyGroupInfoDao.findPolicyByServiceGroupInfoList(ids);
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
}
|
||
|
||
// 分组配置界面使用
|
||
public List<CommonGroupInfo> findCommonGroupInfosByType(int type) {
|
||
List<CommonGroupInfo> list=commonGroupManageDao.findCommonGroupInfosByType(type);
|
||
return list;
|
||
}
|
||
|
||
// 导出时使用
|
||
public List<CommonGroupInfo> findGroupInfoList(String ids) {
|
||
List<CommonGroupInfo> list=commonGroupManageDao.findGroupInfoList(ids);
|
||
return list;
|
||
}
|
||
|
||
/**
|
||
* 更新分组状态 policy_group_info ud_flag:0(无有效的域配置) 1(存在有效的域配置)
|
||
* @param policyGroupInfos
|
||
* @param groupType
|
||
*/
|
||
public void updateGroupStatus(List<PolicyGroupInfo> policyGroupInfos, Integer groupType) {
|
||
/**
|
||
* void updateUdFlag(String groupIds, Integer udFlag, Integer groupType);
|
||
*/
|
||
for(PolicyGroupInfo info:policyGroupInfos){
|
||
boolean update=false;
|
||
Integer udFlag = 0;
|
||
Integer size = 0;
|
||
if(groupType == Constants.IP_OBJ_GROUP_TYPE) { // IP
|
||
size = ipCommGroupCfgDao.getCfgInfoByGroupIds(info.getServiceGroupId().toString());
|
||
}else if(groupType == Constants.URL_OBJ_GROUP_TYPE) { // URL
|
||
size = urlCommGroupDao.getCfgInfoByGroupIds(info.getServiceGroupId().toString());
|
||
}else if(groupType == Constants.DOMAIN_OBJ_GROUP_TYPE) { // Domain
|
||
size = domainCommGroupDao.getCfgInfoByGroupIds(info.getServiceGroupId().toString());
|
||
}else if(groupType == Constants.SUBID_OBJ_GROUP_TYPE) { // ScriberId
|
||
size = scriberIdCommGroupDao.getCfgInfoByGroupIds(info.getServiceGroupId().toString());
|
||
}
|
||
|
||
if(size > 0 && info.getUdFlag().equals(0)) {
|
||
update=true;
|
||
udFlag = 1;
|
||
info.setUdFlag(udFlag);
|
||
}else if(size == 0 /*&& !info.getUdFlag().equals(2)*/){
|
||
update=true;
|
||
info.setUdFlag(udFlag);
|
||
}
|
||
if(update){
|
||
commonGroupManageDao.updateGroupStatus(info.getServiceGroupId().toString(), udFlag, groupType);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
public CommonGroupInfo getGroupInfo(CommonGroupInfo searchCfg) {
|
||
return commonGroupManageDao.getGroupInfo(searchCfg);
|
||
}
|
||
|
||
/**
|
||
* 更新字典缓存
|
||
* @return
|
||
*/
|
||
/*private void updatePolicyGroupDict(String groupTypes) {
|
||
String dictKey = Constants.CACHE_IPGROUP_DICT;
|
||
if(groupTypes.contains(",")) {
|
||
String[] split = groupTypes.split(",");
|
||
List<String> asList = Arrays.asList(split);
|
||
HashSet<String> strings = new HashSet<>(asList);
|
||
for (String str : strings) {
|
||
Integer groupType = Integer.valueOf(str);
|
||
if(groupType.equals(7)) {
|
||
dictKey = Constants.CACHE_URLGROUP_DICT;
|
||
}else if(groupType.equals(8)) {
|
||
dictKey = Constants.CACHE_DOMAINGROUP_DICT;
|
||
}else if(groupType.equals(9)) {
|
||
dictKey = Constants.CACHE_SCRIBERGROUP_DICT;
|
||
}
|
||
List<CommonGroupInfo> groupInfo = findCommonGroupInfosByType(groupType);
|
||
CacheUtils.put(dictKey, groupInfo);
|
||
}
|
||
|
||
}else {
|
||
Integer groupType = Integer.valueOf(groupTypes);
|
||
if(groupType.equals(7)) {
|
||
dictKey = Constants.CACHE_URLGROUP_DICT;
|
||
}else if(groupType.equals(8)) {
|
||
dictKey = Constants.CACHE_DOMAINGROUP_DICT;
|
||
}else if(groupType.equals(9)) {
|
||
dictKey = Constants.CACHE_SCRIBERGROUP_DICT;
|
||
}
|
||
List<CommonGroupInfo> groupInfo = findCommonGroupInfosByType(groupType);
|
||
CacheUtils.put(dictKey, groupInfo);
|
||
|
||
}
|
||
|
||
}*/
|
||
|
||
/**
|
||
* 校验执行删除操作后分组下域配置是否为空
|
||
* @param grouIdAndCfgId
|
||
* @param groupType
|
||
* @return
|
||
*/
|
||
public List<Integer> ajaxCheckIsLastOneCfg(String grouIdAndCfgId, Integer groupType) {
|
||
// 组织格式
|
||
Map<Integer,List<Integer>> map = new HashMap<Integer,List<Integer>>();
|
||
for(String str : grouIdAndCfgId.split(",")) {
|
||
Integer groupId = Integer.valueOf(str.substring(0, str.indexOf("_")));
|
||
Integer cfgId = Integer.valueOf(str.substring(str.indexOf("_")+1));
|
||
if(map.containsKey(groupId)) {
|
||
List<Integer> list = map.get(groupId);
|
||
list.add(cfgId);
|
||
map.put(groupId, list);
|
||
}else {
|
||
List<Integer> list = new ArrayList<Integer>();
|
||
list.add(cfgId);
|
||
map.put(groupId, list);
|
||
}
|
||
}
|
||
|
||
List<Integer> resultList = new ArrayList<Integer>();
|
||
for (Integer groupId : map.keySet()) {
|
||
List<Integer> cfgIdList = map.get(groupId);
|
||
String cfgIds = "";
|
||
for (Integer cfgId : cfgIdList) {
|
||
cfgIds += "," + cfgId;
|
||
}
|
||
Integer size = 0;
|
||
if(groupType == Constants.IP_OBJ_GROUP_TYPE) { // IP
|
||
size = commonGroupManageDao.ajaxCheckIsLastOneCfg(groupId, cfgIds.substring(1), IpCommCfg.getIndexTable());
|
||
}else if(groupType == Constants.URL_OBJ_GROUP_TYPE) { // URL
|
||
size = commonGroupManageDao.ajaxCheckIsLastOneCfg(groupId, cfgIds.substring(1), UrlCommCfg.getTablename());
|
||
}else if(groupType == Constants.DOMAIN_OBJ_GROUP_TYPE) { // Domain
|
||
size = commonGroupManageDao.ajaxCheckIsLastOneCfg(groupId, cfgIds.substring(1), DomainCommCfg.getTablename());
|
||
}else if(groupType == Constants.SUBID_OBJ_GROUP_TYPE) { // ScriberId
|
||
size = commonGroupManageDao.ajaxCheckIsLastOneCfg(groupId, cfgIds.substring(1), ScriberIdCommCfg.getTablename());
|
||
}
|
||
|
||
if(size == 0) { // 0为分组下的最后一条配置,获取配置ID提示
|
||
Integer compileId = commonGroupManageDao.getCompileIdByGroupId(","+groupId+",");
|
||
if(compileId != null) {
|
||
resultList.add(compileId);
|
||
}
|
||
}
|
||
}
|
||
return resultList;
|
||
}
|
||
|
||
/**
|
||
* 分组域配置新增或修改时调用:获取service group id
|
||
* @param cfg
|
||
* @return
|
||
*/
|
||
public Integer getServiceGroupId(String groupName, Integer groupType) {
|
||
CommonGroupInfo entity = new CommonGroupInfo();
|
||
int serviceGroupId = 0;
|
||
entity.setGroupType(groupType);
|
||
entity.setGroupName(groupName.toUpperCase());
|
||
|
||
CommonGroupInfo groupInfo = commonGroupManageDao.getGroupInfo(entity);
|
||
if(groupInfo == null) {
|
||
// 获取GroupId
|
||
List<Integer> groupIds= ConfigServiceUtil.getId(2,1);
|
||
if(groupIds.size()>0) {
|
||
serviceGroupId =groupIds.get(0).intValue();
|
||
}else {
|
||
throw new MaatConvertException("Get group id failed");
|
||
}
|
||
entity.setServiceGroupId(serviceGroupId);
|
||
|
||
Date createTime=new Date();
|
||
entity.setIsValid(1);
|
||
entity.setCreatorId(UserUtils.getUser().getId());
|
||
entity.setCreateTime(createTime);
|
||
commonGroupManageDao.insert(entity);
|
||
}else {
|
||
serviceGroupId = groupInfo.getServiceGroupId();
|
||
}
|
||
return serviceGroupId;
|
||
|
||
}
|
||
|
||
public Integer getCountByGroupName(Set<String> set, Integer groupType) {
|
||
Integer count = commonGroupManageDao.getCountByGroupName(set, groupType);
|
||
return set.size()-count;
|
||
}
|
||
|
||
|
||
}
|