package com.jiayue.ipfcst.fileupload.job;

import com.jiayue.ipfcst.common.data.constant.enums.FileTypeEnum;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.common.data.job.BaseJob;
import com.jiayue.ipfcst.console.service.ElectricFieldService;
import com.jiayue.ipfcst.fileupload.service.IUploadFileService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * 上报文件生成定时任务
 *
 * @author xsl
 * @version 3.0
 * @since 2020/4/15 9:16
 */
@SuppressWarnings("ALL")
@Slf4j
public abstract class BaseUploadFileJob extends BaseJob {

  @Autowired
  private Map<String, IUploadFileService> uploadFileServiceMap;
  @Autowired
  private ElectricFieldService electricFieldService;

  @Override
  public boolean execute(JobDataMap jobDataMap) {
    String fileType = this.getFileType();
    String pro = "";
    ElectricField electricField = electricFieldService.get();
    if (electricField != null) {
      pro = electricField.getProvinceEnum().name();
    } else {
      log.error("场站为空不能执行文件生成：" + FileTypeEnum.valueOf(fileType).getMessage());
      return false;
    }
    log.info("开始执行生成上报文件[" + FileTypeEnum.valueOf(fileType).getMessage() + "]定时任务");
    if (uploadFileServiceMap != null) {
      IUploadFileService uploadFileService = uploadFileServiceMap.get(pro.toLowerCase() + "UploadFileService");
      if (uploadFileService instanceof IUploadFileService){
        uploadFileService.generateFile(fileType,null);
        log.info("上报文件[" + FileTypeEnum.valueOf(fileType).getMessage() + "]定时任务执行完成");
      }
      else{
        log.error(pro+"省份没有文件生成实现类");
        return false;
      }
    } else {
      log.error("生成上报文件定时任务没有实现类");
      return false;
    }
    return true;
  }

  public abstract String getFileType();
}
