邮件:修改邮件逻辑
This commit is contained in:
+14
-19
@@ -1,29 +1,22 @@
|
||||
/**
|
||||
* 邮件的模板,在GM后台能看到的邮件列表
|
||||
*/
|
||||
import BaseModel from './BaseModel';
|
||||
import { getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
import { nowSeconds } from '../pubUtils/timeUtil';
|
||||
import { GM_MAIL_TYPE } from '../consts/constModules/mailConst';
|
||||
|
||||
class RewardInter {
|
||||
class Reward {
|
||||
@prop({ required: true })
|
||||
id: number;
|
||||
@prop({ required: true })
|
||||
count: number;
|
||||
}
|
||||
|
||||
class SendRole {
|
||||
@prop({ required: true })
|
||||
roleId: string;
|
||||
}
|
||||
|
||||
export default class GMMail extends BaseModel {
|
||||
|
||||
@prop({ required: true })
|
||||
serverId: number;
|
||||
|
||||
@prop({ required: true, type: SendRole, default:[], _id: false })
|
||||
sendRoles: SendRole[];
|
||||
|
||||
@prop({ required: true, type: RewardInter, default: [], _id: false })
|
||||
goods: RewardInter[];
|
||||
@prop({ required: true, type: Reward, default: [], _id: false })
|
||||
goods: Reward[];
|
||||
|
||||
@prop({ required: true, default: nowSeconds() })
|
||||
sendTime: number;
|
||||
@@ -37,13 +30,13 @@ export default class GMMail extends BaseModel {
|
||||
@prop({ required: true })
|
||||
sendName: string;
|
||||
|
||||
@prop({ required: true })
|
||||
gmMailType: number; //1:群体邮件,2:分服务器邮件
|
||||
@prop({ required: true, enum: GM_MAIL_TYPE })
|
||||
mailType: GM_MAIL_TYPE; // 1-单人邮件 2-多人邮件 3-全服邮件
|
||||
|
||||
public static async addMail(params: {serverId: number, sendRoles: SendRole[], sendName: string, endTime: number, content: string, gmMailType: number, sendTime?: number, goods?: RewardInter[] }, lean = true) {
|
||||
public static async addMail(params: GMMailTypeParam) {
|
||||
const doc = new GMMailModel();
|
||||
let mail = Object.assign(doc.toJSON(), params);
|
||||
mail = await GMMailModel.findOneAndUpdate({ _id: mail._id }, { $set: mail }, {upsert: true, new: true}).lean(lean);
|
||||
mail = await GMMailModel.findOneAndUpdate({ _id: mail._id }, { $set: mail }, {upsert: true, new: true}).lean();
|
||||
return mail;
|
||||
}
|
||||
|
||||
@@ -66,4 +59,6 @@ export default class GMMail extends BaseModel {
|
||||
|
||||
export const GMMailModel = getModelForClass(GMMail);
|
||||
|
||||
export interface GMMailType extends Pick<DocumentType<GMMail>, keyof GMMail> { };
|
||||
export interface GMMailType extends Pick<DocumentType<GMMail>, keyof GMMail> { };
|
||||
|
||||
export type GMMailTypeParam = Partial<GMMailType>; // 将所有字段变成可选项
|
||||
+87
-56
@@ -1,61 +1,94 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
/**
|
||||
* 多人邮件
|
||||
*/
|
||||
import MailTemp from './MailTemp';
|
||||
import { index, getModelForClass, prop, DocumentType, Ref, mongoose } from '@typegoose/typegoose';
|
||||
import { nowSeconds } from '../pubUtils/timeUtil';
|
||||
import { MAIL_STATUS, MAIL_TYPE } from '../consts/constModules/mailConst';
|
||||
import { findWhere } from 'underscore';
|
||||
|
||||
class RewardInter {
|
||||
@prop({ required: true })
|
||||
id: number;
|
||||
@prop({ required: true })
|
||||
count: number;
|
||||
}
|
||||
class SendRole {
|
||||
class RoleStatus {
|
||||
@prop({ required: true })
|
||||
roleId: string;
|
||||
@prop({ required: true })
|
||||
status: number;
|
||||
}
|
||||
|
||||
@index({ sendRoles: 1 })
|
||||
@index({ mailId: 1, type: 1 })
|
||||
export default class GroupMail extends BaseModel {
|
||||
@prop({ required: true, type: SendRole, default: [], _id: false })
|
||||
sendRoles: SendRole[];
|
||||
@index({ 'roleStatus.roleId': 1 })
|
||||
export default class GroupMail extends MailTemp {
|
||||
|
||||
@prop({ required: true })
|
||||
mailId: string;
|
||||
|
||||
@prop({ required: true, type:RewardInter, default: [], _id: false})
|
||||
goods: RewardInter[];
|
||||
@prop({ required: true, type: RoleStatus })
|
||||
roleStatus: RoleStatus[];
|
||||
|
||||
@prop({ required: true, default: nowSeconds() })
|
||||
sendTime: number;
|
||||
|
||||
@prop({ required: true, default: nowSeconds() })
|
||||
endTime: number;
|
||||
|
||||
@prop({ required: true })
|
||||
params: string[];
|
||||
|
||||
@prop({ required: true })
|
||||
mailTemType: number; //1:表示模板邮件,2:表示系统邮件
|
||||
|
||||
@prop({ required: true })
|
||||
sendName: string;
|
||||
public static async addGroupMails(mails: Array<GroupMailType>) {
|
||||
await GroupMailModel.insertMany(mails);
|
||||
/**
|
||||
* @description 根据玩家id获得所有有效邮件
|
||||
* @param {String} roleId 玩家id
|
||||
*/
|
||||
public static async findByRoleId(roleId: string) {
|
||||
const result: GroupMailType[] = await GroupMailModel.find({
|
||||
'roleStatus.roleId': roleId,
|
||||
'roleStatus.status': { $in: [ MAIL_STATUS.CREATE, MAIL_STATUS.READ, MAIL_STATUS.RECEIVED ] },
|
||||
sendTime:{ $lte: nowSeconds() }, endTime: { $gte: nowSeconds() }
|
||||
}).populate('mail').lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async addGroupMail(params:{ sendRoles: SendRole[],mailId: string, mailTemType:number, goods?: Array<RewardInter>, sendName?: string, endTime?:number, sendTime?: number, params?:string[] }) {
|
||||
/**
|
||||
* @description 创建邮件
|
||||
* @param params
|
||||
*/
|
||||
public static async addMail(params: GroupMailTypeParam) {
|
||||
const doc = new GroupMailModel();
|
||||
const mail = Object.assign(doc.toJSON(), params);
|
||||
await GroupMailModel.create(mail);
|
||||
return mail;
|
||||
delete mail._id;
|
||||
let rec: GroupMailType = await GroupMailModel.findByIdAndUpdate(doc._id, mail, { new: true, upsert: true }).populate('mail').lean();
|
||||
return rec;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id删除单条邮件
|
||||
* @param _id
|
||||
*/
|
||||
public static async delMailById(_id: string, roleId: string) {
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate(
|
||||
{
|
||||
_id, sendTime:{ $lte: nowSeconds() },
|
||||
$or: [{
|
||||
roleStatus: {$elemMatch: {roleId, status: MAIL_STATUS.READ}}, hasGoods:false
|
||||
}, {
|
||||
roleStatus: {$elemMatch: {roleId, status: MAIL_STATUS.RECEIVED}}, hasGoods: true
|
||||
}]
|
||||
},
|
||||
{ $set:{ 'roleStatus.$.status': MAIL_STATUS.DELETE } },
|
||||
{ new: true }
|
||||
).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据玩家id批量删除
|
||||
* @param _id
|
||||
* @param roleId
|
||||
*/
|
||||
public static async delMailsByRoleId(roleId: string) {
|
||||
const ids: { _id: string }[] = await GroupMailModel.find( {
|
||||
sendTime:{ $lte: nowSeconds() },
|
||||
$or: [{
|
||||
roleStatus: {$elemMatch: {roleId, status: MAIL_STATUS.READ}}, hasGoods:false
|
||||
}, {
|
||||
roleStatus: {$elemMatch: {roleId, status: MAIL_STATUS.RECEIVED}}, hasGoods: true
|
||||
}]
|
||||
},).select('_id').lean();
|
||||
const result: GroupMailType[] = [];
|
||||
for(let _id of ids) {
|
||||
let rec = await GroupMailModel.findOneAndUpdate({ _id, 'roleStatus.roleId': roleId }, { $set:{ 'roleStatus.$.status': MAIL_STATUS.DELETE }}, { new: true }).lean();
|
||||
result.push(rec);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async getGroupMailsByRoleId(roleId: string, lean = true) {
|
||||
const result: GroupMailType[] = await GroupMailModel.find({ 'sendRoles.roleId': roleId, 'sendRoles.status': { $ne: -1}, sendTime:{$lte: nowSeconds()} }).lean(lean);
|
||||
const result: GroupMailType[] = await GroupMailModel.find({ 'roleStatus.roleId': roleId, 'roleStatus.status': { $ne: -1}, sendTime:{$lte: nowSeconds()} }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -65,7 +98,7 @@ export default class GroupMail extends BaseModel {
|
||||
}
|
||||
|
||||
public static async pushRoleMail( roleId: string, mailId: string, mailTemType: number, lean = true) {
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({ mailId, mailTemType, sendTime:{$lte: nowSeconds()} }, {$push: {sendRoles: {roleId, status: 0}}}).lean(lean);
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({ mailId, mailTemType, sendTime:{$lte: nowSeconds()} }, {$push: {roleStatus: {roleId, status: 0}}}).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -74,40 +107,38 @@ export default class GroupMail extends BaseModel {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async updateMailByStatus(_id: string, roleId: string, status: number, conditions: number[], lean = true) {
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({ _id, 'sendRoles.roleId': roleId,sendTime:{$lte: nowSeconds()}, 'sendRoles.status': {$in: conditions} }, {$set:{ 'sendRoles.$.status': status }}, { new: true }).lean(lean);
|
||||
public static async updateStatusWithCondition(_id: string, roleId: string, status: number, conditions: number[], lean = true) {
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({ _id, 'roleStatus.roleId': roleId, sendTime:{$lte: nowSeconds()}, 'roleStatus.status': {$in: conditions} }, {$set:{ 'roleStatus.$.status': status }}, { new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async deleteMail(_id: string, roleId: string, status: number, lean = true) {
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({ _id, 'sendRoles.roleId': roleId, sendTime:{$lte: nowSeconds()}}, {$set:{ 'sendRoles.$.status': status }}, { new: true }).lean(lean);
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({ _id, 'roleStatus.roleId': roleId, sendTime:{$lte: nowSeconds()}}, {$set:{ 'roleStatus.$.status': status }}, { new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async findReadAndRewardsMails(roleId: string, lean = true) {
|
||||
const ids: GroupMailType[] = await GroupMailModel.find({ sendTime:{$lte: nowSeconds()}, sendRoles: { $elemMatch: { roleId, $or:[ {$and: [{status: MAIL_STATUS.READ}, {goods: []}]},{status: MAIL_STATUS.RECEIVED} ] } } }).select('_id').lean(lean);
|
||||
const ids: GroupMailType[] = await GroupMailModel.find({ sendTime:{$lte: nowSeconds()}, roleStatus: { $elemMatch: { roleId, $or:[ {$and: [{status: MAIL_STATUS.READ}, { hasGoods: true }]},{status: MAIL_STATUS.RECEIVED} ] } } }).select('_id').lean(lean);
|
||||
return ids;
|
||||
}
|
||||
|
||||
public static async updateMailStatus(ids: string[], status: number, roleId: string, lean = true) {
|
||||
public static async updateStatusByIds(ids: string[], status: number, roleId: string, lean = true) {
|
||||
let mails = [];
|
||||
for(let id of ids) {
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({_id: id, "sendRoles.roleId": roleId}, { $set:{ 'sendRoles.$.status': status } }, { new: true }).lean(lean);
|
||||
let { status: resStatus } = findWhere(result.sendRoles, { roleId });
|
||||
mails.push({id: result._id, status: resStatus, mailType: MAIL_TYPE.GROUPMAIL});
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({_id: id, "roleStatus.roleId": roleId}, { $set:{ 'roleStatus.$.status': status } }, { new: true }).lean(lean);
|
||||
let { status: resStatus } = findWhere(result.roleStatus, { roleId });
|
||||
mails.push({id: result._id, status: resStatus, /*mailType: MAIL_TYPE.GROUPMAIL*/});
|
||||
}
|
||||
return mails;
|
||||
}
|
||||
|
||||
public static async delMail(_id: string, roleId: string, lean = true) {
|
||||
const result: GroupMailType = await GroupMailModel.findOneAndUpdate({ _id, sendTime:{ $lte: nowSeconds() },
|
||||
sendRoles: { $elemMatch: { roleId, $or:[ {$and: [{status: MAIL_STATUS.READ}, {goods: []}]},{status: MAIL_STATUS.RECEIVED} ] } } },
|
||||
{ $set:{ 'sendRoles.$.status': MAIL_STATUS.DELETE } }, { new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询有奖励的邮件
|
||||
* @param roleId
|
||||
* @param lean
|
||||
*/
|
||||
public static async findRewardsMails(roleId: string, lean = true) {
|
||||
const result: GroupMailType[] = await GroupMailModel.find({ sendRoles: { $elemMatch:{roleId, status:{ $in: [ MAIL_STATUS.CREATE, MAIL_STATUS.READ ] }} }, sendTime:{$lte: nowSeconds()} }).select('_id goods mailId mailTemType status').lean(lean);
|
||||
const result: GroupMailType[] = await GroupMailModel.find({ roleStatus: { $elemMatch:{roleId, status:{ $in: [ MAIL_STATUS.CREATE, MAIL_STATUS.READ ] }} }, sendTime:{$lte: nowSeconds()} }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
+83
-61
@@ -1,59 +1,97 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
import MailTemp from './MailTemp';
|
||||
import { index, getModelForClass, prop, DocumentType, Ref, mongoose } from '@typegoose/typegoose';
|
||||
import { nowSeconds } from '../pubUtils/timeUtil';
|
||||
import { MAIL_STATUS, MAIL_TYPE } from '../consts/constModules/mailConst';
|
||||
|
||||
class RewardInter {
|
||||
@prop({ required: true })
|
||||
id: number;
|
||||
@prop({ required: true })
|
||||
count: number;
|
||||
}
|
||||
|
||||
|
||||
@index({ roleId: 1 })
|
||||
@index({ roleId: 1, mailId: 1, type: 1 })
|
||||
|
||||
export default class Mail extends BaseModel {
|
||||
export default class Mail extends MailTemp {
|
||||
|
||||
@prop({ required: true })
|
||||
roleId: string;
|
||||
|
||||
@prop({ required: true })
|
||||
mailId: string;
|
||||
|
||||
@prop({ required: true, type: RewardInter, default: [], _id: false })
|
||||
goods: Array<RewardInter>;
|
||||
|
||||
@prop({ required: true, default: nowSeconds() })
|
||||
sendTime: number;
|
||||
|
||||
@prop({ required: true, type: String, _id: false})
|
||||
params: string[];
|
||||
|
||||
@prop({ required: true, default: MAIL_STATUS.CREATE })
|
||||
status: number;
|
||||
|
||||
@prop({ required: true })
|
||||
mailTemType: number; //1:表示从gm下发的邮件,2:表示系统邮件
|
||||
|
||||
@prop({ required: true })
|
||||
sendName: string;
|
||||
|
||||
@prop({ required: true })
|
||||
endTime: number;
|
||||
public static async addMails(mails: Array<MailType>) {
|
||||
await MailModel.insertMany(mails);
|
||||
/**
|
||||
* @description 根据玩家id获得所有有效邮件
|
||||
* @param {String} roleId 玩家id
|
||||
*/
|
||||
public static async findByRoleId(roleId: string) {
|
||||
const result: MailType[] = await MailModel.find({
|
||||
roleId,
|
||||
status: { $in: [ MAIL_STATUS.CREATE, MAIL_STATUS.READ, MAIL_STATUS.RECEIVED ] },
|
||||
sendTime:{ $lte: nowSeconds() }, endTime: { $gte: nowSeconds() }
|
||||
}).populate('mail').lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async addMail(params: { roleId: string, goods: Array<RewardInter>, sendName: string, mailId: string, endTime: number, mailTemType: number, params?: string[], sendTime?: number }) {
|
||||
/**
|
||||
* @description 创建邮件
|
||||
* @param {MailTypeParam} params 邮件内容
|
||||
*/
|
||||
public static async addMail(params: MailTypeParam) {
|
||||
const doc = new MailModel();
|
||||
const mail = Object.assign(doc.toJSON(), params);
|
||||
await MailModel.create(mail);
|
||||
return mail;
|
||||
delete mail._id;
|
||||
let rec: MailType = await MailModel.findByIdAndUpdate(doc._id, mail, { new: true, upsert: true }).lean();
|
||||
return rec;
|
||||
}
|
||||
|
||||
public static async getMailsByRoleId(roleId: string, lean = true) {
|
||||
const result: MailType[] = await MailModel.find({ roleId, status: { $ne: -1 }, sendTime:{$lte: nowSeconds()} }).lean(lean);
|
||||
/**
|
||||
* 根据id更新单个邮件状态
|
||||
* @param _id
|
||||
* @param status 邮件状态
|
||||
* @param conditions 可更新邮件状态
|
||||
*/
|
||||
public static async updateStatusWithCondition(_id: string, status: MAIL_STATUS, conditions: MAIL_STATUS[]) {
|
||||
const result: MailType = await MailModel.findOneAndUpdate(
|
||||
{ _id, status: { $in: conditions }, sendTime:{$lte: nowSeconds()} },
|
||||
{ $set: { status } }, { new: true }
|
||||
).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id更新多条邮件状态
|
||||
* @param ids
|
||||
* @param status
|
||||
* @param lean
|
||||
*/
|
||||
public static async updateStatusByIds(ids: string[], status: number, lean = true) {
|
||||
let mails = [];
|
||||
for(let id of ids) {
|
||||
const result: MailType = await MailModel.findOneAndUpdate({_id: id}, { $set:{ 'status': status } }, { new: true }).lean(lean);
|
||||
mails.push({id: result._id, status: result.status, /* mailType: MAIL_TYPE.SINGLEMAIL */});
|
||||
}
|
||||
return mails;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id删除单条邮件
|
||||
* @param _id
|
||||
*/
|
||||
public static async delMailById(_id: string) {
|
||||
const result: MailType = await MailModel.findOneAndUpdate(
|
||||
{ _id, $or: [{ $and: [{ status: MAIL_STATUS.READ, hasGoods: false }] }, { status: MAIL_STATUS.RECEIVED }], sendTime:{$lte: nowSeconds()} },
|
||||
{ $set: { status: MAIL_STATUS.DELETE } },
|
||||
{ new: true }
|
||||
).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据玩家id批量删除
|
||||
* @param _id
|
||||
* @param roleId
|
||||
*/
|
||||
public static async delMailsByRoleId(roleId: string) {
|
||||
const ids: { _id: string }[] = await MailModel.find({ roleId, $or: [{ $and: [{ status: MAIL_STATUS.READ, hasGoods: false }] }, { status: MAIL_STATUS.RECEIVED }], sendTime:{$lte: nowSeconds()} }).select('_id').lean();
|
||||
let result: MailType[] = [];
|
||||
for(let _id of ids) {
|
||||
let rec = await MailModel.findByIdAndUpdate(_id, { $set:{ status: MAIL_STATUS.DELETE }}, { new: true }).lean();
|
||||
result.push(rec);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -67,37 +105,21 @@ export default class Mail extends BaseModel {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async updateMailByStatus(_id: string, conditions: number[], update: MailTypeParam, lean = true) {
|
||||
const result: MailType = await MailModel.findOneAndUpdate({ _id, status: { $in: conditions }, sendTime:{$lte: nowSeconds()} }, { $set: update }, { new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async updateMail(_id: string, update: MailTypeParam, lean = true) {
|
||||
const result: MailType = await MailModel.findOneAndUpdate({ _id, sendTime:{$lte: nowSeconds()} }, { $set: update }, { new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async findReadAndRewardsMails(roleId: string, lean = true) {
|
||||
const ids: MailType[] = await MailModel.find({ roleId, $or: [{ $and: [{ status: MAIL_STATUS.READ, goods: [] }] }, { status: MAIL_STATUS.RECEIVED }], sendTime:{$lte: nowSeconds()} }).select('_id').lean(lean);
|
||||
return ids;
|
||||
}
|
||||
|
||||
public static async updateMailStatus(ids: string[], status: number, lean = true) {
|
||||
let mails = [];
|
||||
for(let id of ids) {
|
||||
const result: MailType = await MailModel.findOneAndUpdate({_id: id}, { $set:{ 'status': status } }, { new: true }).lean(lean);
|
||||
mails.push({id: result._id, status: result.status, mailType: MAIL_TYPE.SINGLEMAIL});
|
||||
}
|
||||
return mails;
|
||||
}
|
||||
|
||||
public static async delMail(_id: string, lean = true) {
|
||||
const result: MailType = await MailModel.findOneAndUpdate({ _id, $or: [{ $and: [{ status: MAIL_STATUS.READ, goods: [] }] }, { status: MAIL_STATUS.RECEIVED }], sendTime:{$lte: nowSeconds()} }, { $set: { status: MAIL_STATUS.DELETE } }, { new: true }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询有奖励的邮件
|
||||
* @param roleId
|
||||
* @param lean
|
||||
*/
|
||||
public static async findRewardsMails(roleId: string, lean = true) {
|
||||
const result: MailType[] = await MailModel.find({ roleId, status: {$in: [ MAIL_STATUS.CREATE, MAIL_STATUS.READ ] }, sendTime:{$lte: nowSeconds()} }).select('_id goods mailId mailTemType status').lean(lean);
|
||||
const result: MailType[] = await MailModel.find({ roleId, status: {$in: [ MAIL_STATUS.CREATE, MAIL_STATUS.READ ] }, sendTime:{$lte: nowSeconds()} }).lean(lean);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { prop, Ref, mongoose } from '@typegoose/typegoose';
|
||||
import GMMail from './GMMail';
|
||||
|
||||
class Reward {
|
||||
@prop({ required: true })
|
||||
id: number;
|
||||
@prop({ required: true })
|
||||
count: number;
|
||||
}
|
||||
|
||||
export default class MailTemp extends BaseModel {
|
||||
|
||||
@prop({ required: true })
|
||||
contentId: number; // dic_email_content.json中的id
|
||||
|
||||
@prop({ ref: 'GMMail', type: mongoose.Schema.Types.ObjectId })
|
||||
mail: Ref<GMMail>;
|
||||
|
||||
@prop({ required: true })
|
||||
sendTime: number;
|
||||
|
||||
@prop({ required: true })
|
||||
endTime: number;
|
||||
|
||||
@prop({ required: true, type: String, _id: false})
|
||||
params: string[];
|
||||
|
||||
@prop({ required: true })
|
||||
sendName: string; // 发件人
|
||||
|
||||
@prop({ required: true })
|
||||
hasGoods: boolean;
|
||||
|
||||
@prop({ required: true, type: Reward, default: [], _id: false })
|
||||
goods: Array<Reward>;
|
||||
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
class RewardInter {
|
||||
class Reward {
|
||||
@prop({ required: true })
|
||||
id: number;
|
||||
@prop({ required: true })
|
||||
count: number;
|
||||
}
|
||||
|
||||
class HeroRewardInter {
|
||||
class HeroReward {
|
||||
@prop({ required: true })
|
||||
hid: number;
|
||||
@prop({ required: true })
|
||||
@@ -27,10 +27,10 @@ export class HeroScores {
|
||||
}
|
||||
|
||||
export interface pvpUpdate {
|
||||
rankGoods?:Array<RewardInter>;
|
||||
rankGoods?:Array<Reward>;
|
||||
oldSeasonData?: SeasonData;
|
||||
show?: boolean;
|
||||
heroGoods?:Array<HeroRewardInter>;
|
||||
heroGoods?:Array<HeroReward>;
|
||||
}
|
||||
|
||||
class SeasonData {
|
||||
@@ -73,11 +73,11 @@ export default class PvpSeasonResult extends BaseModel {
|
||||
@prop({ required: true, default: true })
|
||||
show: boolean;
|
||||
|
||||
@prop({ required: true, type: HeroRewardInter, default: [], _id: false})
|
||||
heroGoods: Array<HeroRewardInter>;
|
||||
@prop({ required: true, type: HeroReward, default: [], _id: false})
|
||||
heroGoods: Array<HeroReward>;
|
||||
|
||||
@prop({ required: true, type: RewardInter, default: [], _id: false})
|
||||
rankGoods: Array<RewardInter>;
|
||||
@prop({ required: true, type: Reward, default: [], _id: false})
|
||||
rankGoods: Array<Reward>;
|
||||
|
||||
public static async updatePvpSeasonResult(roleId: string, update: pvpUpdate , lean = true) {
|
||||
let result: PvpSeasonResultType = await PvpSeasonResultModel.findOneAndUpdate({roleId}, {$set: update}, {upsert: true, new: true}).lean(lean);
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* 全服邮件
|
||||
*/
|
||||
import MailTemp from './MailTemp';
|
||||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
import { nowSeconds } from '../pubUtils/timeUtil';
|
||||
import { MAIL_STATUS } from '../consts/constModules/mailConst';
|
||||
|
||||
class RoleStatus {
|
||||
@prop({ required: true })
|
||||
roleId: string;
|
||||
@prop({ required: true })
|
||||
status: number;
|
||||
}
|
||||
|
||||
@index({ sendRoles: 1 })
|
||||
export default class ServerMail extends MailTemp {
|
||||
@prop({ required: true })
|
||||
serverId: number;
|
||||
|
||||
@prop({ required: true, type: RoleStatus })
|
||||
roleStatus: RoleStatus[];
|
||||
|
||||
@prop({ required: true, type: String })
|
||||
delRoles: string[];
|
||||
|
||||
/**
|
||||
* @description 根据玩家id获得所有有效邮件
|
||||
* @param {String} roleId 玩家id
|
||||
* @param {Number} serverId 服务器id
|
||||
*/
|
||||
public static async findByRoleId(roleId: string, serverId: number) {
|
||||
const result: ServerMailType[] = await ServerMailModel.find({
|
||||
serverId,
|
||||
delRoles: { $ne: roleId },
|
||||
sendTime:{ $lte: nowSeconds() }, endTime: { $gte: nowSeconds() }
|
||||
}).populate('mail').lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 创建邮件
|
||||
* @param params
|
||||
*/
|
||||
public static async addMail(params: ServerMailTypeParam) {
|
||||
const doc = new ServerMailModel();
|
||||
const mail = Object.assign(doc.toJSON(), params);
|
||||
delete mail._id;
|
||||
let rec: ServerMailType = await ServerMailModel.findByIdAndUpdate(doc._id, mail, { new: true, upsert: true }).lean();
|
||||
return rec;
|
||||
}
|
||||
/**
|
||||
* 根据id删除单条邮件
|
||||
* @param _id
|
||||
* @param lean
|
||||
*/
|
||||
public static async delMailById(_id: string, roleId: string) {
|
||||
const result: ServerMailType = await ServerMailModel.findOneAndUpdate(
|
||||
{
|
||||
sendTime:{ $lte: nowSeconds() },
|
||||
$or: [{
|
||||
roleStatus: {$elemMatch: {roleId, status: MAIL_STATUS.READ}}, hasGoods:false
|
||||
}, {
|
||||
roleStatus: {$elemMatch: {roleId, status: MAIL_STATUS.RECEIVED}}, hasGoods: true
|
||||
}]
|
||||
},
|
||||
{ $set:{ 'roleStatus.$.status': MAIL_STATUS.DELETE, $push: { 'delRoles': roleId } } },
|
||||
{ new: true }
|
||||
).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据玩家id批量删除
|
||||
* @param _id
|
||||
* @param roleId
|
||||
*/
|
||||
public static async delMailsByRoleId(roleId: string) {
|
||||
const ids: { _id: string }[] = await ServerMailModel.find(
|
||||
{
|
||||
sendTime:{ $lte: nowSeconds() },
|
||||
$or: [{
|
||||
roleStatus: {$elemMatch: {roleId, status: MAIL_STATUS.READ}}, hasGoods:false
|
||||
}, {
|
||||
roleStatus: {$elemMatch: {roleId, status: MAIL_STATUS.RECEIVED}}, hasGoods: true
|
||||
}]
|
||||
}
|
||||
).select('_id').lean();
|
||||
let result: ServerMailType[] = [];
|
||||
for(let _id of ids) {
|
||||
let rec = await ServerMailModel.findOneAndUpdate({ _id, 'roleStatus.roleId': roleId }, { $set:{ 'roleStatus.$.status': MAIL_STATUS.DELETE }, $push: { delRoles: roleId }}, { new: true }).lean();
|
||||
result.push(rec);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static async updateStatusWithCondition(_id: string, roleId: string, status: number, conditions: number[], servermail?: ServerMailType) {
|
||||
if(!servermail) servermail = await ServerMailModel.findById(_id).lean();
|
||||
let { roleStatus } = servermail;
|
||||
let hasRole = roleStatus.find(cur => cur.roleId == roleId);
|
||||
let myStatus = hasRole?hasRole.status: MAIL_STATUS.CREATE;
|
||||
if(conditions.indexOf(myStatus) == -1) return null;
|
||||
if(hasRole) {
|
||||
servermail = await ServerMailModel.findOneAndUpdate({ _id, 'roleStatus.roleId': roleId }, { $set: { 'roleStatus.$.status': status } }, {new: true}).lean();
|
||||
} else {
|
||||
servermail = await ServerMailModel.findByIdAndUpdate(_id, { $push: { roleStatus: { roleId, status } } }, {new: true}).lean();
|
||||
}
|
||||
return servermail
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询有奖励的邮件
|
||||
* @param roleId
|
||||
* @param lean
|
||||
*/
|
||||
public static async findRewardsMails(serverId: number, roleId: string, lean = true) {
|
||||
const allMails: ServerMailType[] = await ServerMailModel.find({ serverId, sendTime:{$lte: nowSeconds()} }).lean(lean);
|
||||
let result: ServerMailType[] = [];
|
||||
for(let mail of allMails) {
|
||||
let { roleStatus } = mail;
|
||||
let hasRole = roleStatus.find(cur => cur.roleId == roleId);
|
||||
if(hasRole) {
|
||||
if(hasRole.status == MAIL_STATUS.CREATE || hasRole.status == MAIL_STATUS.READ) {
|
||||
result.push(mail);
|
||||
}
|
||||
} else {
|
||||
result.push(mail);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const ServerMailModel = getModelForClass(ServerMail);
|
||||
|
||||
export interface ServerMailType extends Pick<DocumentType<ServerMail>, keyof ServerMail>{}
|
||||
export type ServerMailTypeParam = Partial<ServerMailType>; // 将所有字段变成可选项
|
||||
Reference in New Issue
Block a user