95 lines
3.2 KiB
TypeScript
95 lines
3.2 KiB
TypeScript
import { COUNTER } from './../consts';
|
|
import { CounterModel } from './Counter';
|
|
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType, mongoose, ReturnModelType } from '@typegoose/typegoose';
|
|
import ServerStategy from './ServerStategy';
|
|
import { ServerlistType } from './Serverlist';
|
|
|
|
|
|
/**
|
|
* 大区数据
|
|
*/
|
|
@index({ id: 1 })
|
|
|
|
export default class Region extends BaseModel {
|
|
@prop({ required: true })
|
|
id: number; // 大区id
|
|
|
|
@prop({ required: true })
|
|
name: string; // 大区名
|
|
|
|
@prop({ required: true })
|
|
prefix: string; // 区名前缀
|
|
|
|
@prop({ required: true })
|
|
env: string; // 环境变量
|
|
|
|
@prop({ required: true })
|
|
gmLink: string; // 短链接地址
|
|
|
|
@prop({ required: true })
|
|
gameHost: string; // 长链接地址
|
|
|
|
@prop({ required: true })
|
|
gmPort: number; // 长链接地址
|
|
|
|
@prop({ required: true })
|
|
latestServer: number; // 最新服
|
|
|
|
@prop({ required: true })
|
|
latestServerUniqId: number; // 最新服唯一id
|
|
|
|
@prop({ required: true })
|
|
serverCount: number; // 总数
|
|
|
|
@prop({ required: true })
|
|
remark: string; // 备注
|
|
|
|
@prop({ required: true, type: () => ServerStategy, _id: false })
|
|
stategy: ServerStategy; // 策略配置
|
|
|
|
public static async createNewRegion(params: RegionUpdate, uid = 1) {
|
|
let id = await CounterModel.getNewCounter(COUNTER.REGION);
|
|
|
|
const rec: RegionType = await RegionModel.findOneAndUpdate({ id }, { $setOnInsert: { ...params, createdBy: uid}, $set: { updatedBy: uid } }, { new: true, upsert: true }).lean();
|
|
return rec;
|
|
}
|
|
|
|
public static async updateRegion(id: number, params: RegionUpdate, uid = 1) {
|
|
const rec: RegionType = await RegionModel.findOneAndUpdate({ id }, { $set: { ...params, updatedBy: uid } }, { new: true }).lean();
|
|
return rec;
|
|
}
|
|
|
|
public static async newServer(id: number, newServer: ServerlistType, uid = 1) {
|
|
const rec: RegionType = await RegionModel.findOneAndUpdate({ id }, {
|
|
$set: { latestServer: newServer.serverId, latestServerUniqId: newServer.id, updatedBy: uid },
|
|
$inc: { serverCount: 1 }
|
|
}, { new: true }).lean();
|
|
return rec;
|
|
}
|
|
public static async getAllRegion() {
|
|
const rec: RegionType[] = await RegionModel.find().select('-_id -stategy').lean();
|
|
return rec;
|
|
}
|
|
|
|
public static async findRegionById(id: number) {
|
|
const rec: RegionType = await RegionModel.findOne({ id }).select('-_id').lean();
|
|
return rec;
|
|
}
|
|
|
|
public static async findRegionByEnv(env: string) {
|
|
const rec: RegionType = await RegionModel.findOne({ env }).select('-_id').lean();
|
|
return rec;
|
|
}
|
|
}
|
|
|
|
export let RegionModel: ReturnModelType<typeof Region, {}>;
|
|
export function loadRegionModel(connect: mongoose.Connection) {
|
|
RegionModel = getModelForClass(Region, {
|
|
existingConnection: connect
|
|
});
|
|
}
|
|
|
|
export interface RegionType extends Pick<DocumentType<Region>, keyof Region> { };
|
|
export type RegionUpdate = Partial<RegionType>; // 将所有字段变成可选项
|