|
@@ -2,67 +2,51 @@ package com.ym.mec.biz.service.impl;
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.dayaedu.cbs.common.enums.music.EMusicAvailableType;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.dayaedu.cbs.domain.wrapper.MusicSheetWrapper;
|
|
|
import com.dayaedu.cbs.openfeign.client.MusicFeignClientService;
|
|
|
import com.dayaedu.cbs.openfeign.wrapper.music.CbsMusicSheetApplicationExtendClientWrapper;
|
|
|
import com.dayaedu.cbs.openfeign.wrapper.music.CbsMusicSheetWrapper;
|
|
|
import com.microsvc.toolkit.common.response.template.R;
|
|
|
-import com.ym.mec.biz.dal.dao.SysMusicScoreAccompanimentDao;
|
|
|
import com.ym.mec.biz.dal.dao.SysMusicScoreCategoriesDao;
|
|
|
import com.ym.mec.biz.dal.dao.SysMusicScoreDao;
|
|
|
-import com.ym.mec.biz.dal.dto.MusicScoreDto;
|
|
|
import com.ym.mec.biz.dal.dto.ReasonDto;
|
|
|
import com.ym.mec.biz.dal.entity.SysMusicScore;
|
|
|
-import com.ym.mec.biz.dal.entity.SysMusicScoreAccompaniment;
|
|
|
import com.ym.mec.biz.dal.entity.SysMusicScoreCategories;
|
|
|
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
|
|
|
import com.ym.mec.biz.dal.page.SysExamSongQueryInfo;
|
|
|
-import com.ym.mec.biz.service.*;
|
|
|
+import com.ym.mec.biz.service.OrganizationService;
|
|
|
+import com.ym.mec.biz.service.SysMusicScoreCategoriesService;
|
|
|
+import com.ym.mec.biz.service.SysMusicScoreService;
|
|
|
import com.ym.mec.common.dal.BaseDAO;
|
|
|
-import com.ym.mec.common.entity.UploadReturnBean;
|
|
|
import com.ym.mec.common.exception.BizException;
|
|
|
import com.ym.mec.common.page.PageInfo;
|
|
|
-import com.ym.mec.common.service.IdGeneratorService;
|
|
|
import com.ym.mec.common.service.impl.BaseServiceImpl;
|
|
|
import com.ym.mec.util.collection.MapUtil;
|
|
|
-import com.ym.mec.util.file.FileUtil;
|
|
|
-import com.ym.mec.util.shell.ScriptUtil;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.URL;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.LinkedBlockingQueue;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
-import static com.ym.mec.biz.dal.enums.ClientTypeEnum.SMART_PRACTICE;
|
|
|
-
|
|
|
@Service
|
|
|
public class SysMusicScoreServiceImpl extends BaseServiceImpl<Integer, SysMusicScore> implements SysMusicScoreService {
|
|
|
|
|
|
@Resource
|
|
|
private SysMusicScoreDao sysMusicScoreDao;
|
|
|
@Resource
|
|
|
- private SysMusicScoreAccompanimentDao sysMusicScoreAccompanimentDao;
|
|
|
- @Resource
|
|
|
- private SysUserService sysUserService;
|
|
|
- @Resource
|
|
|
private SysMusicScoreCategoriesDao sysMusicScoreCategoriesDao;
|
|
|
@Resource
|
|
|
private SysMusicScoreCategoriesService sysMusicScoreCategoriesService;
|
|
|
@Resource
|
|
|
- private IdGeneratorService idGeneratorService;
|
|
|
- @Resource
|
|
|
- private UploadFileService uploadFileService;
|
|
|
- @Resource
|
|
|
private OrganizationService organizationService;
|
|
|
@Resource
|
|
|
private MusicFeignClientService musicFeignClientService;
|
|
@@ -80,64 +64,6 @@ public class SysMusicScoreServiceImpl extends BaseServiceImpl<Integer, SysMusicS
|
|
|
return sysMusicScoreDao;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void add(MusicScoreDto musicScoreDto) {
|
|
|
- SysMusicScore sysMusicScore = musicScoreDto.getSysMusicScore();
|
|
|
- sysMusicScore.setCreateUserId(sysUserService.getUserId());
|
|
|
- sysMusicScoreDao.insert(sysMusicScore);
|
|
|
- List<SysMusicScoreAccompaniment> sysMusicScoreAccompaniments = musicScoreDto.getSysMusicScoreAccompaniments();
|
|
|
- if (CollectionUtils.isNotEmpty(sysMusicScoreAccompaniments)) {
|
|
|
- sysMusicScoreAccompaniments.forEach(e -> {
|
|
|
- e.setIsOpenMetronome(sysMusicScore.getIsOpenMetronome());
|
|
|
- });
|
|
|
- sysMusicScoreAccompanimentDao.batchInsert(sysMusicScoreAccompaniments, sysMusicScore.getId(), sysMusicScore.getIsOpenMetronome());
|
|
|
- }
|
|
|
- Integer categoriesId = sysMusicScore.getMusicScoreCategoriesId();
|
|
|
- if (categoriesId != null && sysMusicScore.getClientType() == SMART_PRACTICE) {
|
|
|
- while (true) {
|
|
|
- SysMusicScoreCategories categories = sysMusicScoreCategoriesDao.get(categoriesId);
|
|
|
- if (categories.getParentId() == 0) {
|
|
|
- categories.setMusicScoreNum(categories.getMusicScoreNum() + 1);
|
|
|
- sysMusicScoreCategoriesDao.update(categories);
|
|
|
- return;
|
|
|
- }
|
|
|
- categoriesId = categories.getParentId();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void del(Integer id) {
|
|
|
- SysMusicScore sysMusicScore = sysMusicScoreDao.get(id);
|
|
|
- if (sysMusicScore == null) {
|
|
|
- throw new BizException("曲目不存在");
|
|
|
- }
|
|
|
- Integer userId = sysUserService.getUserId();
|
|
|
- if (!Objects.equals(sysMusicScore.getCreateUserId(), userId)) {
|
|
|
- throw new BizException("无权限删除");
|
|
|
- }
|
|
|
- sysMusicScoreDao.delete(id);
|
|
|
- sysMusicScoreAccompanimentDao.deleteBySongId(id);
|
|
|
- Integer categoriesId = sysMusicScore.getMusicScoreCategoriesId();
|
|
|
- if (categoriesId != null && sysMusicScore.getClientType() == SMART_PRACTICE) {
|
|
|
- while (true) {
|
|
|
- SysMusicScoreCategories categories = sysMusicScoreCategoriesDao.get(categoriesId);
|
|
|
- if (categories.getParentId() == 0) {
|
|
|
- Integer musicScoreNum = categories.getMusicScoreNum() - 1;
|
|
|
- if (musicScoreNum < 0) {
|
|
|
- musicScoreNum = 0;
|
|
|
- }
|
|
|
- categories.setMusicScoreNum(musicScoreNum);
|
|
|
- sysMusicScoreCategoriesDao.update(categories);
|
|
|
- return;
|
|
|
- }
|
|
|
- categoriesId = categories.getParentId();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private String getParentTreeName(Integer categoriesId) {
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
while (true) {
|
|
@@ -363,77 +289,50 @@ public class SysMusicScoreServiceImpl extends BaseServiceImpl<Integer, SysMusicS
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 进行转码
|
|
|
- *
|
|
|
- * @param fileUrl
|
|
|
- * @return
|
|
|
- */
|
|
|
- private String doTranscod(String fileUrl) {
|
|
|
- String transcodUrl = "";
|
|
|
- if (StringUtils.isEmpty(fileUrl)) {
|
|
|
- return transcodUrl;
|
|
|
+ @Override
|
|
|
+ public void sync() {
|
|
|
+ Date date = new Date();
|
|
|
+ // 100个线程的无界线程池
|
|
|
+ ThreadPoolExecutor executorService = new ThreadPoolExecutor(50, 50,
|
|
|
+ 0L, TimeUnit.MILLISECONDS,
|
|
|
+ new LinkedBlockingQueue<>());
|
|
|
+// while (true) {
|
|
|
+ IPage<MusicSheet> page = baseMapper.selectSyncPage(new Page<>(1, -1));
|
|
|
+ List<MusicSheet> records = page.getRecords();
|
|
|
+ if (CollectionUtils.isEmpty(records)) {
|
|
|
+ return;
|
|
|
}
|
|
|
- //文件扩展名
|
|
|
- String expand = FileUtil.getFileExpandByPath(fileUrl);
|
|
|
- //将远程文件写入本地临时文件路径
|
|
|
- String tempFileName = getTempFileName(expand);
|
|
|
- File tempFile = new File(tempFileName);
|
|
|
- //转码后文件路径
|
|
|
- String tempTranscodFileName = getTempFileName(expand);
|
|
|
- try {
|
|
|
- URL url = new URL(fileUrl);
|
|
|
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
- //设置超时间为3秒
|
|
|
- conn.setConnectTimeout(3 * 1000);
|
|
|
- //防止屏蔽程序抓取而返回403错误
|
|
|
- conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
-
|
|
|
- FileUtil.inputStreamToFile(conn.getInputStream(), tempFile);
|
|
|
-
|
|
|
- String script = "ffmpeg -i " + tempFileName + " -vn -c:a libmp3lame -b:a 320k -ac 2 " + tempTranscodFileName;
|
|
|
- int i = ScriptUtil.callScript(script);
|
|
|
- if (i == 0) {
|
|
|
- //转码成功
|
|
|
- FileInputStream inputStream = new FileInputStream(tempTranscodFileName);
|
|
|
- UploadReturnBean uploadReturnBean = uploadFileService.uploadFile(inputStream, expand);
|
|
|
- transcodUrl = uploadReturnBean.getUrl();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- //删除服务器中的文件
|
|
|
- if (tempFile.exists()) {
|
|
|
- tempFile.delete();
|
|
|
- }
|
|
|
- File tempTranscodFile = new File(tempTranscodFileName);
|
|
|
- if (tempTranscodFile.exists()) {
|
|
|
- tempTranscodFile.delete();
|
|
|
+ for (MusicSheet record : records) {
|
|
|
+ if (record.getCbsMusicSheetId() != null && !DateUtil.format(record.getCreateTime(),"yyyy-MM-dd HH:mm:ss").equals("2024-06-25 00:00:00")) {
|
|
|
+ continue;
|
|
|
}
|
|
|
- }
|
|
|
- return transcodUrl;
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 通过网络文件路径获取临时文件路径
|
|
|
- *
|
|
|
- * @param fileUrl
|
|
|
- * @return
|
|
|
- */
|
|
|
- String tempPath = "/usr/temp";
|
|
|
-
|
|
|
- private String getTempFileName(String expand) {
|
|
|
- //没有目录则创建
|
|
|
- File file = new File(tempPath);
|
|
|
- if (!file.exists()) {
|
|
|
- file.mkdirs();
|
|
|
+ executorService.submit(() ->
|
|
|
+ {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ syncMusicSheet(record, date);
|
|
|
+ }catch (Exception e) {
|
|
|
+ log.error("曲目同步异常,曲目ID:{}", record.getId(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
}
|
|
|
- //.mp3
|
|
|
- String fileName = tempPath + File.separator + idGeneratorService.generatorId("tempFile");
|
|
|
- if (StringUtils.isNotEmpty(expand)) {
|
|
|
- fileName += "." + expand;
|
|
|
+
|
|
|
+ curPage++;
|
|
|
+// }
|
|
|
+
|
|
|
+ while (!executorService.getQueue().isEmpty()) {
|
|
|
+ try {
|
|
|
+ log.info("线程池中正在执行的任务数量:{},等待数量:{}", executorService.getActiveCount(),executorService.getQueue().size());
|
|
|
+ Thread.sleep(1000);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.error("线程等待异常", e);
|
|
|
+ }
|
|
|
}
|
|
|
- return fileName;
|
|
|
+ log.info("-------------------- 曲目数据同步成功! -------------------------------");
|
|
|
+ executorService.shutdown();
|
|
|
}
|
|
|
|
|
|
private void getAllCategoryIdList(List<Integer> categoryIdList, List<SysMusicScoreCategories> sysMusicScoreCategoriesList) {
|