package com.nis.web.service.configuration; import info.monitorenter.cpdetector.util.FileUtil; 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 java.util.UUID; import net.sf.json.JSONObject; 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.exceptions.MaatConvertException; import com.nis.util.ConfigServiceUtil; import com.nis.util.FileUtils; 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 getAvFileSampleList(Page page, AvFileSampleCfg entity){ // 生成数据权限过滤条件(dsf为dataScopeFilter的简写,在xml中使用 ${sqlMap.dsf}调用权限SQL) entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"a")); entity.setPage(page); List list = avCfgDao.getAvFileSampleList(entity); page.setList(list); return page; } public Page getAvSignSampleList(Page page, AvSignSampleCfg entity){ // 生成数据权限过滤条件(dsf为dataScopeFilter的简写,在xml中使用 ${sqlMap.dsf}调用权限SQL) entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"a")); entity.setPage(page); List list = avCfgDao.getAvSignSampleList(entity); page.setList(list); return page; } public void saveOrUpdateAvFileSample(AvFileSampleCfg entity){ if(entity.getCfgId()==null){ entity.setCreatorId(UserUtils.getUser().getId()); entity.setCreateTime(new Date()); //调用服务接口获取compileId String result = ""; try { result = ConfigServiceUtil.getId(1,1); } catch (Exception e) { e.printStackTrace(); logger.info("获取编译ID出错"); throw new MaatConvertException(":"+e.getMessage()); } Integer compileId = Integer.parseInt(result); if(compileId!=0){ entity.setCompileId(compileId); avCfgDao.insertAvFileSample(entity); }else{ throw new MaatConvertException(""); } }else{ entity.setEditorId(UserUtils.getUser().getId()); entity.setEditTime(new Date()); entity.setIsValid(0); entity.setIsAudit(0); avCfgDao.updateAvFileSample(entity); } } public void saveOrUpdateAvSignSample(AvSignSampleCfg entity){ if(entity.getCfgId()==null){ entity.setCreatorId(UserUtils.getUser().getId()); entity.setCreateTime(new Date()); //调用服务接口获取compileId String result = ""; try { result = ConfigServiceUtil.getId(1,1); } catch (Exception e) { e.printStackTrace(); logger.info("获取编译ID出错"); throw new MaatConvertException(":"+e.getMessage()); } Integer compileId = Integer.parseInt(result); if(compileId!=0){ entity.setCompileId(compileId); avCfgDao.insertAvSignSample(entity); }else{ throw new MaatConvertException(""); } }else{ entity.setEditorId(UserUtils.getUser().getId()); entity.setEditTime(new Date()); entity.setIsValid(0); entity.setIsAudit(0); avCfgDao.updateAvSignSample(entity); } } 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(Integer isAudit,Integer isValid,String ids){ AvFileSampleCfg entity = new AvFileSampleCfg(); String[] idArray = ids.split(","); List list = new ArrayList(); Gson gson=new GsonBuilder().disableHtmlEscaping() .excludeFieldsWithoutExposeAnnotation() .create(); for(String id :idArray){ entity = getAvFileSampleById(Long.parseLong(id)); entity.setIsAudit(isAudit); entity.setIsValid(isValid); entity.setAuditorId(UserUtils.getUser().getId()); entity.setAuditTime(new Date()); if(isAudit==1){ //音视频文件上传接口调用 try { File srcFile = new File(entity.getSrcPath()); Map srcMap = new HashMap(); srcMap.put("filetype", FileUtils.getSuffix(srcFile.getName(), false)); srcMap.put("createTime", entity.getCreateTime()); srcMap.put("key",FileUtils.getPrefix(srcFile.getName(), false)); srcMap.put("fileName", srcFile.getName()); srcMap.put("checksum", entity.getSrcMd5()); String result1 = ConfigServiceUtil.postFileCfg(null, srcFile, JSONObject.fromObject(srcMap)); logger.info("音视频源文件上传响应信息:"+result1); //获取文件上传响应信息(文件访问路径) Map srcRes = gson.fromJson(result1,Map.class); entity.setSrcUrl(srcRes.get("data").toString()); File smapleFile = new File(entity.getSamplePath()); Map sampleMap = new HashMap(); sampleMap.put("filetype", FileUtils.getSuffix(smapleFile.getName(), false)); sampleMap.put("createTime", entity.getCreateTime()); sampleMap.put("key",FileUtils.getPrefix(smapleFile.getName(), false)); sampleMap.put("fileName", smapleFile.getName()); sampleMap.put("checksum", entity.getSampleMd5()); String result2 = ConfigServiceUtil.postFileCfg(null, srcFile, JSONObject.fromObject(sampleMap)); logger.info("音视频样例文件上传响应信息:"+result2); Map sampleRes = gson.fromJson(result1,Map.class); //获取文件上传响应信息(文件访问路径) entity.setSampleUrl(sampleRes.get("data").toString()); } catch (Exception e) { e.printStackTrace(); logger.info("音视频文件样例配置下发失败"); throw new MaatConvertException(":"+e.getMessage()); } } avCfgDao.auditAvFileSample(entity); list.add(entity); } if(isAudit==1){ //调用服务接口下发配置数据 String json=gson.toJson(list); logger.info("音视频文件样例下发配置参数:"+json); //调用服务接口下发配置 try { String result = ConfigServiceUtil.postCallbackCfg(json); logger.info("音视频文件样例配置下发响应信息:"+result); } catch (Exception e) { e.printStackTrace(); logger.info("音视频文件样例配置下发失败"); throw new MaatConvertException(":"+e.getMessage()); } }else if(isAudit==3){ //调用服务接口取消配置 String json=gson.toJson(list); logger.info("音视频文件样例下发配置参数:"+json); //调用服务接口下发配置 try { String result = ConfigServiceUtil.put(json,2); logger.info("音视频文件样例取消配置响应信息:"+result); } catch (Exception e) { e.printStackTrace(); logger.info("音视频文件样取消配置失败"); throw new MaatConvertException(":"+e.getMessage()); } } } 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 auditAvSignSample(Integer isAudit,Integer isValid,String ids){ AvSignSampleCfg entity = new AvSignSampleCfg(); String[] idArray = ids.split(","); List list = new ArrayList(); Gson gson=new GsonBuilder().disableHtmlEscaping() .excludeFieldsWithoutExposeAnnotation() .create(); for(String id :idArray){ entity = getAvSignSampleById(Long.parseLong(id)); entity.setIsAudit(isAudit); entity.setIsValid(isValid); entity.setAuditorId(UserUtils.getUser().getId()); entity.setAuditTime(new Date()); avCfgDao.auditAvSignSample(entity); list.add(entity); } if(isAudit==1){ //调用服务接口下发配置数据 String json=gson.toJson(list); logger.info("文件样例下发配置参数:"+json); //调用服务接口下发配置 try { String result = ConfigServiceUtil.postCallbackCfg(json); logger.info("音视频标志样例配置下发响应信息:"+result); } catch (Exception e) { e.printStackTrace(); logger.info("音视频标志样例配置下发失败"); throw new MaatConvertException(":"+e.getMessage()); } }else if(isAudit==3){ //调用服务接口取消配置 String json=gson.toJson(list); logger.info("标志样例下发配置参数:"+json); //调用服务接口取消配置 try { String result = ConfigServiceUtil.put(json, 2); logger.info("音视频标志样例配置取消配置响应信息:"+result); } catch (Exception e) { e.printStackTrace(); logger.info("音视频标志样例取消配置失败"); throw new MaatConvertException(":"+e.getMessage()); } } } }