363 lines
16 KiB
Java
363 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.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.PolicyGroupInfo;
|
||
import com.nis.domain.basics.ScriberIdCommCfg;
|
||
import com.nis.domain.configuration.BaseStringCfg;
|
||
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.StringUtils;
|
||
import com.nis.web.controller.configuration.ObjectGroupController;
|
||
import com.nis.web.dao.CrudDao;
|
||
import com.nis.web.dao.basics.CommonGroupManageDao;
|
||
import com.nis.web.dao.basics.PolicyGroupInfoDao;
|
||
import com.nis.web.dao.basics.ScriberIdCommGroupDao;
|
||
import com.nis.web.dao.configuration.CommonPolicyDao;
|
||
import com.nis.web.security.UserUtils;
|
||
import com.nis.web.service.CrudService;
|
||
import com.nis.web.service.SpringContextHolder;
|
||
|
||
@Service
|
||
public class ScriberIdCommGroupService extends CrudService<CrudDao<ScriberIdCommCfg>, ScriberIdCommCfg>{
|
||
|
||
@Autowired
|
||
private ScriberIdCommGroupDao scriberIdCommGroupDao;
|
||
@Autowired
|
||
private PolicyGroupInfoDao policyGroupInfoDao;
|
||
@Autowired
|
||
private CommonPolicyDao commonPolicyDao;
|
||
@Autowired
|
||
private CommonGroupManageDao commonGroupManageDao;
|
||
|
||
private CommonGroupManageService groupManageService = SpringContextHolder.getBean(CommonGroupManageService.class);
|
||
|
||
public Page<ScriberIdCommCfg> findPage(Page<ScriberIdCommCfg> page, ScriberIdCommCfg entity) {
|
||
entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"r"));
|
||
entity.setPage(page);
|
||
List<ScriberIdCommCfg> list=scriberIdCommGroupDao.findAllPageList(entity);
|
||
for (int i = 0; i < list.size(); i++) {
|
||
String key=list.get(i).getCfgKeywords().replace("***and***", " ");
|
||
list.get(i).setCfgKeywords(key);
|
||
}
|
||
page.setList(list);
|
||
return page;
|
||
}
|
||
|
||
public List<ScriberIdCommCfg> findByPage(String ids) {
|
||
List<ScriberIdCommCfg> list=scriberIdCommGroupDao.findByPage(ids);
|
||
return list;
|
||
}
|
||
|
||
public ScriberIdCommCfg get(Long id ) {
|
||
return scriberIdCommGroupDao.get(id);
|
||
}
|
||
|
||
// 新增
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void saveCommGroupCfg(CfgIndexInfo entity) {
|
||
Date createTime=new Date();
|
||
if(entity.getScriberIdCommGroupList()!=null) {
|
||
|
||
// 获取分组ID
|
||
Integer serviceGroupId = groupManageService.getServiceGroupId(entity.getGroupName(), Constants.SUBID_OBJ_GROUP_TYPE);
|
||
List<PolicyGroupInfo> policyGroupInfos=policyGroupInfoDao.findPolicyByServiceGroupInfoList(serviceGroupId+"");
|
||
|
||
for (ScriberIdCommCfg cfg : entity.getScriberIdCommGroupList()) {
|
||
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
|
||
|
||
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.setIsHexbin(2);
|
||
cfg.setCreateTime(createTime);
|
||
cfg.setCreatorId(entity.getCurrentUser().getId());
|
||
cfg.setIsValid(Constants.VALID_NO);
|
||
cfg.setIsAudit(Constants.AUDIT_NOT_YET);
|
||
if(cfg.getCfgKeywords().contains("***and***")){
|
||
cfg.setExprType(1);
|
||
}else{
|
||
cfg.setExprType(0);
|
||
}
|
||
cfg.setGroupId(serviceGroupId);
|
||
scriberIdCommGroupDao.insertCommGroupCfg(cfg);
|
||
}
|
||
// 更新分组状态
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.SUBID_OBJ_GROUP_TYPE);
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
|
||
}
|
||
}
|
||
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void update(ScriberIdCommCfg entity){
|
||
Date editTime=new Date();
|
||
entity.setIsValid(0);
|
||
entity.setIsAudit(0);
|
||
entity.setEditTime(editTime);
|
||
entity.setEditorId(entity.getCurrentUser().getId());
|
||
if(entity.getCfgKeywords().contains("***and***")){
|
||
entity.setExprType(1);
|
||
}else{
|
||
entity.setExprType(0);
|
||
}
|
||
|
||
// 获取分组ID
|
||
Integer serviceGroupId = groupManageService.getServiceGroupId(entity.getGroupName(), Constants.SUBID_OBJ_GROUP_TYPE);
|
||
entity.setGroupId(serviceGroupId);
|
||
scriberIdCommGroupDao.update(entity);
|
||
|
||
// 更新分组状态
|
||
List<PolicyGroupInfo> policyGroupInfos = new ArrayList<PolicyGroupInfo>();
|
||
policyGroupInfos.addAll(policyGroupInfoDao.findPolicyByServiceGroupInfoList(entity.getUserRegion1())); // old
|
||
if(!entity.getUserRegion1().equals(entity.getGroupId().toString())) { // 分组已修改
|
||
policyGroupInfos.addAll(policyGroupInfoDao.findPolicyByServiceGroupInfoList(entity.getGroupId().toString())); // new
|
||
}
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.SUBID_OBJ_GROUP_TYPE);
|
||
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
}
|
||
|
||
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void delete(String ids, String groupIds, ScriberIdCommCfg cfg, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||
if(ids == null) {
|
||
groupIds = "";
|
||
String cfgIds = "";
|
||
String serviceGroupIds = "";
|
||
|
||
// 删除操作后获取 ①需配置更新的分组(存在域配置)②需配置取消的分组(无域配置) -> 配置分别操作更新或取消
|
||
List<ScriberIdCommCfg> set = scriberIdCommGroupDao.getGroupIds(cfg);
|
||
if(set.size() == 0) { // 无可删除配置
|
||
return;
|
||
}else {
|
||
for (ScriberIdCommCfg scriberIdCommCfg : set) {
|
||
Integer groupId = scriberIdCommCfg.getGroupId();
|
||
cfgIds += ","+scriberIdCommCfg.getCfgId();
|
||
if(!serviceGroupIds.contains(groupId+"")) {
|
||
serviceGroupIds += ","+groupId;
|
||
}
|
||
}
|
||
cfgIds = cfgIds.substring(1);
|
||
serviceGroupIds = serviceGroupIds.substring(1);
|
||
}
|
||
scriberIdCommGroupDao.delete(cfgIds);
|
||
|
||
String[] strings = serviceGroupIds.split(",");
|
||
serviceGroupIds = "";
|
||
for (String groupId : strings) {
|
||
Integer size = scriberIdCommGroupDao.getCfgInfoByGroupIds(groupId);
|
||
if(size == 0) { // 需配置取消
|
||
serviceGroupIds += ","+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();
|
||
|
||
// 配置未生效
|
||
/*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.SUBID_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.SUBID_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);
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
}
|
||
|
||
}else {
|
||
List<PolicyGroupInfo> policyGroupInfos=policyGroupInfoDao.findPolicyByServiceGroupInfoList(groupIds);
|
||
scriberIdCommGroupDao.delete(ids);
|
||
// 更新分组状态
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.SUBID_OBJ_GROUP_TYPE);
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
public List<ScriberIdCommCfg> getByIds(String ids) {
|
||
if(ids==null) {
|
||
throw new RuntimeException("ids is null!");
|
||
}
|
||
return scriberIdCommGroupDao.getByIds(ids);
|
||
}
|
||
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void saveAndSend(FunctionRegionDict regionDict, FunctionServiceDict serviceDict, List<BaseStringCfg<?>> cfgs, Map<String, Integer> groupIdMap) {
|
||
logger.warn("Start to save ScriberId,size:"+cfgs.size());
|
||
long start=System.currentTimeMillis();
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
// 导入域配置 -> ①分组不存在,新增分组并返回groupId ②分组存在,获取groupId -> 数据 入库 -> 更新分组状态 -> 判断是否配置下发
|
||
try{
|
||
String ids = "";
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
||
for(BaseStringCfg<?> cfg:cfgs) {
|
||
ScriberIdCommCfg commCfg=new ScriberIdCommCfg();
|
||
BeanUtils.copyProperties(cfg, commCfg);
|
||
|
||
Integer groupId = commonGroupManageDao.getGroupIdByGroupName(commCfg.getGroupName(), Constants.SUBID_OBJ_GROUP_TYPE);
|
||
if(groupId == null) {
|
||
groupId = groupIdMap.get(commCfg.getGroupName());
|
||
}else {
|
||
if(!ids.contains(groupId+"")) {
|
||
ids += ","+groupId;
|
||
}
|
||
}
|
||
commCfg.setGroupId(groupId);
|
||
((ScriberIdCommGroupDao) batchSqlSession.getMapper(ScriberIdCommGroupDao.class)).insertCommGroupCfg(commCfg);
|
||
}
|
||
batchSqlSession.commit();
|
||
|
||
if(groupIdMap.size() > 0) {
|
||
Long creatorId = UserUtils.getUser().getId();
|
||
Date createTime = new Date();
|
||
List<CommonGroupInfo> insertGroupList = new ArrayList<CommonGroupInfo>();
|
||
for (String groupName : groupIdMap.keySet()) {
|
||
CommonGroupInfo groupInfo = new CommonGroupInfo();
|
||
groupInfo.setCreatorId(creatorId);
|
||
groupInfo.setCreateTime(createTime);
|
||
groupInfo.setGroupName(groupName);
|
||
groupInfo.setGroupType(Constants.SUBID_OBJ_GROUP_TYPE);
|
||
groupInfo.setServiceGroupId(groupIdMap.get(groupName));
|
||
groupInfo.setUdFlag(1);
|
||
insertGroupList.add(groupInfo);
|
||
}
|
||
commonGroupManageDao.insertBatch(insertGroupList);
|
||
}
|
||
|
||
if(StringUtils.isNotBlank(ids)) {
|
||
// 更新分组状态
|
||
String serviceGroupIds = ids.substring(1);
|
||
List<PolicyGroupInfo> policyGroupInfos = policyGroupInfoDao.findPolicyByServiceGroupInfoList(serviceGroupIds);
|
||
groupManageService.updateGroupStatus(policyGroupInfos, Constants.SUBID_OBJ_GROUP_TYPE);
|
||
// 判断是否配置下发
|
||
transObjGroupToMaat(policyGroupInfos);
|
||
}
|
||
|
||
}finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
|
||
}
|
||
long end=System.currentTimeMillis();
|
||
logger.warn("Save ScriberId finish,cost:"+(end-start));
|
||
cfgs.clear();
|
||
cfgs=null;
|
||
}
|
||
|
||
/**
|
||
* 获取会配置取消的CompileId
|
||
* @param searchCfg 携带列表检索条件
|
||
* @return List<Integer> CompileId集合
|
||
*/
|
||
public List<Integer> ajaxCheckIsLastOneCfgBatch(ScriberIdCommCfg searchCfg) {
|
||
List<Integer> resultList = new ArrayList<Integer>();
|
||
|
||
// 获取所有准备删除的数据,并组织格式 {"groupId1":[cfgId1, cfgId2, ...], ...}
|
||
Map<Integer, String> map = new HashMap<Integer, String>();
|
||
List<ScriberIdCommCfg> cfgList = scriberIdCommGroupDao.findAllPageList(searchCfg);
|
||
for (ScriberIdCommCfg 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, ScriberIdCommCfg.getTablename());
|
||
if(size == 0) { // 0为分组下的最后一条配置,获取配置ID提示
|
||
Map<String,String> searchMap = new HashMap<String,String>();
|
||
searchMap.put("subscribeIdGroup", ","+groupId+",");
|
||
Integer compileId = commonGroupManageDao.getCompileIdByGroupId(gsonToJson(searchMap));
|
||
if(compileId != null) {
|
||
resultList.add(compileId);
|
||
}
|
||
}
|
||
}
|
||
return resultList;
|
||
|
||
}
|
||
|
||
}
|