VideoInfoServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package top.lvzhiqiang.service.impl;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import org.springframework.scheduling.annotation.Async;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Propagation;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import top.lvzhiqiang.config.WebAppConfig;
  9. import top.lvzhiqiang.dto.JavAllInfo;
  10. import top.lvzhiqiang.entity.*;
  11. import top.lvzhiqiang.mapper.*;
  12. import top.lvzhiqiang.service.VideoInfoService;
  13. import top.lvzhiqiang.util.DateUtils;
  14. import javax.annotation.Resource;
  15. import java.io.File;
  16. import java.time.Instant;
  17. import java.time.LocalDate;
  18. import java.time.ZoneOffset;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22. /**
  23. * 电影信息ServiceImpl
  24. *
  25. * @author lvzhiqiang
  26. * 2022/4/4 15:28
  27. */
  28. @Service
  29. public class VideoInfoServiceImpl extends BaseServiceImpl<Object> implements VideoInfoService {
  30. @Resource
  31. private VideoInfoMapper videoInfoMapper;
  32. @Resource
  33. private VideoGenresMapper videoGenresMapper;
  34. @Resource
  35. private VideoCastMapper videoCastMapper;
  36. @Resource
  37. private VideoInfoCastMapper videoInfoCastMapper;
  38. @Resource
  39. private VideoInfoGenresMapper videoInfoGenresMapper;
  40. /**
  41. * 查询所有
  42. *
  43. * @return
  44. */
  45. @Override
  46. public List<VideoInfo> findAll() {
  47. return videoInfoMapper.findAll();
  48. }
  49. /**
  50. * 分页查询所有
  51. *
  52. * @return
  53. */
  54. @Override
  55. public PageInfo<VideoInfo> findAll(int pageNo, int pageSize) {
  56. PageHelper.startPage(pageNo, pageSize);
  57. List<VideoInfo> videoInfoList = videoInfoMapper.findAll();
  58. PageInfo<VideoInfo> videoInfoPageInfo = new PageInfo<>(videoInfoList);
  59. return videoInfoPageInfo;
  60. }
  61. @Override
  62. public PageInfo<VideoInfo> getVideoInfoPage(Map<String, Object> params) {
  63. // 转换成like
  64. paramsToLike(params, "keyword");
  65. // 分页
  66. paramsToPagination(params);
  67. // 排序
  68. paramsToSort(params);
  69. List<VideoInfo> videoInfoList = videoInfoMapper.getVideoInfoList(params);
  70. PageInfo<VideoInfo> videoInfoPageInfo = new PageInfo<>(videoInfoList);
  71. return videoInfoPageInfo;
  72. }
  73. /**
  74. * 初始化数据
  75. */
  76. @Override
  77. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  78. @Async
  79. public void initData() {
  80. long startTime = System.currentTimeMillis();
  81. DicCode dicCode = WebAppConfig.dicCodeList.stream().filter(x -> 1 == x.getType() && "pic_path".equals(x.getCodeKey())).findFirst().get();
  82. if (dicCode == null) {
  83. return;
  84. }
  85. String picPath = dicCode.getCodeValue();
  86. JavAllInfo javAllInfo = new JavAllInfo();
  87. getAllFilePaths(picPath, javAllInfo);
  88. saveInfo(javAllInfo);
  89. long endTime = System.currentTimeMillis();
  90. System.err.println((endTime - startTime) / 1000);
  91. }
  92. // 递归获取某目录下的所有子目录以及子文件
  93. private void getAllFilePaths(String filePath, JavAllInfo javAllInfo) {
  94. File[] files = new File(filePath).listFiles();
  95. if (files == null) {
  96. return;
  97. }
  98. int type = 0;
  99. if (filePath.contains("骑兵")) {
  100. type = 1;
  101. } else if (filePath.contains("步兵")) {
  102. type = 2;
  103. }
  104. for (File file : files) {
  105. if (file.isDirectory()) {
  106. // 文件夹
  107. getAllFilePaths(file.getAbsolutePath(), javAllInfo);
  108. } else {
  109. String fileName = file.getName();
  110. if (fileName.endsWith(".jpg") || (fileName.endsWith(".lnk") && fileName.contains(".jpg"))) {
  111. String parentName = file.getParentFile().getName();
  112. // 识别码
  113. String name = fileName.substring(10).replace(".jpg", "").trim();
  114. String[] nameArr = name.split("\\s+");
  115. try {
  116. boolean isMain = false;
  117. if (fileName.endsWith(".jpg")) {
  118. isMain = true;
  119. // 获取正片信息
  120. VideoInfo videoInfo = new VideoInfo();
  121. // 发行日期
  122. String issueDate = fileName.substring(0, 10);
  123. videoInfo.setIssueDate(LocalDate.parse(issueDate, DateUtils.dateFormatter));
  124. videoInfo.setIdentificationCode(nameArr[0]);
  125. // 名称
  126. if (nameArr.length > 1) {
  127. videoInfo.setName(name.substring(nameArr[0].length()).trim());
  128. } else {
  129. videoInfo.setName(nameArr[0]);
  130. }
  131. // 类型
  132. videoInfo.setType(type);
  133. // 图片URL
  134. videoInfo.setImgUrl(parentName.concat("/").concat(fileName));
  135. // 创建时间 TODO
  136. // 修改时间
  137. videoInfo.setCreateTime(Instant.ofEpochMilli(file.lastModified()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime());
  138. // 主体是谁
  139. videoInfo.setMainWho(parentName);
  140. javAllInfo.getVideoInfoList().add(videoInfo);
  141. }
  142. if (parentName.contains("类别")) {
  143. // 获取类别
  144. String videoGenres = parentName.replace("(类别)", "");
  145. javAllInfo.getVideoGenresSet().add(videoGenres);
  146. VideoInfoGenres videoInfoGenres = new VideoInfoGenres();
  147. videoInfoGenres.setIdentificationCode(nameArr[0]);
  148. videoInfoGenres.setName(videoGenres);
  149. videoInfoGenres.setType(isMain ? 1 : 2);
  150. javAllInfo.getVideoInfoGenresSet().add(videoInfoGenres);
  151. } else if (parentName.contains("优)")) {
  152. // 获取演员
  153. String videoCast = "";
  154. if (parentName.contains("(男")) {
  155. videoCast = parentName.replace("(男优)", "");
  156. javAllInfo.getVideoCastMap().put(videoCast, "1");
  157. } else if (parentName.contains("(女")) {
  158. videoCast = parentName.replace("(女优)", "");
  159. javAllInfo.getVideoCastMap().put(videoCast, "2");
  160. }
  161. VideoInfoCast videoInfoCast = new VideoInfoCast();
  162. videoInfoCast.setIdentificationCode(nameArr[0]);
  163. videoInfoCast.setName(videoCast);
  164. videoInfoCast.setType(isMain ? 1 : 2);
  165. javAllInfo.getVideoInfoCastSet().add(videoInfoCast);
  166. }
  167. } catch (Exception e) {
  168. System.err.println("error:" + file.getAbsolutePath());
  169. System.err.println("error reason:" + e.getMessage());
  170. }
  171. }
  172. }
  173. }
  174. }
  175. // 保存所有文件
  176. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  177. public void saveInfo(JavAllInfo javAllInfo) {
  178. // 删除所有
  179. videoGenresMapper.deleteAll();
  180. videoInfoMapper.deleteAll();
  181. videoCastMapper.deleteAll();
  182. videoGenresMapper.deleteAll();
  183. videoInfoCastMapper.deleteAll();
  184. // 保存分类
  185. Set<String> videoGenresSet = javAllInfo.getVideoGenresSet();
  186. //List<VideoGenres> videoGenresList = new ArrayList<>();
  187. for (String s : videoGenresSet) {
  188. VideoGenres videoGenres = new VideoGenres();
  189. videoGenres.setName(s);
  190. videoGenresMapper.insert(videoGenres);
  191. System.out.println(videoGenres);
  192. //videoGenresList.add(videoGenres);
  193. }
  194. //Map<String, VideoGenres> stringVideoGenresMap = videoGenresList.stream().collect(Collectors.toMap(VideoGenres::getName, Function.identity(), (k1, k2) -> k2));
  195. // 保存演员
  196. Map<String, String> videoCastMap = javAllInfo.getVideoCastMap();
  197. //List<VideoCast> videoCastList = new ArrayList<>();
  198. for (Map.Entry<String, String> entry : videoCastMap.entrySet()) {
  199. VideoCast videoCast = new VideoCast();
  200. videoCast.setName(entry.getKey());
  201. videoCast.setType(Integer.parseInt(entry.getValue()));
  202. videoCastMapper.insert(videoCast);
  203. System.out.println(videoCast);
  204. //videoCastList.add(videoCast);
  205. }
  206. // Map<String, VideoCast> stringVideoCastMap = videoCastList.stream().collect(Collectors.toMap(VideoCast::getName, Function.identity(), (k1, k2) -> k2));
  207. // 保存影片信息
  208. List<VideoInfo> videoInfoList = javAllInfo.getVideoInfoList();
  209. int videoInfoCount = videoInfoMapper.insertList(videoInfoList);
  210. System.out.println("videoInfoCount:" + videoInfoCount);
  211. //for (VideoInfo videoInfo : videoInfoList) {
  212. // try {
  213. // videoInfoMapper.insert(videoInfo);
  214. // System.out.println("success:" + videoInfo);
  215. // } catch (Exception e) {
  216. // e.printStackTrace();
  217. // System.out.println("error:" + videoInfo);
  218. // }
  219. //}
  220. // 保存影片类别关联信息
  221. Set<VideoInfoGenres> videoInfoGenresSet = javAllInfo.getVideoInfoGenresSet();
  222. videoInfoGenresMapper.insertList(videoInfoGenresSet);
  223. // 保存影片类别关联信息
  224. Set<VideoInfoCast> videoInfoCastSet = javAllInfo.getVideoInfoCastSet();
  225. videoInfoCastMapper.insertList(videoInfoCastSet);
  226. }
  227. }