161 lines
6.0 KiB
TypeScript
161 lines
6.0 KiB
TypeScript
import BaseModel from './BaseModel';
|
||
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
||
import { RoleModel } from './Role';
|
||
import { genCode } from '../pubUtils/util';
|
||
import { SearchArtifactParam } from '../domain/backEndField/search';
|
||
|
||
/**
|
||
* 宝物
|
||
*/
|
||
@modelOptions({ schemaOptions: { id: false } })
|
||
@index({ roleId: 1, seqId: 1, id: 1 })
|
||
@index({ roleId: 1, quality: 1 })
|
||
@index({ batchCode: 1 })
|
||
@index({ status: 1 })
|
||
|
||
export default class Artifact extends BaseModel {
|
||
|
||
// 主键: artifact,不同形态的宝物分开词条
|
||
@prop({ required: true })
|
||
seqId: number | string; // 唯一id
|
||
|
||
@prop({ required: true })
|
||
roleId: string; // 玩家id
|
||
|
||
@prop({ required: true })
|
||
roleName: string; // 玩家名
|
||
|
||
@prop({ required: true })
|
||
id: number; // 物品id
|
||
|
||
@prop({ required: true })
|
||
artifactId: number; // 宝物id
|
||
|
||
@prop({ required: true, default: 0 })
|
||
lv: number; // 强化等级
|
||
|
||
@prop({ required: true, default: 0 })
|
||
quality: number; // 品质 1-5 蓝紫橙红金
|
||
|
||
@prop({ required: true, default: 0 })
|
||
qualityStage: number; // 品质+n,0开始
|
||
|
||
@prop({ required: true, default: 0 })
|
||
hid: number; // 装备的武将
|
||
|
||
@prop({ required: false })
|
||
batchCode: string; // 一键合成批处理
|
||
|
||
@prop({ required: true, default: 1 })
|
||
status: number; // 装备 1-生成 0-被合成删除
|
||
|
||
public static async findbySeqIds(roleId: string, seqIds: (number | string)[], select?: string) {
|
||
const result: ArtifactModelType[] = await ArtifactModel.find({ roleId, seqId: { $in: seqIds }, status: 1 }).select(select).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async findbySeqId(roleId: string, seqId: number | string, select?: string) {
|
||
const result: ArtifactModelType = await ArtifactModel.findOne({ roleId, seqId, status: 1 }).select(select).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async findbyHids(roleId: string, hids: number[]) {
|
||
const result: ArtifactModelType[] = await ArtifactModel.find({ roleId, hid: { $in: hids }, status: 1 }).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async findbyRole(roleId: string, select = '') {
|
||
const result: ArtifactModelType[] = await ArtifactModel.find({ roleId, status: 1 }).select(select).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async createArtifact(artifactInfo: ArtifactModelUpdate) {
|
||
const seqId = genCode(10);
|
||
|
||
const doc = new ArtifactModel();
|
||
const artifact = Object.assign(doc.toJSON(), { seqId }, artifactInfo);
|
||
return artifact;
|
||
}
|
||
|
||
public static async createArtifacts(roleId: string, artifactInfos: ArtifactModelUpdate[]) {
|
||
let insertInfo: ArtifactModelType[] = [];
|
||
for (let artifactInfo of artifactInfos) {
|
||
let artifact = await this.createArtifact(artifactInfo);
|
||
insertInfo.push(artifact);
|
||
}
|
||
await ArtifactModel.insertMany(insertInfo);
|
||
await RoleModel.increaseArtifact(roleId, insertInfo.length);
|
||
return insertInfo;
|
||
}
|
||
|
||
public static async putOnOrOff(roleId: string, seqId: number | string, hid: number, lv?: number) {
|
||
let update: ArtifactModelUpdate = { hid };
|
||
if (lv != undefined) update.lv = lv;
|
||
let rec: ArtifactModelType = await ArtifactModel.findOneAndUpdate({ roleId, seqId }, { $set: update }, { new: true }).lean();
|
||
return rec;
|
||
}
|
||
|
||
public static async deleteBySeqIds(roleId: string, seqIds: (number | string)[]) {
|
||
let result: ArtifactModelType[] = await ArtifactModel.findbySeqIds(roleId, seqIds);
|
||
let delResult: { n: number, nModified: number, ok: number } = await ArtifactModel.updateMany({ roleId, seqId: { $in: seqIds } }, { $set: { status: 0 } });
|
||
await RoleModel.increaseArtifact(roleId, -1 * delResult.nModified);
|
||
return result;
|
||
}
|
||
|
||
public static async updateInfoBySeqId(roleId: string, seqId: number | string, update: ArtifactModelUpdate) {
|
||
let rec: ArtifactModelType = await ArtifactModel.findOneAndUpdate({ roleId, seqId }, { $set: update }, { new: true }).lean();
|
||
return rec;
|
||
}
|
||
|
||
public static async findByQuality(roleId: string, quality: number) {
|
||
let result: ArtifactModelType[] = await ArtifactModel.find({ roleId, quality, status: 1, hid: 0, qualityStage: 0 }).lean();
|
||
return result;
|
||
}
|
||
|
||
public static async findByBatchCode(batchCode: string) {
|
||
let result: ArtifactModelType[] = await ArtifactModel.find({ batchCode, status: 1 }).lean();
|
||
return result;
|
||
}
|
||
|
||
private static getSearchObj(form: SearchArtifactParam) {
|
||
let { roleId, minId, maxId, isShowDel } = form
|
||
let searchObj = {};
|
||
if (roleId) searchObj['roleId'] = roleId;
|
||
if (isShowDel && isShowDel == 1) {
|
||
searchObj['status'] = 1
|
||
}
|
||
|
||
if (minId !== undefined && maxId !== undefined) {
|
||
searchObj['id'] = { $gte: minId, $lte: maxId };
|
||
}
|
||
if (minId !== undefined && maxId == undefined) {
|
||
searchObj['id'] = { $gte: minId };
|
||
}
|
||
if (minId == undefined && maxId != undefined) {
|
||
searchObj['id'] = { $lte: maxId };
|
||
}
|
||
|
||
return searchObj
|
||
}
|
||
|
||
public static async countByCondition(form: SearchArtifactParam = {}) {
|
||
let searchObj = this.getSearchObj(form);
|
||
const result = await ArtifactModel.count(searchObj);
|
||
return result;
|
||
}
|
||
|
||
public static async findByRoleIdAndIsShowDel(form) {
|
||
let searchObj = this.getSearchObj(form);
|
||
//.select("roleId roleName id artifactId artifactName lv quality qualityStage status")
|
||
const result: ArtifactModelType[] = await ArtifactModel.find(searchObj).lean();
|
||
return result;
|
||
}
|
||
}
|
||
|
||
|
||
export const ArtifactModel = getModelForClass(Artifact);
|
||
|
||
export interface ArtifactModelType extends Pick<DocumentType<Artifact>, keyof Artifact> {
|
||
artifactName: string;
|
||
}
|
||
export type ArtifactModelUpdate = Partial<ArtifactModelType>; // 将所有字段变成可选项
|