package com.jiayue.ipfcst.fileupload.controller;

import com.jiayue.ipfcst.aop.SaveValidate;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.constant.enums.*;
import com.jiayue.ipfcst.common.data.entity.UploadFileChannel;
import com.jiayue.ipfcst.common.data.repository.UploadURLRepository;
import com.jiayue.ipfcst.fileupload.service.UploadFileChannelService;
import com.jiayue.ipfcst.fileupload.service.UploadURLService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 上报通道restful接口
 */
@RestController
@Slf4j
@RequestMapping("uploadFileChannel")
public class UploadFileChannelController {

  private final UploadFileChannelService uploadFileChannelService;
  private final UploadURLService uploadURLService;
  private final UploadURLRepository uploadURLRepository;

  @Autowired
  public UploadFileChannelController(UploadFileChannelService uploadFileChannelService,UploadURLService uploadURLService,UploadURLRepository uploadURLRepository) {
    this.uploadFileChannelService = uploadFileChannelService;
    this.uploadURLService = uploadURLService;
    this.uploadURLRepository = uploadURLRepository;
  }

  /**
   * 新增通道
   *
   * @param uploadFileChannel
   * @return
   */
  @SneakyThrows
  @PostMapping
  @SaveValidate
  public ResponseVO add(@RequestBody UploadFileChannel uploadFileChannel) {
    UploadFileChannel save = uploadFileChannelService.save(uploadFileChannel);
    return ResponseVO.success(save);
  }

  /**
   * 修改通道
   *
   * @param uploadFileChannel
   * @return
   */
  @SneakyThrows
  @PutMapping
  @SaveValidate
  @Transactional
  public ResponseVO update(@RequestBody UploadFileChannel uploadFileChannel) {

    uploadFileChannelService.save(uploadFileChannel);
    try {
      if (uploadFileChannel.getBackupC()==null){
        uploadURLRepository.deleteByUploadChannelId(uploadFileChannel.getId());
      }
    }catch (Exception e){
      uploadURLRepository.deleteByUploadChannelId(uploadFileChannel.getId());
    }
    return ResponseVO.success(1);
  }

  /**
   * 删除通道
   *
   * @param id
   * @return
   */
  @SneakyThrows
  @DeleteMapping("/{id}")
  @SaveValidate
  public ResponseVO delete(@PathVariable Integer id) {
    UploadFileChannel uploadFileChannel = uploadFileChannelService.getById(id);
    uploadFileChannelService.delete(uploadFileChannel);
    uploadURLService.deleteByChannelId(id);
    return ResponseVO.success(1);
  }

  /**
   * 获取通道
   *
   * @return
   */
  @SneakyThrows
  @GetMapping
  public ResponseVO get() {
    List<UploadFileChannel> uploadFileChannels = uploadFileChannelService.get();
    return ResponseVO.success(uploadFileChannels);

  }

//  /**
//   * 获取通道（分页）
//   */
//  @SneakyThrows
//  @GetMapping(value = "/{page}/{size}")
//  public ResponseVO getPage(@PathVariable Integer page, @PathVariable Integer size) {
//    UploadFileChannel uploadFileChannel = new UploadFileChannel();
//    Page<UploadFileChannel> uploadFileChannels = uploadFileChannelService.getPage(uploadFileChannel, page, size);
//    return ResponseVO.success(uploadFileChannels);
//
//  }


  /**
   * 获取通道状态枚举类型
   *
   * @return
   */
  @SneakyThrows
  @GetMapping(value = "/getChannelStatusEnum")
  public ResponseVO getChannelStatusEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (ChannelStatusEnum e : ChannelStatusEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取通道状态枚举类型异常");
      return ResponseVO.fail(list);
    }
  }

  /**
   * 获取上报协议枚举类
   *
   * @return
   */
  @SneakyThrows
  @GetMapping(value = "/getUploadProtocolEnum")
  public ResponseVO getUploadProtocolEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (UploadProtocolEnum e : UploadProtocolEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取上报协议枚举类型异常");
      return ResponseVO.fail(list);
    }
  }


  /**
   * 获取上报文件字符集
   *
   * @return
   */
  @SneakyThrows
  @GetMapping(value = "/getUploadFileCharSetEnum")
  public ResponseVO getUploadFileCharSetEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (UploadFileCharSetEnum e : UploadFileCharSetEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取上报文件字符集类型异常");
      return ResponseVO.fail(list);
    }
  }

  /**
   * 获取上报文件名称长度枚举
   *
   * @return
   */
  @SneakyThrows
  @GetMapping(value = "/getUploadFileNameLengthEnum")
  public ResponseVO getUploadFileNameLengthEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (UploadFileNameLengthEnum e : UploadFileNameLengthEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取上报文件名称长度枚举类型异常");
      return ResponseVO.fail(list);
    }
  }

  /**
   * 获取上报文件单次传输字节枚举类型
   *
   * @return
   */
  @SneakyThrows
  @GetMapping(value = "/getUploadFileSingleByteEnum")
  public ResponseVO getUploadFileSingleByteEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (UploadFileSingleByteEnum e : UploadFileSingleByteEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取上报文件单次传输字节枚举类型异常");
      return ResponseVO.fail(list);
    }
  }

  /**
   * 获取ftp模式
   *
   * @return
   */
  @SneakyThrows
  @GetMapping(value = "/getFtpPassiveModeEnum")
  public ResponseVO getFtpPassiveModeEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (FtpPassiveModeEnum e : FtpPassiveModeEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取ftp模式类型异常");
      return ResponseVO.fail(list);
    }
  }

  /**
   * 获取通道文件生成规则
   *
   * @return
   */
  @SneakyThrows
  @GetMapping(value = "/getChannelFileRuleEnum")
  public ResponseVO getChannelFileRuleEnum() {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map;
    try {
      for (ChannelFileRuleEnum e : ChannelFileRuleEnum.values()) {
        map = new HashMap<>();
        map.put("label", e.getMessage());
        map.put("key", e.name());
        map.put("value", e.name());
        list.add(map);
      }
      return ResponseVO.success(list);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 获取通道文件生成规则类型异常");
      return ResponseVO.fail(list);
    }
  }
  /**
   * 删除通道
   *
   * @param id
   * @return
   */
  @SneakyThrows
  @DeleteMapping("/deleteByObjId/{id}")
  @SaveValidate
  public ResponseVO deleteByObjId(@PathVariable Integer id) {
    try {
      uploadFileChannelService.deleteByObjId(id);
      return ResponseVO.success(1);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(" 根据ObjectId删除通道异常");
      return ResponseVO.fail();
    }


  }

}

