Commit c5485f21 authored by duanruiming's avatar duanruiming

[update] 推送神思策略控制接口

parent d1caa6c5
package net.wanji.opt.synthesis.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import net.wanji.common.framework.rest.JsonViewObject;
import net.wanji.opt.synthesis.pojo.StrategyControlVO;
import net.wanji.opt.synthesis.service.StrategyControlService;
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 javax.annotation.Resource;
import javax.ws.rs.core.MediaType;
/**
* @author duanruiming
* @date 2024/11/03 12:42
* @description 神思电子获取1.1策略计划基础信息
*/
@Api(value = "StrategyControlController", description = "策略控制")
@RequestMapping("/strategyControl")
@RestController
public class StrategyControlController {
@Resource
private StrategyControlService strategyControlService;
@ApiOperation(value = "策略控制信息操作", notes = "策略控制信息操作",
response = JsonViewObject.class,
produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
@PostMapping(value = "/strategyInfoOperation",
produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
@ApiResponses({
@ApiResponse(code = 200, message = "OK", response = StrategyControlVO.class),
})
public JsonViewObject strategyInfoOperation(@RequestBody StrategyControlVO strategyControlVO) throws Exception {
return strategyControlService.strategyInfoOperation(strategyControlVO);
}
}
package net.wanji.opt.synthesis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author duanruiming
* @date 2024/11/03 14:09
* @description 神思电子返回实体
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
private String code;
private String msg;
private Object data;
}
package net.wanji.opt.synthesis.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author duanruiming
* @date 2024/11/03 18:13
*/
@Data
public class StrategyControlDataReq {
/**
* 路口干线Id
*/
private String biz_id;
/**
* 类型 0-路口 1-干线
*/
private Integer biz_type;
/**
* 策略类型 0-绿波带 1-失衡 2-溢出 3-空放
*/
private Integer strategy;
private String schedule_start;
private String schedule_end;
private List<Time_table> time;
/**
* 调控频率(秒),策略下发的频率(只针对失衡和绿波带有效)
*/
private Integer freq;
/**
* 状态(1=开启,0=停止)
*/
private Integer status;
/**
* 操作(insert=新增,update=更新,delete=删除)
*/
private String action;
@Data
@NoArgsConstructor
public static class Time_table {
/**
* 星期周日至周六 0-6 7为每天不限时
*/
private Integer week;
/**
* 时间段,格式为“开始时间-结束时间”,时间格式为hh:mm:ss,范围00:00:00到23:59:59
*/
private String[] time_list;
}
}
package net.wanji.opt.synthesis.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
/**
* @author duanruiming
* @date 2024/11/03 18:18
*/
@Data
@ApiModel(value = "StrategyControlDataVO", description = "策略控制操作数据实体")
public class StrategyControlDataVO {
@ApiModelProperty(value = "路口干线Id")
private String bizId;
@ApiModelProperty(value = "类型 0-路口 1-干线")
private Integer bizType;
@ApiModelProperty(value = "策略类型 0-绿波带 1-失衡 2-溢出 3-空放")
private Integer strategy;
@ApiModelProperty(value = "开始时间日期")
private Date scheduleStart;
@ApiModelProperty(value = "结束时间日期")
private Date scheduleEnd;
@ApiModelProperty(value = "日计划表")
private List<StrategyControlDataVO.TimeTable> time;
@ApiModelProperty(value = "调控频率(秒),策略下发的频率(只针对失衡和绿波带有效)")
private Integer freq;
@ApiModelProperty(value = "状态(1=开启,0=停止)")
private Integer status;
@ApiModelProperty(value = "操作(insert=新增,update=更新,delete=删除)")
private String action;
@Data
@NoArgsConstructor
public static class TimeTable {
@ApiModelProperty(value = "星期周日至周六 0-6 7为每天不限时")
private Integer week;
@ApiModelProperty(value = "时间段,格式为“开始时间-结束时间”,时间格式为hh:mm:ss,范围00:00:00到23:59:59")
private String[] timeList;
}
}
package net.wanji.opt.synthesis.pojo;
import lombok.Data;
import java.util.List;
/**
* @author duanruiming
* @date 2024/11/03 14:12
* @description 策略基础信息推送请求
*/
@Data
public class StrategyControlReq {
private List<StrategyControlDataReq> data;
}
package net.wanji.opt.synthesis.pojo;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.util.List;
/**
* @author duanruiming
* @date 2024/11/03 17:31
*/
@Data
@ApiModel(value = "StrategyControlVO", description = "策略控制操作实体")
public class StrategyControlVO {
private List<StrategyControlDataVO> dataList;
}
package net.wanji.opt.synthesis.service;
import net.wanji.opt.synthesis.pojo.Result;
import net.wanji.opt.synthesis.pojo.StrategyControlReq;
/**
* @author duanruiming
* @date 2024/11/03 14:06
*/
public interface PushStrategyControlService {
void insert();
void update();
void delete();
Result push(StrategyControlReq req) throws Exception;
}
package net.wanji.opt.synthesis.service;
import net.wanji.common.framework.rest.JsonViewObject;
import net.wanji.opt.synthesis.pojo.StrategyControlVO;
/**
* @author duanruiming
* @date 2024/11/03 17:36
*/
public interface StrategyControlService {
JsonViewObject strategyInfoOperation(StrategyControlVO strategyControlVO) throws Exception;
}
package net.wanji.opt.synthesis.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import net.wanji.common.tool.resttool.RestTemplateTool;
import net.wanji.common.utils.tool.JacksonUtils;
import net.wanji.opt.synthesis.pojo.Result;
import net.wanji.opt.synthesis.pojo.StrategyControlReq;
import net.wanji.opt.synthesis.service.PushStrategyControlService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Objects;
/**
* @author duanruiming
* @date 2024/11/03 16:25
*/
@Service
@Slf4j
public class PushStrategyControlServiceImpl implements PushStrategyControlService {
@Override
public void insert() {
}
@Override
public void update() {
}
@Override
public void delete() {
}
@Override
public Result push(StrategyControlReq req) throws Exception {
String url = "";
try {
ObjectMapper mapper = JacksonUtils.getInstance();
String resultStr = RestTemplateTool.post(url, mapper.writeValueAsString(req));
Result result = mapper.readValue(resultStr, Result.class);
if (Objects.nonNull(result)) {
if (StringUtils.endsWithIgnoreCase("200", result.getCode())) {
return result;
}
return result;
} else {
log.error("策略推送url:{}失败:返回数据异常", url);
return new Result("500", "服务调用失败", null);
}
} catch (Exception e) {
log.error("策略控制推送url:{},失败:{}", url, e);
throw new Exception(e);
}
}
}
package net.wanji.opt.synthesis.service.impl;
import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import net.wanji.common.enums.DateStyle;
import net.wanji.common.framework.Constants;
import net.wanji.common.framework.rest.JsonViewObject;
import net.wanji.common.utils.tool.JacksonUtils;
import net.wanji.opt.synthesis.pojo.*;
import net.wanji.opt.synthesis.service.PushStrategyControlService;
import net.wanji.opt.synthesis.service.StrategyControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
/**
* @author duanruiming
* @date 2024/11/03 17:37
*/
@Slf4j
@Service
public class StrategyControlServiceImpl implements StrategyControlService {
@Autowired
private PushStrategyControlService pushStrategyControlService;
@Override
public JsonViewObject strategyInfoOperation(StrategyControlVO strategyControlVO) throws Exception {
try {
StrategyControlReq req = convertReq(strategyControlVO);
// 存库
// 调用推送
//StrategyControlDataReq detailData = new StrategyControlDataReq();
//detailData.setBiz_id("241940");
//detailData.setBiz_type(0);
//detailData.setStrategy(3);
//Date date = new Date();
//detailData.setSchedule_end(DateUtil.format(date, DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
//detailData.setSchedule_end(DateUtil.format(date, DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
//StrategyControlDataReq.Time_table timeTable = new StrategyControlDataReq.Time_table();
//timeTable.setWeek(1);
//timeTable.setTime_list(new String[]{"00:00-01:00", "01:00-02:00"});
//StrategyControlDataReq.Time_table timeTable1 = new StrategyControlDataReq.Time_table();
//timeTable1.setWeek(1);
//timeTable1.setTime_list(new String[]{"02:00-03:00", "03:00-04:00"});
//detailData.setTime(Arrays.asList(timeTable1, timeTable));
//detailData.setFreq(1000 * 60);
//detailData.setStrategy(1);
//detailData.setAction("insert");
// todo 测试数据
//req.setData(Arrays.asList(detailData));
Result result = pushStrategyControlService.push(req);
if (Objects.nonNull(result)) {
JsonViewObject jsonViewObject = JsonViewObject.newInstance();
jsonViewObject.setCode(Integer.valueOf(result.getCode()));
jsonViewObject.setMessage(result.getMsg());
jsonViewObject.setContent(result.getData());
jsonViewObject.setStatus(StringUtils.equals("200", result.getCode()) ? Constants.JsonView.STATUS_SUCCESS : Constants.JsonView.STATUS_FAIL);
return jsonViewObject;
} else {
return JsonViewObject.newInstance().fail("推送神思策略控制失败!");
}
} catch (Exception e) {
log.error("策略操作失败:{}", e);
throw new Exception(e);
}
}
private StrategyControlReq convertReq(StrategyControlVO vo) throws Exception {
try {
List<StrategyControlDataVO> dataList = vo.getDataList();
StrategyControlReq req = new StrategyControlReq();
if (!CollectionUtils.isEmpty(dataList)) {
List<StrategyControlDataReq> dataReqList = new ArrayList<>(dataList.size());
for (StrategyControlDataVO dataVO : dataList) {
StrategyControlDataReq detailData = new StrategyControlDataReq();
detailData.setBiz_id(dataVO.getBizId());
detailData.setBiz_type(dataVO.getBizType());
detailData.setStrategy(dataVO.getStrategy());
detailData.setSchedule_start(DateUtil.format(dataVO.getScheduleStart(), DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
detailData.setSchedule_end(DateUtil.format(dataVO.getScheduleEnd(), DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
List<StrategyControlDataVO.TimeTable> timeTableList = dataVO.getTime();
List<StrategyControlDataReq.Time_table> timeTables = new ArrayList<>(timeTableList.size());
timeTableList.forEach(item -> {
StrategyControlDataReq.Time_table timeTable = new StrategyControlDataReq.Time_table();
timeTable.setTime_list(item.getTimeList());
timeTable.setWeek(item.getWeek());
timeTables.add(timeTable);
});
detailData.setTime(timeTables);
detailData.setFreq(dataVO.getFreq());
detailData.setStatus(dataVO.getStatus());
detailData.setAction(dataVO.getAction());
dataReqList.add(detailData);
req.setData(dataReqList);
}
}
return req;
} catch (Exception e) {
log.error("策略控制操作数据转换异常:{}", e);
throw new Exception(e);
}
}
public static void main(String[] args) throws Exception {
StrategyControlDataReq detailData = new StrategyControlDataReq();
detailData.setBiz_id("241940");
detailData.setBiz_type(0);
detailData.setStrategy(3);
Date date = new Date();
detailData.setSchedule_start(DateUtil.format(date, DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
detailData.setSchedule_end(DateUtil.format(date, DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
StrategyControlDataReq.Time_table timeTable = new StrategyControlDataReq.Time_table();
timeTable.setWeek(1);
timeTable.setTime_list(new String[]{"00:00-01:00", "01:00-02:00"});
StrategyControlDataReq.Time_table timeTable1 = new StrategyControlDataReq.Time_table();
timeTable1.setWeek(2);
timeTable1.setTime_list(new String[]{"02:00-03:00", "03:00-04:00"});
detailData.setTime(Arrays.asList(timeTable1, timeTable));
detailData.setFreq(1000 * 60);
detailData.setStatus(1);
detailData.setAction("insert");
// todo 测试数据
StrategyControlReq req = new StrategyControlReq();
req.setData(Arrays.asList(detailData));
System.err.println(JacksonUtils.getInstance().writeValueAsString(req));
System.err.println(new Date());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment