package com.shanghaichengdi.ghjgitem.controller;

import com.alibaba.fastjson.JSONObject;
import com.shanghaichengdi.ghjgitem.constant.HttpStatus;
import com.shanghaichengdi.ghjgitem.domain.WaterLevelData;
import com.shanghaichengdi.ghjgitem.service.WaterLevelDataService;
import com.shanghaichengdi.ghjgitem.util.CommonTools;
import com.shanghaichengdi.ghjgitem.vo.ObjectBodyVo;
import com.shanghaichengdi.ghjgitem.vo.request.WaterLevelDataReq;
import com.shanghaichengdi.ghjgitem.vo.response.ResultRes;
import com.shanghaichengdi.ghjgitem.vo.response.ReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("waterLevel")
public class WaterLevelDataController extends CommonController{

    @Resource
    private WaterLevelDataService service;

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

}
