| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- 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<Object> 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<VideoInfo> findAll() {
- return videoInfoMapper.findAll();
- }
- /**
- * 分页查询所有
- *
- * @return
- */
- @Override
- public PageInfo<VideoInfo> findAll(int pageNo, int pageSize) {
- PageHelper.startPage(pageNo, pageSize);
- List<VideoInfo> videoInfoList = videoInfoMapper.findAll();
- PageInfo<VideoInfo> videoInfoPageInfo = new PageInfo<>(videoInfoList);
- return videoInfoPageInfo;
- }
- @Override
- public PageInfo<VideoInfo> getVideoInfoPage(Map<String, Object> params) {
- // 转换成like
- paramsToLike(params, "keyword");
- // 分页
- paramsToPagination(params);
- // 排序
- paramsToSort(params);
- List<VideoInfo> videoInfoList = videoInfoMapper.getVideoInfoList(params);
- PageInfo<VideoInfo> 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<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();
- 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<VideoInfoGenres> videoInfoGenresSet = javAllInfo.getVideoInfoGenresSet();
- videoInfoGenresMapper.insertList(videoInfoGenresSet);
- // 保存影片类别关联信息
- Set<VideoInfoCast> videoInfoCastSet = javAllInfo.getVideoInfoCastSet();
- videoInfoCastMapper.insertList(videoInfoCastSet);
- }
- }
|