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

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.two.VideoPostMapper;
import cn.reghao.tnb.sb.multi.mybatis.model.one.DataBlock;
import cn.reghao.tnb.sb.multi.mybatis.model.one.FileMeta;
import cn.reghao.tnb.sb.multi.mybatis.model.two.CamRecord;
import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author reghao
 * @date 2025-09-21 20:51:44
 */
@Slf4j
@Service
public class CamService {
    @Autowired
    VideoPostMapper videoPostMapper;
    @Autowired
    FileMetaMapper fileMetaMapper;
    @Autowired
    DataBlockMapper dataBlockMapper;

    String srcDir = "/opt/disk/13f654c8-af87-4710-aac9-7aa086c99aec";
    public void startBak() {
        // cam201
        long camId = 52584124416L;

        // 2022-10-24 -> 2023-06-30
        String destDir1 = "/root/mnt/1";
        int start1 = 0;
        int pageSize = 4612;
        getCamRecord(camId, start1, pageSize, destDir1);

        // 2023-07-01 -> 2024-01-31
        String destDir2 = "/root/mnt/2";
        int start2 = 4612;
        pageSize = 1116;
        getCamRecord(camId, start2, pageSize, destDir2);

        // 2024-02-01 -> 2024-10-31
        int start3 = 5728;
        pageSize = 1303;
        //getCamRecord(camId, start3, pageSize, destDir1);

        // 2024-11-01 -> 2025-06-30
        int start4 = 7031;
        pageSize = 4698;
        //getCamRecord(camId, start4, pageSize, destDir2);

        // 2025-07-01 -> 2025-09-30
        int start5 = 11729;
        pageSize = 1000;
        //getCamRecord(camId, start5, pageSize, destDir1);
    }

    void divide(double d1, double d2) {
        /*int scale = 2;
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        BigDecimal result = b1.divide(b2, scale, RoundingMode.HALF_UP);*/

        DecimalFormat df = new DecimalFormat("#.##");
        df.setRoundingMode(RoundingMode.HALF_UP);
        String result2 = df.format(d1/d2);
        System.out.println(result2);
    }

    void getCamRecord(long camId, int start, int end, String destDir) {
        List<CamRecord> camRecordList = videoPostMapper.findCamRecord(camId, start, end);
        long total = 0L;
        for (CamRecord camRecord : camRecordList) {
            String objectId = camRecord.getVideoFileId();
            FileMeta fileMeta = fileMetaMapper.findByObjectId(objectId);
            if (fileMeta == null) {
                log.info("objectId {} 关联的 FileMeta 不存在", objectId);
                continue;
            }

            String contentId = fileMeta.getContentId();
            DataBlock dataBlock = dataBlockMapper.findByContentId(contentId);
            if (dataBlock == null) {
                log.info("contentId {} 关联的 DataBlock 不存在", contentId);
            } else {
                total += dataBlock.getSize();

                /*String absolutePath = dataBlock.getAbsolutePath();
                String absolutePath1 = absolutePath.replace(srcDir, destDir);
                log.info("copy {} to {}", absolutePath, absolutePath1);

                File srcFile = new File(absolutePath);
                if (!srcFile.exists()) {
                    log.info("srcFile {} not exist", absolutePath);
                    continue;
                }
                File destFile = new File(absolutePath1);
                try {
                    FileUtils.copyFile(srcFile, destFile);
                    dataBlockMapper.updateReplica(dataBlock.getId(), 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
            }
        }

        double gib = 1024D*1024*1024;
        divide(total, gib);
    }

    public void moveCamFile() {
        String src = "/disk/a";
        String dest = "/opt/disk/13f654c8-af87-4710-aac9-7aa086c99aec";

        List<DataBlock> dataBlockList = dataBlockMapper.findByDisk();
        for (DataBlock dataBlock : dataBlockList) {
            String absolutePath = dataBlock.getAbsolutePath();
            String absolutePath1 = absolutePath.replace(src, dest);

            File srcFile = new File(absolutePath);
            if (!srcFile.exists()) {
                log.error("srcFile {} not exist", absolutePath);
                continue;
            }

            File destFile = new File(absolutePath1);
            if (destFile.exists()) {
                log.error("destFile {} exist", absolutePath1);
                continue;
            }

            log.info("move file {} to {}", absolutePath, absolutePath1);
            /*try {
                FileUtils.moveFile(srcFile, destFile);
                dataBlockMapper.updatePath(dataBlock.getId(), absolutePath1);
                FileUtils.deleteQuietly(srcFile);
            } catch (IOException e) {
                e.printStackTrace();
            }*/
        }
    }
}
