package com.shanghaichengdi.ghjgitem.controller;

import com.alibaba.fastjson.JSONObject;
import com.shanghaichengdi.ghjgitem.constant.HttpStatus;
import com.shanghaichengdi.ghjgitem.service.CommonService;
import com.shanghaichengdi.ghjgitem.util.CommonTools;
import com.shanghaichengdi.ghjgitem.vo.BoatAISVo;
import com.shanghaichengdi.ghjgitem.vo.CCbCargoDeclareRecVo;
import com.shanghaichengdi.ghjgitem.vo.CCbOnShipCrewRecVo;
import com.shanghaichengdi.ghjgitem.vo.ObjectBodyVo;
import com.shanghaichengdi.ghjgitem.vo.request.BoatAisRealTimeReq;
import com.shanghaichengdi.ghjgitem.vo.request.ReportSubInfoReq;
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
@Slf4j
@RequestMapping("/report")
public class ReportController extends CommonController {

  @Resource
  CommonService commonService;

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

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