道具事务
This commit is contained in:
+51
-51
@@ -1,7 +1,7 @@
|
|||||||
import BaseModel from './BaseModel';
|
import BaseModel from './BaseModel';
|
||||||
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
||||||
const _ = require('underscore');
|
const _ = require('underscore');
|
||||||
const Transaction = require('mongoose-transactions');
|
// const Transaction = require('mongoose-transactions');
|
||||||
@index({ roleId: 1, id: 1 })
|
@index({ roleId: 1, id: 1 })
|
||||||
@index({ seqId: 1 })
|
@index({ seqId: 1 })
|
||||||
@modelOptions({ schemaOptions: { id: false } })
|
@modelOptions({ schemaOptions: { id: false } })
|
||||||
@@ -50,59 +50,59 @@ export default class Item extends BaseModel {
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number }>, lean = true) {
|
public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number }>, lean = true) {
|
||||||
// let updateItems = new Array<{ id: number, count: number, ratio?: number }>(), hasError: boolean = false, result = new Array();
|
let updateItems = new Array<{ id: number, count: number, ratio?: number }>(), hasError: boolean = false, result = new Array();
|
||||||
// for (let { id, count, ratio } of items) {
|
for (let { id, count, ratio } of items) {
|
||||||
// let rec: ItemType;
|
let rec: ItemType;
|
||||||
// if (!ratio) {
|
if (!ratio) {
|
||||||
// rec = await ItemModel.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: -1 * count } }, { new: true }).lean(lean);
|
rec = await ItemModel.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: -1 * count } }, { new: true }).lean(lean);
|
||||||
// } else {
|
} else {
|
||||||
// rec = await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: ratio * count } }, { new: true, upsert: true }).lean(lean);
|
rec = await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: ratio * count } }, { new: true, upsert: true }).lean(lean);
|
||||||
// }
|
|
||||||
// if (!!rec) {
|
|
||||||
// let index = _.findIndex(result,{id})
|
|
||||||
// if (!!result[index]) {
|
|
||||||
// result[index] = { id: rec.id, count: rec.count }
|
|
||||||
// } else {
|
|
||||||
// result.push({ id: rec.id, count: rec.count });
|
|
||||||
// }
|
|
||||||
// updateItems.push({ id, count, ratio });
|
|
||||||
// } else {
|
|
||||||
// hasError = true; break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if (hasError) { // 数量不足
|
|
||||||
// for (let { id, count, ratio } of updateItems) {
|
|
||||||
// if (!ratio) ratio = -1;
|
|
||||||
// await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: -ratio * count } }, { new: true }).lean(lean);
|
|
||||||
// }
|
|
||||||
// return { hasError: true }
|
|
||||||
// } else {
|
|
||||||
// return { hasError: false, result }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number}>, lean = true) {
|
|
||||||
const transaction = new Transaction();
|
|
||||||
let hasError: boolean = false, result = new Array();
|
|
||||||
try {
|
|
||||||
for (let { id, count, ratio } of items) {
|
|
||||||
if (ratio) ratio = -1;
|
|
||||||
await transaction.update({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true});
|
|
||||||
const rec: ItemType = await transaction.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true}).lean(lean);
|
|
||||||
if (!!rec) {
|
|
||||||
result.push({ id: rec.id, count: rec.count });
|
|
||||||
} else {
|
|
||||||
hasError = true; break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
await transaction.run();
|
if (!!rec) {
|
||||||
return {hasError, result};
|
let index = _.findIndex(result,{id})
|
||||||
} catch (error) {
|
if (!!result[index]) {
|
||||||
await transaction.rollback().catch(console.error);
|
result[index] = { id: rec.id, count: rec.count }
|
||||||
transaction.clean();
|
} else {
|
||||||
return {error};
|
result.push({ id: rec.id, count: rec.count });
|
||||||
|
}
|
||||||
|
updateItems.push({ id, count, ratio });
|
||||||
|
} else {
|
||||||
|
hasError = true; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasError) { // 数量不足
|
||||||
|
for (let { id, count, ratio } of updateItems) {
|
||||||
|
if (!ratio) ratio = -1;
|
||||||
|
await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: -ratio * count } }, { new: true }).lean(lean);
|
||||||
|
}
|
||||||
|
return { hasError: true }
|
||||||
|
} else {
|
||||||
|
return { hasError: false, result }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// public static async decreaseItems(roleId: string, items: Array<{ id: number, count: number, ratio?: number}>, lean = true) {
|
||||||
|
// const transaction = new Transaction();
|
||||||
|
// let hasError: boolean = false, result = new Array();
|
||||||
|
// try {
|
||||||
|
// for (let { id, count, ratio } of items) {
|
||||||
|
// if (ratio) ratio = -1;
|
||||||
|
// await transaction.update({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true});
|
||||||
|
// const rec: ItemType = await transaction.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: ratio * count } }, { new: true}).lean(lean);
|
||||||
|
// if (!!rec) {
|
||||||
|
// result.push({ id: rec.id, count: rec.count });
|
||||||
|
// } else {
|
||||||
|
// hasError = true; break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// await transaction.run();
|
||||||
|
// return {hasError, result};
|
||||||
|
// } catch (error) {
|
||||||
|
// await transaction.rollback().catch(console.error);
|
||||||
|
// transaction.clean();
|
||||||
|
// return {error};
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
public static async deleteAccount(roleId: string) {
|
public static async deleteAccount(roleId: string) {
|
||||||
let result = await ItemModel.deleteMany({ roleId });
|
let result = await ItemModel.deleteMany({ roleId });
|
||||||
|
|||||||
Reference in New Issue
Block a user