120 lines
5.8 KiB
TypeScript
120 lines
5.8 KiB
TypeScript
import {Application, BackendSession, ChannelService} from 'pinus';
|
|
import { MailModel } from '../../../db/Mail';
|
|
import { GroupMailModel } from '../../../db/GroupMail';
|
|
import { resResult } from '../../../pubUtils/util';
|
|
import { STATUS } from '../../../consts/statusCode';
|
|
import { findWhere } from 'underscore';
|
|
import { MAIL_STATUS, MAIL_TEM_TYPE, MAIL_TYPE } from '../../../consts/constModules/mailConst';
|
|
import { handleCost, addItems } from '../../../services/rewardService';
|
|
import { mongoose } from '@typegoose/typegoose';
|
|
const { ObjectId } = mongoose.Types;
|
|
import { nowSeconds } from '../../../pubUtils/timeUtil';
|
|
export default function(app: Application) {
|
|
return new MailHandler(app);
|
|
}
|
|
|
|
export class MailHandler {
|
|
constructor(private app: Application) {
|
|
|
|
}
|
|
|
|
public async refrshMails(msg:{}) {
|
|
|
|
}
|
|
|
|
public async readMail(msg: { id: string, mailType: number }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
let { id, mailType } = msg;
|
|
let mail;
|
|
if (mailType == MAIL_TYPE.SINGLEMAIL) {
|
|
mail = await MailModel.updateMailByStatus(id, [MAIL_STATUS.CREATE], { status: MAIL_STATUS.READ });
|
|
} else if (mailType == MAIL_TYPE.GROUPMAIL) {
|
|
mail = await GroupMailModel.updateMailByStatus(id, roleId, MAIL_STATUS.READ, [MAIL_STATUS.CREATE]);
|
|
}
|
|
if (!mail)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
return resResult(STATUS.SUCCESS, { mails: [{ id, status: MAIL_STATUS.READ, mailType }] });
|
|
}
|
|
|
|
public async delMails(msg: { id: string, mailType: number, type: number }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
let { id, type, mailType } = msg;
|
|
if (type == 1) {//单个删除
|
|
if (mailType == MAIL_TYPE.SINGLEMAIL) {
|
|
let mail = await MailModel.delMail(id);
|
|
if (!mail)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
return resResult(STATUS.SUCCESS, { mails: [{ id, status: MAIL_STATUS.DELETE, mailType }] });
|
|
} else if (mailType == MAIL_TYPE.GROUPMAIL) {
|
|
let mail = await GroupMailModel.delMail(id, roleId);
|
|
if (!mail)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
return resResult(STATUS.SUCCESS, { mails: [{ id, status: MAIL_STATUS.DELETE, mailType }] });
|
|
} else {
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
}
|
|
} else {//一键删除
|
|
let mails = [];
|
|
let resGroupMailIds = await GroupMailModel.findReadAndRewardsMails(roleId);
|
|
let groupMailIds = resGroupMailIds.map(({_id})=>{
|
|
mails.push({id:_id, status: MAIL_STATUS.DELETE, mailType: MAIL_TYPE.GROUPMAIL});
|
|
return _id;
|
|
});
|
|
await GroupMailModel.updateMailStatus(groupMailIds, MAIL_STATUS.DELETE, roleId);
|
|
let resMailIds = await MailModel.findReadAndRewardsMails(roleId);
|
|
let mailIds = resMailIds.map(({_id})=>{
|
|
mails.push({id: _id, status: MAIL_STATUS.DELETE, mailType: MAIL_TYPE.SINGLEMAIL});
|
|
return _id;
|
|
});
|
|
await MailModel.updateMailStatus(mailIds, MAIL_STATUS.DELETE);
|
|
return resResult(STATUS.SUCCESS, { mails });
|
|
}
|
|
}
|
|
|
|
public async getMailRewards(msg: { id: string, mailType: number, type: number }, session: BackendSession) {
|
|
let roleId: string = session.get('roleId');
|
|
let roleName: string = session.get('roleName');
|
|
let sid: string = session.get('sid');
|
|
let serverId: number = session.get('serverId');
|
|
let { id, type, mailType } = msg;
|
|
let mailGoods = [];
|
|
let mails = [];
|
|
let nowTime: number = nowSeconds();
|
|
if (type == 1) {//单个领取
|
|
let mail;
|
|
if (mailType == MAIL_TYPE.SINGLEMAIL) {
|
|
mail = await MailModel.updateMailByStatus(id, [MAIL_STATUS.READ, MAIL_STATUS.CREATE], { status: MAIL_STATUS.RECEIVED });
|
|
if (!mail)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
} else if (mailType == MAIL_TYPE.GROUPMAIL) {
|
|
mail = await GroupMailModel.updateMailByStatus(id, roleId, MAIL_STATUS.RECEIVED, [MAIL_STATUS.READ, MAIL_STATUS.CREATE]);
|
|
if (!mail)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
} else {
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
}
|
|
mails.push({ id, status: MAIL_STATUS.RECEIVED, mailType });
|
|
if (mail.mailTemType == MAIL_TEM_TYPE.GMTYPE) {
|
|
let gmMail = await this.app.rpc.gm.gmRemote.getUseGmMailById.toServer('gm-server-1', mail.mailId, serverId, nowTime);
|
|
if (!gmMail)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
mailGoods.push(...gmMail.goods);
|
|
} else {
|
|
mailGoods.push(...mail.goods);
|
|
}
|
|
} else {//一键领取
|
|
let groupMailRewards = await GroupMailModel.findRewardsMails(roleId);
|
|
let mailRewards = await MailModel.findRewardsMails(roleId);
|
|
let { mailGoods: goods, mailIds, groupMailIds} = await this.app.rpc.gm.gmRemote.getUseMails.toServer('gm-server-1', serverId, groupMailRewards, mailRewards);
|
|
mailGoods = goods;
|
|
if (!!groupMailIds.length)
|
|
mails = await GroupMailModel.updateMailStatus(groupMailIds, MAIL_STATUS.RECEIVED, roleId);
|
|
if (!!mailIds.length)
|
|
mails = await MailModel.updateMailStatus(mailIds, MAIL_STATUS.RECEIVED);
|
|
}
|
|
let resGoods = [];
|
|
if (!!mailGoods && !!mailGoods.length)
|
|
resGoods = await addItems(roleId, roleName, sid, mailGoods);
|
|
return resResult(STATUS.SUCCESS, { mails, goods: resGoods });
|
|
}
|
|
} |