import {Application, BackendSession, ChannelService} from 'pinus'; import { MailModel, MailType } from '../../../db/Mail'; import { GroupMailModel, GroupMailType } from '../../../db/GroupMail'; import { resResult } from '../../../pubUtils/util'; import { STATUS } from '../../../consts/statusCode'; import { MAIL_STATUS, MAIL_TEM_TYPE, GM_MAIL_TYPE } from '../../../consts/constModules/mailConst'; import { addItems } from '../../../services/rewardService'; import { getMails } from '../../../services/mailService'; import { ServerMailModel, ServerMailType } from '../../../db/ServerMail'; import { MailParam } from '../../../domain/roleField/mail'; import { RewardInter } from '../../../pubUtils/interface'; export default function(app: Application) { return new MailHandler(app); } export class MailHandler { constructor(private app: Application) { } public async getMails(msg:{}, session: BackendSession) { const roleId: string = session.get('roleId'); const serverId: number = session.get('serverId'); const list = await getMails(roleId, serverId); return resResult(STATUS.SUCCESS, { list }); } 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 == GM_MAIL_TYPE.SINGLE) { mail = await MailModel.updateStatusWithCondition(id, MAIL_STATUS.READ, [MAIL_STATUS.CREATE]); } else if (mailType == GM_MAIL_TYPE.GROUP) { mail = await GroupMailModel.updateStatusWithCondition(id, roleId, MAIL_STATUS.READ, [MAIL_STATUS.CREATE]); } else if (mailType == GM_MAIL_TYPE.SERVER) { mail = await ServerMailModel.updateStatusWithCondition(id, roleId, MAIL_STATUS.READ, [MAIL_STATUS.CREATE]) } // if (!mail) // return resResult(STATUS.WRONG_PARMS); return resResult(STATUS.SUCCESS, { mails: mail?[{ 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) {//单个删除 let mail: MailType|GroupMailType|ServerMailType; if (mailType == GM_MAIL_TYPE.SINGLE) { mail = await MailModel.delMailById(id); } else if (mailType == GM_MAIL_TYPE.GROUP) { mail = await GroupMailModel.delMailById(id, roleId); } else if (mailType == GM_MAIL_TYPE.SERVER) { mail = await ServerMailModel.delMailById(id, roleId); } if (!mail) return resResult(STATUS.WRONG_PARMS); return resResult(STATUS.SUCCESS, { mails: [{ id: mail._id, mailType, status: MAIL_STATUS.DELETE}] }); } else {//一键删除 let singlemails = await MailModel.delMailsByRoleId(roleId); let groupMails = await GroupMailModel.delMailsByRoleId(roleId); let servermails = await ServerMailModel.delMailsByRoleId(roleId); let mails: { id: string, mailType: GM_MAIL_TYPE, status: MAIL_STATUS }[] = []; for(let mail of singlemails) { mails.push({ id: mail._id, mailType: GM_MAIL_TYPE.SINGLE, status: MAIL_STATUS.DELETE }); } for(let mail of groupMails) { mails.push({ id: mail._id, mailType: GM_MAIL_TYPE.GROUP, status: MAIL_STATUS.DELETE }); } for(let mail of servermails) { mails.push({ id: mail._id, mailType: GM_MAIL_TYPE.SERVER, status: 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 mails: MailParam[] = []; if (type == 1) {//单个领取 let mail: MailType|GroupMailType|ServerMailType; if (mailType == GM_MAIL_TYPE.SINGLE) { mail = await MailModel.updateStatusWithCondition(id, MAIL_STATUS.RECEIVED, [MAIL_STATUS.READ, MAIL_STATUS.CREATE]); } else if (mailType == GM_MAIL_TYPE.GROUP) { mail = await GroupMailModel.updateStatusWithCondition(id, roleId, MAIL_STATUS.RECEIVED, [MAIL_STATUS.READ, MAIL_STATUS.CREATE]); } else if (mailType == GM_MAIL_TYPE.SERVER) { mail = await ServerMailModel.updateStatusWithCondition(id, roleId, MAIL_STATUS.RECEIVED, [MAIL_STATUS.READ, MAIL_STATUS.CREATE]); } if (!mail) return resResult(STATUS.WRONG_PARMS); mails.push(new MailParam(mailType, mail, roleId)); } else {//一键领取 let singlemails = await MailModel.findRewardsMails(roleId); let groupMails = await GroupMailModel.findRewardsMails(roleId); let servermails = await ServerMailModel.findRewardsMails(serverId, roleId); for(let mail of singlemails) { mail = await MailModel.updateStatusWithCondition(mail._id, MAIL_STATUS.RECEIVED, [MAIL_STATUS.READ, MAIL_STATUS.CREATE]); if(mail) mails.push(new MailParam(GM_MAIL_TYPE.SINGLE, mail, roleId)); } for(let mail of groupMails) { mail = await GroupMailModel.updateStatusWithCondition(mail._id, roleId, MAIL_STATUS.RECEIVED, [MAIL_STATUS.READ, MAIL_STATUS.CREATE]); if(mail) mails.push(new MailParam(GM_MAIL_TYPE.GROUP, mail, roleId)); } for(let mail of servermails) { mail = await ServerMailModel.updateStatusWithCondition(mail._id, roleId, MAIL_STATUS.RECEIVED, [MAIL_STATUS.READ, MAIL_STATUS.CREATE]); if(mail) mails.push(new MailParam(GM_MAIL_TYPE.SERVER, mail, roleId)); } } let mailGoods: RewardInter[] = []; for(let mail of mails) { mailGoods.push(...mail.goods) } let resGoods = await addItems(roleId, roleName, sid, mailGoods); return resResult(STATUS.SUCCESS, { mails, goods: resGoods }); } }