xy-server/game/mail/MailData.ts
2025-04-23 09:34:08 +08:00

144 lines
4.3 KiB
TypeScript

import SKDateUtil from "../gear/SKDateUtil";
import SKDataUtil from "../gear/SKDataUtil";
import { EMailState } from "../role/EEnum";
import MailItemData from "./MailItemData";
import SKLogger from "../gear/SKLogger";
/**
* 邮件数据
*/
export default class MailData {
// 服务器索引
server_id: number = 0;
// 邮件索引
mail_id: number = 0;
// 邀请码
invite: string = "";
// 公会索引
guild_id: number = 0;
// 角色编码
role_id: number = 0;
// 邮件标题
title: string;
// 邮件内容
content: string;
// 附件最多5个
gets: MailItemData[];
// 邮件日期
date: Date;
// 邮件状态
state: EMailState;
constructor() {
this.state = EMailState.UN_GET;
this.date = new Date();
}
// 从数据库中粘贴邮件数据
parseDB(row: any) {
this.server_id = row.server_id;
this.mail_id = row.mail_id;
this.invite = row.invite;
this.guild_id = row.guild_id;
this.role_id = row.role_id;
this.title = row.title;
this.content = row.content;
let gets = SKDataUtil.jsonBy(row.gets);
if (SKDataUtil.isArray(gets)) {
this.gets = [];
for (let get of gets) {
let item: MailItemData = new MailItemData(get.itemId, get.itemCount);
this.gets.push(item);
}
}
this.date = new Date(row.date);
}
// 从网络请求中粘贴邮件数据
parseReq(req: any): string {
let title = req.query.title;
let content = req.query.content;
if (SKDataUtil.isEmptyString(title)) {
return "邮件:标题不能为空";
}
if (SKDataUtil.isEmptyString(content)) {
return "邮件:内容不能为空";
}
this.title = title;
this.content = content;
this.invite = SKDataUtil.stringBy(req.query.invite);
this.guild_id = SKDataUtil.numberBy(req.query.guild_id, 0);
this.role_id = SKDataUtil.numberBy(req.query.role_id, 0);
let gets = SKDataUtil.jsonBy(req.query.gets);
if (SKDataUtil.isArray(gets)) {
this.gets = [];
for (let get of gets) {
let item: MailItemData = new MailItemData(get.item_id, get.item_count);
this.gets.push(item);
}
}
SKLogger.debug(`${this.role_id}:${this.content}`)
return "";
}
toObj(): any {
let gets: any[] = [];
if (!SKDataUtil.isEmptyArray(this.gets)) {
for (let item of this.gets) {
gets.push(item.toObj());
}
}
let mailId = parseInt(this.mail_id.toString());
let result = {
mailId: mailId,
state: this.state,
title: this.title,
content: this.content,
gets: gets,
date: SKDateUtil.formatMsgTime(this.date),
};
return result;
}
// 系统邮件--数据库存储对象
toSystemMailDB(): any {
let gets: any[] = [];
if (!SKDataUtil.isEmptyArray(this.gets)) {
for (let item of this.gets) {
gets.push(item.toObj());
}
}
let result = {
server_id: this.server_id,
invite: this.invite,
guild_id: this.guild_id,
role_id: this.role_id,
title: this.title,
content: this.content,
gets: SKDataUtil.toJson(gets, "[]"),
date: `FROM_UNIXTIME(${Math.floor(this.date.valueOf() / 1000)})`,
state: this.state,
};
return result;
}
// 角色邮件-数据库存储对象
toRoleMailDB():any{
let result = {
state: this.state,
};
return result;
}
// 克隆
clone(): MailData {
let result = new MailData();
result.server_id = this.server_id;
result.mail_id = this.mail_id;
result.invite = this.invite;
result.guild_id = this.guild_id;
result.role_id = this.role_id;
result.title = this.title;
result.content = this.content;
result.gets = this.gets;
result.date = this.date;
result.state = EMailState.UN_GET;
return result;
}
}