wangrong
2022-03-16 a45591fc22c088fe45fafb651b0c4575cc5c2f0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// src/schedule/hello.ts
import { Provide, Inject, Schedule, CommonSchedule } from '@midwayjs/decorator';
import { DataService } from '../service/dataapi/dataapi';
import * as fs from 'fs';
import { ILogger } from '@midwayjs/logger';
 
/**
 * 日志定时任务
 */
@Provide()
@Schedule({
    cron: '00 53 15 * * *', // 每天0点执行
    type: 'worker', // 指定某一个 worker 执行
})
export class BaseEntitySchedule implements CommonSchedule {
    @Inject()
    dataService: DataService;
 
    @Inject()
    logger: ILogger;
 
    // 定时执行的具体任务
    async exec() {
        this.logger.info('生成综合实体类任务开始执行');
        const startTime = Date.now();
        if (fs.existsSync('../entity/app/commentity.ts')) {
            console.log(' file is exists')
        } else {
            console.log(' file is not exists')
            // API
            fs.writeFile('./src/app/modules/base/entity/app/commentity.ts', 
            `import { EntityModel } from '@midwayjs/orm';
            import { BaseEntity } from '@cool-midway/core';
            import { Column } from 'typeorm';
            
            /**
             * 文件空间信息
             */
            @EntityModel('t_sys_comm_column')
            export class CommEntity extends BaseEntity {
              @Column({ comment: '地址' })
              url: string;
            
              @Column({ comment: '类型' })
              type: string;
            
              @Column({ comment: '分类ID', type: 'bigint', nullable: true })
              classifyId: number;
            }`, (error) => {
                // 创建失败
                if(error){
                    console.log(`创建失败:${error}`)
                }
                // 创建成功
                console.log(`创建成功!`)
                
            })
        }
        const columns = await this.dataService.getData({"bid":"getColumns", "pageindex":"1", "pagesize":"300"})  //bid_obj_[0], query?.pageindex || null , query?.pagesize || null, query
        console.log(columns)
        //读取文件内容,并转化为Json对象
        // let userBugsJson = JSON.parse(fs.readFileSync("app/public/static/Data.json", "utf8"));
        // //获取Json里key为data的数据
        // const data = userBugsJson['data'];
        // return data;
        this.logger.info(`生成综合实体类任务结束,耗时:${Date.now() - startTime}ms`);
    }
}