🐞 fix(gvg): 批量保存数据后返回有时无法查询

This commit is contained in:
luying
2023-02-18 17:30:49 +08:00
parent cbbacd27b5
commit 8065a81988
3 changed files with 26 additions and 37 deletions
+17 -18
View File
@@ -123,33 +123,32 @@ export default class GVGLeagueFarm extends BaseModel {
}
public static async plant(configId: number, leagueCode: string, farmId: number, fields: { fieldId: number, seedType: number, time: number, output: number, outputStr: string, index: number }[], roleId: string) {
const batchCode = genCode(10);
const result = await GVGLeagueFarmModel.bulkWrite(fields.map(({ fieldId, seedType, time, output, outputStr, index }) => {
return { updateOne: { filter: { configId, leagueCode, farmId, fieldId, lockRoleId: roleId }, update: { $set: { seedType, unlockTime: getFutureTime(), harvestTime: nowSeconds() + time, batchCode, output, outputStr, index }} } }
}));
if(result.modifiedCount == 0) return [];
const fieldResult: GVGLeagueFarmType[] = await GVGLeagueFarmModel.find({ batchCode }).sort({ fieldId: 1 }).select('-_id -createdAt -updatedAt -__v').lean();
const fieldResult: GVGLeagueFarmType[] = [];
for(let { fieldId, seedType, time, output, outputStr, index } of fields) {
let obj: GVGLeagueFarmType = await GVGLeagueFarmModel.findOneAndUpdate({ configId, leagueCode, farmId, fieldId, lockRoleId: roleId }, {$set: { seedType, unlockTime: getFutureTime(), harvestTime: nowSeconds() + time, output, outputStr, index }}, { new: true }).lean();
if(obj) fieldResult.push(obj);
}
return fieldResult;
}
public static async harvest(configId: number, leagueCode: string, farmId: number, fieldIds: number[], roleId: string) {
const batchCode = genCode(10);
const result = await GVGLeagueFarmModel.bulkWrite(fieldIds.map(fieldId => {
return { updateOne: { filter: { configId, leagueCode, farmId, fieldId, lockRoleId: roleId, harvestTime: { $lt: nowSeconds() } }, update: { $set: { unlockTime: 0, harvestTime: 0, lockRoleId: '', lockRoleName: '', batchCode }} } }
}));
if(result.modifiedCount == 0) return [];
const fieldResult: GVGLeagueFarmType[] = await GVGLeagueFarmModel.find({ batchCode }).sort({ fieldId: 1 }).select('-_id -createdAt -updatedAt -__v -configId -leagueCode').lean();
await GVGLeagueFarmModel.updateMany({ batchCode }, { $set: { seedType: 0 } });
const fieldResult: GVGLeagueFarmType[] = [], _ids: string[] = [];
for(let fieldId of fieldIds) {
let obj: GVGLeagueFarmType = await GVGLeagueFarmModel.findOneAndUpdate(
{ configId, leagueCode, farmId, fieldId, lockRoleId: roleId, harvestTime: { $lt: nowSeconds() } },
{ $set: { unlockTime: 0, harvestTime: 0, lockRoleId: '', lockRoleName: '', index: 0 } }, {new: true}).lean();
if(obj) { fieldResult.push(obj); _ids.push(obj._id);}
}
await GVGLeagueFarmModel.updateMany({ _id: { $in: _ids } }, { $set: { seedType: 0 } });
return fieldResult;
}
public static async helpHarvest(configId: number, leagueCode: string, farmId: number, roleId: string) {
const batchCode = genCode(10);
const result = await GVGLeagueFarmModel.updateMany({ configId, leagueCode, farmId, lockRoleId: roleId, harvestTime: { $lt: nowSeconds() } },
{ $set: { unlockTime: 0, harvestTime: 0, lockRoleId: '', lockRoleName: '', batchCode }}, { new: true });
const fieldResult: GVGLeagueFarmType[] = await GVGLeagueFarmModel.find({ configId, leagueCode, farmId, lockRoleId: roleId, harvestTime: { $lt: nowSeconds() } }).lean();
const _ids = fieldResult.map(cur => cur._id);
const result = await GVGLeagueFarmModel.updateMany({ _id: { $in: _ids } },
{ $set: { unlockTime: 0, harvestTime: 0, lockRoleId: '', lockRoleName: '', seedType: 0, index: 0 }}, { new: true });
if(result.modifiedCount == 0) return [];
const fieldResult: GVGLeagueFarmType[] = await GVGLeagueFarmModel.find({ batchCode }).lean();
await GVGLeagueFarmModel.updateMany({ batchCode }, { $set: { seedType: 0 } });
return fieldResult
}