返回 AiToEarn
controller.ts
1 /*
2 * @Author: nevin
3 * @Date: 2025-01-20 22:02:54
4 * @LastEditTime: 2025-03-28 13:33:08
5 * @LastEditors: nevin
6 * @Description: autoRun AutoRun
7 */
8 import { Controller, Icp, Inject, Scheduled } from '../core/decorators';
9 import { AutoRunService } from './service';
10 import { AutoRunStatus, AutoRunType } from '../../db/models/autoRun';
11 import { getUserInfo } from '../user/comment';
12 import { EtEvent } from '../../global/event';
13 import { autoRunTypeEtTag } from './comment';
14 import { AutoRunRecordStatus } from '../../db/models/autoRunRecord';
15
16 @Controller()
17 export class AutoRunController {
18 @Inject(AutoRunService)
19 private readonly autoRunService!: AutoRunService;
20
21 /**
22 * 创建进程
23 */
24 @Icp('ICP_AUTO_RUN_CREATE')
25 async createAutoRun(
26 event: Electron.IpcMainInvokeEvent,
27 info: {
28 accountId: number;
29 type: AutoRunType;
30 cycleType: string;
31 },
32 data: Record<string, any>, // 对象
33 ) {
34 const userInfo = getUserInfo();
35
36 const autoRun = await this.autoRunService.createAutoRun(
37 {
38 userId: userInfo.id,
39 ...info,
40 },
41 data,
42 );
43
44 return autoRun;
45 }
46
47 /**
48 * 进程列表
49 */
50 @Icp('ICP_AUTO_RUN_LIST')
51 async getAutoRunList(
52 event: Electron.IpcMainInvokeEvent,
53 pageInfo: {
54 page: number;
55 pageSize: number;
56 },
57 query: {
58 type?: AutoRunType;
59 status?: AutoRunStatus;
60 cycleType?: string;
61 accountId?: number;
62 dataId?: string;
63 },
64 ) {
65 const list = await this.autoRunService.findAutoRunList(pageInfo, query);
66 return list;
67 }
68
69 /**
70 * 更新进程状态
71 */
72 @Icp('ICP_AUTO_RUN_STATUS')
73 async updateAutoRunStatus(
74 event: Electron.IpcMainInvokeEvent,
75 id: number,
76 status: AutoRunStatus,
77 ) {
78 const autoRun = await this.autoRunService.updateAutoRunStatus(id, status);
79
80 return autoRun;
81 }
82
83 /**
84 * 创建进程记录
85 */
86 @Icp('ICP_AUTO_RUN_RECORD_CREATE')
87 async createAutoRunRecord(
88 event: Electron.IpcMainInvokeEvent,
89 autoRunId: number,
90 ) {
91 const autoData = await this.autoRunService.findAutoRunById(autoRunId);
92 if (!autoData) return null;
93 const autoRunRecord =
94 await this.autoRunService.createAutoRunRecord(autoData);
95
96 return autoRunRecord;
97 }
98
99 /**
100 * 更新进程记录状态
101 */
102 @Icp('ICP_AUTO_RUN_RECORD_STATUS')
103 async updateAutoRunRecordStatus(
104 event: Electron.IpcMainInvokeEvent,
105 id: number,
106 status: number,
107 ) {
108 const autoRun = await this.autoRunService.updateAutoRunRecordStatus(
109 id,
110 status,
111 );
112
113 return autoRun;
114 }
115
116 /**
117 * 获取进程记录列表
118 */
119 @Icp('ICP_AUTO_RUN_RECORD_LIST')
120 async getCommentList(
121 event: Electron.IpcMainInvokeEvent,
122 pageInfo: {
123 page: number;
124 pageSize: number;
125 },
126 query: {
127 autoRunId: number;
128 type?: AutoRunType;
129 status?: AutoRunRecordStatus;
130 cycleType?: string;
131 },
132 ): Promise<any> {
133 const list = await this.autoRunService.findAutoRunRecordList(
134 pageInfo,
135 query,
136 );
137
138 return list;
139 }
140
141 // 立即执行进程
142 @Icp('ICP_RUN_NOW_AUTO_RUN')
143 async autoRunStart(event: Electron.IpcMainInvokeEvent, id: number) {
144 const item = await this.autoRunService.findAutoRunById(id);
145 if (!item) return false;
146
147 const tag = autoRunTypeEtTag.get(item.type);
148 if (!tag) return false;
149
150 EtEvent.emit(tag, item);
151
152 console.log('---- autoRunStart ----');
153 return true;
154 }
155
156 // 每5分钟进行一次自动启动
157 @Scheduled('*/1 * * * *', 'all_auto_run_start')
158 async syncAllAutoRunStart() {
159 console.log('---- syncAllAutoRunStart ----');
160 try {
161 const userInfo = getUserInfo();
162
163 const autoRunList = await this.autoRunService.findAutoRunListOfNeedRun(
164 userInfo.id,
165 );
166
167 autoRunList.map(async (item) => {
168 const tag = autoRunTypeEtTag.get(item.type);
169 if (!tag) return;
170
171 const needRun = await this.autoRunService.isNeedAutoRunToRun(item);
172 if (!needRun) return;
173
174 EtEvent.emit(tag, item);
175 });
176 } catch (error) {
177 console.error('---- syncAllAutoRunStart ---- error', error);
178 }
179 }
180 }
181
181 lines TYPESCRIPT