Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com
55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
import { Controller, Post, Get, Query, Inject } from "@midwayjs/core";
|
|
import { Context } from "@midwayjs/koa";
|
|
import { InjectRepository } from "@midwayjs/sequelize";
|
|
import { Repository } from "sequelize-typescript";
|
|
import { IccAnalogSyncService } from "../service/icc_analog_sync.service";
|
|
import ElectricityData from "../entity/electricity_data.entity";
|
|
|
|
@Controller("/api/electricity-data")
|
|
export class ElectricityDataController {
|
|
@Inject() ctx!: Context;
|
|
@Inject() iccAnalogSyncService!: IccAnalogSyncService;
|
|
@InjectRepository(ElectricityData)
|
|
electricityDataRepo!: Repository<ElectricityData>;
|
|
|
|
@Post("/sync") async sync() {
|
|
const result = await this.iccAnalogSyncService.syncFromIcc(this.ctx);
|
|
return { code: 200, message: "模拟量数据同步完成", data: result };
|
|
}
|
|
|
|
/** 查询某线路最近一次同步的模拟量快照(取最大 collect_time 的全部模拟量行) */
|
|
@Get("/latest") async latest(@Query("lineId") lineId?: string) {
|
|
const id = Number(lineId);
|
|
if (!id || Number.isNaN(id)) {
|
|
return { code: 400, message: "缺少有效的 lineId 参数" };
|
|
}
|
|
const latest = await this.electricityDataRepo.findOne({
|
|
where: { lineId: id },
|
|
order: [["collectTime", "DESC"]],
|
|
attributes: ["lineCode", "lineName", "imei", "customerId", "collectTime"],
|
|
});
|
|
if (!latest) {
|
|
return { code: 200, message: "该线路暂无模拟量数据", data: { lineId: id, hasData: false, collectTime: null, list: [] } };
|
|
}
|
|
const rows = await this.electricityDataRepo.findAll({
|
|
where: { lineId: id, collectTime: latest.getDataValue("collectTime") as Date },
|
|
order: [["analogType", "ASC"]],
|
|
attributes: ["analogType", "analogName", "analogUnit", "analogValue"],
|
|
});
|
|
return {
|
|
code: 200,
|
|
message: "ok",
|
|
data: {
|
|
lineId: id,
|
|
lineCode: latest.getDataValue("lineCode"),
|
|
lineName: latest.getDataValue("lineName"),
|
|
imei: latest.getDataValue("imei"),
|
|
customerId: latest.getDataValue("customerId"),
|
|
collectTime: latest.getDataValue("collectTime"),
|
|
hasData: true,
|
|
list: rows,
|
|
},
|
|
};
|
|
}
|
|
}
|