aozhiwei b760238c2f 1
2022-01-24 14:49:32 +08:00

135 lines
4.2 KiB
JavaScript

const util = require('util');
const utils = require('./utils');
const db = require("./db");
const log = require("./log");
const ordermgr = require('./ordermgr');
const C = require('./C');
class BoxRepairer {
constructor(boxOrder) {
this.boxOrder = boxOrder;
}
async repairPaidOrder() {
await this.internalRepairPaidOrder();
ordermgr.removePendingOrder(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 {
let 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();
ordermgr.removePendingOrder(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 {
let 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();
ordermgr.removePendingOrder(this.boxOrder.getOrderId());
}
async internalRepairFailOrder() {
const logClass = 'repairFailOrder';
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 {
let 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;