package top.lvzhiqiang.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; 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.VideoInfoService; import top.lvzhiqiang.util.DateUtils; import javax.annotation.Resource; import java.io.File; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneOffset; import java.util.List; import java.util.Map; import java.util.Set; /** * 电影信息ServiceImpl * * @author lvzhiqiang * 2022/4/4 15:28 */ @Service public class VideoInfoServiceImpl extends BaseServiceImpl implements VideoInfoService { @Resource private VideoInfoMapper videoInfoMapper; @Resource private VideoGenresMapper videoGenresMapper; @Resource private VideoCastMapper videoCastMapper; @Resource private VideoInfoCastMapper videoInfoCastMapper; @Resource private VideoInfoGenresMapper videoInfoGenresMapper; /** * 查询所有 * * @return */ @Override public List findAll() { return videoInfoMapper.findAll(); } /** * 分页查询所有 * * @return */ @Override public PageInfo findAll(int pageNo, int pageSize) { PageHelper.startPage(pageNo, pageSize); List videoInfoList = videoInfoMapper.findAll(); PageInfo videoInfoPageInfo = new PageInfo<>(videoInfoList); return videoInfoPageInfo; } @Override public PageInfo getVideoInfoPage(Map params) { // 转换成like paramsToLike(params, "keyword"); // 分页 paramsToPagination(params); // 排序 paramsToSort(params); List videoInfoList = videoInfoMapper.getVideoInfoList(params); PageInfo videoInfoPageInfo = new PageInfo<>(videoInfoList); return videoInfoPageInfo; } /** * 初始化数据 */ @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) @Async public void initData() { long startTime = System.currentTimeMillis(); DicCode dicCode = WebAppConfig.dicCodeList.stream().filter(x -> 1 == x.getType() && "pic_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); } // 递归获取某目录下的所有子目录以及子文件 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()); } } } } } // 保存所有文件 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void saveInfo(JavAllInfo javAllInfo) { // 删除所有 videoGenresMapper.deleteAll(); videoInfoMapper.deleteAll(); videoCastMapper.deleteAll(); videoGenresMapper.deleteAll(); videoInfoCastMapper.deleteAll(); // 保存分类 Set videoGenresSet = javAllInfo.getVideoGenresSet(); //List 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 stringVideoGenresMap = videoGenresList.stream().collect(Collectors.toMap(VideoGenres::getName, Function.identity(), (k1, k2) -> k2)); // 保存演员 Map videoCastMap = javAllInfo.getVideoCastMap(); //List videoCastList = new ArrayList<>(); for (Map.Entry 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 stringVideoCastMap = videoCastList.stream().collect(Collectors.toMap(VideoCast::getName, Function.identity(), (k1, k2) -> k2)); // 保存影片信息 List videoInfoList = javAllInfo.getVideoInfoList(); int videoInfoCount = videoInfoMapper.insertList(videoInfoList); System.out.println("videoInfoCount:" + videoInfoCount); //for (VideoInfo videoInfo : videoInfoList) { // try { // videoInfoMapper.insert(videoInfo); // System.out.println("success:" + videoInfo); // } catch (Exception e) { // e.printStackTrace(); // System.out.println("error:" + videoInfo); // } //} // 保存影片类别关联信息 Set videoInfoGenresSet = javAllInfo.getVideoInfoGenresSet(); videoInfoGenresMapper.insertList(videoInfoGenresSet); // 保存影片类别关联信息 Set videoInfoCastSet = javAllInfo.getVideoInfoCastSet(); videoInfoCastMapper.insertList(videoInfoCastSet); } }