/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/28
 */
package org.springblade.modules.business.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.constant.RtuDataInfoConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.business.data.entity.RtuDataWeatherEntity;
import org.springblade.modules.business.data.service.IRtuDataWeatherService;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;


/***
 * Date:2022/8/28
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/rtu/data/weather")
@AllArgsConstructor
@Api(value = "气象实时数据管理", tags = "气象实时数据管理")
public class RtuDataWeatherController extends BladeController {
    private final IRtuDataWeatherService weatherService;

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/test/{code}")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "")
    public R test(@PathVariable(name = "code") String code) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rtuCode", code);
        jsonObject.put(RtuDataInfoConstant.RTU_DATA_COLLECT_TIME_KEY, new Date());
        jsonObject.put(RtuDataInfoConstant.RTU_DATA_UP_TIME_KEY, new Date());
        jsonObject.put(RtuDataInfoConstant.RTU_DATA_PICK_TIME_KEY, new Date());
        jsonObject.put("AI", 1.2);
        jsonObject.put("MST", 2.3);
        kafkaTemplate.send("topic-ywxt-cloud-rtu-data-weather", jsonObject.toJSONString());
        return R.success("ok");
    }

    /**
     * 详情
     */
    @GetMapping("/detail/{code}")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "")
    public R<JSONObject> detail(@PathVariable(name = "code") String code) {
        JSONObject data = new JSONObject();
        LambdaQueryWrapper<RtuDataWeatherEntity> wrapper = Wrappers.<RtuDataWeatherEntity>query().lambda();
        wrapper.eq(RtuDataWeatherEntity::getRtuCode, code);
        wrapper.orderByDesc(RtuDataWeatherEntity::getTm);
        wrapper.last("limit 1");
        RtuDataWeatherEntity weatherEntity = weatherService.getOne(wrapper);
        data.put("rtuCode", code);
        data.put("tm", weatherEntity.getTm());
        data.put("ai", weatherEntity.getAi());
        data.put("mst", weatherEntity.getMst());
        return R.data(data);
    }
}
