package cn.reghao.oss.store.rpc.disk;

import cn.reghao.oss.api.dto.FileInfo;
import cn.reghao.oss.api.iface.disk.DiskService;
import cn.reghao.oss.store.db.mapper.FileMetaMapper;
import cn.reghao.oss.store.service.PutObjectService;
import cn.reghao.jutil.jdk.db.Page;
import cn.reghao.jutil.jdk.db.PageList;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2023-05-18 20:39:45
 */
@Slf4j
@DubboService
@Service
public class DiskServiceImpl implements DiskService {
    private PutObjectService putObjectService;
    private final FileMetaMapper fileMetaMapper;
    private final int pageSize = 10;

    public DiskServiceImpl(PutObjectService putObjectService, FileMetaMapper fileMetaMapper) {
        this.fileMetaMapper = fileMetaMapper;
        this.putObjectService = putObjectService;
    }

    @Override
    public PageList<FileInfo> getFileList(String pid, int pageNumber) {
        Page page = new Page(pageNumber, pageSize);
        int total = fileMetaMapper.countByPid(pid);
        List<FileInfo> list = fileMetaMapper.findFileInfoByPage(page, pid);
        PageList<FileInfo> pageList = PageList.pageList(pageNumber, pageSize, total, list);
        return pageList;
    }

    @Override
    public PageList<FileInfo> getFileCard(String pid, int pageNumber) {
        Page page = new Page(pageNumber, pageSize);
        int total = fileMetaMapper.countByPid(pid);
        List<FileInfo> list = fileMetaMapper.findFileInfo1ByPage(page, pid);
        PageList<FileInfo> pageList = PageList.pageList(pageNumber, pageSize, total, list);
        return pageList;
    }

    @Override
    public List<FileInfo> getDirectories(String pid) {
        List<FileInfo> list = fileMetaMapper.findDirectories(pid);
        return list;
    }

    @Override
    public PageList<FileInfo> search(String keyword, int pageNumber) {
        Page page = new Page(pageNumber, pageSize);
        int total = fileMetaMapper.countByKeyword(keyword);
        List<FileInfo> list = fileMetaMapper.findKeywordByPage(page, keyword);
        PageList<FileInfo> pageList = PageList.pageList(pageNumber, pageSize, total, list);
        return pageList;
    }
}
