|
@@ -343,13 +343,11 @@ module.exports = app => {
|
|
|
return await transaction.query(sql, sqlParam);
|
|
|
}
|
|
|
|
|
|
- _newExprAfterChangeOrder(expr, orderPart1, orderPart2) {
|
|
|
- if (!expr) return [false, expr];
|
|
|
-
|
|
|
+ _splitExprByOrder(expr) {
|
|
|
const orderParam = [...expr.matchAll(/f\d+/ig)];
|
|
|
if (orderParam.length === 0) return [false, expr];
|
|
|
|
|
|
- const exprPart = [], newExprPart = [];
|
|
|
+ const exprPart = [];
|
|
|
for (const [i, op] of orderParam.entries()) {
|
|
|
if (i === 0) {
|
|
|
exprPart.push(expr.substring(0, op.index));
|
|
@@ -360,6 +358,16 @@ module.exports = app => {
|
|
|
exprPart.push(op[0]);
|
|
|
if (i === orderParam.length - 1) exprPart.push(expr.substring(op.index + op[0].length, expr.length));
|
|
|
}
|
|
|
+ return [true, exprPart];
|
|
|
+ }
|
|
|
+
|
|
|
+ _newExprAfterChangeOrder(expr, orderPart1, orderPart2) {
|
|
|
+ if (!expr) return [false, expr];
|
|
|
+ const [hasOrder, exprPart] = this._splitExprByOrder(expr);
|
|
|
+ if (!hasOrder) return [false, expr];
|
|
|
+
|
|
|
+ const newExprPart = [];
|
|
|
+
|
|
|
let change = false;
|
|
|
for (const ep of exprPart) {
|
|
|
if (ep === orderPart1) {
|
|
@@ -411,6 +419,72 @@ module.exports = app => {
|
|
|
throw err;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ _newExprAfterDelete(expr, orderParts) {
|
|
|
+ if (!expr) return [false, expr];
|
|
|
+ const [hasOrder, exprPart] = this._splitExprByOrder(expr);
|
|
|
+ if (!hasOrder) return [false, expr];
|
|
|
+
|
|
|
+ const newExprPart = [];
|
|
|
+
|
|
|
+ let change = false;
|
|
|
+ for (const ep of exprPart) {
|
|
|
+ const orderPart = orderParts.find(x => {return x.orgOrder === ep; });
|
|
|
+ if (orderPart) {
|
|
|
+ change = true;
|
|
|
+ newExprPart.push(orderPart.newOrder);
|
|
|
+ } else {
|
|
|
+ newExprPart.push(ep);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return [change, newExprPart.join('')];
|
|
|
+ }
|
|
|
+
|
|
|
+ async del(id) {
|
|
|
+ if (!this.ctx.tender || !this.ctx.stage) throw '数据错误';
|
|
|
+ // 检查是否可以删除
|
|
|
+ const pay = await this.ctx.service.pay.getDataByCondition({id: id});
|
|
|
+ if (!pay) {
|
|
|
+ throw '合同支付项不存在';
|
|
|
+ } else if (pay.ptype !== payConst.payType.normal) {
|
|
|
+ throw '该合同支付项不可删除';
|
|
|
+ }
|
|
|
+
|
|
|
+ const stagePays = await this.getStagePays(this.ctx.stage);
|
|
|
+ const orderParts = [], updateStageData = [], updateData = [];
|
|
|
+ for (const sp of stagePays) {
|
|
|
+ if (sp.order === pay.order) {
|
|
|
+ orderParts.push({ orgOrder: `f${sp.order}`, newOrder: '#ref!'});
|
|
|
+ } else if (sp.order > pay.order) {
|
|
|
+ orderParts.push({ orgOrder: `f${sp.order}`, newOrder: `f${sp.order-1}`});
|
|
|
+ } else {
|
|
|
+ updateData.push({id: sp.pid, order: sp.order -1 });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (const sp of stagePays) {
|
|
|
+ const [change, newExpr] = this._newExprAfterDelete(sp.expr, orderParts);
|
|
|
+ if (change) {
|
|
|
+ updateStageData.push({ condition: { where: { sid: this.ctx.stage.id, pid: sp.pid } }, update: { expr: newExpr } });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除合同支付
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ // 假删除
|
|
|
+ const result = await transaction.update(this.ctx.service.pay.tableName, { id: id, valid: false });
|
|
|
+ if (result.affectedRows !== 1) throw '删除合同支付项失败';
|
|
|
+ await transaction.updateRows(this.ctx.service.pay.tableName, updateData);
|
|
|
+ for (const usd of updateStageData) {
|
|
|
+ await transaction.update(this.tableName, usd.update, usd.condition);
|
|
|
+ }
|
|
|
+ await transaction.commit();
|
|
|
+ return true;
|
|
|
+ } catch(err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return StagePay;
|