package cn.reghao.tnb.sb.multi.mybatis.service;

import cn.reghao.tnb.sb.multi.mybatis.mapper.four.CamRecordMapper;
import cn.reghao.tnb.sb.multi.mybatis.mapper.one.DataBlockMapper;
import cn.reghao.tnb.sb.multi.mybatis.mapper.one.FileMetaMapper;
import cn.reghao.tnb.sb.multi.mybatis.mapper.three.UserAccountMapper;
import cn.reghao.tnb.sb.multi.mybatis.mapper.two.*;
import cn.reghao.tnb.sb.multi.mybatis.model.one.FileMeta;
import cn.reghao.tnb.sb.multi.mybatis.model.three.UserAccount;
import cn.reghao.tnb.sb.multi.mybatis.model.two.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-03-15 18:40:35
 */
@Service
public class ImageService {
    @Autowired
    private CameraPhotoMapper cameraPhotoMapper;
    @Autowired
    private ImageFileMapper imageFileMapper;
    @Autowired
    private ImagePostMapper imagePostMapper;
    @Autowired
    private VideoPostMapper videoPostMapper;
    @Autowired
    private UserAccountMapper userAccountMapper;
    @Autowired
    private FileMetaMapper fileMetaMapper;
    @Autowired
    private DataBlockMapper dataBlockMapper;
    @Autowired
    MallReplyPhotoMapper mallReplyPhotoMapper;
    @Autowired
    CamRecordMapper camRecordMapper;

    public void process() {
        /*List<CamRecord> camRecordList = camRecordMapper.findAll();
        for (CamRecord camRecord : camRecordList) {
            String videoFileId = camRecord.getVideoFileId();
            FileMeta fileMeta = fileMetaMapper.findByObjectId(videoFileId);
            if (fileMeta == null) {
                //camRecordMapper.deleteByRecordId(camRecord.getRecordId());
                System.out.printf("objectId -> %s\n", videoFileId);
            }
        }*/

        /*List<FileMeta> list = fileMetaMapper.findAll();
        for (FileMeta fileMeta : list) {
            String objectId = fileMeta.getObjectId();
            CamRecord camRecord = camRecordMapper.findByVideoFileId(objectId);
            if (camRecord == null) {
                System.out.printf("objectId -> %s\n", objectId);
            }
        }*/

        //process6();
        //deleteDataBlock();
        System.out.println("process done...");
    }

    private void process1() {
        List<MallReplyPhoto> list = mallReplyPhotoMapper.findAll();
        List<CameraPhoto> cameraPhotos = list.stream().map(CameraPhoto::new).collect(Collectors.toList());
        cameraPhotoMapper.saveAll(cameraPhotos);
    }

    private void process2() {
        List<String> objectIds = new ArrayList<>();
        List<CameraPhoto> list = cameraPhotoMapper.findAll();
        for (CameraPhoto cameraPhoto : list) {
            String objectId = cameraPhoto.getObjectId();
            ImageFile imageFile = imageFileMapper.findByImageFileId(objectId);
            if (imageFile != null) {
                long albumId = imageFile.getAlbumId();
                ImagePost imagePost = imagePostMapper.findImagePost(albumId);
                if (imagePost != null) {
                    cameraPhotoMapper.updateSetFrom(objectId, 1);
                    //System.out.println("http://tnb.reghao.cn/image/" + imagePost.getAlbumId());
                }
            } else {
                objectIds.add(objectId);
            }
        }

        /*objectIds.forEach(id -> {
            cameraPhotoMapper.updateSetDeleted(id);
            System.out.printf("%s updated\n", id);
        });*/
    }

    private void process3() {
        List<Long> list1 = imagePostMapper.findImageUser();
        List<Long> list2 = imagePostMapper.findVideoUser();
        Set<Long> set = new TreeSet<>(list1);
        for (Long id : list2) {
            set.add(id);
        }

        for (Long id : set) {
            UserAccount userAccount = userAccountMapper.findUserAccount(id);
            if (userAccount != null) {
                String screenName = userAccount.getScreenName();
                if (screenName.startsWith("tnb")) {
                    String screenName1 = screenName.replace("_", "");
                    userAccountMapper.updateScreenName(id, screenName1);
                }
            }
        }
    }

