123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 'use strict';
- /**
- * 附属工程量(ancillary gcl)
- *
- * @author Mai
- * @date
- * @version
- */
- module.exports = app => {
- class AncillaryGcl extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'ancillary_gcl';
- }
- async _addDatas(data) {
- const qtyDecimal = this.ctx.tender.info.decimal.qty;
- const user_id = this.ctx.session.sessionUser.accountId;
- const datas = data instanceof Array ? data : [data];
- const insertData = [];
- for (const d of datas) {
- if (!d.lid || !d.g_order) throw '新增其他数据,提交的数据错误';
- const nd = {
- id: this.uuid.v4(), tid: this.ctx.tender.id,
- add_user_id: user_id, update_user_id: user_id,
- lid: d.lid, g_order: d.g_order,
- };
- if (d.name) nd.name = d.name || '';
- if (d.unit) nd.unit = d.unit || '';
- if (d.quantity) nd.quantity = this.ctx.helper.round(d.quantity || 0, qtyDecimal);
- if (d.expr) nd.expr = d.expr || '';
- if (d.memo) nd.memo = d.memo || '';
- insertData.push(nd);
- }
- await this.db.insert(this.tableName, insertData);
- return await this.getAllDataByCondition({
- where: { id: this.ctx.helper._.map(insertData, 'id') }
- });
- }
- async _delDatas (data) {
- if (!data || data.length === 0) throw '提交数据错误';
- const orgDatas = await this.getAllDataByCondition({ where: { id: data } });
- if (!orgDatas || orgDatas.length === 0) throw '删除的辅助工程量不存在';
- const bills = await this.ctx.service.ledger.getDataById(orgDatas[0].lid);
- let billsAnc = await this.getAllDataByCondition({ where: { tid: bills.tid, lid: bills.id } });
- billsAnc = billsAnc.filter(ba => {
- return data.indexOf(ba.id) < 0;
- });
- billsAnc.sort((x, y) => { return x.g_order - y.g_order; });
- const updateData = [];
- billsAnc.forEach((x, i) => {
- if (x.g_order !== i + 1) updateData.push({ id: x.id, g_order: i + 1});
- });
- const conn = await this.db.beginTransaction();
- try {
- await conn.delete(this.tableName, { id: data });
- if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
- await conn.commit();
- return [data, updateData];
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- }
- async _updateDatas (data) {
- if (!data || data.length === 0) throw '提交数据错误';
- const qtyDecimal = this.ctx.tender.info.decimal.qty;
- const user_id = this.ctx.session.sessionUser.accountId;
- const datas = data instanceof Array ? data : [data];
- const orgDatas = await this.getAllDataByCondition({
- where: { id: this.ctx.helper._.map(datas, 'id') }
- });
- if (!orgDatas || orgDatas.length === 0) throw '修改的辅助工程量不存在';
- const uDatas = [];
- for (const d of datas) {
- const od = orgDatas.find(x => { return x.id === d.id; });
- if (!od) continue;
- const nd = { id: od.id, update_user_id: user_id };
- if (d.name !== undefined) nd.name = d.name;
- if (d.g_order !== undefined) nd.g_order = d.g_order;
- if (d.unit !== undefined) nd.unit = d.unit;
- if (d.quantity !== undefined) nd.quantity = this.ctx.helper.round(d.quantity, qtyDecimal);
- if (d.expr !== undefined) nd.expr = d.expr || '';
- if (d.memo !== undefined) nd.memo = d.memo || '';
- uDatas.push(nd);
- }
- if (uDatas.length > 0) {
- await this.db.updateRows(this.tableName, uDatas);
- return uDatas;
- } else {
- return [];
- }
- }
- async updateDatas(data) {
- const result = {add: [], del: [], update: []};
- try {
- if (data.add) {
- result.add = await this._addDatas(data.add);
- }
- if (data.update) {
- result.update = await this._updateDatas(data.update);
- }
- if (data.del) {
- [result.del, result.update] = await this._delDatas(data.del);
- }
- return result;
- } catch (err) {
- if (err.stack) {
- throw err;
- } else {
- result.err = err.toString();
- return result;
- }
- }
- }
- }
- return AncillaryGcl;
- };
|