| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- '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.pid || !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, pid: d.pid, g_order: d.g_order,
- };
- if (d.is_aux !== undefined) nd.is_aux = d.is_aux;
- 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.drawing_code !== undefined) nd.drawing_code = d.drawing_code;
- 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 pos = await this.ctx.service.pos.getDataById(orgDatas[0].pid);
- let posAnc = await this.getAllDataByCondition({ where: { tid: pos.tid, pid: pos.id } });
- posAnc = posAnc.filter(ba => {
- return data.indexOf(ba.id) < 0;
- });
- posAnc.sort((x, y) => { return x.g_order - y.g_order; });
- const updateData = [];
- posAnc.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 this.ctx.service.ancillaryGclDetail.deleteAncGclPartData(conn, orgDatas[0].tid, data);
- 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.is_aux !== undefined) nd.is_aux = d.is_aux;
- 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 (!od.calc_template) {
- if (d.quantity !== undefined) nd.quantity = this.ctx.helper.round(d.quantity, qtyDecimal);
- if (d.expr !== undefined) nd.expr = d.expr || '';
- }
- if (d.drawing_code !== undefined) nd.drawing_code = d.drawing_code;
- 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 _setCalcTemplate(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 = [], detailUpdate = [];
- 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, calc_template: d.calc_template, expr: '', quantity: 0 };
- uDatas.push(nd);
- }
- const conn = await this.db.beginTransaction();
- try {
- if (datas[0].calc_template) {
- for (const ud of uDatas) {
- const [du, quantity] = await this.ctx.service.ancillaryGclDetail.resetAncGclCalcTemplate(conn, ud.id, ud.calc_template);
- detailUpdate.push(...du);
- ud.quantity = this.ctx.helper.round(quantity, qtyDecimal);
- }
- } else {
- await this.ctx.service.ancillaryGclDetail.deleteAncGclPartData(conn, orgDatas[0].tid, uDatas.map(x => { return x.id; }));
- }
- await conn.updateRows(this.tableName, uDatas);
- await conn.commit();
- return [uDatas, detailUpdate];
- } catch (err) {
- this.ctx.log(err);
- await conn.rollback();
- throw '修改计算模板失败';
- }
- }
- async _pasteBlock(data) {
- if (!(data.block instanceof Array)) throw '提交数据错误';
- const qtyDecimal = this.ctx.tender.info.decimal.qty;
- const user_id = this.ctx.session.sessionUser.accountId;
- const pos = await this.ctx.service.pos.getDataById(data.pid);
- const existAncGcl = await this.getAllDataByCondition({ where : { pid: data.pid }, orders: [['g_order', 'DESC']]});
- const maxOrder = existAncGcl.length > 0 ? existAncGcl[0].g_order : 0;
- const insertAncGcl = [], insertDetail = [];
- for (const [i, b] of data.block.entries()) {
- const nig = {
- id: this.uuid.v4(), tid: this.ctx.tender.id,
- add_user_id: user_id, update_user_id: user_id,
- lid: pos.lid, pid: pos.id, g_order: maxOrder + i + 1,
- is_aux: b.is_aux || 0, name: b.name || '', unit: b.unit || '',
- drawing_code: b.drawing_code || '', memo: b.memo || '',
- quantity: b.quantity || 0, expr: b.expr || '', calc_template: b.calc_template || ''
- };
- insertAncGcl.push(nig);
- if (!b.calc_template) continue;
- let sumQty = 0;
- for (const cd of b.calcDetail) {
- const newDetail = {
- id: this.uuid.v4(), tid: this.ctx.tender.id, lid: pos.lid, pid: pos.id, ag_id: nig.id,
- create_user_id: user_id, update_user_id: user_id,
- agd_order: cd.agd_order,
- str1 : cd.str1 || '', str2 : cd.str1 || '', str3 : cd.str1 || '', str4 : cd.str1 || '',
- num_a : cd.num_a || 0, num_b : cd.num_b || 0, num_c : cd.num_c || 0, num_d : cd.num_d || 0,
- num_e : cd.num_e || 0, num_f : cd.num_f || 0, num_g : cd.num_g || 0, num_h : cd.num_h || 0,
- num_i : cd.num_i || 0, num_j : cd.num_j || 0, num_k : cd.num_k || 0, num_l : cd.num_l || 0,
- num_m : cd.num_m || 0, num_n : cd.num_n || 0, num_o : cd.num_o || 0, num_p : cd.num_p || 0,
- num_q : cd.num_q || 0, num_r : cd.num_r || 0, num_s : cd.num_s || 0, num_t : cd.num_t || 0,
- num_u : cd.num_u || 0,
- qty: cd.qty || 0, expr: cd.expr || 0, spec: cd.spec || ''
- };
- sumQty = this.ctx.helper.add(sumQty, newDetail.qty);
- insertDetail.push(newDetail);
- }
- nig.quantity = this.ctx.helper.round(sumQty, qtyDecimal);
- }
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.insert(this.tableName, insertAncGcl);
- if (insertDetail.length > 0) await transaction.insert(this.ctx.service.ancillaryGclDetail.tableName, insertDetail);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return [ insertAncGcl, { add: insertDetail }];
- }
- async updateDatas(data) {
- const result = { add: [], del: [], update: [], details: null };
- 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);
- }
- if (data.calcTmpl) {
- [result.update, result.details] = await this._setCalcTemplate(data.calcTmpl);
- }
- if (data.pasteBlock) {
- [result.add, result.details] = await this._pasteBlock(data.pasteBlock);
- }
- return result;
- } catch (err) {
- if (err.stack) {
- throw err;
- } else {
- result.err = err.toString();
- return result;
- }
- }
- }
- async deleteBillsPartData(transaction, tid, lid) {
- await transaction.delete(this.tableName, { tid: tid, lid: lid });
- }
- async deletePosPartData(transaction, tid, pid) {
- await transaction.delete(this.tableName, { tid: tid, pid: pid });
- }
- async getUsedCalcTemplate(tid) {
- const sql = `SELECT calc_template, count(id) as count FROM ${this.tableName} WHERE tid = ? and calc_template <> '' GROUP BY calc_template`;
- const result = await this.db.query(sql, [tid]);
- return result;
- }
- }
- return AncillaryGcl;
- };
|