package com.nis.web.service.configuration; import java.io.File; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.nis.domain.Page; import com.nis.domain.configuration.AreaIpCfg; import com.nis.domain.configuration.BaseStringCfg; import com.nis.domain.configuration.CfgIndexInfo; import com.nis.domain.configuration.ComplexkeywordCfg; import com.nis.domain.configuration.FileDigestCfg; import com.nis.domain.configuration.HttpBodyCfg; import com.nis.domain.configuration.IpPortCfg; import com.nis.domain.configuration.MailKeywordCfg; import com.nis.domain.configuration.NtcSubscribeIdCfg; import com.nis.domain.maat.MaatCfg; import com.nis.domain.maat.ToMaatBean; import com.nis.domain.maat.ToMaatResult; import com.nis.domain.maat.MaatCfg.DigestCfg; import com.nis.domain.maat.MaatCfg.GroupCfg; import com.nis.domain.maat.MaatCfg.IpCfg; import com.nis.domain.maat.MaatCfg.NumBoundaryCfg; import com.nis.domain.maat.MaatCfg.StringCfg; import com.nis.exceptions.MaatConvertException; import com.nis.util.ConfigServiceUtil; import com.nis.util.Constants; import com.nis.util.FileUtils; import com.nis.util.JsonMapper; import com.nis.util.StringUtil; import com.nis.web.dao.configuration.AreaIpCfgDao; import com.nis.web.dao.configuration.MailCfgDao; import com.nis.web.dao.configuration.StringCfgDao; import com.nis.web.security.UserUtils; import com.nis.web.service.CrudService; /** * 邮件相关配置事务类 * @author dell * */ @Service public class MailCfgService extends CrudService { @Autowired protected MailCfgDao mailCfgDao; @Autowired protected AreaIpCfgDao areaIpCfgDao; @Autowired protected StringCfgDao stringCfgDao; public Page getMailList(Page page, CfgIndexInfo entity){ // 生成数据权限过滤条件(dsf为dataScopeFilter的简写,在xml中使用 ${sqlMap.dsf}调用权限SQL) entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"a")); entity.setPage(page); List list = mailCfgDao.getMailList(entity); page.setList(list); return page; } public CfgIndexInfo getMailCfg(Long cfgId,Integer compileId){ CfgIndexInfo entity = mailCfgDao.getCfgIndexInfo(cfgId,compileId); List ipPortList = mailCfgDao.getIpPortList(entity); List keywordList = mailCfgDao.getMailKeywordList(entity); List digestList = mailCfgDao.getMailFileDigestList(entity); List areaCfg = areaIpCfgDao.getByCompileId(entity.getCompileId()); List ntcSubscribeIdList = stringCfgDao.findSubscribeIdCfgListByCfgIndexInfo(entity); entity.setIpPortList(ipPortList); entity.setComplexList(keywordList); entity.setDigestList(digestList); entity.setNtcSubscribeIdCfgList(ntcSubscribeIdList); entity.setAreaCfg(areaCfg); return entity; } public Map exportmail(CfgIndexInfo entity){ Map dataMap=new HashMap(); List ipPortList = mailCfgDao.getIpPortList(entity); entity.setCfgType("NTC_MAIL_HDR"); List mailHdrList =mailCfgDao.getMailKeywordList(entity); entity.setCfgType("NTC_MAIL_BODY"); List mailBodyList = mailCfgDao.getMailKeywordList(entity); List subscribeIdList = stringCfgDao.findSubscribeIdCfgListByCfgIndexInfo(entity); dataMap.put("NTC_SUBSCRIBE_ID", subscribeIdList); dataMap.put("NTC_MAIL_BODY", mailBodyList); dataMap.put("NTC_MAIL_HDR", mailHdrList); dataMap.put("NTC_UNIVERSAL_IP", ipPortList); return dataMap; } public void saveMailCfg(List complexkeywordCfgs){ for(ComplexkeywordCfg cfg:complexkeywordCfgs){ mailCfgDao.saveMailKeywordCfg(cfg); } } /** * 保存mail配置 * @param entity */ public void saveMailCfg(CfgIndexInfo entity,ToMaatResult result){ //设置区域运营商信息 setAreaEffectiveIds(entity); int isValid=0; if(!StringUtil.isEmpty(entity.getIsValid()) && entity.getIsValid()==1) { isValid=1; } entity.setIsValid(0); entity.setIsAudit(0); if(entity.getCfgId()==null){ Integer compileId = 0; try { List idList = ConfigServiceUtil.getId(1, 1); if(idList!=null && idList.size()>0){ compileId = idList.get(0); } } catch (MaatConvertException e) { logger.info("获取编译ID出错"); throw new MaatConvertException(""); } entity.setCompileId(compileId); entity.setCreateTime(new Date()); entity.setCreatorId(entity.getCurrentUser().getId()); mailCfgDao.saveCfgIndex(entity); if(entity.getIpPortList()!=null){ for(IpPortCfg cfg:entity.getIpPortList()){ BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); mailCfgDao.saveIpPortCfg(cfg); } } if(entity.getComplexList()!=null){ for(ComplexkeywordCfg cfg:entity.getComplexList()){ if(StringUtils.isNotBlank(cfg.getCfgKeywords())){ BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); if(StringUtils.isNoneBlank(cfg.getDistrictShowName()) && cfg.getDistrict().equals("others")){ cfg.setDistrict(cfg.getDistrictShowName()); } mailCfgDao.saveMailKeywordCfg(cfg); } } } if(entity.getDigestList()!=null){ for(FileDigestCfg cfg:entity.getDigestList()){ BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); if(result!=null && result.getData()!=null){ cfg.setRawLen(result.getData().getRawLen()); cfg.setDigest(result.getData().getDigest()); cfg.setFileUrl(result.getData().getAccessUrl()); } mailCfgDao.saveMailFileDigestCfg(cfg); } } if(entity.getNtcSubscribeIdCfgList()!=null){ for(NtcSubscribeIdCfg cfg:entity.getNtcSubscribeIdCfgList()){ if(StringUtils.isNotBlank(cfg.getCfgKeywords())){ BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); stringCfgDao.saveSubscribeIdCfg(cfg); } } } //保存区域IP信息 if(entity.getAreaCfg()!=null){ for(AreaIpCfg cfg:entity.getAreaCfg()){ cfg.initDefaultValue(); BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); areaIpCfgDao.saveAreaIpCfg(cfg); } } //处理定时任务 handelScheduleCfg(entity, entity.getIndexTable(), entity); if(isValid==1) { entity.setIsAudit(1); entity.setIsValid(1); auditMailCfg(entity, entity.getIsAudit(),Constants.INSERT_ACTION); } }else{ entity.setEditorId(entity.getCurrentUser().getId()); entity.setEditTime(new Date()); mailCfgDao.updateCfgIndex(entity); //无效子配置后,再新增子配置 mailCfgDao.deleteMailIpCfg(entity); mailCfgDao.deleteMailKeywordCfg(entity); mailCfgDao.deleteMailFileDigestCfg(entity); stringCfgDao.deleteSubscribeIdCfgByCfgIndexInfo(entity); AreaIpCfg area = new AreaIpCfg(); area.setCompileId(entity.getCompileId()); area.setFunctionId(entity.getFunctionId()); areaIpCfgDao.deleteAreaIpCfg(area); entity.setCreateTime(new Date()); entity.setCreatorId(entity.getCurrentUser().getId()); if(entity.getIpPortList()!=null){ for(IpPortCfg cfg:entity.getIpPortList()){ BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); mailCfgDao.saveIpPortCfg(cfg); } } if(entity.getComplexList()!=null){ for(ComplexkeywordCfg cfg:entity.getComplexList()){ if(StringUtils.isNotBlank(cfg.getCfgKeywords())){ BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); if(StringUtils.isNoneBlank(cfg.getDistrictShowName()) && cfg.getDistrict().equals("others")){ cfg.setDistrict(cfg.getDistrictShowName()); } mailCfgDao.saveMailKeywordCfg(cfg); } } } if(entity.getDigestList()!=null){ for(FileDigestCfg cfg:entity.getDigestList()){ BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); if(result!=null && result.getData()!=null){ cfg.setRawLen(result.getData().getRawLen()); cfg.setDigest(result.getData().getDigest()); cfg.setFileUrl(result.getData().getAccessUrl()); } mailCfgDao.saveMailFileDigestCfg(cfg); } } if(entity.getNtcSubscribeIdCfgList()!=null){ for(NtcSubscribeIdCfg cfg:entity.getNtcSubscribeIdCfgList()){ if(StringUtils.isNotBlank(cfg.getCfgKeywords())){ BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); stringCfgDao.saveSubscribeIdCfg(cfg); } } } //保存区域IP信息 if(entity.getAreaCfg()!=null){ for(AreaIpCfg cfg:entity.getAreaCfg()){ cfg.initDefaultValue(); BeanUtils.copyProperties(entity, cfg,new String[]{"cfgRegionCode","cfgType"}); areaIpCfgDao.saveAreaIpCfg(cfg); } } //处理定时任务 handelScheduleCfg(entity, entity.getIndexTable(), entity); if(isValid==1) { entity.setIsAudit(1); entity.setIsValid(1); auditMailCfg(entity, entity.getIsAudit(),Constants.INSERT_ACTION); } } } public void updateMailCfgValid(Integer isValid,String ids,Integer functionId){ String[] idArray = ids.split(","); for(String id :idArray){ CfgIndexInfo entity = new CfgIndexInfo(); entity.setCfgId(Long.parseLong(id)); entity.setIsValid(isValid); entity.setEditorId(UserUtils.getUser().getId()); entity.setEditTime(new Date()); entity.setTableName(CfgIndexInfo.getTablename()); entity.setFunctionId(functionId); mailCfgDao.updateCfgValid(entity); //查询子配置 entity = this.getMailCfg(Long.parseLong(id),entity.getCompileId()); if(entity.getIpPortList()!=null && entity.getIpPortList().size()>0){ IpPortCfg cfg = new IpPortCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(IpPortCfg.getTablename()); mailCfgDao.updateCfgValid(cfg); } if(entity.getComplexList()!=null && entity.getComplexList().size()>0) { MailKeywordCfg cfg = new MailKeywordCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(MailKeywordCfg.getTablename()); mailCfgDao.updateCfgValid(cfg); } if(entity.getNtcSubscribeIdCfgList()!=null && entity.getNtcSubscribeIdCfgList().size()>0) { NtcSubscribeIdCfg cfg = new NtcSubscribeIdCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(NtcSubscribeIdCfg.getTablename()); mailCfgDao.updateCfgValid(cfg); } //保存区域IP信息 if(entity.getAreaCfg()!=null && entity.getAreaCfg().size()>0){ AreaIpCfg cfg = new AreaIpCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(AreaIpCfg.getTablename()); mailCfgDao.updateCfgValid(cfg); } //处理定时任务 handelScheduleCfg(entity, entity.getIndexTable(), entity); } } public void auditMailCfg(CfgIndexInfo entity,Integer isAudit,Integer opAction) throws MaatConvertException{ //修改数据库审核状态信息 entity.setTableName(CfgIndexInfo.getTablename()); entity.setIsAudit(isAudit); entity.setAuditorId(UserUtils.getUser().getId()); entity.setAuditTime(new Date()); mailCfgDao.auditCfg(entity); ToMaatBean maatBean = new ToMaatBean(); MaatCfg maatCfg = new MaatCfg(); List configCompileList = new ArrayList(); List groupRelationList = new ArrayList(); List ipRegionList = new ArrayList(); List strRegionList = new ArrayList(); List numRegionList = new ArrayList(); List digestRegionList = new ArrayList(); List areaIpRegionList = new ArrayList(); //查询子配置并修改审核状态 entity = this.getMailCfg(entity.getCfgId(),entity.getCompileId()); if(entity.getIpPortList()!=null && entity.getIpPortList().size()>0){ IpPortCfg cfg = new IpPortCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(IpPortCfg.getTablename()); mailCfgDao.auditCfg(cfg); if(isAudit==1){ Map map = cfgConvert(ipRegionList,entity.getIpPortList(),1,entity,groupRelationList); groupRelationList=map.get("groupList"); ipRegionList=map.get("dstList"); if(map.get("numRegionList")!=null){ numRegionList.addAll(map.get("numRegionList")); } } } if(entity.getComplexList()!=null && entity.getComplexList().size()>0){ MailKeywordCfg cfg = new MailKeywordCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(MailKeywordCfg.getTablename()); mailCfgDao.auditCfg(cfg); if(isAudit==1){ Map map = cfgConvert(strRegionList,entity.getComplexList(),2,entity,groupRelationList); groupRelationList=map.get("groupList"); strRegionList=map.get("dstList"); } } if(entity.getDigestList()!=null && entity.getDigestList().size()>0){ FileDigestCfg cfg = new FileDigestCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(FileDigestCfg.getTablename()); mailCfgDao.auditCfg(cfg); if(isAudit==1){ Map map = cfgConvert(digestRegionList,entity.getDigestList(),5,entity,groupRelationList); groupRelationList=map.get("groupList"); digestRegionList=map.get("dstList"); } } if(entity.getNtcSubscribeIdCfgList()!=null && entity.getNtcSubscribeIdCfgList().size()>0){ NtcSubscribeIdCfg cfg = new NtcSubscribeIdCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(NtcSubscribeIdCfg.getTablename()); mailCfgDao.auditCfg(cfg); if(isAudit==1){ Map map = cfgConvert(strRegionList,entity.getNtcSubscribeIdCfgList(),2,entity,groupRelationList); groupRelationList=map.get("groupList"); strRegionList=map.get("dstList"); } } //保存区域IP信息 if(entity.getAreaCfg()!=null && entity.getAreaCfg().size()>0){ AreaIpCfg cfg = new AreaIpCfg(); BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"}); cfg.setTableName(AreaIpCfg.getTablename()); mailCfgDao.auditCfg(cfg); if(isAudit==1){ Map map = cfgConvert(areaIpRegionList,entity.getAreaCfg(),1,entity,groupRelationList); groupRelationList=map.get("groupList"); areaIpRegionList=map.get("dstList"); } } if(isAudit!=1){ //处理定时任务 handelScheduleCfg(entity, entity.getIndexTable(), entity); } //构造提交综合服务参数格式,一条配置提交一次综合服务 if(isAudit==1){ maatCfg.initDefaultValue(); BeanUtils.copyProperties(entity, maatCfg); maatCfg.setAction(entity.getAction()); maatCfg.setAuditTime(entity.getAuditTime()); maatCfg.setIpRegionList(ipRegionList); maatCfg.setStrRegionList(strRegionList); maatCfg.setNumRegionList(numRegionList); maatCfg.setDigestRegionList(digestRegionList); maatCfg.setGroupRelationList(groupRelationList); maatCfg.setGroupNum(groupRelationList.size()); maatCfg.setAreaIpRegionList(areaIpRegionList); maatCfg.setIsValid(entity.getIsValid()); configCompileList.add(maatCfg); maatBean.setConfigCompileList(configCompileList); maatBean.setAuditTime(entity.getAuditTime()); maatBean.setCreatorName(entity.getCurrentUser().getName()); maatBean.setVersion(Constants.MAAT_VERSION); maatBean.setOpAction(opAction); //调用服务接口下发配置数据 String json=gsonToJson(maatBean); logger.info("mail配置下发配置参数:"+json); //调用服务接口下发配置 ToMaatResult result = ConfigServiceUtil.postMaatCfg(json); logger.info("mail配置下发响应信息:"+result.getMsg()); }else if(isAudit==3){ maatCfg.setCompileId(entity.getCompileId()); maatCfg.setServiceId(entity.getServiceId()); maatCfg.setIsValid(0);//无效 configCompileList.add(maatCfg); maatBean.setConfigCompileList(configCompileList); maatBean.setAuditTime(entity.getAuditTime()); maatBean.setCreatorName(entity.getCurrentUser().getName()); maatBean.setVersion(Constants.MAAT_VERSION); maatBean.setOpAction(Constants.UPDATE_ACTION); //调用服务接口取消配置 String json=gsonToJson(maatBean); logger.info("mail配置下发配置参数:"+json); //调用服务接口下发配置 ToMaatResult result = ConfigServiceUtil.put(json,1); logger.info("mail配置取消配置响应信息:"+result.getMsg()); } } }