391 lines
16 KiB
Java
391 lines
16 KiB
Java
package com.nis.web.service.basics;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Properties;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
|
||
import org.apache.commons.collections.CollectionUtils;
|
||
import org.apache.ibatis.session.ExecutorType;
|
||
import org.apache.ibatis.session.SqlSession;
|
||
import org.apache.ibatis.session.SqlSessionFactory;
|
||
import org.springframework.beans.BeanUtils;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import com.nis.domain.FunctionRegionDict;
|
||
import com.nis.domain.FunctionServiceDict;
|
||
import com.nis.domain.Page;
|
||
import com.nis.domain.basics.CommonGroupInfo;
|
||
import com.nis.domain.basics.IpCommCfg;
|
||
import com.nis.domain.basics.PolicyGroupInfo;
|
||
import com.nis.domain.configuration.BaseIpCfg;
|
||
import com.nis.domain.configuration.CfgIndexInfo;
|
||
import com.nis.exceptions.MaatConvertException;
|
||
import com.nis.util.ConfigServiceUtil;
|
||
import com.nis.util.Constants;
|
||
import com.nis.util.StringUtil;
|
||
import com.nis.util.StringUtils;
|
||
import com.nis.web.controller.configuration.ObjectGroupController;
|
||
import com.nis.web.dao.basics.CommonGroupManageDao;
|
||
import com.nis.web.dao.basics.IpCommGroupCfgDao;
|
||
import com.nis.web.dao.basics.PolicyGroupInfoDao;
|
||
import com.nis.web.dao.configuration.CommonPolicyDao;
|
||
import com.nis.web.security.UserUtils;
|
||
import com.nis.web.service.BaseService;
|
||
import com.nis.web.service.SpringContextHolder;
|
||
@Service
|
||
public class IpCommGroupCfgService extends BaseService {
|
||
|
||
@Autowired
|
||
private IpCommGroupCfgDao ipCommGroupCfgDao;
|
||
@Autowired
|
||
private PolicyGroupInfoDao policyGroupInfoDao;
|
||
@Autowired
|
||
private CommonPolicyDao commonPolicyDao;
|
||
@Autowired
|
||
private CommonGroupManageDao commonGroupManageDao;
|
||
|
||
private CommonGroupManageService groupManageService = SpringContextHolder.getBean(CommonGroupManageService.class);
|
||
|
||
/**
|
||
* @param page
|
||
* @param entity
|
||
* @return
|
||
*/
|
||
public Page<IpCommCfg> findPage(Page<IpCommCfg> page, IpCommCfg entity) {
|
||
entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"r"));
|
||
entity.setPage(page);
|
||
List<IpCommCfg> list=ipCommGroupCfgDao.findPage(entity);
|
||
page.setList(list);
|
||
return page;
|
||
}
|
||
|
||
public List<IpCommCfg> findByPage(String ids) {
|
||
List<IpCommCfg> list=ipCommGroupCfgDao.findByPage(ids);
|
||
return list;
|
||
}
|
||
|
||
public IpCommCfg get(Long id ) {
|
||
return ipCommGroupCfgDao.get(id);
|
||
}
|
||
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void update(IpCommCfg entity){
|
||
Date editTime=new Date();
|
||
// 获取分组ID
|
||
Integer serviceGroupId = groupManageService.getServiceGroupId(entity.getGroupName(), Constants.IP_OBJ_GROUP_TYPE);
|
||
entity.setGroupId(serviceGroupId);
|
||
entity.setEditTime(editTime);
|
||
entity.setEditorId(entity.getCurrentUser().getId());
|
||
ipCommGroupCfgDao.update(entity);
|
||
|
||
// 更新分组状态
|
||
List<PolicyGroupInfo> policyGroupInfos = new ArrayList<PolicyGroupInfo>();
|
||
policyGroupInfos.addAll(policyGroupInfoDao.findPolicyByServiceGroupInfoList(entity.getUserRegion1())); // old
|
||
if(!entity.getUserRegion1().equals(entity.getGroupId()+"")) { // 分组已修改
|
||
policyGroupInfos.addAll(policyGroupInfoDao.findPolicyByServiceGroupInfoList(entity.getGroupId()+"")); // new
|
||
}
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.IP_OBJ_GROUP_TYPE);
|
||
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
}
|
||
|
||
|
||
public List<IpCommCfg> getByIds(String ids) {
|
||
if(ids==null) {
|
||
throw new RuntimeException("ids is null!");
|
||
}
|
||
return ipCommGroupCfgDao.getByIds(ids);
|
||
}
|
||
|
||
//新增
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void saveIpCommGroupCfg(CfgIndexInfo entity) {
|
||
Date createTime=new Date();
|
||
if(CollectionUtils.isNotEmpty(entity.getIpCommGroupCfgList())) {
|
||
|
||
// 获取分组ID
|
||
Integer serviceGroupId = groupManageService.getServiceGroupId(entity.getGroupName(), Constants.IP_OBJ_GROUP_TYPE);
|
||
List<PolicyGroupInfo> policyGroupInfos=policyGroupInfoDao.findPolicyByServiceGroupInfoList(serviceGroupId+"");
|
||
|
||
for (IpCommCfg cfg : entity.getIpCommGroupCfgList()) {
|
||
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId","userregion3"});
|
||
|
||
Integer regionId = 0;
|
||
List<Integer> regionIds = ConfigServiceUtil.getId(3, 1);
|
||
if(regionIds!=null && regionIds.size()>0){
|
||
regionId = regionIds.get(0);
|
||
}else {
|
||
throw new MaatConvertException("Get region id failed");
|
||
}
|
||
cfg.setRegionId(regionId);
|
||
|
||
cfg.setRequestId(0);
|
||
cfg.setClassify("0");
|
||
cfg.setAttribute("0");
|
||
cfg.setLable("0");
|
||
cfg.setCreateTime(createTime);
|
||
cfg.setCreatorId(entity.getCurrentUser().getId());
|
||
cfg.setIsValid(Constants.VALID_NO);
|
||
cfg.setIsAudit(Constants.AUDIT_NOT_YET);
|
||
cfg.setGroupId(serviceGroupId);
|
||
ipCommGroupCfgDao.insertForBatch(cfg);
|
||
|
||
}
|
||
// 更新分组状态
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.IP_OBJ_GROUP_TYPE);
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
}
|
||
|
||
}
|
||
|
||
public void delete(String ids, String groupIds, IpCommCfg cfg, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||
if(ids == null) {
|
||
long start = System.currentTimeMillis();
|
||
|
||
Page<IpCommCfg> page = new Page<IpCommCfg>(request, response, "r");
|
||
page.setOrderBy("");
|
||
page.setPageSize(Constants.MAAT_JSON_SEND_SIZE);
|
||
page.setPageNo(1);
|
||
page.setLastPage(false);
|
||
|
||
boolean hasData = true;
|
||
while(hasData){
|
||
page.setPageNo(1);
|
||
page.setLastPage(false);
|
||
cfg.setIsValid(null);
|
||
|
||
List<IpCommCfg> list = this.findPage(page, cfg).getList();
|
||
if(!StringUtil.isEmpty(list)) {
|
||
groupIds = "";
|
||
String cfgIds = "";
|
||
String serviceGroupIds = "";
|
||
|
||
for (IpCommCfg ipCommCfg : list) {
|
||
Integer groupId = ipCommCfg.getGroupId();
|
||
cfgIds += ","+ipCommCfg.getCfgId();
|
||
if(!serviceGroupIds.contains(groupId+"")) {
|
||
serviceGroupIds += ","+groupId;
|
||
}
|
||
}
|
||
cfgIds = cfgIds.substring(1);
|
||
serviceGroupIds = serviceGroupIds.substring(1);
|
||
ipCommGroupCfgDao.delete(cfgIds);
|
||
|
||
// 删除操作后获取 ①需配置更新的分组(存在域配置)②需配置取消的分组(无域配置)-> 配置分别操作更新或取消
|
||
String[] strings = serviceGroupIds.split(",");
|
||
serviceGroupIds = "";
|
||
for (String groupId : strings) {
|
||
Integer size = ipCommGroupCfgDao.getCfgInfoByGroupIds(groupId);
|
||
if(size == 0) { // 需配置取消
|
||
boolean flag = groupManageService.checkCancelCfg(groupId, Constants.IP_OBJ_GROUP_TYPE);
|
||
if(flag) { // 需配置更新
|
||
serviceGroupIds += ","+groupId;
|
||
}else {
|
||
groupIds += ","+groupId;
|
||
}
|
||
}else { // 需配置更新
|
||
groupIds += ","+groupId;
|
||
}
|
||
}
|
||
|
||
// 配置取消
|
||
if(StringUtils.isNotBlank(serviceGroupIds)) {
|
||
serviceGroupIds = serviceGroupIds.substring(1);
|
||
ObjectGroupController groupController = SpringContextHolder.getBean(ObjectGroupController.class);
|
||
Page<CfgIndexInfo> auditPage = new Page<CfgIndexInfo>(request,response,"r");
|
||
Properties props = groupController.getMsgProp();
|
||
|
||
// 组下无域配置common_group_ids -> "",ud_flag -> 0
|
||
CfgIndexInfo entity = new CfgIndexInfo();
|
||
entity.setUserRegion5(serviceGroupIds);
|
||
entity.setFunctionId(888);
|
||
entity.setCommonGroupIds("");
|
||
commonPolicyDao.updateCfgIndexCommonGroupIdsBatch(entity);
|
||
List<PolicyGroupInfo> policyGroupInfos=policyGroupInfoDao.findPolicyByServiceGroupInfoList(serviceGroupIds);
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.IP_OBJ_GROUP_TYPE);
|
||
|
||
entity.setServiceId(0);
|
||
entity.setIsValid(Constants.VALID_NO);
|
||
entity.setIsAudit(Constants.AUDIT_NOT_YES);
|
||
entity.setBatchAuditValue("1");
|
||
entity.setBatchValidValue("1,0");
|
||
groupController.auditAll(auditPage, Constants.AUDIT_NOT_YES, entity, props);
|
||
}
|
||
// 配置更新
|
||
if(StringUtils.isNotBlank(groupIds)) {
|
||
groupIds = groupIds.substring(1);
|
||
List<PolicyGroupInfo> policyGroupInfos = policyGroupInfoDao.findPolicyByServiceGroupInfoList(groupIds);
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.IP_OBJ_GROUP_TYPE);
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
}
|
||
}else {
|
||
hasData = false;
|
||
}
|
||
}
|
||
|
||
/*
|
||
// 配置取消
|
||
if(StringUtils.isNotBlank(serviceGroupIds)) {
|
||
serviceGroupIds = serviceGroupIds.substring(1);
|
||
ObjectGroupController groupController = SpringContextHolder.getBean(ObjectGroupController.class);
|
||
Page<CfgIndexInfo> auditPage = new Page<CfgIndexInfo>(request,response,"r");
|
||
Properties props = groupController.getMsgProp();
|
||
|
||
// 配置未生效
|
||
String updateStatusGroupIds = "";
|
||
for (String groupId : serviceGroupIds.split(",")) {
|
||
List<CfgIndexInfo> list = commonPolicyDao.getObjGroupListByGroupIds(groupId, 888, Constants.VALID_NO);
|
||
if(list.size() > 0) {
|
||
updateStatusGroupIds += ","+groupId;
|
||
}
|
||
}
|
||
if(StringUtils.isNotBlank(updateStatusGroupIds)) {
|
||
updateStatusGroupIds = updateStatusGroupIds.substring(1);
|
||
CfgIndexInfo entity = new CfgIndexInfo();
|
||
entity.setUserRegion5(updateStatusGroupIds);
|
||
entity.setFunctionId(888);
|
||
entity.setCommonGroupIds("");
|
||
entity.setIsValid(Constants.VALID_NO);
|
||
commonPolicyDao.updateCfgIndexCommonGroupIdsBatch(entity);
|
||
|
||
List<PolicyGroupInfo> policyGroupInfos=policyGroupInfoDao.findPolicyByServiceGroupInfoList(updateStatusGroupIds);
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.IP_OBJ_GROUP_TYPE);
|
||
}
|
||
|
||
// 配置已生效×
|
||
// 组下无域配置common_group_ids -> "",ud_flag -> 0
|
||
CfgIndexInfo entity = new CfgIndexInfo();
|
||
entity.setUserRegion5(serviceGroupIds);
|
||
entity.setFunctionId(888);
|
||
entity.setCommonGroupIds("");
|
||
commonPolicyDao.updateCfgIndexCommonGroupIdsBatch(entity);
|
||
List<PolicyGroupInfo> policyGroupInfos=policyGroupInfoDao.findPolicyByServiceGroupInfoList(serviceGroupIds);
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.IP_OBJ_GROUP_TYPE);
|
||
|
||
entity.setServiceId(0);
|
||
entity.setIsValid(Constants.VALID_NO);
|
||
entity.setIsAudit(Constants.AUDIT_NOT_YES);
|
||
entity.setBatchAuditValue("1");
|
||
entity.setBatchValidValue("1,0");
|
||
groupController.auditAll(auditPage, Constants.AUDIT_NOT_YES, entity, props);
|
||
}
|
||
*/
|
||
|
||
long end = System.currentTimeMillis();
|
||
logger.warn("IP组域配置批量删除耗时:"+(end-start));
|
||
|
||
}else {
|
||
List<PolicyGroupInfo> policyGroupInfos = policyGroupInfoDao.findPolicyByServiceGroupInfoList(groupIds);
|
||
ipCommGroupCfgDao.delete(ids);
|
||
// 更新分组状态
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.IP_OBJ_GROUP_TYPE);
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
|
||
}
|
||
}
|
||
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void saveAndSend(FunctionRegionDict regionDict, FunctionServiceDict serviceDict, List<BaseIpCfg> cfgs, Map<String, Integer> groupIdMap, List<CommonGroupInfo> insertGroupList) {
|
||
logger.warn("Start to save IP,size:"+cfgs.size());
|
||
long start=System.currentTimeMillis();
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
// 导入域配置 -> ①分组不存在,新增分组并返回serviceGroupId ②分组存在,获取serviceGroupId -> 数据入库 -> 更新分组状态 -> 判断是否配置下发
|
||
try{
|
||
String ids = "";
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
||
for(BaseIpCfg cfg:cfgs) {
|
||
IpCommCfg ipCommCfg=new IpCommCfg();
|
||
BeanUtils.copyProperties(cfg, ipCommCfg);
|
||
|
||
Integer groupId = commonGroupManageDao.getGroupIdByGroupName(ipCommCfg.getGroupName(), Constants.IP_OBJ_GROUP_TYPE);
|
||
if(groupId == null) { // 分组不存在,新增 UdFlag为1
|
||
groupId = groupIdMap.get(ipCommCfg.getGroupName());
|
||
}else {
|
||
if(!ids.contains(groupId+"")) {
|
||
ids += ","+groupId;
|
||
}
|
||
}
|
||
ipCommCfg.setGroupId(groupId);
|
||
((IpCommGroupCfgDao) batchSqlSession.getMapper(IpCommGroupCfgDao.class)).insertForBatch(ipCommCfg);
|
||
|
||
}
|
||
batchSqlSession.commit();
|
||
|
||
if(insertGroupList.size() > 0) {
|
||
commonGroupManageDao.insertBatch(insertGroupList);
|
||
}
|
||
|
||
if(StringUtils.isNotBlank(ids)) {
|
||
// 更新分组状态
|
||
String serviceGroupIds = ids.substring(1);
|
||
List<PolicyGroupInfo> policyGroupInfos = policyGroupInfoDao.findPolicyByServiceGroupInfoList(serviceGroupIds);
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.IP_OBJ_GROUP_TYPE);
|
||
// 判断是否配置下发
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
}
|
||
|
||
}finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
|
||
}
|
||
long end=System.currentTimeMillis();
|
||
logger.warn("Save IP finish,cost:"+(end-start));
|
||
cfgs.clear();
|
||
cfgs=null;
|
||
}
|
||
|
||
/**
|
||
* 获取会配置取消的CompileId
|
||
* @param searchCfg 携带列表检索条件
|
||
* @return List<Integer> CompileId集合
|
||
*/
|
||
public List<Integer> ajaxCheckIsLastOneCfgBatch(IpCommCfg searchCfg) {
|
||
List<Integer> resultList = new ArrayList<Integer>();
|
||
|
||
// 获取所有准备删除的数据,并组织格式 {"groupId1":[cfgId1, cfgId2, ...], ...}
|
||
Map<Integer, String> map = new HashMap<Integer, String>();
|
||
List<IpCommCfg> cfgList = ipCommGroupCfgDao.findPage(searchCfg);
|
||
for (IpCommCfg cfg : cfgList) {
|
||
Integer groupId = cfg.getGroupId();
|
||
String value = "";
|
||
if(map.containsKey(groupId)) {
|
||
value = map.get(groupId);
|
||
}
|
||
value += ","+cfg.getCfgId();
|
||
map.put(groupId, value);
|
||
}
|
||
|
||
// 校验执行删除后分组下是否还有域配置,若没有则获取引用该分组的CompileId
|
||
for (Integer groupId : map.keySet()) {
|
||
String cfgIds = map.get(groupId).substring(1);
|
||
Integer size = commonGroupManageDao.ajaxCheckIsLastOneCfg(groupId, cfgIds, IpCommCfg.getIndexTable());
|
||
if(size == 0) { // 0为分组下的最后一条配置,获取配置ID提示
|
||
Map<String,String> searchMap = new HashMap<String,String>();
|
||
searchMap.put("ipGroup", ","+groupId+",");
|
||
Integer compileId = commonGroupManageDao.getCompileIdByGroupId(gsonToJson(searchMap));
|
||
if(compileId != null) {
|
||
resultList.add(compileId);
|
||
}
|
||
}
|
||
}
|
||
return resultList;
|
||
|
||
}
|
||
|
||
private void checkCancelCfg() {
|
||
// TODO Auto-generated method stub
|
||
|
||
}
|
||
|
||
} |