// 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`);
    }
}



