package ink.wgink.module.file.service.fileclient.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.file.dao.fileclient.IFileClientDao;
import ink.wgink.module.file.manager.FileRemoteManager;
import ink.wgink.module.file.pojo.dtos.fileclient.FileClientDTO;
import ink.wgink.module.file.pojo.vos.fileremote.FileClientVO;
import ink.wgink.module.file.service.fileclient.IFileClientService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: FileRemoteServiceImpl
 * @Description: 文件远程
 * @Author: wanggeng
 * @Date: 2022/8/4 21:33
 * @Version: 1.0
 */
@Service
public class FileClientServiceImpl extends DefaultBaseService implements IFileClientService {

    @Autowired
    private IFileClientDao fileRemoteDao;

    @Override
    public void save(FileClientVO fileClientVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(fileClientVO);
        params.put("fileClientId", UUIDUtil.getUUID());
        params.put("accessKey", UUIDUtil.getUUID());
        params.put("accessSecret", UUIDUtil.get32UUID() + UUIDUtil.get32UUID());
        setSaveInfo(params);
        params.put("isDelete", 0);
        fileRemoteDao.save(params);

        FileRemoteManager.getInstance().refresh();
    }

    @Override
    public void remove(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("fileClientIds", ids);
        fileRemoteDao.remove(params);

        FileRemoteManager.getInstance().refresh();
    }

    @Override
    public void update(String fileClientId, FileClientVO fileClientVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(fileClientVO);
        params.put("fileClientId", fileClientId);
        setUpdateInfo(params);
        fileRemoteDao.update(params);

        FileRemoteManager.getInstance().refresh();
    }

    @Override
    public FileClientDTO get(Map<String, Object> params) {
        params = params == null ? getHashMap(0) : params;
        return fileRemoteDao.get(params);
    }

    @Override
    public FileClientDTO get(String fileClientId) {
        Map<String, Object> params = getHashMap(2);
        params.put("fileClientId", fileClientId);
        return fileRemoteDao.get(params);
    }

    @Override
    public List<FileClientDTO> list(Map<String, Object> params) {
        return fileRemoteDao.list(params);
    }

    @Override
    public SuccessResultList<List<FileClientDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<FileClientDTO> fileClientDTOS = list(page.getParams());
        PageInfo<FileClientDTO> pageInfo = new PageInfo<>(fileClientDTOS);
        return new SuccessResultList<>(fileClientDTOS, pageInfo.getPageNum(), pageInfo.getTotal());
    }
}
