package com.jiayue.ipfcst.fileupload.controller;

import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.constant.enums.FileStatusEnum;
import com.jiayue.ipfcst.common.data.constant.enums.FileTypeEnum;
import com.jiayue.ipfcst.common.data.constant.enums.UploadProtocolEnum;
import com.jiayue.ipfcst.common.data.entity.UploadFileLog;
import com.jiayue.ipfcst.fileupload.service.UploadFileLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("uploadFileLog")
@Slf4j
public class UploadFileLogController {

  private final UploadFileLogService uploadFileLogService;

  @Autowired
  public UploadFileLogController(UploadFileLogService uploadFileLogService) {
    this.uploadFileLogService = uploadFileLogService;
  }

  /**
   * 根据上报对象、时间、文件类型、文件状态查询文件日志
   *
   * @param uploadObjectId 上报对象
   * @param createTime     时间
   * @param fileType       文件类型
   * @param fileStatus     文件状态
   * @return
   */
  @GetMapping("getFileLog/{uploadObjectId}/{createTime}/{fileType}/{fileStatus}")
  public ResponseVO get(@PathVariable("uploadObjectId") Integer uploadObjectId, @PathVariable("createTime") String createTime,
                        @PathVariable("fileType") String fileType, @PathVariable("fileStatus") String fileStatus) {
    List<UploadFileLog> list = uploadFileLogService.queryFileLog(uploadObjectId, createTime, fileType, fileStatus);
    return ResponseVO.success(list);
  }

  @GetMapping("/getFileStatusEnum")
  public ResponseVO getFileStatusEnum() {
    List list = new ArrayList<>();
    for (FileStatusEnum fileStatusEnum : FileStatusEnum.values()) {
      Map map = new HashMap();
      map.put("name", fileStatusEnum.name());
      map.put("message", fileStatusEnum.getMessage());
      list.add(map);
    }
    return ResponseVO.success(list);
  }

  @GetMapping("/getUploadProtocolEnum")
  public ResponseVO getUploadProtocolEnum() {
    List list = new ArrayList<>();
    for (UploadProtocolEnum uploadProtocolEnum : UploadProtocolEnum.values()) {
      Map map = new HashMap();
      map.put("name", uploadProtocolEnum.name());
      map.put("message", uploadProtocolEnum.getMessage());
      list.add(map);
    }
    return ResponseVO.success(list);
  }

  @GetMapping("/getFileTypeEnum")
  public ResponseVO getFileTypeEnum() {
    List list = new ArrayList<>();
    for (FileTypeEnum fileTypeEnum : FileTypeEnum.values()) {
      Map map = new HashMap();
      map.put("name", fileTypeEnum.name());
      map.put("message", fileTypeEnum.getMessage());
      list.add(map);
    }
    return ResponseVO.success(list);
  }

  @PostMapping("/uploadFile")
  public ResponseVO uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("uploadObject") String id, @RequestParam("FileTypeEnum") String fileType) {
    Integer objectId = Integer.parseInt(id);

    Boolean b = uploadFileLogService.uploadFile(objectId, fileType, file);
    if (b){
      return ResponseVO.success("上传成功！");
    }else {
      return ResponseVO.fail("上传失败！");
    }
  }

  @DeleteMapping("/{id}")
  public ResponseVO deleteById(@PathVariable("id") Integer id){
    uploadFileLogService.deleteById(id);
    return ResponseVO.success(1);
  }

  @PostMapping("/cleanCache")
  public ResponseVO cleanCache(@RequestBody UploadFileLog uploadFileLog){
    uploadFileLogService.cleanCache(uploadFileLog);
    return ResponseVO.success(1);
  }


  /**
   * 文件补报
   * @param uploadFileLog
   * @return
   */
  @PostMapping("/report")
  public ResponseVO report(@RequestBody UploadFileLog uploadFileLog){
    uploadFileLogService.report(uploadFileLog);
    return ResponseVO.success(1);
  }

  /**
   * 一键文件补报
   * @param uploadFileLogList
   * @return
   */
  @PostMapping("/quickReport")
  public ResponseVO quickReport(@RequestBody List<UploadFileLog> uploadFileLogList){
    uploadFileLogService.quickReport(uploadFileLogList);
    return ResponseVO.success(1);
  }
}
