12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- module.exports = app => {
- class ChangeSettleList extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'change_settle_list';
- }
- async isSettle(cid) {
- const result = await this.getDataByCondition({ cid });
- return result && result.length > 0;
- }
- async updateChangeList(cid, settleBills, settlePos, list) {
- const removeList = [];
- const updateList = [];
- const changeSettleListData = await this.getAllDataByCondition({ where: { cid } });
- const settleStatus = this.ctx.service.settle.settleStatus;
- const transaction = await this.db.beginTransaction();
- try {
- for (const cl of list) {
- const settleInfo = cl.mx_id ? this._.find(settlePos, { lid: cl.gcl_id, pid: cl.mx_id }) : this._.find(settleBills, { lid: cl.gcl_id });
- if (settleInfo && settleInfo.settle_status === settleStatus.finish) {
- const changeSettleInfo = cl.mx_id ? this._.find(changeSettleListData, { gcl_id: cl.gcl_id, mx_id: cl.mx_id }) : this._.find(changeSettleListData, { gcl_id: cl.gcl_id });
- if (!changeSettleInfo) {
- removeList.push(cl.id);
- } else {
- if (changeSettleInfo.amount !== cl.spamount || changeSettleInfo.amount !== cl.camount) {
- cl.camount = changeSettleInfo.amount;
- cl.spamount = changeSettleInfo.amount;
- updateList.push({ id: cl.id, camount: cl.camount, spamount: cl.spamount });
- }
- }
- }
- }
- // list根据removeList对应删除
- if (removeList.length > 0) {
- for (const id of removeList) {
- const index = this._.findIndex(list, { id });
- if (index !== -1) {
- list.splice(index, 1);
- }
- }
- await transaction.delete(this.ctx.service.changeAuditList.tableName, { id: removeList });
- }
- if (updateList.length > 0) await transaction.update(this.ctx.service.changeAuditList.tableName, updateList);
- // 重算变更令金额
- if (removeList.length > 0 || updateList.length > 0) {
- await this.ctx.service.changeAuditList.reCalcTp(transaction, cid);
- }
- await transaction.commit();
- } catch (err) {
- console.log(err);
- await transaction.rollback();
- throw err;
- }
- return removeList.length;
- }
- }
- return ChangeSettleList;
- };
|