后台:问卷
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
import { SearchSurveyParam } from '../domain/backEndField/search';
|
||||
import { Reward } from '../domain/battleField/pvp';
|
||||
import { genCode } from '../pubUtils/util';
|
||||
|
||||
@index({ id: 1 })
|
||||
@index({ seasonNum: 1 })
|
||||
export default class Survey extends BaseModel {
|
||||
|
||||
@prop({ required: true })
|
||||
code: string; // 唯一id
|
||||
|
||||
@prop({ required: true })
|
||||
surveyId: string; // 问卷id
|
||||
|
||||
@prop({ required: true })
|
||||
surveyName: string; // 问卷id
|
||||
surveyName: string; // 问卷标题
|
||||
|
||||
@prop({ required: true })
|
||||
roleIndex: number; // 问卷id
|
||||
roleIndex: number; // 玩家
|
||||
|
||||
@prop({ required: true })
|
||||
reward: string; // 奖励
|
||||
@prop({ required: true, type: Reward, _id: false })
|
||||
reward: Reward[]; // 奖励
|
||||
|
||||
@prop({ required: true })
|
||||
mailContent: string; // 邮件文字
|
||||
@@ -24,9 +30,49 @@ export default class Survey extends BaseModel {
|
||||
let rec: SurveyType = await SurveyModel.findOne({ surveyId }).lean();
|
||||
return rec;
|
||||
}
|
||||
|
||||
public static async updateSurvey(code: string, param: SurveyUpdate) {
|
||||
if(code == 'new') code = genCode(6);
|
||||
let rec: SurveyType = await SurveyModel.findOneAndUpdate({ code }, { $set: {...param, code} }, {new: true, upsert: true}).lean();
|
||||
return rec;
|
||||
}
|
||||
|
||||
public static async deleteSurvey(code: string) {
|
||||
let rec: SurveyType = await SurveyModel.deleteMany({ code }).lean();
|
||||
return rec;
|
||||
}
|
||||
|
||||
private static getSearchObj(form: SearchSurveyParam) {
|
||||
let searchObj = {};
|
||||
if (form.surveyName) searchObj['roleName'] = { $regex: new RegExp(form.surveyName.toString(), 'i') };
|
||||
return searchObj
|
||||
}
|
||||
|
||||
public static async findByCondition(page: number, pageSize: number, sortField: string = 'updatedAt', sortOrder: string = 'descend', form: SearchSurveyParam = {}) {
|
||||
|
||||
let searchObj = this.getSearchObj(form);
|
||||
let sort = {};
|
||||
if (sortField && sortOrder) {
|
||||
if (sortOrder == 'ascend') {
|
||||
sort[sortField] = 1;
|
||||
} else if (sortOrder == 'descend') {
|
||||
sort[sortField] = -1;
|
||||
}
|
||||
}
|
||||
const result: SurveyType[] = await SurveyModel.find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort(sort).lean({ getters: true, virtuals: true });
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public static async countByCondition(form: SearchSurveyParam = {}) {
|
||||
|
||||
let searchObj = this.getSearchObj(form);
|
||||
const result = await SurveyModel.count(searchObj);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const SurveyModel = getModelForClass(Survey);
|
||||
|
||||
export interface SurveyType extends Pick<DocumentType<Survey>, keyof Survey> {};
|
||||
export interface SurveyType extends Pick<DocumentType<Survey>, keyof Survey> { };
|
||||
export type SurveyUpdate = Partial<SurveyType>; // 将所有字段变成可选项
|
||||
|
||||
@@ -108,4 +108,8 @@ export interface SearchOrderParam {
|
||||
state?: number; // 订单状态
|
||||
createTimeStart?: number;
|
||||
createTimeEnd?: number;
|
||||
}
|
||||
}
|
||||
|
||||
export interface SearchSurveyParam {
|
||||
surveyName?: string; // 问卷名
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user