    private void process4() {
        List<UserAccount> existList = userAccountMapper.findExist();
        List<UserAccount> nonExistList = userAccountMapper.findNonExist(existList.size());
        if (existList.size() != nonExistList.size()) {
            System.out.println("list len not matched...");
            return;
        }

        for (int i = 0; i < existList.size(); i++) {
            UserAccount oldUserAccount = existList.get(i);
            UserAccount newUserAccount = nonExistList.get(i);
            long oldUser = oldUserAccount.getUserId();
            long newUser = newUserAccount.getUserId();

            List<ImagePost> list = imagePostMapper.findImagePostByUserId(oldUser);
            if (!list.isEmpty()) {
                imagePostMapper.updateUserId(oldUser, newUser);
            }
            videoPostMapper.updatePublishBy(oldUser, newUser);

            String screenName = oldUserAccount.getScreenName();
            String screenName1 = String.format("tnb_%s", oldUser);
            userAccountMapper.updateScreenName(oldUser, screenName1);

            String screenName2 = screenName;
            if (screenName.startsWith("tnb")) {
                screenName2  = String.format("tnb_%s", newUser);
            }
            userAccountMapper.updateScreenName(newUser, screenName2);

            System.out.printf("%s -> %s processed\n", oldUser, newUser);
        }
    }

    private void process5() {
        List<UserAccount> existList = userAccountMapper.findExist();
        for (UserAccount userAccount : existList) {
            long userId = userAccount.getUserId();
            List<ImagePost> list = imagePostMapper.findImagePostByUserId(userId);
            if (!list.isEmpty()) {
                System.out.printf("image %s not empty\n", userId);
            }

            List<VideoPost> list1 = videoPostMapper.findVideoPostByUserId(userId);
            if (!list1.isEmpty()) {
                System.out.printf("video %s not empty\n", userId);
            }

            String screenName = String.format("tnb_%s", userId);
            userAccountMapper.updateScreenName(userId, screenName);
        }

        long end = 18000;
        for (long start = 17929; start < end+1; start++) {
            String screenName = String.format("tnb_%s", start);
            //userAccountMapper.updateScreenName(start, screenName);
        }

        /*long newUser = 18001;
        List<Long> imageUsers = imagePostMapper.findImageUser();
        for (int i = 2; i < imageUsers.size(); i++) {
            Long oldUser = imageUsers.get(i);
            List<ImagePost> list = imagePostMapper.findImagePostByUserId(oldUser);
            if (!list.isEmpty()) {
                imagePostMapper.updateUserId(oldUser, newUser);
            } else {
                System.out.println("empty");
            }

            System.out.printf("%s -> %s processed\n", oldUser, newUser);
            newUser++;
        }*/
    }

    /**
     * 逻辑删除文件
     *
     * @param
     * @return
     * @date 2025-05-01 15:05:902
     */
    private void deleteFileMeta() {
        List<ImagePost> list = imagePostMapper.findAll();
        for (ImagePost imagePost : list) {
            long albumId = imagePost.getAlbumId();
            List<ImageFile> imageFileList = imageFileMapper.findByAlbumId(albumId);
            imageFileList.forEach(imageFile -> {
                String objectId = imageFile.getObjectId();
                FileMeta fileMeta = fileMetaMapper.findByObjectId(objectId);
                if (fileMeta != null) {
                    String contentId = fileMeta.getContentId();
                    List<FileMeta> fileMetaList = fileMetaMapper.findByContentId(contentId);
                    if (fileMetaList.size() == 1) {
                        dataBlockMapper.updateSetDelete(contentId);
                    }

                    fileMetaMapper.updateSetDeleteByObjectId(objectId);
                }
            });
        }
    }

    /**
     * 物理删除文件
     *
     * @param
     * @return
     * @date 2025-05-01 15:05:631
     */
    private void deleteDataBlock() {
        List<FileMeta> list = fileMetaMapper.findByDeleted();
        for (FileMeta fileMeta : list) {
            String contentId = fileMeta.getContentId();
            dataBlockMapper.updateSetDelete(contentId);
            /*DataBlock dataBlock = dataBlockMapper.findByContentId(contentId);
            if (dataBlock != null) {
                String absolutePath = dataBlock.getAbsolutePath();
                FileUtils.deleteQuietly(new File(absolutePath));
            }*/
        }

        /*List<DataBlock> dataBlockList = dataBlockMapper.findByDeleted();
        for (DataBlock dataBlock : dataBlockList) {
            String absolutePath = dataBlock.getAbsolutePath();
            FileUtils.deleteQuietly(new File(absolutePath));
        }*/
    }
}
