package com.bonus.canteen.core.common.export.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.common.export.constant.ExportFileTypeEnum;
import com.bonus.canteen.core.common.export.model.ExportRecord;
import com.bonus.canteen.core.common.export.po.ExcelExportParam;
import com.bonus.canteen.core.common.export.service.ExportAsyncManager;
import com.bonus.canteen.core.common.export.service.ExportRecordService;
import com.bonus.canteen.core.common.export.service.ExportUploadManager;
import com.bonus.canteen.core.common.page.PageDTO;
import com.bonus.canteen.core.common.page.PageVO;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

@Service
public class ExportApi {
   private static final Logger log = LoggerFactory.getLogger(ExportApi.class);
   @Resource
   @Lazy
   private ExportAsyncManager exportAsyncManager;
   @Resource
   @Lazy
   private ExportRecordService exportRecordService;
   @Resource
   @Lazy
   private ExportUploadManager exportUploadManager;

   public ExportRecord createRecord(String fileName, ExportFileTypeEnum type, boolean ifPrivate, LocalDateTime expiredTime, File tempFile) throws Exception {
      log.info("人工创建导出记录：{}", fileName);
      String fileUrl = this.exportUploadManager.upload(this.exportAsyncManager.getFileFullName(fileName), tempFile, TenantContextHolder.getTenantId().toString());
      return this.exportRecordService.createFinished(fileName, type, ifPrivate, expiredTime != null ? expiredTime : LocalDateTime.now().plusDays(7L), fileUrl, tempFile.length());
   }

   public <T> ExportRecord startExcelExportTask(ExcelExportParam<T> param) {
      return this.exportAsyncManager.startExcelExportTask(param);
   }

   public <T> ExportRecord startExcelExportTaskByPage(String fileName, String sheetName, Class<T> headClass, List<String> columnNames, PageDTO pageDTO, T statLine, Supplier<PageVO<T>> pageQueryFunc) {
      return this.exportAsyncManager.startExcelExportTaskByPage(this.exportAsyncManager.getFileFullName(fileName), sheetName, headClass, columnNames, pageDTO, statLine, pageQueryFunc);
   }

   public <T, R> ExportRecord startExcelExportTaskByPage(String fileName, String sheetName, Class<R> headClass, List<String> columnNames, PageDTO pageDTO, T statLine, Supplier<PageVO<T>> pageQueryFunc, Function<T, R> mapFunc) {
      return this.exportAsyncManager.startExcelExportTaskByPage(this.exportAsyncManager.getFileFullName(fileName), sheetName, headClass, columnNames, pageDTO, statLine, pageQueryFunc, mapFunc);
   }

   public <T, R> ExportRecord startExcelExportTaskByMpPage(String fileName, String sheetName, Class<R> headClass, List<String> columnNames, Page page, T statLine, Supplier<Page<T>> pageQueryFunc, Function<T, R> mapFunc) {
      return this.exportAsyncManager.startExcelExportTaskByMPPage(this.exportAsyncManager.getFileFullName(fileName), sheetName, headClass, columnNames, page, statLine, pageQueryFunc, mapFunc);
   }
}
