aozhiwei 3f1563e985 1
2022-01-25 13:07:59 +08:00

134 lines
4.2 KiB
JavaScript

const util = require('util');
const utils = require('./utils');
const db = require("./db");
const log = require("./log");
const C = require("./C");
class BoxRepairer {
constructor(boxOrder) {
this.boxOrder = boxOrder;
}
async repairPaidOrder() {
await this.internalRepairPaidOrder();
utils.emitEvent(C.REMOVE_PENDING_ORDER_EVENT, this.boxOrder.getOrderId());
}
async internalRepairPaidOrder() {
const logClass = 'repairPaidOrder';
const {err, boxDb} = await this.getBoxDb(logClass);
if (err || !boxDb) {
utils.throwError(util.format('%s box not found orderDb:%s err:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
err
)
);
return;
}
log.info(util.format('%s begin orderDb:%s boxDb:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
JSON.stringify(boxDb)
)
);
if (boxDb['state'] == C.BOX_STATE_PAID) {
await this.boxOrder.setDone(logClass);
} else {
const err = await this.boxOrder.setBoxPaidState(logClass, boxDb);
if (!err) {
await this.boxOrder.setDone(logClass);
}
}
log.info(util.format('%s end orderDb:%s boxDb:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
JSON.stringify(boxDb)
)
);
}
async repairExpiredOrder() {
await this.internalRepairExpiredOrder();
utils.emitEvent(C.REMOVE_PENDING_ORDER_EVENT, this.boxOrder.getOrderId());
}
async internalRepairExpiredOrder() {
const logClass = 'repairExpiredOrder';
const {err, boxDb} = await this.getBoxDb(logClass);
if (err || !boxDb) {
log.warning(util.format('%s box not found orderDb:%s err:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
err
)
);
return;
}
log.info(util.format('%s begin orderDb:%s boxDb:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
JSON.stringify(boxDb)
));
if (!boxDb['order_valid']) {
await this.boxOrder.setDone(logClass);
} else {
const err = await this.boxOrder.setBoxOrderInvalid(logClass, boxDb);
if (!err) {
await this.boxOrder.setDone(logClass);
}
}
log.info(util.format('%s end orderDb:%s boxDb:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
JSON.stringify(boxDb)
)
);
}
async repairFailOrder() {
await this.internalRepairFailOrder();
utils.emitEvent(C.REMOVE_PENDING_ORDER_EVENT, this.boxOrder.getOrderId());
}
async internalRepairFailOrder() {
const logClass = 'repairFailOrder';
const {err, boxDb} = await this.boxOrder.getBoxDb(logClass);
if (err || !boxDb) {
log.warning(util.format('%s box not found orderDb:%s err:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
err
));
return;
}
log.info(util.format('%s begin orderDb:%s boxDb:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
JSON.stringify(boxDb)
)
);
if (!boxDb['order_valid']) {
await this.boxOrder.setDone(logClass);
} else {
const err = await this.boxOrder.setBoxOrderInvalid(logClass, boxDb);
if (!err) {
await this.boxOrder.setDone(logClass);
}
}
log.info(util.format('%s end orderDb:%s boxDb:%s',
logClass,
JSON.stringify(this.boxOrder.getOrderDb()),
JSON.stringify(boxDb)
)
);
}
}
exports.BoxRepairer = BoxRepairer;