package com.jiayue.ipfcst.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.service.ElectricFieldService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 展示配置restful接口
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/7 10:12
 */
@RestController
@Slf4j
public class ElectricFieldController {
  private final ElectricFieldService electricFieldService;

  @Autowired
  public ElectricFieldController(ElectricFieldService electricFieldService){
    this.electricFieldService = electricFieldService;


  }

  /**
   * 场站信息查询
   * @return result
   */
  @GetMapping(value="electricField")
  public ResponseVO displayConfigQuery() {
    ElectricField electricField;
    try{
      electricField = electricFieldService.get();
      return ResponseVO.success(electricField);
    }catch(Exception e){
      e.printStackTrace();
      log.error("查询场站信息错误");
      return ResponseVO.fail(e.toString());
    }
  }

  /**
   * 合同结束日期查询
   * @return result
   */
  @GetMapping(value="getExpirationTime")
  public ResponseVO contractEndTime() {
    String contractEndTimeStr;
    try{
      contractEndTimeStr = electricFieldService.getElectricField().getBackupC();
      if(contractEndTimeStr != null && !contractEndTimeStr.equals("")){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date contractEndTime = format.parse(contractEndTimeStr);
        //获取当前系统时间的0分0秒
        long nowTime = DateUtil.beginOfDay(DateTime.now()).getTime();
        long day = 1000 * 24 * 60 * 60;
        long different = contractEndTime.getTime()-nowTime;
        different = (different/day);
        return ResponseVO.success(different);
      }else{
        return ResponseVO.success(null);
      }
    }catch(Exception e){
      e.printStackTrace();
      log.error("查询合同结束日期失败");
      return ResponseVO.fail(e.toString());
    }
  }


}
