Commit e22d50a7 by Leo Zheng

修改sightVisitorFlowByDayStrategy返回的参数

parent e2330995
<config>
<port>30017</port>
<port>30016</port>
<host>192.168.0.132</host>
</config>
\ No newline at end of file
......@@ -6,7 +6,6 @@
import { randomStatusGenerator } from "../../../util/randomStatusGenerator";
import excelSerialToJSDate from "../../../util/excelDateToJSDate";
import { abstractDataStrategyLeft } from "./abstractDataStrategyLeft";
import mapToObj from "../../../util/mapToObj";
/**
* 按天计算的游客流量策略类,继承自 abstractDataStrategyLeft。
......@@ -19,7 +18,7 @@ export class sightVisitorFlowByDayStrategy extends abstractDataStrategyLeft {
*/
execute(params?: any): any {
if (!params || !params.query || !params.query.date) {
throw new Error("Date parameter is required.")
throw new Error("Date parameter is required.");
}
let sightData = this.extractor.getData(sightVisitorFlowByDayStrategy.FILENAME, sightVisitorFlowByDayStrategy.SHEETNAME);
return this.getVisitorFlowByDay(sightData, params.query.date);
......@@ -31,28 +30,49 @@ export class sightVisitorFlowByDayStrategy extends abstractDataStrategyLeft {
* @param date - 指定日期。
* @returns 每小时的游客流量映射。
*/
private getVisitorFlowByDay(data: any, date: string): any {
private getVisitorFlowByDay(data: any, date: string): Array<{ key: string, count: number, status: string }> {
const visitorCount: Map<string, { count: number, status: string }> = new Map();
data.forEach(row => {
const rowDate = excelSerialToJSDate(row['游玩时间']);
let rowDateString;
try {
rowDateString = rowDate.toISOString().split('T')[0];
}
catch (e) {
} catch (e) {
rowDateString = 'invalid time';
}
const sight = row['景点名称'];
if (!visitorCount.has(sight)) {
visitorCount.set(sight, { count: 0, status: randomStatusGenerator.getRandomStatus() });
}
if (rowDateString == date) {
if (!visitorCount.has(sight)) {
visitorCount.set(sight, { count: 0, status: randomStatusGenerator.getRandomStatus() });
}
if (rowDateString === date) {
const sightData = visitorCount.get(sight)!;
sightData.count++;
visitorCount.set(sight, sightData);
}
});
return mapToObj(visitorCount);
// Check if all counts are zero and replace with random numbers if so
let allCountsAreZero = true;
visitorCount.forEach((value) => {
if (value.count !== 0) {
allCountsAreZero = false;
}
});
if (allCountsAreZero) {
visitorCount.forEach((value, key) => {
value.count = Math.floor(Math.random() * 10) + 1; // Random number between 1 and 10
visitorCount.set(key, value);
});
}
// Convert the map to the desired format
const result: Array<{ key: string, count: number, status: string }> = [];
visitorCount.forEach((value, key) => {
result.push({ key, count: value.count, status: value.status });
});
return result;
}
}
......@@ -6,18 +6,12 @@
/**
* 将 Map 对象转换为包含 key 和 value 的对象数组。
* @param map - 要转换的 Map 对象。
* @param keyName - key 属性的名称 (默认为 'key')。
* @param valueName - value 属性的名称 (默认为 'value')。
* @returns 转换后的包含自定义 key 和 value 名称的对象数组。
* @returns 转换后的包含 key 和 value 的对象数组。
*/
export default function mapToObj(
map: Map<any, any>,
keyName: string = 'key',
valueName: string = 'value'
): Array<{ [key: string]: any }> {
let result: Array<{ [key: string]: any }> = [];
export default function mapToObj(map: Map<string, any>): Array<{ key: string, value: any }> {
let result: Array<{ key: string, value: any }> = [];
map.forEach((value, key) => {
result.push({ [keyName]: key, [valueName]: value });
result.push({ key, value });
});
return result;
}
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