package com.jiayue.ipfcst.fileupload.controller;

import com.jiayue.ipfcst.aop.SaveValidate;
import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.constant.enums.FileTypeEnum;
import com.jiayue.ipfcst.common.data.entity.UploadFileChannel;
import com.jiayue.ipfcst.common.data.entity.UploadObject;
import com.jiayue.ipfcst.common.data.repository.UploadObjectRepository;
import com.jiayue.ipfcst.fileupload.service.UploadFileChannelService;
import com.jiayue.ipfcst.fileupload.service.UploadObjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("uploadObject")
@Slf4j
public class UploadObjectController {
  @Autowired
  private final UploadObjectService uploadObjectService;
  private final UploadFileChannelService uploadFileChannelService;

  public UploadObjectController(UploadObjectService uploadObjectService, UploadFileChannelService uploadFileChannelService) {
    this.uploadObjectService = uploadObjectService;
    this.uploadFileChannelService = uploadFileChannelService;
  }
  @Autowired UploadObjectRepository uploadObjectRepository;
  /**
   * 获取通道对象
   *
   * @return
   */
  @GetMapping()
  public ResponseVO get() {
    List list = new ArrayList();
    List<UploadObject> uploadObjectList = uploadObjectService.get();
    return ResponseVO.success(uploadObjectList);
  }

  /**
   * 保存通道对象
   *
   * @param uploadObject
   * @return
   */
  @PostMapping
  @SaveValidate
  public ResponseVO add(@RequestBody UploadObject uploadObject) {
    try {
      uploadObjectService.save(uploadObject);
      return ResponseVO.success(1);
    } catch (Exception be) {
      log.error("保存通道对象出错" + be);
      return ResponseVO.success("失败");
    }
  }

  /**
   * 保存通道对象
   *
   * @param uploadObject
   * @return
   */
  @PutMapping
  @SaveValidate
  public ResponseVO update(@RequestBody UploadObject uploadObject) {
    try {
      uploadObjectService.save(uploadObject);
      return ResponseVO.success(1);
    } catch (Exception be) {
      log.error("修改通道对象出错" + be);
      return ResponseVO.success("失败");
    }
  }


  /**
   * 删除通道对象
   *
   * @param id
   * @return
   */
  @DeleteMapping("/{id}")
  @SaveValidate
  public ResponseVO delete(@PathVariable Integer id) {
    try {
      uploadFileChannelService.deleteByObjId(id);
      uploadObjectService.delete(id);

      return ResponseVO.success(1);
    } catch (Exception be) {
      log.error("保存通道对象出错" + be);
      return ResponseVO.success("失败");
    }
  }

  /**
   * 获取文件上报类型
   */
  @GetMapping("getFileType")
  public ResponseVO getFileType() {
    List list = new ArrayList();
    try {
      for (FileTypeEnum e : FileTypeEnum.values()) {
        Map 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);
    }
  }

  /**
   * 根据上报id来查询
   * @param uploadObjectId
   * @return UploadObject 上报对象
   */
  @GetMapping("getByuploadObjectId/{uploadObjectId}")
  public ResponseVO getFileTypeByupId(@PathVariable("uploadObjectId") Integer uploadObjectId){
    List list = new ArrayList();
    UploadObject uploadObject = uploadObjectRepository.findById(uploadObjectId).get();
    list.add(uploadObject);
    return ResponseVO.success(list);
  }

  /**
   * 获取对应省份的上报文件类型
   */
  @GetMapping("getFileTypeByProvince")
  public ResponseVO getFileTypeByProvince(){
    Map<String, String> uploadFileType = uploadObjectService.getUploadFileType();
    List<Map<String, String>> list = new ArrayList<>();
    for (String key : uploadFileType.keySet()) {
      Map<String, String> map = new HashMap<>();
      //map.keySet()返回的是所有key的值
      String value = uploadFileType.get(key);//得到每个key多对用value的值
      map.put("label", value);
      map.put("key", key);
      map.put("value", key);
      list.add(map);
    }
    return ResponseVO.success(list);
  }


  /**
   * 获取所有FTPSFTP通道 whc
   */
  @GetMapping("getFtpSftpChannel")
  public ResponseVO getAllFtpSftp(){
    //获取所有上报对象
    List<UploadObject> uploadObjectList = uploadObjectService.get();
    List<UploadFileChannel> uploadFileChannelList = new ArrayList<>();
    // 过滤出ftp/sftp的上报对象
    uploadObjectList = uploadObjectList.stream().filter(s -> s.getUploadProtocolEnum().toString().equals("E4") || s.getUploadProtocolEnum().toString().equals("E5")).collect(Collectors.toList());
    if (uploadObjectList.size() > 0) {
      // 获取通道信息
      List<UploadFileChannel> uploadFileChannels = uploadFileChannelService.get();
      //获取FTP/SFTP通道
      for(int i =0 ; i<uploadObjectList.size();i++){
        for(int j = 0 ; j<uploadFileChannels.size() ; j++){
          if(uploadObjectList.get(i).getId().equals(uploadFileChannels.get(j).getUploadObjectId())){
            uploadFileChannelList.add(uploadFileChannels.get(j));
          }
        }
      }
    }
    return ResponseVO.success(uploadFileChannelList);
  }


  /**
   * 检查E402 单机转发上送文件是否符合配置要求
   *
   * @return Boolean
   */
  @GetMapping("/checkE402")
  public ResponseVO checkE402() throws BusinessException {

    return ResponseVO.success(uploadObjectService.checkE402());
  }
}
