123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- module.exports = app => {
- class StageStash extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'stage_stash';
- }
- async getValidStash(tid) {
- const sql = `SELECT id, sorder, create_time, uid, uname, remark FROM ${this.tableName} WHERE tid = ? and valid and TO_DAYS(NOW()) - TO_DAYS(create_time) <= 30 ORDER BY create_time DESC`;
- return await this.db.query(sql, [tid]);
- }
- async addStash(stage, remark = '') {
- const bills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
- const pos = await this.ctx.service.stagePos.getLastestStageData2(stage.tid, stage.id);
- const change = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
- const timestamp = (new Date()).getTime();
- const filepath = `${this.ctx.session.sessionProject.id}-${stage.tid}-${timestamp}.json`;
- await this.ctx.hisOss.put(this.ctx.stashOssPath + filepath, Buffer.from(JSON.stringify({bills, pos, change}), 'utf8'));
- const result = await this.db.insert(this.tableName, {
- pid: this.ctx.session.sessionProject.id, tid: stage.tid, sid: stage.id, sorder: stage.order,
- uid: this.ctx.session.sessionUser.accountId, uname: this.ctx.session.sessionUser.name,
- filepath, info: {status: stage.status, flow: stage.curAuditor ? stage.curAuditor.user_id : stage.uid}, remark
- });
- return result.insertId;
- }
- async delStash(id) {
- const stash = await this.getDataById(id);
- if (!stash) throw '暂存计量不存在';
- if (stash.tid !== this.ctx.tender.id) throw '非当前标段暂存计量,不可操作';
- await this.deleteById(id);
- await this.ctx.hisOss.delete(this.ctx.stashOssPath + stash.filepath);
- }
- async loadLedgerDataFromOss(filepath) {
- const File = await this.ctx.hisOss.get(this.ctx.stashOssPath + filepath);
- if (File.res.status !== 200) return '获取暂存计量有误';
- const result = JSON.parse(File.content);
- return result;
- }
- async loadAndAnalysis(filepath) {
- const data = await this.loadLedgerDataFromOss(filepath);
- const billsIndex = {};
- for (const b of data.bills) {
- billsIndex[b.lid] = b;
- }
- for (const p of data.pos) {
- if (!billsIndex[p.lid]) continue;
- if (!billsIndex[p.lid].pos) billsIndex[p.lid].pos = [];
- billsIndex[p.lid].pos.push(p);
- }
- for (const c of data.change) {
- if (!billsIndex[c.lid]) continue;
- if (billsIndex[c.lid].pos) {
- const p = billsIndex[c.lid].pos.find(x => { return x.pid === c.pid });
- if (!p) continue;
- if (!p.change) p.change = [];
- p.change.push(c);
- } else {
- if (!billsIndex[c.lid].change) billsIndex[c.lid].change = [];
- billsIndex[c.lid].change.push(c);
- }
- }
- return data.bills;
- }
- async reCalcStashData(stage, data) {
- const decimal = this.ctx.tender.info.decimal;
- const insertBillsData = [], insertPosData = [], insertChangeData = [];
- const bills = await this.ctx.service.ledger.getAllDataByCondition({ where: { tender_id: stage.tid, is_leaf: true } });
- const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: stage.tid } });
- const said = this.ctx.session.sessionUser.accountId;
- for (const d of data) {
- const b = bills.find(x => { return x.id === d.lid });
- if (!b) continue;
- const nbs = { tid: stage.tid, sid: stage.id, said, lid: b.id, times: 1, order: 0 };
- if (d.pos) {
- for (const bp of d.pos) {
- const p = pos.find(x => { return x.id === bp.pid});
- if (!p) continue;
- const nps = { tid: stage.tid, sid: stage.id, said, lid: b.id, pid: p.id, times: 1, order: 0 };
- nps.contract_qty = this.ctx.helper.round(bp.contract_qty, decimal.qty);
- nps.qc_qty = 0;
- insertPosData.push(nps);
- if (bp.change) {
- for (const c of bp.change) {
- if (!c.qty) continue;
- const ncs = { tid: stage.tid, sid: stage.id, lid: b.id, pid: p.id, stimes: 1, sorder: 0, cid: c.cid, cbid: c.cbid, minus: c.minus };
- const validQty = await this.ctx.service.stageChangeFinal.getChangeBillsValidQty(c.cbid);
- ncs.qty = validQty >= c.qty ? c.qty : validQty;
- insertChangeData.push(ncs);
- if (!ncs.minus) nps.qc_qty = await this.ctx.helper.add(nps.qc_qty, ncs.qty);
- }
- }
- nbs.contract_qty = this.ctx.helper.add(nbs.contract_qty, nps.contract_qty);
- nbs.qc_qty = this.ctx.helper.add(nbs.qc_qty, nps.qc_qty);
- }
- nbs.contract_tp = this.ctx.helper.mul(nbs.contract_qty, b.unit_price, decimal.tp);
- nbs.qc_tp = this.ctx.helper.mul(nbs.qc_qty, b.unit_price, decimal.tp);
- } else {
- if (b.is_tp) {
- nbs.contract_qty = 0;
- nbs.contract_tp = this.ctx.helper.round(d.contract_tp, decimal.tp);
- nbs.qc_qty = 0;
- nbs.qc_tp = 0;
- } else {
- nbs.contract_qty = this.ctx.helper.round(d.contract_qty, decimal.qty);
- nbs.contract_tp = this.ctx.helper.mul(nbs.contract_qty, b.unit_price, decimal.tp);
- nbs.qc_qty = 0;
- if (d.change) {
- for (const c of d.change) {
- if (!c.qty) continue;
- const ncs = { tid: stage.tid, sid: stage.id, lid: d.id, pid: -1, stimes: 1, sorder: 0, cid: c.cid, cbid: c.cbid, minus: c.minus };
- const validQty = await this.ctx.service.stageChangeFinal.getChangeBillsValidQty(c.cbid);
- ncs.qty = validQty >= c.qty ? c.qty : validQty;
- insertChangeData.push(ncs);
- if (!ncs.minus) nbs.qc_qty = await this.ctx.helper.add(nbs.qc_qty, ncs.qty);
- }
- }
- nbs.qc_tp = this.ctx.helper.mul(nbs.qc_qty, b.unit_price, decimal.tp);
- }
- }
- insertBillsData.push(nbs);
- }
- return [insertBillsData, insertPosData, insertChangeData]
- }
- async recover(stage, id) {
- const stash = await this.getDataById(id);
- if (!stash) throw '暂存计量不存在';
- if (stash.tid !== stage.tid) throw '暂存计量不可导入';
- const stashData = await this.loadAndAnalysis(stash.filepath);
- const [insertBills, insertPos, insertChange] = await this.reCalcStashData(stage, stashData);
- const conn = await this.db.beginTransaction();
- try {
- await conn.delete(this.ctx.service.stageBills.tableName, { sid: stage.id });
- await conn.delete(this.ctx.service.stageChange.tableName, { sid: stage.id });
- await conn.delete(this.ctx.service.stagePos.tableName, { sid: stage.id });
- if (insertBills.length > 0) conn.insert(this.ctx.service.stageBills.tableName, insertBills);
- if (insertPos.length > 0) conn.insert(this.ctx.service.stagePos.tableName, insertPos);
- if (insertChange.length > 0) conn.insert(this.ctx.service.stageChange.tableName, insertChange);
- await conn.commit();
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- };
- }
- return StageStash;
- };
|