diff --git a/shared/db/Item.ts b/shared/db/Item.ts index 9612e18e6..45aa80159 100644 --- a/shared/db/Item.ts +++ b/shared/db/Item.ts @@ -1,7 +1,7 @@ import BaseModel from './BaseModel'; import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose'; const _ = require('underscore'); -const Transaction = require('mongoose-transactions'); +// const Transaction = require('mongoose-transactions'); @index({ roleId: 1, id: 1 }) @index({ seqId: 1 }) @modelOptions({ schemaOptions: { id: false } }) @@ -50,59 +50,59 @@ export default class Item extends BaseModel { return items; } - // 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(); - // for (let { id, count, ratio } of items) { - // let rec: ItemType; - // if (!ratio) { - // rec = await ItemModel.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: -1 * count } }, { new: true }).lean(lean); - // } else { - // 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; - } + 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(); + for (let { id, count, ratio } of items) { + let rec: ItemType; + if (!ratio) { + rec = await ItemModel.findOneAndUpdate({ roleId, id, count: { $gte: count } }, { $inc: { count: -1 * count } }, { new: true }).lean(lean); + } else { + rec = await ItemModel.findOneAndUpdate({ roleId, id }, { $inc: { count: ratio * count } }, { new: true, upsert: true }).lean(lean); } - await transaction.run(); - return {hasError, result}; - } catch (error) { - await transaction.rollback().catch(console.error); - transaction.clean(); - return {error}; + 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(); + // return {hasError, result}; + // } catch (error) { + // await transaction.rollback().catch(console.error); + // transaction.clean(); + // return {error}; + // } + // } public static async deleteAccount(roleId: string) { let result = await ItemModel.deleteMany({ roleId });