Newer
Older
newfiber-data-adapter / src / main / java / org / springnewfiber / dataadapter / newfiber / controller / WaterQualityController.java
@dongjt dongjt on 14 Nov 2022 3 KB 水质船对接
package org.springnewfiber.dataadapter.newfiber.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
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;
import org.springnewfiber.dataadapter.config.BladeRedis;
import org.springnewfiber.dataadapter.config.R;
import org.springnewfiber.dataadapter.entity.PtReceiveBaseModel;
import org.springnewfiber.dataadapter.newfiber.entity.WaterQualityDto;
import org.springnewfiber.dataadapter.newfiber.entity.WaterQualityEntity;
import org.springnewfiber.dataadapter.sswj.util.RealTimeSerializer;
import org.springnewfiber.dataadapter.xf.XfDataEnum;
import org.springnewfiber.dataadapter.xf.enums.EDataAccessType;
import org.springnewfiber.dataadapter.xf.service.DataAccessRecordService;

@RestController
@RequiredArgsConstructor
//@PreAuth(AuthConstant.PERMISSION_ALL)
@RequestMapping("/waterQuality")
@Api(value = "水质船监测数据接口", tags = "水质船监测数据接口")
@Slf4j
public class WaterQualityController {
    private final RabbitTemplate rabbitTemplate;
    private final DataAccessRecordService dataAccessRecordService;
    private final BladeRedis redis;

    @PostMapping("/newfiber-dhgx")
    public R xfInterface(@RequestBody WaterQualityDto dto) {
        log.error("data:{},flag:{}", JSONObject.toJSONString(dto), XfDataEnum.dhgx_szq.getRemark());
        dataAccessRecordService.save(EDataAccessType.IflytekRealtime, XfDataEnum.dhgx_szq.getCode(), JSONObject.toJSONString(dto));
        if(redis.exists(dto.getSt())){
            WaterQualityDto  old=  redis.get(dto.getSt());
                if(old!=null&& DateUtil.betweenMs(old.getTt(),dto.getTt())>=(1000*4*60*60)){
                    WaterQualityEntity entity = new WaterQualityEntity();
                    BeanUtils.copyProperties(dto, entity);
                    entity.setStcd(dto.getSt());
                    entity.setStnm(dto.getSt());
                    entity.setTm(dto.getTt());
                    PtReceiveBaseModel model = RealTimeSerializer.xfObjectToRealMap(entity);
                    log.info("cover:{}", JSONObject.toJSONString(model));
                    rabbitTemplate.convertAndSend(JSONObject.toJSONString(model));
                    redis.set(dto.getSt(),dto);
                }
        }else {
            WaterQualityEntity entity = new WaterQualityEntity();
            BeanUtils.copyProperties(dto, entity);
            entity.setStcd(dto.getSt());
            entity.setStnm(dto.getSt());
            entity.setTm(dto.getTt());
            PtReceiveBaseModel model = RealTimeSerializer.xfObjectToRealMap(entity);
            log.info("cover:{}", JSONObject.toJSONString(model));
            rabbitTemplate.convertAndSend(JSONObject.toJSONString(model));
            redis.set(dto.getSt(),dto);
        }
        return R.status(true);
    }


    public static void main(String[] args) {
        String bigDateStr="2022-10-10 08:00:00";
        String endDateStr="2022-10-10 09:00:00";
       long time= DateUtil.betweenMs(DateUtil.parse(bigDateStr, DatePattern.NORM_DATETIME_PATTERN),DateUtil.parse(endDateStr, DatePattern.NORM_DATETIME_PATTERN));
        System.out.println(time/60/1000/60);
    }
}