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.ObjectBodyVo;
import com.shanghaichengdi.ghjgitem.vo.request.PortDrawReq;
import com.shanghaichengdi.ghjgitem.vo.request.ReportSubInfoReq;
import com.shanghaichengdi.ghjgitem.vo.response.ResultRes;
import com.shanghaichengdi.ghjgitem.vo.response.ReturnMsg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("portDraw")
public class PortDrawPictureShareController extends CommonController {
    @Resource
    CommonService commonService;

    @PostMapping("getPortDrawPicture")
    public ReturnMsg getPortDrawPicture(@RequestBody @Validated ObjectBodyVo vo) {
        if (!checkDbKey(vo.getKey())) {
            return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
        }
        String paramJsonData = getParamJsonData(vo.getData());
        PortDrawReq portDrawReq = JSONObject.parseObject(paramJsonData,
                PortDrawReq.class);
        if (StringUtils.isBlank(portDrawReq.getDrawId()) && StringUtils.isBlank(portDrawReq.getCertNumber())) {
            return ReturnMsg.error(HttpStatus.BAD_REQUEST, "参数有缺失,请确认您输入的参数正确!");
        }
        List<String> objects = commonService.getPortDrawPicture(portDrawReq);
        ResultRes resultRes = ResultRes.encapsulationResult(portDrawReq.getPageIndex(),
                portDrawReq.getPageSize(),
                CommonTools.getTotal(objects), objects);
        String jsonString = JSONObject.toJSONString(resultRes);
        String encodedData = resultEncode(jsonString);
        return ReturnMsg.success("获取成功", encodedData);
    }
}
