/**
 * @desc 页面历史记录表
 */
import { AutoIncrement, Column, DataType, Model, PrimaryKey, Table, AllowNull, ForeignKey, BelongsTo, DefaultScope, Scopes } from 'sequelize-typescript';
import { PageInfo } from './pageInfo';

@Scopes({
  relative: {
    attributes: ['id', 'author', 'updatedAt', 'createdAt']
  }
})

 @Table({
   modelName: 'page_history',
   freezeTableName: true
 })
 
 export class PageHistory extends Model<PageHistory> {
 
   @PrimaryKey
   @AutoIncrement
   @Column({
     type: DataType.INTEGER(11)
   })
   id: number;
 
   @Column({
     field: 'page_data',
     type: DataType.TEXT
   })
   page: string;
 
   @Column({
     field: 'page_name',
     type: DataType.STRING(32)
   })
   pageName: string;
 
   @Column({
     field: 'page_describe',
     type: DataType.STRING(255)
   })
   pageDescribe: string;
 
   @Column({
     field: 'page_keywords',
     type: DataType.STRING(255)
   })
   pageKeywords: string;
 
   @Column({
     field: 'uuid',
     type: DataType.UUID
   })
   uuid: string;
 
  //  @BelongsTo(() => PageInfo)
  //  pageInfo: PageInfo;

   @Column({
     field: 'redirectUrl',
     type: DataType.STRING(255),
     get() {
       const redirectUrl = this.getDataValue('redirectUrl');
       return !redirectUrl ? '' : redirectUrl;
     },
   })
   redirectUrl: string;
 
   @Column({
     field: 'validEndTime',
     type: DataType.STRING(32),
     get() {
       const validEndTime = this.getDataValue('validEndTime');
       return !validEndTime ? '' : validEndTime;
     },
   })
   validEndTime: string;
 
   @Column({
     field: 'validStartTime',
     type: DataType.STRING(32),
     get() {
       const validStartTime = this.getDataValue('validStartTime');
       return !validStartTime ? '' : validStartTime;
     },
   })
   validStartTime: string;
 
   @Column({
     type: DataType.INTEGER(1)
   })
   enable: number;
 
   @AllowNull(false)
   @Column({
     type: DataType.STRING(32)
   })
   author: string;
 
   @Column({
     type: DataType.STRING(32),
     field: 'cover_image'
   })
   coverImage: string;
 
   @Column({
     type: DataType.INTEGER(1),
     field: 'is_template'
   })
   isTemplate: number;
 
   @Column({
     type: DataType.INTEGER(1),
     field: 'is_publish'
   })
   isPublish: number;
 
   @Column({
     type: DataType.DATE,
     field: 'updated_at',
     get() {
       const moment = require('moment');
       const updatedAt = this.getDataValue('updatedAt');
       return moment(updatedAt).utc().zone(-8).format('YYYY-MM-DD HH:mm:ss');
     },
   })
   updatedAt: string;

   @Column({
    type: DataType.DATE,
    field: 'created_at',
    get() {
      const moment = require('moment');
      const createdAt = this.getDataValue('createdAt');
      return moment(createdAt).format('YYYY-MM-DD HH:mm:ss');
    },
  })
  createdAt: string;
 
   @ForeignKey(() => PageInfo)
   @Column({
    field: 'page_id',
    type: DataType.INTEGER(11)
   })
   pageId: number;

   @Column({
     type: DataType.INTEGER(11),
     field: 'tenant_id',
   })
   tenantId: number;
 }
 
 export default () => PageHistory;