package com.mvc.controller.beihai;


import com.mvc.model.beihai.TRawdata;
import com.mvc.netty.dealconstant.ResultTag;
import com.mvc.netty.dealconstant.ResultType;
import com.mvc.service.beihai.RawdataService;
import com.mvc.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rawdata")
public class RawdataController {
    private static final Logger log = LoggerFactory.getLogger(RawdataController.class);

    @Autowired
    private RawdataService rawdataService;

    //dddddddddddddddddddddddddddddd
    @RequestMapping("/list/{telemetryStationCode}/{pageSize}/{offset}")
    @ResponseBody
    public Map<String, Object> queryPage(TRawdata rawdata) {
        return rawdataService.findPage(rawdata);
    }


    //dddddddddddddddddddddddddddddd
    //检索分类设备最新数据
    @RequestMapping("/list/classify")
    @ResponseBody
    public Map<String, Object> queryNewBySearch(Integer classifyId, String eqSearch, Integer companyId) {
        Map<String, Object> retMap = new HashMap<>();

        List<TRawdata> rawdataList = null;
        try {
            rawdataList = rawdataService.findNewBySearch(classifyId, eqSearch, companyId);
            retMap.put(ResultTag.ERROR.getTag(), ResultType.SUCCESS.getResult());
            retMap.put(ResultTag.DATA.getTag(), rawdataList);
        } catch (Exception e) {
            retMap.put(ResultTag.ERROR.getTag(), ResultType.FAILURE.getResult());
            retMap.put(ResultTag.MESSAGE.getTag(), "获取报文信息失败");
        }

        return retMap;
    }

    //dddddddddddddddddddddddddddddd
    @RequestMapping("/between/{telemetryStationCode}")
    @ResponseBody
    public Map<String, String> getBetweenDate(TRawdata rawdata) {
        Map<String, String> retMap = new HashMap<>();
        Date lastest = rawdataService.findLastestTime(rawdata);
        if (lastest == null) {
            lastest = new Date();
        }
        retMap.put("endTime", DateUtil.formateDate(lastest,"yyyy-MM-dd HH:mm"));
        retMap.put("startTime", DateUtil.getTimeStr(lastest, -(24 * 60 * 60),"yyyy-MM-dd HH:mm"));
        return retMap;
    }
}
