import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
import { genCode } from '../pubUtils/util';

/**
  * 举报记录
**/
@modelOptions({ schemaOptions: { id: false } })
@index({ code: 1 })
export default class AccuseRec extends BaseModel {
  @prop({ required: true, default: '' })
  code: string; // 唯一标识
  @prop({ required: true, default: '' })
  roleId: string; // 举报人的 roleId
  @prop({ required: true, default: '' })
  roleName: string; // 举报人名字
  @prop({ required: true, default: '' })
  targetRoleId: string; // 被举报人的 roleId
  @prop({ required: true, default: '' })
  targetRoleName: string; // 被举报人的名字

  @prop({ required: true, default: '' })
  targetMsgCode: string; // 被举报的消息标识
  @prop({ required: true, default: 0 })
  reason: number; // 举报原因：1，涉政；2，广告；3，辱骂；4，欺诈；5，其它
  @prop({ required: true, default: 0 })
  result: number; // 处理结果：0，默认值，未处理；1，删除；2，忽略
  @prop({ required: true, default: '' })
  remarks: string; // 处理备注

  public static async createRec(data: AccueseParam) {
    const code = genCode(8);
    const docData = new AccuseRecModel();
    const result: AccuseRecType = await AccuseRecModel.findOneAndUpdate({ code }, { ...docData.toJSON(), ...data, code }, { upsert: true, new: true }).select('-_id').lean();
    return result;
  }

  private static getSearchObj(form: {}) {
    let searchObj = {};
    console.log(form);
    return searchObj
  }

  public static async findByCondition(page: number, pageSize: number, sortField: string, sortOrder: string, form: {} = {}) {

    let searchObj = this.getSearchObj(form);
    let sort = {};
    if(sortField && sortOrder) {
        if(sortOrder == 'ascend') {
            sort[sortField] = 1;
        } else if (sortOrder == 'descend') {
            sort[sortField] = -1;
        }
    }
    console.log('page', page, 'pageSize', pageSize, 'sort', sort)
    const result: AccuseRecType[] = await AccuseRecModel.find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort(sort).lean({ getters: true, virtuals: true });
    return result;

  }

  public static async countByCondition(form: {_id?: string, content?: string} = {}) {

      let searchObj = this.getSearchObj(form);
      const result = await AccuseRecModel.count(searchObj);
      return result;
  }
}

export const AccuseRecModel = getModelForClass(AccuseRec);

export interface AccuseRecType extends Pick<DocumentType<AccuseRec>, keyof AccuseRec>{}
export type AccueseParam = Partial<AccuseRecType>;
