|
@@ -0,0 +1,283 @@
|
|
|
|
|
+package top.lvzhiqiang.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import top.lvzhiqiang.config.WebAppConfig;
|
|
|
|
|
+import top.lvzhiqiang.dto.JavAllInfo;
|
|
|
|
|
+import top.lvzhiqiang.entity.*;
|
|
|
|
|
+import top.lvzhiqiang.mapper.*;
|
|
|
|
|
+import top.lvzhiqiang.service.BgService;
|
|
|
|
|
+import top.lvzhiqiang.util.DateUtils;
|
|
|
|
|
+import top.lvzhiqiang.util.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.io.*;
|
|
|
|
|
+import java.time.Instant;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.ZoneOffset;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Bg ServiceImpl
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author lvzhiqiang
|
|
|
|
|
+ * 2022/4/16 16:10
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class BgServiceImpl implements BgService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private VideoGenresMapper videoGenresMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private VideoCastMapper videoCastMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private VideoInfoCastMapper videoInfoCastMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private VideoInfoGenresMapper videoInfoGenresMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private VideoInfoMapper videoInfoMapper;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private IcodePoolMapper icodePoolMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 初始化骑兵数据
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
|
|
|
|
+ @Async
|
|
|
|
|
+ public void initQibingData() {
|
|
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+ DicCode dicCode = WebAppConfig.dicCodeList.stream().filter(x -> 1 == x.getType() && "qibing_path".equals(x.getCodeKey())).findFirst().get();
|
|
|
|
|
+ if (dicCode == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ String picPath = dicCode.getCodeValue();
|
|
|
|
|
+
|
|
|
|
|
+ JavAllInfo javAllInfo = new JavAllInfo();
|
|
|
|
|
+ getAllFilePaths(picPath, javAllInfo);
|
|
|
|
|
+
|
|
|
|
|
+ saveInfo(javAllInfo);
|
|
|
|
|
+
|
|
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
|
|
+ System.err.println((endTime - startTime) / 1000);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 上传识别码文件
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param is
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
|
|
|
|
+ @Async
|
|
|
|
|
+ public void uploadFile4IdentificationCode(InputStream is) {
|
|
|
|
|
+ List<String> uploadIcodeList = readFromIcodeStream(is);
|
|
|
|
|
+ List<String> icodePoolList = icodePoolMapper.findIcode();
|
|
|
|
|
+
|
|
|
|
|
+ // 过滤库中已存在的
|
|
|
|
|
+ Integer beforeUploadSize = uploadIcodeList.size();
|
|
|
|
|
+ uploadIcodeList.removeAll(icodePoolList);
|
|
|
|
|
+
|
|
|
|
|
+ log.warn("uploadFile4IdentificationCode:beforeUpload={},icodePool={},afterUpload={}", beforeUploadSize, icodePoolList.size(), uploadIcodeList.size());
|
|
|
|
|
+
|
|
|
|
|
+ // 插入
|
|
|
|
|
+ int num = 0;
|
|
|
|
|
+ if (uploadIcodeList.size() > 0) {
|
|
|
|
|
+ num = icodePoolMapper.insertList(uploadIcodeList);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.warn("uploadFile4IdentificationCode:success={}", num);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 递归获取某目录下的所有子目录以及子文件
|
|
|
|
|
+ private void getAllFilePaths(String filePath, JavAllInfo javAllInfo) {
|
|
|
|
|
+ File[] files = new File(filePath).listFiles();
|
|
|
|
|
+ if (files == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int type = 0;
|
|
|
|
|
+ if (filePath.contains("骑兵")) {
|
|
|
|
|
+ type = 1;
|
|
|
|
|
+ } else if (filePath.contains("步兵")) {
|
|
|
|
|
+ type = 2;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (File file : files) {
|
|
|
|
|
+ if (file.isDirectory()) {
|
|
|
|
|
+ // 文件夹
|
|
|
|
|
+ getAllFilePaths(file.getAbsolutePath(), javAllInfo);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ String fileName = file.getName();
|
|
|
|
|
+ if (fileName.endsWith(".jpg") || (fileName.endsWith(".lnk") && fileName.contains(".jpg"))) {
|
|
|
|
|
+ String parentName = file.getParentFile().getName();
|
|
|
|
|
+ // 识别码
|
|
|
|
|
+ String name = fileName.substring(10).replace(".jpg", "").trim();
|
|
|
|
|
+ String[] nameArr = name.split("\\s+");
|
|
|
|
|
+ try {
|
|
|
|
|
+ boolean isMain = false;
|
|
|
|
|
+ if (fileName.endsWith(".jpg")) {
|
|
|
|
|
+ isMain = true;
|
|
|
|
|
+ // 获取正片信息
|
|
|
|
|
+ VideoInfo videoInfo = new VideoInfo();
|
|
|
|
|
+ // 发行日期
|
|
|
|
|
+ String issueDate = fileName.substring(0, 10);
|
|
|
|
|
+ videoInfo.setIssueDate(LocalDate.parse(issueDate, DateUtils.dateFormatter));
|
|
|
|
|
+ videoInfo.setIdentificationCode(nameArr[0]);
|
|
|
|
|
+ // 名称
|
|
|
|
|
+ if (nameArr.length > 1) {
|
|
|
|
|
+ videoInfo.setName(name.substring(nameArr[0].length()).trim());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ videoInfo.setName(nameArr[0]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 类型
|
|
|
|
|
+ videoInfo.setType(type);
|
|
|
|
|
+ // 图片URL
|
|
|
|
|
+ videoInfo.setImgUrl(parentName.concat("/").concat(fileName));
|
|
|
|
|
+ // 创建时间 TODO
|
|
|
|
|
+ // 修改时间
|
|
|
|
|
+ videoInfo.setCreateTime(Instant.ofEpochMilli(file.lastModified()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime());
|
|
|
|
|
+ // 主体是谁
|
|
|
|
|
+ videoInfo.setMainWho(parentName);
|
|
|
|
|
+
|
|
|
|
|
+ javAllInfo.getVideoInfoList().add(videoInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (parentName.contains("类别")) {
|
|
|
|
|
+ // 获取类别
|
|
|
|
|
+ String videoGenres = parentName.replace("(类别)", "");
|
|
|
|
|
+ javAllInfo.getVideoGenresSet().add(videoGenres);
|
|
|
|
|
+
|
|
|
|
|
+ VideoInfoGenres videoInfoGenres = new VideoInfoGenres();
|
|
|
|
|
+ videoInfoGenres.setIdentificationCode(nameArr[0]);
|
|
|
|
|
+ videoInfoGenres.setName(videoGenres);
|
|
|
|
|
+ videoInfoGenres.setType(isMain ? 1 : 2);
|
|
|
|
|
+ javAllInfo.getVideoInfoGenresSet().add(videoInfoGenres);
|
|
|
|
|
+ } else if (parentName.contains("优)")) {
|
|
|
|
|
+ // 获取演员
|
|
|
|
|
+ String videoCast = "";
|
|
|
|
|
+ if (parentName.contains("(男")) {
|
|
|
|
|
+ videoCast = parentName.replace("(男优)", "");
|
|
|
|
|
+ javAllInfo.getVideoCastMap().put(videoCast, "1");
|
|
|
|
|
+ } else if (parentName.contains("(女")) {
|
|
|
|
|
+ videoCast = parentName.replace("(女优)", "");
|
|
|
|
|
+ javAllInfo.getVideoCastMap().put(videoCast, "2");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ VideoInfoCast videoInfoCast = new VideoInfoCast();
|
|
|
|
|
+ videoInfoCast.setIdentificationCode(nameArr[0]);
|
|
|
|
|
+ videoInfoCast.setName(videoCast);
|
|
|
|
|
+ videoInfoCast.setType(isMain ? 1 : 2);
|
|
|
|
|
+ javAllInfo.getVideoInfoCastSet().add(videoInfoCast);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.err.println("error:" + file.getAbsolutePath());
|
|
|
|
|
+ System.err.println("error reason:" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (!fileName.endsWith(".jpg") && !fileName.endsWith(".lnk")) {
|
|
|
|
|
+ String[] nameArr = fileName.substring(0, fileName.lastIndexOf(".")).split("\\s+");
|
|
|
|
|
+ String parentName = file.getParentFile().getName();
|
|
|
|
|
+
|
|
|
|
|
+ javAllInfo.getVideoUrlMap().put(nameArr[1], parentName.concat("/").concat(fileName));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 保存所有文件
|
|
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
|
|
|
|
+ public void saveInfo(JavAllInfo javAllInfo) {
|
|
|
|
|
+ // 删除所有
|
|
|
|
|
+ videoGenresMapper.deleteAll();
|
|
|
|
|
+ videoInfoMapper.deleteAll();
|
|
|
|
|
+ videoCastMapper.deleteAll();
|
|
|
|
|
+ videoGenresMapper.deleteAll();
|
|
|
|
|
+ videoInfoCastMapper.deleteAll();
|
|
|
|
|
+
|
|
|
|
|
+ // 保存分类
|
|
|
|
|
+ Set<String> videoGenresSet = javAllInfo.getVideoGenresSet();
|
|
|
|
|
+ //List<VideoGenres> videoGenresList = new ArrayList<>();
|
|
|
|
|
+ for (String s : videoGenresSet) {
|
|
|
|
|
+ VideoGenres videoGenres = new VideoGenres();
|
|
|
|
|
+ videoGenres.setName(s);
|
|
|
|
|
+ videoGenresMapper.insert(videoGenres);
|
|
|
|
|
+ System.out.println(videoGenres);
|
|
|
|
|
+ //videoGenresList.add(videoGenres);
|
|
|
|
|
+ }
|
|
|
|
|
+ //Map<String, VideoGenres> stringVideoGenresMap = videoGenresList.stream().collect(Collectors.toMap(VideoGenres::getName, Function.identity(), (k1, k2) -> k2));
|
|
|
|
|
+
|
|
|
|
|
+ // 保存演员
|
|
|
|
|
+ Map<String, String> videoCastMap = javAllInfo.getVideoCastMap();
|
|
|
|
|
+ //List<VideoCast> videoCastList = new ArrayList<>();
|
|
|
|
|
+ for (Map.Entry<String, String> entry : videoCastMap.entrySet()) {
|
|
|
|
|
+ VideoCast videoCast = new VideoCast();
|
|
|
|
|
+ videoCast.setName(entry.getKey());
|
|
|
|
|
+ videoCast.setType(Integer.parseInt(entry.getValue()));
|
|
|
|
|
+ videoCastMapper.insert(videoCast);
|
|
|
|
|
+ System.out.println(videoCast);
|
|
|
|
|
+ //videoCastList.add(videoCast);
|
|
|
|
|
+ }
|
|
|
|
|
+ // Map<String, VideoCast> stringVideoCastMap = videoCastList.stream().collect(Collectors.toMap(VideoCast::getName, Function.identity(), (k1, k2) -> k2));
|
|
|
|
|
+
|
|
|
|
|
+ // 保存影片信息
|
|
|
|
|
+ List<VideoInfo> videoInfoList = javAllInfo.getVideoInfoList();
|
|
|
|
|
+ Map<String, String> videoUrlMap = javAllInfo.getVideoUrlMap();
|
|
|
|
|
+ videoInfoList.parallelStream().forEach(e -> {
|
|
|
|
|
+ e.setVideoUrl(videoUrlMap.get(e.getIdentificationCode()));
|
|
|
|
|
+ });
|
|
|
|
|
+ //for (VideoInfo videoInfo : videoInfoList) {
|
|
|
|
|
+ // try {
|
|
|
|
|
+ // videoInfoMapper.insert(videoInfo);
|
|
|
|
|
+ // System.out.println("success:" + videoInfo);
|
|
|
|
|
+ // } catch (Exception e) {
|
|
|
|
|
+ // e.printStackTrace();
|
|
|
|
|
+ // System.out.println("error:" + videoInfo);
|
|
|
|
|
+ // }
|
|
|
|
|
+ //}
|
|
|
|
|
+
|
|
|
|
|
+ int videoInfoCount = videoInfoMapper.insertList(videoInfoList);
|
|
|
|
|
+ System.out.println("videoInfoCount:" + videoInfoCount);
|
|
|
|
|
+
|
|
|
|
|
+ // 保存影片类别关联信息
|
|
|
|
|
+ Set<VideoInfoGenres> videoInfoGenresSet = javAllInfo.getVideoInfoGenresSet();
|
|
|
|
|
+ videoInfoGenresMapper.insertList(videoInfoGenresSet);
|
|
|
|
|
+ // 保存影片类别关联信息
|
|
|
|
|
+ Set<VideoInfoCast> videoInfoCastSet = javAllInfo.getVideoInfoCastSet();
|
|
|
|
|
+ videoInfoCastMapper.insertList(videoInfoCastSet);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<String> readFromIcodeStream(InputStream inputStream) {
|
|
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
|
|
+ BufferedReader br = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ br = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
|
|
+ String line;
|
|
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
|
|
+ if (StringUtils.isNotEmpty(line)) {
|
|
|
|
|
+ list.add(line.trim().toUpperCase());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (br != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ br.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return list.stream().distinct().collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|