package com.shcd.controller;

import com.alibaba.fastjson.JSON;
import com.shanghaigeography.entity.ResultMsg;
import com.shanghaigeography.eum.ResultState;
import com.shcd.service.SpStatementInlandLowService;
import com.shcd.service.SpStatementQuarterReportService;
import com.shcd.service.SpStatementShipUsageMonthService;
import com.shcd.vo.GenerateReportVo;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/ghjg/sp/report")
@Slf4j
public class SpStatementController {

  @Resource
  SpStatementInlandLowService spStatementInlandLowService;
  @Resource
  SpStatementShipUsageMonthService spStatementShipUsageMonthService;
  @Resource
  private SpStatementQuarterReportService spStatementQuarterReportService;

  /**
   * 生成内河低压分析表-各区上报非标设备数据
   *
   * @param vo
   * @return
   */
  @PostMapping("/report1")
  public String generateReport1(@RequestBody @Validated GenerateReportVo vo) {
    try {
      spStatementInlandLowService.makeReport1(vo.getCurrent(), vo.getLast());
      return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("生成成功")
          .setData(JSON.toJSONString(null)).toJSON();
    } catch (Exception e) {
      e.printStackTrace();
      return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("生成失败")
          .setData(JSON.toJSONString(e.getMessage())).toJSON();
    }

  }

  /**
   * 生成内河低压分析表-市级平台自动上传标准设备数据
   *
   * @param vo
   * @return
   */
  @PostMapping("/report2")
  public String generateReport2(@RequestBody @Validated GenerateReportVo vo) {
    try {
      spStatementInlandLowService.makeReport2(vo.getCurrent(), vo.getLast(), vo.getLastTwo(),
          vo.getLastThree());
      return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("生成成功")
          .setData(JSON.toJSONString(null)).toJSON();
    } catch (Exception e) {
      e.printStackTrace();
      return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("生成失败")
          .setData(JSON.toJSONString(e.getMessage())).toJSON();
    }
  }


  /**
   * 生成上海市长江经济带船舶使用岸电情况报表
   *
   * @param vo
   * @return
   */
  @PostMapping("/shPowerUse/report")
  public String generateShPowerUse(@RequestBody @Validated GenerateReportVo vo) {
    try {
      spStatementShipUsageMonthService.makeShangHaiShorePowerUsageRecord(vo.getCurrent());
      return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("生成成功")
          .setData(JSON.toJSONString(null)).toJSON();
    } catch (Exception e) {
      e.printStackTrace();
      return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("生成失败")
          .setData(JSON.toJSONString(e.getMessage())).toJSON();
    }
  }

  /**
   * 生成季度报表
   *
   * @param vo
   * @return
   */
  @PostMapping("/quarter/report")
  public String generateQuarterReport(@RequestBody @Validated GenerateReportVo vo) {
    try {
      spStatementQuarterReportService.generateReportSpQuarterReport(vo.getCurrent());
      return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("生成成功")
          .setData(JSON.toJSONString(null)).toJSON();
    } catch (Exception e) {
      e.printStackTrace();
      return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("生成失败")
          .setData(JSON.toJSONString(e.getMessage())).toJSON();
    }
  }
}
