This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/web/service/configuration/AvCfgService.java

431 lines
15 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.nis.web.service.configuration;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.nis.domain.Page;
import com.nis.domain.configuration.AvFileSampleCfg;
import com.nis.domain.configuration.AvSignSampleCfg;
import com.nis.domain.maat.ToMaatResult;
import com.nis.domain.maat.ToMaatResult.ResponseData;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.ConfigServiceUtil;
import com.nis.util.Constants;
import com.nis.util.FileUtils;
import com.nis.util.StringUtil;
import com.nis.web.dao.configuration.AvCfgDao;
import com.nis.web.security.UserUtils;
import com.nis.web.service.BaseService;
/**
* 音视频配置相关事务类
* @author dell
*
*/
@Service
public class AvCfgService extends BaseService{
// @Autowired
// protected FunctionRegionDictDao functionRegionDictDao;
// @Autowired
// protected FunctionServiceDictDao functionServiceDictDao;
@Autowired
protected AvCfgDao avCfgDao;
public AvFileSampleCfg getAvFileSampleById(Long cfgId){
return avCfgDao.getAvFileSampleById(cfgId);
}
public AvSignSampleCfg getAvSignSampleById(Long cfgId){
return avCfgDao.getAvSignSampleById(cfgId);
}
public Page<AvFileSampleCfg> getAvFileSampleList(Page<AvFileSampleCfg> page, AvFileSampleCfg entity){
// 生成数据权限过滤条件dsf为dataScopeFilter的简写在xml中使用 ${sqlMap.dsf}调用权限SQL
entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"a"));
entity.setPage(page);
List<AvFileSampleCfg> list = avCfgDao.getAvFileSampleList(entity);
page.setList(list);
return page;
}
public Page<AvSignSampleCfg> getAvSignSampleList(Page<AvSignSampleCfg> page, AvSignSampleCfg entity){
// 生成数据权限过滤条件dsf为dataScopeFilter的简写在xml中使用 ${sqlMap.dsf}调用权限SQL
entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"a"));
entity.setPage(page);
List<AvSignSampleCfg> list = avCfgDao.getAvSignSampleList(entity);
page.setList(list);
return page;
}
public List<AvSignSampleCfg> getSignSampleList(AvSignSampleCfg entity){
List<AvSignSampleCfg> list = avCfgDao.getAvSignSampleList(entity);
return list;
}
public void saveOrUpdateAvFileSample(AvFileSampleCfg entity){
//设置区域运营商信息
setAreaEffectiveIds(entity);
if(entity.getCfgId()==null){
entity.setCreatorId(UserUtils.getUser().getId());
entity.setCreateTime(new Date());
//调用服务接口获取compileId
Integer compileId = 0;
try {
List<Integer> compileIds = ConfigServiceUtil.getId(1,1);
if(!StringUtil.isEmpty(compileIds)){
compileId = compileIds.get(0);
}
} catch (Exception e) {
e.printStackTrace();
logger.info("获取编译ID出错");
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+e.getMessage());
}
if(compileId!=0){
entity.setCompileId(compileId);
entity.setIsSampleCreated(0);
File uploadSrcFile = new File(entity.getSrcPath());
String srcMd5 = FileUtils.getFileMD5(uploadSrcFile);
entity.setSrcMd5(srcMd5);
avCfgDao.insertAvFileSample(entity);
//调用外部脚本,生成样例文件
entity = createSampleFileParam(entity);
avCfgDao.updateAvFileSample(entity);
}else{
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
}else{
AvFileSampleCfg oldEntity = this.getAvFileSampleById(entity.getCfgId());
entity.setEditorId(UserUtils.getUser().getId());
entity.setEditTime(new Date());
entity.setIsValid(0);
entity.setIsAudit(0);
if(!oldEntity.getSrcMd5().equals(entity.getSrcMd5())){
//删除旧的文件
FileUtils.deleteFile(oldEntity.getSrcPath());
FileUtils.deleteFile(oldEntity.getSamplePath());
//调用外部脚本,生成样例文件
entity = createSampleFileParam(entity);
}
avCfgDao.updateAvFileSample(entity);
}
}
/**
* 处理创建样例文件所需输入输出参数
* @param entity
* @return
*/
public AvFileSampleCfg createSampleFileParam(AvFileSampleCfg entity){
List list = new ArrayList();
Map<String,Object> map = new HashMap();
map.put("srcFile", entity.getSrcPath());
map.put("dstFile", entity.getSamplePath());
map.put("resultFile", entity.getResultPath());
map.put("fileId", entity.getCfgId());
list.add(map);
String param = gsonToJson(list);
String sampleCreatelProc = "";
if(entity.getCfgType().equals(Constants.AV_SAMPLE_AUDIO_REGION)||
entity.getCfgType().equals(Constants.AV_SAMPLE_VOIP_REGION)){//音频样例配置或VOIP样例配置
sampleCreatelProc = Constants.AUDIO_SAMPLE_CREATE_PROC;
if(Constants.AUDIO_SAMPLE_PROC_PARAM_IS_TRANSLATION){
param = StringEscapeUtils.escapeJava(param);
}
if(Constants.AUDIO_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
param = "'"+param+"'";
}
}else if(entity.getCfgType().equals(Constants.AV_SAMPLE_VIDEO_REGION)){//视频样例配置
sampleCreatelProc = Constants.VIDEO_SAMPLE_CREATE_PROC;
if(Constants.VIDEO_SAMPLE_PROC_PARAM_IS_TRANSLATION){
param = StringEscapeUtils.escapeJava(param);
}
if(Constants.VIDEO_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
param = "'"+param+"'";
}
}else if(entity.getCfgType().equals(Constants.AV_SAMPLE_PICTURE_REGION)){//图片样例配置
sampleCreatelProc = Constants.PICTURE_SAMPLE_CREATE_PROC;
if(Constants.PICTURE_SAMPLE_PROC_PARAM_IS_TRANSLATION){
param = StringEscapeUtils.escapeJava(param);
}
if(Constants.PICTURE_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
param = "'"+param+"'";
}
}
logger.info("调用外部程序输入参数:"+param);
Map resultMap = execShell(sampleCreatelProc,param);
logger.info("调用外部程序结果:"+resultMap);
if(resultMap.get("exitStatus").equals(0)){//调用外部程序成功
String out = resultMap.get("out").toString();//输出参数
JSONArray resArray =JSONArray.fromObject(out);
JSONObject resObject = resArray.getJSONObject(0);
logger.info("调用外部程序输出参数:"+resObject);
int state = resObject.getInt("state");
int fileSize = resObject.getInt("fileSize");
Integer fileId = resObject.getInt("fileId");
if(state==1 && fileSize>0){//成功
entity.setIsSampleCreated(1);//样例文件创建成功
}else{
entity.setIsSampleCreated(-1);//样例文件创建失败
}
if(fileId.equals(entity.getCfgId().intValue())){
File uploadSampleFile = new File(entity.getSamplePath());
String sampleMd5 = FileUtils.getFileMD5(uploadSampleFile);
entity.setSampleMd5(sampleMd5);
}else{
logger.info("调用外部程序返回fileId错误,输入的fileId为"+entity.getCfgId()+",输出的fileId为"+fileId);
}
}else{
throw new MaatConvertException(resultMap.get("error")!=null?resultMap.get("error").toString():resultMap.get("message").toString());
}
return entity;
}
public void saveOrUpdateAvSignSample(AvSignSampleCfg entity){
//设置区域运营商信息
setAreaEffectiveIds(entity);
if(entity.getCfgId()==null){
entity.setCreatorId(UserUtils.getUser().getId());
entity.setCreateTime(new Date());
//调用服务接口获取compileId
Integer compileId = 0;
try {
List<Integer> compileIds = ConfigServiceUtil.getId(1,1);
if(!StringUtil.isEmpty(compileIds)){
compileId = compileIds.get(0);
}
} catch (Exception e) {
e.printStackTrace();
logger.info("获取编译ID出错");
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+e.getMessage());
}
if(compileId!=0){
entity.setCompileId(compileId);
avCfgDao.insertAvSignSample(entity);
}else{
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
}
}else{
entity.setEditorId(UserUtils.getUser().getId());
entity.setEditTime(new Date());
entity.setIsValid(0);
entity.setIsAudit(0);
avCfgDao.updateAvSignSample(entity);
}
//添加即时生效
entity.setIsValid(1);
entity.setIsAudit(1);
audioAuditAvSignSample(entity,1);
}
public void updateAvFileSampleValid(Integer isAudit,Integer isValid,String ids){
AvFileSampleCfg entity = new AvFileSampleCfg();
String[] idArray = ids.split(",");
for(String id :idArray){
entity.setCfgId(Long.parseLong(id));
entity.setIsAudit(isAudit);
entity.setIsValid(isValid);
entity.setEditorId(UserUtils.getUser().getId());
entity.setEditTime(new Date());
avCfgDao.updateAvFileSampleValid(entity);
}
}
public void auditAvFileSample(AvFileSampleCfg entity,Integer isAudit) throws MaatConvertException{
//修改数据库审核状态信息
avCfgDao.auditAvFileSample(entity);
List<AvFileSampleCfg> list = new ArrayList<AvFileSampleCfg>();
//一条配置提交一次综合服务
if(isAudit==1){
list.add(entity);
//调用服务接口下发配置数据
String json=gsonToJson(list);
logger.info("音视频文件样例下发配置参数:"+json);
//调用服务接口下发配置
ToMaatResult result = ConfigServiceUtil.postCallbackCfg(json);
logger.info("音视频文件样例配置下发响应信息:"+result.getMsg());
}else if(isAudit==3){
AvFileSampleCfg cfg = new AvFileSampleCfg();
cfg.setIsValid(0);
cfg.setCompileId(entity.getCompileId());
cfg.setServiceId(entity.getServiceId());
list.add(cfg);
//调用服务接口取消配置
String json=gsonToJson(list);
logger.info("音视频文件样例下发配置参数:"+json);
//调用服务接口下发配置
ToMaatResult result = ConfigServiceUtil.put(json,2);
logger.info("音视频文件样例取消配置响应信息:"+result.getMsg());
}
}
public void updateAvSignSampleValid(Integer isAudit,Integer isValid,String ids){
AvSignSampleCfg entity = new AvSignSampleCfg();
String[] idArray = ids.split(",");
for(String id :idArray){
entity.setCfgId(Long.parseLong(id));
entity.setIsAudit(isAudit);
entity.setIsValid(isValid);
entity.setEditorId(UserUtils.getUser().getId());
entity.setEditTime(new Date());
avCfgDao.updateAvSignSampleValid(entity);
}
}
public void audioAuditAvSignSample(AvSignSampleCfg entity,Integer isAudit) throws MaatConvertException{
avCfgDao.auditAvSignSample(entity);
List<AvSignSampleCfg> list = new ArrayList<AvSignSampleCfg>();
if(isAudit==1){
list.add(entity);
//调用服务接口下发配置数据
String json=gsonToJson(list);
logger.info("文件样例下发配置参数:"+json);
//调用服务接口下发配置
ToMaatResult result = ConfigServiceUtil.postCallbackCfg(json);
logger.info("音视频标志样例配置下发响应信息:"+result.getMsg());
}else if(isAudit==3){
AvSignSampleCfg cfg = new AvSignSampleCfg();
cfg.setIsValid(0);
cfg.setCompileId(entity.getCompileId());
cfg.setServiceId(entity.getServiceId());
list.add(cfg);
//调用服务接口取消配置
String json=gsonToJson(list);
logger.info("标志样例下发配置参数:"+json);
//调用服务接口取消配置
ToMaatResult result = ConfigServiceUtil.put(json, 2);
logger.info("音视频标志样例配置取消配置响应信息:"+result.getMsg());
}
}
public void auditAvSignSample(AvSignSampleCfg entity) throws MaatConvertException{
avCfgDao.updateAvSignSampleValid(entity);
avCfgDao.auditAvSignSample(entity);
List<AvSignSampleCfg> list = new ArrayList<AvSignSampleCfg>();
list.add(entity);
//调用服务接口取消配置
String json=gsonToJson(list);
logger.info("标志状态变更:"+json);
//调用服务接口取消配置
ToMaatResult result = ConfigServiceUtil.put(json, 2);
logger.info("音视频标志样状态变更响应信息:"+result.getMsg());
}
/*public void auditAvSignSample(AvSignSampleCfg entity,Integer isAudit) throws MaatConvertException{
avCfgDao.auditAvSignSample(entity);
List<AvSignSampleCfg> list = new ArrayList<AvSignSampleCfg>();
if(isAudit==1){
list.add(entity);
//调用服务接口下发配置数据
String json=gsonToJson(list);
logger.info("文件样例下发配置参数:"+json);
//调用服务接口下发配置
ToMaatResult result = ConfigServiceUtil.postCallbackCfg(json);
logger.info("音视频标志样例配置下发响应信息:"+result.getMsg());
}else if(isAudit==3){
AvSignSampleCfg cfg = new AvSignSampleCfg();
cfg.setIsValid(0);
cfg.setCompileId(entity.getCompileId());
cfg.setServiceId(entity.getServiceId());
list.add(cfg);
//调用服务接口取消配置
String json=gsonToJson(list);
logger.info("标志样例下发配置参数:"+json);
//调用服务接口取消配置
ToMaatResult result = ConfigServiceUtil.put(json, 2);
logger.info("音视频标志样例配置取消配置响应信息:"+result.getMsg());
}
}*/
/**
* 调用shell脚本 返回运行结果
*
* @param shellName
* @param params
* @return
*/
public Map<String, Object> execShell(String shellName,
String... params) {
Map<String, Object> result = new HashMap<String, Object>();
StringBuilder sb = new StringBuilder();
sb.append(shellName);
for (String temp : params) {
sb.append(" " + temp);
}
String os = System.getProperty("os.name").toLowerCase();
String cmd1 = "";
String cmd2 = "";
if(os.contains("windows")){
cmd1 = "cmd.exe";
cmd2 = "/c";
}else{
cmd1 = "/bin/sh";
cmd2 = "-c";
}
logger.info("调用脚本信息,cmd1:"+cmd1+",cmd2:"+cmd2);
String cmdarray[] = new String[] {cmd1, cmd2 ,sb.toString() };
BufferedReader br = null;
BufferedReader bre = null;
try {
Process exec = Runtime.getRuntime().exec(cmdarray);
exec.getInputStream();
br = new BufferedReader(
new InputStreamReader(exec.getInputStream()));
bre = new BufferedReader(new InputStreamReader(
exec.getErrorStream()));
String s = null;
StringBuilder out = new StringBuilder();
while ((s = br.readLine()) != null) {
out.append(s);
}
result.put("out", out.toString());//输出参数
out.setLength(0);//清空
while ((s = bre.readLine()) != null) {
out.append(s);
}
result.put("error", out.toString());//错误信息
int waitFor = exec.waitFor();
logger.info("调用脚本:"+shellName+",执行返回状态值:"+waitFor);
result.put("exitStatus", waitFor);//执行状态
} catch (Exception e) {
e.printStackTrace();
logger.error("调用 " + shellName + " 脚本异常", e);
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bre != null)
try {
bre.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}