后台:礼包码
This commit is contained in:
119
shared/db/GiftCode.ts
Normal file
119
shared/db/GiftCode.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
||||
import { CounterModel } from './Counter';
|
||||
import { COUNTER } from '../consts';
|
||||
|
||||
class Rewards {
|
||||
@prop({ required: true })
|
||||
id: number;
|
||||
@prop({ required: true })
|
||||
count: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 举报记录
|
||||
**/
|
||||
@modelOptions({ schemaOptions: { id: false } })
|
||||
export default class GiftCode extends BaseModel {
|
||||
@prop({ required: true, default: '' })
|
||||
id: number; // 唯一id
|
||||
|
||||
@prop({ required: true, default: '' })
|
||||
name: string; // 礼包码名
|
||||
|
||||
@prop({ required: true, default: false })
|
||||
isLimit: boolean; // 每个码是否有使用次数限制
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
count: number; // 每个码可使用次数
|
||||
|
||||
@prop({ required: true, type: Rewards, _id: false })
|
||||
goods: Rewards[]; // 奖励
|
||||
|
||||
@prop({ required: true })
|
||||
beginTime: Date; // 开始时间
|
||||
|
||||
@prop({ required: true })
|
||||
endTime: Date; // 结束时间
|
||||
|
||||
@prop({ required: true, default: '' })
|
||||
remark: string; // 备注
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
generateType: number; // 生成类型
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
generateCnt: number; // 已生成条数,giftCodeDetail的数量
|
||||
|
||||
@prop({ required: true, default: '' })
|
||||
generateCode: string; // 单条的生成的那一个code
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
usedNum: number; // 使用次数
|
||||
|
||||
public static async findData(id: number) {
|
||||
let rec: GiftCodeType = await GiftCodeModel.findOne({ id }).lean();
|
||||
return rec;
|
||||
}
|
||||
|
||||
public static async updateData(id: string|number, values: GiftCodeParam, uid = 1) {
|
||||
if(id == 'new') {
|
||||
id = await CounterModel.getNewCounter(COUNTER.GIFT_CODE);
|
||||
}
|
||||
let doc = new GiftCodeModel();
|
||||
let createObj = doc.toJSON();
|
||||
delete values.id;
|
||||
delete createObj._id;
|
||||
delete createObj.id;
|
||||
for(let key in values) {
|
||||
delete createObj[key];
|
||||
}
|
||||
|
||||
let rec: GiftCodeType = await GiftCodeModel.findOneAndUpdate({ id }, { $set: {...values, updatedBy: uid}, $setOnInsert: { ...createObj, createdBy: uid } },
|
||||
{ new: true, upsert: true }).lean(true);
|
||||
return rec;
|
||||
}
|
||||
|
||||
public static async increaseUsedNum(id: number) {
|
||||
let result: GiftCodeType = await GiftCodeModel.findOneAndUpdate({ id }, { $inc: { usedNum: 1 } }, { new: true }).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
private static getSearchObj(form: { name?: string, current?: boolean }) {
|
||||
let searchObj = {};
|
||||
if (form.name != undefined) searchObj['name'] = { $regex: new RegExp(form.name.toString(), 'i') };
|
||||
if (form.current) {
|
||||
searchObj['beginTime'] = { $lte: new Date };
|
||||
searchObj['endTime'] = { $gte: new Date };
|
||||
}
|
||||
return searchObj
|
||||
}
|
||||
|
||||
public static async findByCondition(page: number, pageSize: number, sortField: string, sortOrder: string, form: { name?: string, current?: boolean } = {}) {
|
||||
|
||||
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: GiftCodeType[] = await GiftCodeModel.find(searchObj, { _id: 0 }).limit(pageSize).skip((page - 1) * pageSize).sort(sort).lean({ getters: true, virtuals: true });
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public static async countByCondition(form: { name?: string, current?: boolean } = {}) {
|
||||
|
||||
let searchObj = this.getSearchObj(form);
|
||||
const result = await GiftCodeModel.count(searchObj);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const GiftCodeModel = getModelForClass(GiftCode);
|
||||
|
||||
export interface GiftCodeType extends Pick<DocumentType<GiftCode>, keyof GiftCode> { }
|
||||
export type GiftCodeParam = Partial<GiftCodeType>;
|
||||
57
shared/db/GiftCodeDetail.ts
Normal file
57
shared/db/GiftCodeDetail.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType, modelOptions, Ref, mongoose } from '@typegoose/typegoose';
|
||||
import GiftCode, { GiftCodeType } from './GiftCode';
|
||||
import { genCode } from '../pubUtils/util';
|
||||
|
||||
/**
|
||||
* 举报记录
|
||||
**/
|
||||
@modelOptions({ schemaOptions: { id: false } })
|
||||
@index({ code: 1 })
|
||||
export default class GiftCodeDetail extends BaseModel {
|
||||
@prop({ required: true, default: '' })
|
||||
code: string; // 兑换码
|
||||
|
||||
@prop({ ref: 'GiftCode', type: mongoose.Schema.Types.ObjectId })
|
||||
giftCode: Ref<GiftCode>;
|
||||
|
||||
@prop({ required: true, default: '' })
|
||||
usedNum: number; // 该码使用次数
|
||||
|
||||
|
||||
// 根据code
|
||||
public static async findByCode(code: string) {
|
||||
let result: GiftCodeDetailType = await GiftCodeDetailModel.findOne({ code }).populate('giftCode').lean(true);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async findByGiftCode(giftCode: GiftCodeType) {
|
||||
let result: GiftCodeDetailType[] = await GiftCodeDetailModel.find({ giftCode: giftCode._id }).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async increaseUsedNum(code: string) {
|
||||
let result: GiftCodeDetailType = await GiftCodeDetailModel.findOneAndUpdate({ code }, { $inc: { usedNum: 1 } }, { new: true }).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async generateOne(giftCode: GiftCodeType, code: string, uid = 1 ) {
|
||||
let result = await GiftCodeDetailModel.insertMany([{ giftCode: giftCode._id, code, usedNum: 0, createdBy: uid, updatedBy: uid }]);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async generateMany(giftCode: GiftCodeType, generateNum: number, codeLen: number, uid = 1) {
|
||||
let insertArr: GiftCodeDetailParam[] = [];
|
||||
for(let i = 0; i < generateNum; i++) {
|
||||
insertArr.push({ giftCode: giftCode._id, code: genCode(codeLen), usedNum: 0, createdBy: uid, updatedBy: uid });
|
||||
}
|
||||
let result = await GiftCodeDetailModel.insertMany(insertArr);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const GiftCodeDetailModel = getModelForClass(GiftCodeDetail);
|
||||
|
||||
export interface GiftCodeDetailType extends Pick<DocumentType<GiftCodeDetail>, keyof GiftCodeDetail> { }
|
||||
export type GiftCodeDetailParam = Partial<GiftCodeDetailType>;
|
||||
36
shared/db/UserGiftCode.ts
Normal file
36
shared/db/UserGiftCode.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType, modelOptions, Ref, mongoose } from '@typegoose/typegoose';
|
||||
import GiftCode, { GiftCodeType } from './GiftCode';
|
||||
|
||||
/**
|
||||
* 举报记录
|
||||
**/
|
||||
@modelOptions({ schemaOptions: { id: false } })
|
||||
@index({ roleId: 1, code: 1 })
|
||||
export default class UserGiftCode extends BaseModel {
|
||||
|
||||
@prop({ required: true, default: '' })
|
||||
roleId: string; // 玩家id
|
||||
|
||||
@prop({ required: true, default: '' })
|
||||
code: string; // 兑换码
|
||||
|
||||
@prop({ ref: 'GiftCode', type: mongoose.Schema.Types.ObjectId })
|
||||
giftCode: Ref<GiftCode>;
|
||||
|
||||
// 根据code
|
||||
public static async findByCode(roleId: string, code: string) {
|
||||
let result: UserGiftCodeType = await UserGiftCodeModel.findOne({ roleId, code }).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async createCode(roleId: string, code: string, gitCode: GiftCodeType) {
|
||||
let result: UserGiftCodeType = await UserGiftCodeModel.findOneAndUpdate({ roleId, code }, { $setOnInsert: { gitCode: gitCode._id } }, { new: true, upsert: true}).lean()
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const UserGiftCodeModel = getModelForClass(UserGiftCode);
|
||||
|
||||
export interface UserGiftCodeType extends Pick<DocumentType<UserGiftCode>, keyof UserGiftCode> { }
|
||||
export type UserGiftCodeParam = Partial<UserGiftCodeType>;
|
||||
Reference in New Issue
Block a user