package com.shanghaichengdi.ghjgitem.controller;

import com.alibaba.fastjson.JSONObject;
import com.shanghaichengdi.ghjgitem.constant.HttpStatus;
import com.shanghaichengdi.ghjgitem.domain.BoatWaterPollutantAcceptF;
import com.shanghaichengdi.ghjgitem.domain.BoatWaterPollutantWarningS;
import com.shanghaichengdi.ghjgitem.service.BoatWaterPollutantAcceptFService;
import com.shanghaichengdi.ghjgitem.service.HyDangerPreDeclareNhService;
import com.shanghaichengdi.ghjgitem.util.CommonTools;
import com.shanghaichengdi.ghjgitem.vo.ObjectBodyVo;
import com.shanghaichengdi.ghjgitem.vo.PollutanWarningInfoVo;
import com.shanghaichengdi.ghjgitem.vo.PollutantAcceptVo;
import com.shanghaichengdi.ghjgitem.vo.request.PollutantBoatWaterReq;
import com.shanghaichengdi.ghjgitem.vo.request.PollutantWarningInfoReq;
import com.shanghaichengdi.ghjgitem.vo.response.ResultRes;
import com.shanghaichengdi.ghjgitem.vo.response.ReturnMsg;
import java.text.ParseException;
import java.util.List;
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("/pollutant")
@Slf4j
public class PollutantAcceptController extends CommonController {

  @Resource
  private BoatWaterPollutantAcceptFService boatWaterPollutantAcceptFService;
  @Resource
  private HyDangerPreDeclareNhService dangerPreDeclareNhService;

  @PostMapping("/getBoatWaterList")
  public ReturnMsg getBoatWaterList(@RequestBody @Validated ObjectBodyVo vo) throws ParseException {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    PollutantBoatWaterReq pollutantBoatWaterReq = JSONObject.parseObject(paramJsonData,
        PollutantBoatWaterReq.class);
    List<String> datesBetween = PollutantBoatWaterReq.getDatesBetween(
        pollutantBoatWaterReq.getStartTime(),
        pollutantBoatWaterReq.getEndTime());
    if (datesBetween.size() > 30) {
      return ReturnMsg.warn("开始时间和结束时间的最大间隔为三十天");
    }
    List<PollutantAcceptVo> pollutantAcceptVoList = boatWaterPollutantAcceptFService.selectBoatWaterList(
        pollutantBoatWaterReq, datesBetween);
    ResultRes resultRes = ResultRes.encapsulationResult(pollutantBoatWaterReq.getPageIndex(),
        pollutantBoatWaterReq.getPageSize(),
        CommonTools.getTotal(pollutantAcceptVoList), pollutantAcceptVoList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }

  @PostMapping("/getWarningInfoList")
  public ReturnMsg getWarningInfoList(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    PollutantWarningInfoReq pollutantWarningInfoReq = JSONObject.parseObject(paramJsonData,
        PollutantWarningInfoReq.class);
    List<PollutanWarningInfoVo> voList = boatWaterPollutantAcceptFService.selectWarningInfoList(
        pollutantWarningInfoReq);
    ResultRes resultRes = ResultRes.encapsulationResult(pollutantWarningInfoReq.getPageIndex(),
        pollutantWarningInfoReq.getPageSize(),
        CommonTools.getTotal(voList), voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }


  @PostMapping("/getWarningHisInfoList")
  public ReturnMsg getWarningHisInfoList(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    PollutantWarningInfoReq pollutantWarningInfoReq = JSONObject.parseObject(paramJsonData,
        PollutantWarningInfoReq.class);
    List<PollutanWarningInfoVo> voList = boatWaterPollutantAcceptFService.selectWarningHisInfoList(
        pollutantWarningInfoReq);
    ResultRes resultRes = ResultRes.encapsulationResult(pollutantWarningInfoReq.getPageIndex(),
        pollutantWarningInfoReq.getPageSize(),
        CommonTools.getTotal(voList), voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }


  @PostMapping("/getBoatWaterListSync")
  public ReturnMsg getBoatWaterListSync(@RequestBody @Validated ObjectBodyVo vo)
      throws ParseException {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    PollutantBoatWaterReq pollutantBoatWaterReq = JSONObject.parseObject(paramJsonData,
        PollutantBoatWaterReq.class);
    List<String> datesBetween = PollutantBoatWaterReq.getDatesBetween(
        pollutantBoatWaterReq.getStartTime(),
        pollutantBoatWaterReq.getEndTime());
    if (datesBetween.size() > 7) {
      return ReturnMsg.warn("开始时间和结束时间的最大间隔为七天");
    }
    List<BoatWaterPollutantAcceptF> pollutantAcceptVoList = boatWaterPollutantAcceptFService.selectBoatWaterListSync(
        pollutantBoatWaterReq, datesBetween);
    ResultRes resultRes = ResultRes.encapsulationResult(pollutantBoatWaterReq.getPageIndex(),
        pollutantBoatWaterReq.getPageSize(),
        CommonTools.getTotal(pollutantAcceptVoList), pollutantAcceptVoList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }


  @PostMapping("/getWarningInfoListSync")
  public ReturnMsg getWarningInfoListSync(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    PollutantWarningInfoReq pollutantWarningInfoReq = JSONObject.parseObject(paramJsonData,
        PollutantWarningInfoReq.class);
    List<BoatWaterPollutantWarningS> voList = boatWaterPollutantAcceptFService.selectWarningInfoListSync(
        pollutantWarningInfoReq);
    ResultRes resultRes = ResultRes.encapsulationResult(pollutantWarningInfoReq.getPageIndex(),
        pollutantWarningInfoReq.getPageSize(),
        CommonTools.getTotal(voList), voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }


  @PostMapping("/getOutRecordRecent")
  public ReturnMsg getOutRecordRecent(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    JSONObject jsonObject = JSONObject.parseObject(paramJsonData);
    List<PollutantAcceptVo> voList = dangerPreDeclareNhService.selectOutRecordRecent(
        (String) jsonObject.get("shipName"));
    ResultRes resultRes = ResultRes.encapsulationResult(voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }

}
