12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- const audit = require('../const/audit');
- module.exports = app => {
- class ChangeAuditList extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'change_audit_list';
- }
- /**
- * 取出变更令清单列表,并按台账清单在前,空白清单在后排序
- * @return {void}
- */
- async getList(cid, order_by = this.ctx.change.order_by) {
- if (order_by) {
- return await this.getAllDataByCondition({ where: { cid }, orders: [['order', 'asc']] });
- }
- const sql = 'SELECT * FROM ?? WHERE `cid` = ? ORDER BY `lid` = "0", `id` asc';
- const sqlParam = [this.tableName, cid];
- const result = await this.db.query(sql, sqlParam);
- return this._.orderBy(result, ['order'], ['asc']);
- }
- /**
- * 移除清单时,同步其后清单order
- * @param transaction - 事务
- * @param {Number} cid - 变更cid
- * @param {Number} order - order之后的
- * @return {Promise<*>}
- * @private
- */
- async _syncOrder(transaction, cid, order, selfOperate = '-', num = 1) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('cid', {
- value: this.db.escape(cid),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('order', {
- value: order,
- operate: '>=',
- });
- this.sqlBuilder.setUpdateData('order', {
- value: num,
- selfOperate,
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
- const data = await transaction.query(sql, sqlParam);
- return data;
- }
- /**
- * 添加空白变更清单
- * @return {void}
- */
- async add(data, delimit = 100) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- let order = null;
- if (this.ctx.change.order_by) {
- if (data) {
- order = parseInt(data) + 1;
- // order以下的清单+1
- await this._syncOrder(transaction, this.ctx.change.cid, order, '+');
- } else {
- order = await this.count({ cid: this.ctx.change.cid });
- order = order ? order + 1 : 1;
- }
- }
- const insertData = {
- tid: this.ctx.tender.id,
- cid: this.ctx.change.cid,
- lid: '0',
- code: '',
- name: '',
- bwmx: '',
- unit: '',
- unit_price: null,
- oamount: 0,
- oamount2: 0,
- camount: 0,
- camount_expr: '',
- samount: '',
- detail: '',
- spamount: 0,
- xmj_code: null,
- xmj_jldy: null,
- xmj_dwgc: null,
- xmj_fbgc: null,
- xmj_fxgc: null,
- gcl_id: '',
- mx_id: '',
- order,
- is_valuation: 1,
- delimit,
- };
- // 新增工料
- const result = await transaction.insert(this.tableName, insertData);
- if (result.affectedRows === 0) {
- throw '新增空白清单数据失败';
- }
- await transaction.commit();
- return await this.getDataById(result.insertId);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 添加台账清单(从新增部位页新增)
- * @return {void}
- */
- async adds(datas) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- let order = null;
- if (this.ctx.change.order_by) {
- const data = this.ctx.change.order_site ? await this.getDataById(this.ctx.change.order_site) : null;
- if (data) {
- order = parseInt(data.order) + 1;
- // order以下的清单+1
- await this._syncOrder(transaction, this.ctx.change.cid, order, '+');
- } else {
- order = await this._getMaxOrder(this.ctx.change.cid);
- order = order ? order + 1 : 1;
- }
- }
- const insertData = [];
- for (const d of datas) {
- d.tid = this.ctx.tender.id;
- d.cid = this.ctx.change.cid;
- d.spamount = d.spamount || null;
- d.detail = d.detail || '';
- d.samount = d.samount || '';
- d.order = order ? order : null;
- order = order ? order + 1 : null;
- insertData.push(d);
- }
- // 新增工料
- const result = await transaction.insert(this.tableName, insertData);
- if (result.affectedRows === 0) {
- throw '添加清单数据失败';
- }
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 批量添加空白变更清单
- * @return {void}
- */
- async batchAdd(data, delimit = 100) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const num = data.num ? parseInt(data.num) : 0;
- if (num < 1 || num > 100) {
- throw '批量添加的空白清单数目不能小于1或大于100';
- }
- let order = null;
- if (this.ctx.change.order_by) {
- if (data) {
- order = parseInt(data.postData) + 1;
- // order以下的清单+1
- await this._syncOrder(transaction, this.ctx.change.cid, order, '+', num);
- } else {
- order = await this._getMaxOrder(this.ctx.change.cid);
- order = order ? order + 1 : 1;
- }
- }
- const insertArray = [];
- for (let i = 0; i < num; i++) {
- const insertData = {
- tid: this.ctx.tender.id,
- cid: this.ctx.change.cid,
- lid: '0',
- code: '',
- name: '',
- bwmx: '',
- unit: '',
- unit_price: null,
- oamount: 0,
- oamount2: 0,
- camount: 0,
- camount_expr: '',
- samount: '',
- detail: '',
- spamount: 0,
- xmj_code: null,
- xmj_jldy: null,
- xmj_dwgc: null,
- xmj_fbgc: null,
- xmj_fxgc: null,
- gcl_id: '',
- mx_id: '',
- order: order ? order + i : null,
- is_valuation: 1,
- delimit,
- };
- insertArray.push(insertData);
- }
- // 新增工料
- const result = await transaction.insert(this.tableName, insertArray);
- if (result.affectedRows !== num) {
- throw '批量添加空白清单数据失败';
- }
- await transaction.commit();
- // // 获取刚批量添加的所有list
- // for (let j = 0; j < num; j++) {
- // insertArray[j].id = result.insertId + j;
- // }
- // return insertArray;
- return await this.getList(this.ctx.change.cid);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async _getMaxOrder(cid) {
- const sql = 'SELECT MAX(`order`) AS `order` FROM ?? WHERE cid = ?';
- const sqlParams = [this.tableName, cid];
- const result = await this.db.queryOne(sql, sqlParams);
- return result ? result.order : 0;
- }
- /**
- * 删除变更清单
- * @param {int} id 清单id
- * @return {void}
- */
- async del(data) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- // 判断是否可删
- await transaction.delete(this.tableName, { id: data.ids });
- // // order以下的清单-1
- if (this.ctx.change.order_by && data.postData) {
- await this._syncOrder(transaction, this.ctx.change.cid, data.postData, '-', data.delLength ? data.delLength : data.ids.length);
- }
- // 重新算变更令总额
- await this.calcCamountSum(transaction);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async dels(data) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- // 判断是否存在调用,存在则报错
- const delList = await this.getAllDataByCondition({ where: { id: data.ids } });
- const sql1 = 'SELECT a.* FROM ?? as b LEFT JOIN ?? as a ON b.cbid = a.id WHERE b.cid = ? AND b.id in (' + this.ctx.helper.getInArrStrSqlFilter(data.ids) + ') GROUP BY b.cbid';
- const sqlParam1 = [this.ctx.service.stageChange.tableName, this.tableName, this.ctx.change.cid];
- const usedList = await transaction.query(sql1, sqlParam1);
- if (usedList.length > 0) {
- throw '清单已被调用,不可删除';
- }
- await transaction.delete(this.tableName, { id: data.ids });
- // // order以下的清单-1
- if (this.ctx.change.order_by) {
- const postData = this.ctx.change.order_site ? await this.getDataById(this.ctx.change.order_site) : null;
- await this._syncOrder(transaction, this.ctx.change.cid, (postData ? postData.order : null), '-', data.ids.length);
- }
- // 重新算变更令总额
- await this.calcCamountSum(transaction);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 修改变更清单
- * @param {Object} data 工料内容
- * @param {int} order 期数
- * @return {void}
- */
- async save(data) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- // const mb_id = data.mb_id;
- // delete data.mb_id;
- await transaction.update(this.tableName, data);
- // await this.calcQuantityByML(transaction, mb_id);
- await this.calcCamountSum(transaction);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 修改变更清单 复制粘贴
- * @param {Object} datas 修改内容
- * @return {void}
- */
- async saveDatas(datas) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- // 判断是否可修改
- // 判断t_type是否为费用
- const transaction = await this.db.beginTransaction();
- try {
- // for (const data of datas) {
- // const mb_id = data.mb_id;
- // delete data.mb_id;
- // await transaction.update(this.tableName, data);
- // await this.calcQuantityByML(transaction, mb_id);
- // }
- await transaction.updateRows(this.tableName, datas);
- await this.calcCamountSum(transaction);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 台账数据清单 重新选择
- * @param {Object} datas 内容
- * @return {void}
- */
- async saveLedgerListDatas(datas, data = null, order_by = this.ctx.change.order_by) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- // 判断是否可修改
- // 判断t_type是否为费用
- const transaction = await this.db.beginTransaction();
- try {
- let usedList = [];
- let order = null;
- if (order_by) {
- if (data) {
- order = parseInt(data) + 1;
- // order以下的清单+1
- await this._syncOrder(transaction, this.ctx.change.cid, order, '+', datas.length);
- } else {
- order = await this.count({ cid: this.ctx.change.cid });
- order = order ? order + 1 : 1;
- }
- } else {
- const sql1 = 'SELECT a.* FROM ?? as b LEFT JOIN ?? as a ON b.cbid = a.id WHERE b.cid = ? GROUP BY b.cbid';
- const sqlParam1 = [this.ctx.service.stageChange.tableName, this.tableName, this.ctx.change.cid];
- usedList = await transaction.query(sql1, sqlParam1);
- // 先删除原本的台账清单数据
- const sql = 'DELETE FROM ?? WHERE cid = ? and lid != "0"';
- const sqlParam = [this.tableName, this.ctx.change.cid];
- await transaction.query(sql, sqlParam);
- }
- const insertDatas = [];
- for (const data of datas) {
- data.tid = this.ctx.tender.id;
- data.cid = this.ctx.change.cid;
- data.spamount = data.camount;
- data.samount = '';
- data.order = order ? order : null;
- order = order ? order + 1 : null;
- insertDatas.push(data);
- }
- if (insertDatas.length > 0) await this.insertBigDatas(transaction, insertDatas);
- await this.calcCamountSum(transaction);
- if (!order_by) {
- // 更新stage_change和stage_change_final的cbid
- if (usedList.length > 0) {
- const updateList = [];
- const sql2 = 'SELECT * FROM ?? WHERE `cid` = ? AND `lid` != "0"';
- const sqlParam2 = [this.tableName, this.ctx.change.cid];
- const newList = await transaction.query(sql2, sqlParam2);
- // const newList = await transaction.select(this.tableName, { where: { cid: this.ctx.change.cid } });
- for (const used of usedList) {
- const findFilter = { lid: used.lid, gcl_id: used.gcl_id, bwmx: used.bwmx };
- if (used.mx_id) findFilter.mx_id = used.mx_id;
- const newone = this._.find(newList, findFilter);
- if (newone) {
- updateList.push({
- row: {
- cbid: newone.id,
- },
- where: {
- cid: this.ctx.change.cid,
- cbid: used.id,
- },
- });
- }
- }
- if (updateList.length > 0) {
- await transaction.updateRows(this.ctx.service.stageChange.tableName, updateList);
- await transaction.updateRows(this.ctx.service.stageChangeFinal.tableName, updateList);
- }
- }
- }
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 台账数据清单 清除部分并重新算原设计总金额
- * @param {Object} datas 内容
- * @return {void}
- */
- async removeLedgerListDatas(datas) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- // 判断是否可修改
- // 判断t_type是否为费用
- const transaction = await this.db.beginTransaction();
- try {
- // 先删除原本的台账清单数据
- // const sql = 'DELETE FROM ?? WHERE cid = ? and lid != "0"';
- // const sqlParam = [this.tableName, this.ctx.change.cid];
- // await transaction.query(sql, sqlParam);
- // const insertDatas = [];
- for (const data of datas) {
- // data.tid = this.ctx.tender.id;
- // data.cid = this.ctx.change.cid;
- // data.spamount = data.camount;
- // data.samount = '';
- // insertDatas.push(data);
- await transaction.delete(this.tableName, { id: data.id });
- }
- // if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
- await this.calcCamountSum(transaction);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async calcCamountSum(transaction, updateTpDecimal = false) {
- // const sql = 'SELECT SUM(ROUND(`camount`*`unit_price`, )) as total_price FROM ?? WHERE cid = ?';
- // const sqlParam = [this.tableName, this.change.cid];
- // const tp = await transaction.queryOne(sql, sqlParam);
- // 防止小数位不精确,采用取值计算
- const sql = 'SELECT unit_price, spamount, is_valuation, gcl_id, unit FROM ?? WHERE cid = ?';
- const sqlParam = [this.tableName, this.ctx.change.cid];
- const changeList = await transaction.query(sql, sqlParam);
- let total_price = 0;
- let positive_tp = 0;
- let negative_tp = 0;
- let valuation_tp = 0;
- let unvaluation_tp = 0;
- const tp_decimal = this.ctx.change.tp_decimal ? this.ctx.change.tp_decimal : this.ctx.tender.info.decimal.tp;
- const up_decimal = this.ctx.change.up_decimal ? this.ctx.change.up_decimal : this.ctx.tender.info.decimal.up;
- const gclChangeList = this._.uniq(this._.map(changeList, 'gcl_id'));
- for (const g of gclChangeList) {
- if (g) {
- const list = this._.filter(changeList, { gcl_id: g });
- let spamount = 0;
- let valuation_amount = 0;
- let unvaluation_amount = 0;
- let unitPrice = 0;
- for (const cl of list) {
- if (cl.spamount) {
- spamount = this.ctx.helper.accAdd(spamount, cl.spamount);
- unitPrice = cl.unit_price;
- if (cl.is_valuation) {
- valuation_amount = this.ctx.helper.accAdd(valuation_amount, cl.spamount);
- } else {
- unvaluation_amount = this.ctx.helper.accAdd(unvaluation_amount, cl.spamount);
- }
- }
- }
- const price = this.ctx.helper.mul(spamount, this.ctx.helper.round(unitPrice, up_decimal), tp_decimal);
- const valuation_price = this.ctx.helper.mul(valuation_amount, this.ctx.helper.round(unitPrice, up_decimal), tp_decimal) || 0;
- const unvaluation_price = this.ctx.helper.mul(unvaluation_amount, this.ctx.helper.round(unitPrice, up_decimal), tp_decimal) || 0;
- valuation_tp = this.ctx.helper.accAdd(valuation_tp, valuation_price);
- unvaluation_tp = this.ctx.helper.accAdd(unvaluation_tp, unvaluation_price);
- total_price = this.ctx.helper.accAdd(total_price, price);
- if (price >= 0) {
- positive_tp = this.ctx.helper.accAdd(positive_tp, price);
- } else {
- negative_tp = this.ctx.helper.accAdd(negative_tp, price);
- }
- } else {
- const list = this._.filter(changeList, { gcl_id: g });
- for (const cl of list) {
- const price = this.ctx.helper.mul(this.ctx.helper.round(cl.unit_price, up_decimal), cl.spamount, tp_decimal);
- total_price = this.ctx.helper.accAdd(total_price, price);
- if (price >= 0) {
- positive_tp = this.ctx.helper.accAdd(positive_tp, price);
- } else {
- negative_tp = this.ctx.helper.accAdd(negative_tp, price);
- }
- if (cl.is_valuation) {
- valuation_tp = this.ctx.helper.accAdd(valuation_tp, price);
- } else {
- unvaluation_tp = this.ctx.helper.accAdd(unvaluation_tp, price);
- }
- }
- }
- }
- const updateData = {
- total_price,
- positive_tp,
- negative_tp,
- valuation_tp,
- unvaluation_tp,
- };
- if (updateTpDecimal) {
- updateData.tp_decimal = tp_decimal;
- updateData.up_decimal = up_decimal;
- }
- const options = {
- where: {
- cid: this.ctx.change.cid,
- },
- };
- await transaction.update(this.ctx.service.change.tableName, updateData, options);
- }
- /**
- * 用户数据数量提交
- * @param {Object} data 内容
- * @return {void}
- */
- async saveAmountData(data) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- // 判断是否可修改
- // 判断t_type是否为费用
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.update(this.tableName, data);
- await this.calcCamountSum(transaction);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async gatherBgBills(tid) {
- const sql = 'SELECT cb.code, cb.name, cb.unit, cb.unit_price, Round(Sum(cb.samount + 0), 6) as quantity' +
- ' FROM ' + this.tableName + ' cb' +
- ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
- ' WHERE cb.tid = ? and c.status = ?' +
- ' GROUP BY code, name, unit, unit_price';
- const param = [tid, audit.flow.status.checked];
- const result = await this.db.query(sql, param);
- for (const b of result) {
- b.total_price = this.ctx.helper.mul(b.unit_price, b.quantity, this.ctx.tender.info.decimal.tp);
- }
- return result;
- }
- /**
- * 报表用
- * Tony Kang
- * @param {tid} tid - 标段id
- * @return {void}
- */
- async getChangeAuditBills(tid, onlyChecked) {
- const sql = 'SELECT cb.*' +
- ' FROM ' + this.tableName + ' cb' +
- ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
- ' WHERE c.tid = ? ' + (onlyChecked ? 'and c.status = 3' : '') +
- ' ORDER BY cb.cid, cb.code';
- const param = [tid];
- const result = await this.db.query(sql, param);
- return result;
- }
- /**
- * 删除变更清单(form 变更新增部位页)
- * Tony Kang
- * @param {String} transaction - 队列
- * @param {String} tid - 标段id
- * @param {Array} ids - id列表
- * @param {String} column - id所属字段
- * @param {String} mx_id - mx_id为空列删除
- * @return {void}
- */
- async deleteDataByRevise(transaction, tid, ids, column = 'gcl_id', mx_id = 'hello') {
- if (ids.length > 0) {
- const addSql = mx_id === '' ? ' AND (`mx_id` is NULL OR `mx_id` = "")' : '';
- const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')' + addSql + ' GROUP BY `cid`';
- const params = [this.tableName, tid];
- const changes = await transaction.query(sql, params);
- if (changes.length > 0) {
- const delData = {
- tid,
- };
- delData[column] = ids;
- await transaction.delete(this.tableName, delData);
- for (const c of changes) {
- // 重算选了此清单的变更令已变更金额
- await this.reCalcTp(transaction, c.cid);
- }
- }
- }
- }
- /**
- * 修改变更清单(form 变更新增部位页台账子节点清单编号编辑)
- * Tony Kang
- * @param {String} transaction - 队列
- * @param {String} tid - 标段id
- * @param {Array} datas - 更新列表
- * @param {String} column - id所属字段
- * @return {void}
- */
- async updateDataByReviseLedger(transaction, tid, datas, column = 'gcl_id') {
- if (datas.length > 0) {
- const ids = this._.map(datas, 'id');
- const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
- const params = [this.tableName, tid];
- const changeAuditLists = await transaction.query(sql, params);
- if (changeAuditLists.length > 0) {
- const updateArr = [];
- const cidList = [];
- for (const ca of changeAuditLists) {
- const d = this._.find(datas, { id: ca[column] });
- if (d.id) {
- const changePosNum = await transaction.count(this.ctx.service.changePos.tableName, { lid: d.id });
- const updateCol = {};
- if (column === 'gcl_id' && d.b_code) updateCol.code = d.b_code;
- if (column === 'gcl_id' && d.quantity !== undefined && changePosNum === 0) updateCol.oamount = d.quantity ? d.quantity : 0;
- if (column === 'gcl_id' && d.unit_price !== undefined) updateCol.unit_price = d.unit_price ? d.unit_price : 0;
- if (column === 'gcl_id' && d.unit !== undefined) updateCol.unit = d.unit;
- if (column === 'gcl_id' && d.name !== undefined) updateCol.name = d.name;
- if (d.b_code !== undefined && d.b_code === null) {
- // 清单升级成了项目节,故删除变更已有的此清单,并找出需要重新计算的变更令
- const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
- const params = [this.tableName, tid, d.id];
- const changes = await transaction.query(sql, params);
- for (const c of changes) {
- if (this._.indexOf(cidList, c.cid) === -1) {
- cidList.push(c.cid);
- }
- }
- const delData = {
- tid,
- };
- delData[column] = d.id;
- await transaction.delete(this.tableName, delData);
- } else {
- const options = {
- row: {},
- where: {},
- };
- options.row = updateCol;
- options.where[column] = d.id;
- if (!this._.isEmpty(options.row)) updateArr.push(options);
- if (updateCol.unit !== undefined || updateCol.unit_price !== undefined) {
- const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
- const params = [this.tableName, tid, d.id];
- const changes = await transaction.query(sql, params);
- for (const c of changes) {
- if (this._.indexOf(cidList, c.cid) === -1) {
- cidList.push(c.cid);
- }
- }
- }
- }
- }
- }
- if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
- if (cidList.length > 0) {
- for (const c of cidList) {
- await this.reCalcTp(transaction, c);
- }
- }
- }
- // 针对项目节更新可能对清单影响判断,修正变更清单项目节编号,细目,单位工程,分部分项工程数据
- for (const data of datas) {
- const select = await transaction.get(this.ctx.service.changeLedger.tableName, { id: data.id });
- if (select && select.is_leaf === 0) {
- const lists = await this.ctx.service.changeLedger.getDataByFullPath(this.ctx.service.changeLedger.tableName, tid, select.full_path + '%', transaction);
- const childLists = this._.filter(lists, { level: select.level + 1 }); // 细目or项目节编号更新
- if (childLists.length > 0) {
- const d = { xmj_code: '', xmj_jldy: '' };
- if (select.code !== null) {
- d.xmj_code = select.code;
- d.xmj_jldy = select.name;
- } else {
- // 再找出上一个项目节节点并更新
- this.newBills = false;
- const parents = await this.ctx.service.changeLedger.getDataByKid(tid, select.ledger_pid);
- d.xmj_code = parents.code;
- d.xmj_jldy = parents.name;
- }
- for (const cl of childLists) {
- await transaction.update(this.tableName, { xmj_code: d.xmj_code, xmj_jldy: d.xmj_jldy }, { where: { tid, gcl_id: cl.id } });
- }
- }
- if (select.code !== null && data.name !== undefined) { // 名称修改则可能影响几个数据
- const secondChildLists = this._.filter(lists, { level: select.level + 2 }); // 分项工程更新
- const thirdChildLists = this._.filter(lists, { level: select.level + 3 }); // 分部工程更新
- const fourthChildLists = this._.filter(lists, { level: select.level + 4 }); // 单位工程更新
- if (secondChildLists.length > 0) {
- for (const sl of secondChildLists) {
- await transaction.update(this.tableName, { xmj_fxgc: select.name }, { where: { tid, gcl_id: sl.id } });
- }
- }
- if (thirdChildLists.length > 0) {
- for (const tl of thirdChildLists) {
- await transaction.update(this.tableName, { xmj_fbgc: select.name }, { where: { tid, gcl_id: tl.id } });
- }
- }
- if (fourthChildLists.length > 0 && select.level === 2) {
- for (const fl of fourthChildLists) {
- await transaction.update(this.tableName, { xmj_dwgc: select.name }, { where: { tid, gcl_id: fl.id } });
- }
- }
- }
- }
- }
- }
- }
- /**
- * 修改变更清单(form 变更新增部位页台账节点清单编号升降级)
- * Tony Kang
- * @param {String} transaction - 队列
- * @param {String} tid - 标段id
- * @param {Array} datas - 更新列表
- * @param {String} column - id所属字段
- * @return {void}
- */
- async updateDataByReviseLedgerUpDownLevel(transaction, tid, datas, column = 'gcl_id') {
- if (datas.length > 0) {
- console.log(datas);
- // const ids = this._.map(datas, 'id');
- // const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
- // const params = [this.tableName, tid];
- // const changeAuditLists = await transaction.query(sql, params);
- // if (changeAuditLists.length > 0) {
- // const updateArr = [];
- // const cidList = [];
- // for (const ca of changeAuditLists) {
- // const d = this._.find(datas, { id: ca[column] });
- // console.log(d);
- // if (d.id) {
- // const changePosNum = await transaction.count(this.ctx.service.changePos.tableName, { lid: d.id });
- // const updateCol = {};
- // if (column === 'gcl_id' && d.b_code !== undefined) updateCol.code = d.b_code;
- // if (column === 'gcl_id' && d.sgfh_qty !== undefined && changePosNum === 0) updateCol.oamount = d.sgfh_qty ? d.sgfh_qty : 0;
- // if (column === 'gcl_id' && d.unit_price !== undefined) updateCol.unit_price = d.unit_price ? d.unit_price : 0;
- // if (column === 'gcl_id' && d.unit !== undefined) updateCol.unit = d.unit;
- // if (column === 'gcl_id' && d.name !== undefined) updateCol.name = d.name;
- // if (d.code !== undefined && d.b_code === null) {
- // // 清单升级成了项目节,故删除变更已有的此清单,并找出需要重新计算的变更令
- // const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
- // const params = [this.tableName, tid, d.id];
- // const changes = await transaction.query(sql, params);
- // for (const c of changes) {
- // if (this._.indexOf(cidList, c.cid) === -1) {
- // cidList.push(c.cid);
- // }
- // }
- // const delData = {
- // tid,
- // };
- // delData[column] = d.id;
- // console.log(delData);
- // await transaction.delete(this.tableName, delData);
- // } else {
- // const options = {
- // row: {},
- // where: {},
- // };
- // options.row = updateCol;
- // options.where[column] = d.id;
- // if (!this._.isEmpty(options.row)) updateArr.push(options);
- // if (updateCol.unit !== undefined || updateCol.unit_price !== undefined) {
- // const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
- // const params = [this.tableName, tid, d.id];
- // const changes = await transaction.query(sql, params);
- // for (const c of changes) {
- // if (this._.indexOf(cidList, c.cid) === -1) {
- // cidList.push(c.cid);
- // }
- // }
- // }
- // }
- // }
- // }
- // console.log(updateArr, cidList);
- // if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
- // if (cidList.length > 0) {
- // for (const c of cidList) {
- // await this.reCalcTp(transaction, c);
- // }
- // }
- // }
- // 针对项目节更新可能对清单影响判断,修正变更清单项目节编号,细目,单位工程,分部分项工程数据
- // for (const data of datas) {
- // const select = await transaction.get(this.ctx.service.changeLedger.tableName, { id: data.id });
- // console.log(select);
- // if (select && select.is_leaf === 0) {
- // const lists = await this.ctx.service.changeLedger.getDataByFullPath(this.ctx.service.changeLedger.tableName, tid, select.full_path + '%', transaction);
- // const childLists = this._.filter(lists, { level: select.level + 1 }); // 细目or项目节编号更新
- // if (childLists.length > 0) {
- // const d = { xmj_code: '', xmj_jldy: '' };
- // if (select.code !== null) {
- // d.xmj_code = select.code;
- // d.xmj_jldy = select.name;
- // } else {
- // // 再找出上一个项目节节点并更新
- // this.newBills = false;
- // const parents = await this.ctx.service.changeLedger.getDataByKid(tid, select.ledger_pid);
- // console.log('hello :', parents);
- // d.xmj_code = parents.code;
- // d.xmj_jldy = parents.name;
- // }
- // for (const cl of childLists) {
- // await transaction.update(this.tableName, { xmj_code: d.xmj_code, xmj_jldy: d.xmj_jldy }, { where: { tid, gcl_id: cl.id } });
- // }
- // }
- // if (select.code !== null && data.name !== undefined) { // 名称修改则可能影响几个数据
- // const secondChildLists = this._.filter(lists, { level: select.level + 2 }); // 分项工程更新
- // const thirdChildLists = this._.filter(lists, { level: select.level + 3 }); // 分部工程更新
- // const fourthChildLists = this._.filter(lists, { level: select.level + 4 }); // 单位工程更新
- // if (secondChildLists.length > 0) {
- // for (const sl of secondChildLists) {
- // await transaction.update(this.tableName, { xmj_fxgc: select.name }, { where: { tid, gcl_id: sl.id } });
- // }
- // }
- // if (thirdChildLists.length > 0) {
- // for (const tl of thirdChildLists) {
- // await transaction.update(this.tableName, { xmj_fbgc: select.name }, { where: { tid, gcl_id: tl.id } });
- // }
- // }
- // if (fourthChildLists.length > 0) {
- // for (const fl of fourthChildLists) {
- // await transaction.update(this.tableName, { xmj_dwgc: select.name }, { where: { tid, gcl_id: fl.id } });
- // }
- // }
- // }
- // }
- // }
- }
- }
- /**
- * 修改变更清单(form 变更新增部位页计量单元编辑)
- * Tony Kang
- * @param {String} transaction - 队列
- * @param {String} tid - 标段id
- * @param {Array} datas - 更新列表
- * @param {String} column - id所属字段
- * @return {void}
- */
- async updateDataByRevisePos(transaction, tid, datas, column = 'mx_id') {
- if (datas.length > 0) {
- const ids = this._.map(datas, 'id');
- const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
- const params = [this.tableName, tid];
- const changeAuditLists = await transaction.query(sql, params);
- if (changeAuditLists.length > 0) {
- const updateArr = [];
- for (const ca of changeAuditLists) {
- const d = this._.find(datas, { id: ca[column] });
- if (d.id) {
- const updateCol = {};
- if (column === 'mx_id' && d.name !== undefined) updateCol.bwmx = d.name;
- if (column === 'mx_id' && d.quantity !== undefined) updateCol.oamount = d.quantity ? d.quantity : 0;
- if (column === 'mx_id' && d.quantity === undefined &&
- ((d.sgfh_expr && d.sgfh_expr === '') || (d.sjcl_expr && d.sjcl_expr === '') || (d.qtcl_expr && d.qtcl_expr === ''))) updateCol.oamount = 0;
- const options = {
- row: {},
- where: {},
- };
- options.row = updateCol;
- options.where[column] = d.id;
- // if (!this._.isEmpty(updateCol)) await transaction.update(this.tableName, updateCol, options);
- if (!this._.isEmpty(options.row)) updateArr.push(options);
- }
- }
- if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
- }
- }
- }
- /**
- * 重算变更令总金额(变更新增部位设置时使用)
- * @param {String} transaction - 队列
- * @param {String} cid - 变更令id
- */
- async reCalcTp(transaction, cid) {
- const change = await transaction.get(this.ctx.service.change.tableName, { cid });
- let count = '';
- if (change.status === audit.flow.status.uncheck || change.status === audit.flow.status.back || change.status === audit.flow.status.revise) {
- count = '`camount`';
- } else if (change.status === audit.flow.status.checking || change.status === audit.flow.status.backnew) {
- count = '`spamount`';
- }
- if (count) {
- const sql = 'SELECT `unit_price`, ' + count + ' as `count` FROM ?? WHERE `cid` = ?';
- const params = [this.tableName, change.cid];
- const caLists = await transaction.query(sql, params);
- let tp = 0;
- const tpUnit = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
- for (const ca of caLists) {
- const catp = this.ctx.helper.round(this.ctx.helper.mul(ca.unit_price, ca.count), tpUnit);
- tp = this.ctx.helper.add(tp, catp);
- }
- console.log(tp);
- if (tp !== change.total_price) {
- const options = {
- where: {
- cid: change.cid,
- },
- };
- const change_update = {
- total_price: tp,
- };
- await transaction.update(this.ctx.service.change.tableName, change_update, options);
- }
- }
- }
- async updateToLedger(transaction, tid, cid) {
- // 找出本条变更属于新增部位的数据
- const allList = await transaction.select(this.tableName, { where: { tid, cid } });
- const result = [];
- const result2 = [];
- for (const l of allList) {
- const changeLedgerInfo = await transaction.get(this.ctx.service.changeLedger.tableName, { id: l.gcl_id });
- if (changeLedgerInfo && this._.findIndex(result, { id: l.gcl_id }) === -1) {
- result.push(changeLedgerInfo);
- }
- const changePosInfo = await transaction.get(this.ctx.service.changePos.tableName, { id: l.mx_id });
- if (changePosInfo) {
- result2.push(changePosInfo);
- }
- }
- // const sql = 'SELECT a.* FROM ?? a LEFT JOIN ?? b ON a.id = b.gcl_id WHERE b.tid = ? AND b.cid = ? GROUP BY a.id';
- // const sqlParam = [this.ctx.service.changeLedger.tableName, this.tableName, tid, cid];
- // const result = await transaction.query(sql, sqlParam);
- // const sql2 = 'SELECT a.* FROM ?? a LEFT JOIN ?? b ON a.id = b.mx_id WHERE b.tid = ? AND b.cid = ?';
- // const sqlParam2 = [this.ctx.service.changePos.tableName, this.tableName, tid, cid];
- // const result2 = await transaction.query(sql2, sqlParam2);
- if (result.length > 0 || result2.length > 0) {
- const changeLedgerGclIdList = this._.map(result, 'id');
- const changeLedgerIdList = this._.uniq(this._.map(result, 'ledger_pid'));// 父节点集合
- const needUpdateLedgerList = [];// 找出需要更新的原台账清单的id
- const needUpdateChangeLedgerList = [];// 找出需要更新的新台账清单的id
- const tpDecimal = this.ctx.tender.info.decimal.tp;
- // 要更新的ledger节点,数量及总数
- for (const data of result2) {
- if (this._.indexOf(changeLedgerGclIdList, data.lid) === -1) {
- const info = this._.find(needUpdateLedgerList, { id: data.lid });
- if (info) {
- info.sgfh_qty = this.ctx.helper.add(info.sgfh_qty, data.sgfh_qty);
- info.sjcl_qty = this.ctx.helper.add(info.sjcl_qty, data.sjcl_qty);
- info.qtcl_qty = this.ctx.helper.add(info.qtcl_qty, data.qtcl_qty);
- info.quantity = this.ctx.helper.add(info.quantity, data.quantity);
- } else {
- needUpdateLedgerList.push({ id: data.lid, sgfh_qty: data.sgfh_qty, sjcl_qty: data.sjcl_qty, qtcl_qty: data.qtcl_qty, quantity: data.quantity });
- }
- } else {
- const info = this._.find(needUpdateChangeLedgerList, { id: data.lid });
- if (info) {
- info.sgfh_qty = this.ctx.helper.add(info.sgfh_qty, data.sgfh_qty);
- info.sjcl_qty = this.ctx.helper.add(info.sjcl_qty, data.sjcl_qty);
- info.qtcl_qty = this.ctx.helper.add(info.qtcl_qty, data.qtcl_qty);
- info.quantity = this.ctx.helper.add(info.quantity, data.quantity);
- } else {
- needUpdateChangeLedgerList.push({ id: data.lid, sgfh_qty: data.sgfh_qty, sjcl_qty: data.sjcl_qty, qtcl_qty: data.qtcl_qty, quantity: data.quantity });
- }
- }
- }
- // 更新到result上
- if (needUpdateChangeLedgerList.length > 0) {
- for (const nucl of needUpdateChangeLedgerList) {
- const now = this._.find(result, { id: nucl.id });
- now.sgfh_qty = nucl.sgfh_qty;
- now.sjcl_qty = nucl.sjcl_qty;
- now.qtcl_qty = nucl.qtcl_qty;
- now.quantity = nucl.quantity;
- now.sgfh_tp = this.ctx.helper.mul(now.sgfh_qty, now.unit_price, tpDecimal);
- now.sjcl_tp = this.ctx.helper.mul(now.sjcl_qty, now.unit_price, tpDecimal);
- now.qtcl_tp = this.ctx.helper.mul(now.qtcl_qty, now.unit_price, tpDecimal);
- now.total_price = this.ctx.helper.mul(now.quantity, now.unit_price, tpDecimal);
- }
- }
- // 更新到ledger上
- if (needUpdateLedgerList.length > 0) {
- for (const nul of needUpdateLedgerList) {
- const ledgerInfo = await this.ctx.service.ledger.getDataById(nul.id);
- ledgerInfo.sgfh_qty = this.ctx.helper.add(ledgerInfo.sgfh_qty, nul.sgfh_qty);
- ledgerInfo.sjcl_qty = this.ctx.helper.add(ledgerInfo.sjcl_qty, nul.sjcl_qty);
- ledgerInfo.qtcl_qty = this.ctx.helper.add(ledgerInfo.qtcl_qty, nul.qtcl_qty);
- ledgerInfo.quantity = this.ctx.helper.add(ledgerInfo.quantity, nul.quantity);
- ledgerInfo.sgfh_tp = this.ctx.helper.mul(ledgerInfo.sgfh_qty, ledgerInfo.unit_price, tpDecimal);
- ledgerInfo.sjcl_tp = this.ctx.helper.mul(ledgerInfo.sjcl_qty, ledgerInfo.unit_price, tpDecimal);
- ledgerInfo.qtcl_tp = this.ctx.helper.mul(ledgerInfo.qtcl_qty, ledgerInfo.unit_price, tpDecimal);
- ledgerInfo.total_price = this.ctx.helper.mul(ledgerInfo.quantity, ledgerInfo.unit_price, tpDecimal);
- await transaction.update(this.ctx.service.ledger.tableName, ledgerInfo);
- }
- }
- // 找出所有新增的父节点并插入到result中
- for (const r of changeLedgerIdList) {
- await this._findParents(transaction, tid, r, result);
- }
- // 插入到计量单元表,并删除变更的计量单元数据, 插入清单表,并删除变更的清单表
- await this._insertByChangeRevise(transaction, tid, cid, result, result2);
- // 更新标段总金额
- const sumSql = 'SELECT Sum(total_price) As total_price, Sum(deal_tp) As deal_tp' +
- ' FROM ' + this.ctx.service.ledger.tableName + this.ctx.helper.whereSql({ tender_id: tid });
- const sum = await transaction.queryOne(sumSql);
- await transaction.update(this.ctx.service.tender.tableName, {
- id: tid,
- total_price: sum.total_price,
- deal_tp: sum.deal_tp,
- });
- // 清除修订及台账的maxLid缓存,防止树结构混乱
- await this.ctx.service.reviseBills._removeCacheMaxLid(tid);
- await this.ctx.service.ledger._removeCacheMaxLid(tid);
- }
- }
- async _findParents(transaction, tid, id, result) {
- const info = await transaction.get(this.ctx.service.changeLedger.tableName, { tender_id: tid, ledger_id: id });
- if (info && this._.findIndex(result, { ledger_id: info.ledger_id }) === -1) {
- result.push(info);
- await this._findParents(transaction, tid, info.ledger_pid, result);
- } else {
- return;
- }
- }
- async _insertByChangeRevise(transaction, tid, cid, ledgerList, posList) {
- if (ledgerList.length > 0) {
- const insertLedgerArr = [];
- for (const l of ledgerList) {
- const insertL = [
- l.id, l.code, l.b_code, l.name, l.unit, l.source, l.remark, l.ledger_id,
- l.ledger_pid, l.level, l.order, l.full_path, l.is_leaf, l.quantity, l.total_price,
- l.unit_price, l.drawing_code, l.memo, l.dgn_qty1, l.dgn_qty2, l.deal_qty, l.deal_tp,
- l.sgfh_qty, l.sgfh_tp, l.sjcl_qty, l.sjcl_tp, l.qtcl_qty, l.qtcl_tp, l.node_type, l.crid, l.ccid,
- l.tender_id, l.sgfh_expr, l.sjcl_expr, l.qtcl_expr, l.check_calc,
- l.ex_memo1, l.ex_memo2, l.ex_memo3,
- ];
- insertLedgerArr.push('(' + this.ctx.helper.getInArrStrSqlFilter(insertL) + ')');
- await transaction.delete(this.ctx.service.changeLedger.tableName, { id: l.id });
- // 日志添加
- await transaction.insert(this.ctx.service.changeReviseLog.tableName, { tid, cid, lid: l.id, name: l.name ? l.name : (l.code ? l.code : ''), create_time: new Date() });
- }
- const bSql = 'Insert Into ' +
- this.ctx.service.ledger.tableName +
- ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
- ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
- ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, ccid, tender_id,' +
- ' sgfh_expr, sjcl_expr, qtcl_expr, check_calc,' +
- ' ex_memo1, ex_memo2, ex_memo3) VALUES ' + insertLedgerArr.join(',') + ';';
- await transaction.query(bSql, []);
- }
- if (posList.length > 0) {
- const insertPosArr = [];
- for (const p of posList) {
- const insertp = [
- p.id, p.tid, p.lid, p.name, p.drawing_code, p.quantity, p.add_stage, p.add_stage_order, p.add_times,
- p.add_user, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty, p.crid, p.ccid, p.porder, p.position,
- p.sgfh_expr, p.sjcl_expr, p.qtcl_expr, p.real_qty,
- p.ex_memo1, p.ex_memo2, p.ex_memo3,
- ];
- insertPosArr.push('(' + this.ctx.helper.getInArrStrSqlFilter(insertp) + ')');
- await transaction.delete(this.ctx.service.changePos.tableName, { id: p.id });
- // 日志添加
- await transaction.insert(this.ctx.service.changeReviseLog.tableName, { tid, cid, pid: p.id, name: p.name, create_time: new Date() });
- }
- const pSql =
- 'Insert Into ' +
- this.ctx.service.pos.tableName +
- ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, add_times, add_user,' +
- ' sgfh_qty, sjcl_qty, qtcl_qty, crid, ccid, porder, position, ' +
- ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
- ' ex_memo1, ex_memo2, ex_memo3) VALUES ' + insertPosArr.join(',') + ';';
- await transaction.query(pSql, []);
- }
- }
- async checkedChangeBills(tid) {
- const DefaultDecimal = this.ctx.tender.info.decimal.tp;
- const sql = 'SELECT cal.*, c.tp_decimal FROM ' + this.ctx.service.changeAuditList.tableName + ' cal LEFT JOIN ' + this.ctx.service.change.tableName + ' c on cal.cid = c.cid where c.tid = ? and c.valid and c.status = ?';
- const changeBills = await this.db.query(sql, [tid, audit.flow.status.checked]);
- changeBills.forEach(x => {
- x.tp_decimal = x.tp_decimal !== 0 ? x.tp_decimal : DefaultDecimal
- });
- return changeBills;
- }
- /**
- * 交换两个清单的顺序
- * @param {Number} id1 - 工料1的id
- * @param {Number} id2 - 工料2的id
- * @returns {Promise<void>}
- */
- async changeOrder(datas) {
- if (!this.ctx.tender || !this.ctx.change) {
- throw '数据错误';
- }
- // const bill1 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id1 });
- // const bill2 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id2 });
- // if (!bill1 || !bill2) {
- // throw '数据错误';
- // }
- const transaction = await this.db.beginTransaction();
- try {
- // const order = bill1.order;
- // bill1.order = bill2.order;
- // bill2.order = order;
- // await transaction.update(this.tableName, { id: bill1.id, order: bill1.order });
- // await transaction.update(this.tableName, { id: bill2.id, order: bill2.order });
- await transaction.updateRows(this.tableName, datas);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async setAllValuation(cid, ids, is_valuation) {
- return await this.db.update(this.tableName, { is_valuation }, {
- where: {
- cid,
- id: ids,
- },
- });
- }
- async getBillsSum(tid) {
- const sql = 'SELECT gcl_id, Sum(qc_qty) AS qc_qty, Sum(qc_tp) AS qc_tp, Sum(qc_minus_qty) AS qc_minus_qty' +
- ' FROM(' +
- ' SELECT cal.gcl_id, Sum(cal.checked_amount) AS qc_qty, Sum(cal.checked_price) AS qc_tp, 0 As qc_minus_qty' +
- ` FROM ${this.tableName} cal LEFT JOIN ${this.ctx.service.change.tableName} c ON cal.cid = c.cid` +
- ' WHERE c.tid = ? AND c.valid AND c.status = ? AND cal.is_valuation' +
- ' GROUP BY cal.gcl_id' +
- ' UNION ALL ' +
- ' SELECT cal.gcl_id, 0 As qc_qty, 0 As qc_tp, Sum(cal.checked_amount) AS qc_minus_qty' +
- ` FROM ${this.tableName} cal LEFT JOIN ${this.ctx.service.change.tableName} c ON cal.cid = c.cid` +
- ' WHERE c.tid = ? AND c.valid AND c.status = ? AND not cal.is_valuation' +
- ' GROUP BY cal.gcl_id) As TEMP' +
- ' GROUP BY gcl_id';
- return await this.db.query(sql, [tid, audit.flow.status.checked, tid, audit.flow.status.checked]);
- }
- async getPosSum(tid) {
- const sql = 'SELECT mx_id, Sum(qc_qty) AS qc_qty, Sum(qc_tp) AS qc_tp, Sum(qc_minus_qty) AS qc_minus_qty' +
- ' FROM(' +
- ' SELECT cal.mx_id, Sum(cal.checked_amount) AS qc_qty, Sum(cal.checked_price) AS qc_tp, 0 As qc_minus_qty' +
- ` FROM ${this.tableName} cal LEFT JOIN ${this.ctx.service.change.tableName} c ON cal.cid = c.cid` +
- ' WHERE c.tid = ? AND c.valid AND c.status = ? AND cal.is_valuation' +
- ' GROUP BY cal.mx_id' +
- ' UNION ALL ' +
- ' SELECT cal.mx_id, 0 As qc_qty, 0 As qc_tp, Sum(cal.checked_amount) AS qc_minus_qty' +
- ` FROM ${this.tableName} cal LEFT JOIN ${this.ctx.service.change.tableName} c ON cal.cid = c.cid` +
- ' WHERE c.tid = ? AND c.valid AND c.status = ? AND not cal.is_valuation' +
- ' GROUP BY cal.mx_id) As TEMP' +
- ' GROUP BY mx_id';
- return await this.db.query(sql, [tid, audit.flow.status.checked, tid, audit.flow.status.checked]);
- }
- }
- return ChangeAuditList;
- };
|