'use strict'; /** * * * @author Mai * @date * @version */ const pushOperate = require('../const/spec_3f').pushOperate; module.exports = app => { class SpecPull extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 's2b_spec_msg'; } async tenderNeedMsg(pid, tid, timing) { const specProj = await this.db.get('zh_s2b_spec_proj', { id: pid }); if (!specProj || !specProj.is_push) return false; switch (specProj.push_tender_type) { case 1: const filter = specProj.filter_tender ? specProj.filter_tender.split(',') : []; if (filter.indexOf(tid + '') >= 0) return false; break; case 2: const access = specProj.access_tender ? specProj.access_tender.split(',') : []; if (access.indexOf(tid + '') < 0) return false; break; } const soleTender = await this.db.get('zh_s2b_spec_tender', { id: tid, is_valid: 1 }); const specPush = await this.db.get('zh_s2b_spec_push', { pid, tid: soleTender ? tid : 0, valid: 1, push_timing: timing }); return !!specPush; } async reportNeedMsg(pid, tid) { const reportMsg = {}; reportMsg.stage = { valid: await this.tenderNeedMsg(pid, tid, pushOperate.report.file), key: pushOperate.report.file}; reportMsg.change = { valid: await this.tenderNeedMsg(pid, tid, pushOperate.report.change_file), key: pushOperate.report.change_file }; reportMsg.change_plan = { valid: await this.tenderNeedMsg(pid, tid, pushOperate.report.change_plan_file), key: pushOperate.report.change_plan_file }; reportMsg.change_apply = { valid: await this.tenderNeedMsg(pid, tid, pushOperate.report.change_apply_file), key: pushOperate.report.change_apply_file }; return reportMsg; } async addLedgerMsg(transaction, pid, tender, timing) { const needMsg = await this.tenderNeedMsg(pid, tender.id, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: tender.id, timing }); } async addReviseMsg(transaction, pid, revise, timing) { const needMsg = await this.tenderNeedMsg(pid, revise.tid, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: revise.tid, rid: revise.id, timing }); } async addStageMsg(transaction, pid, stage, timing) { const needMsg = await this.tenderNeedMsg(pid, stage.tid, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: stage.tid, sid: stage.id, timing }); } async addAdvanceMsg(transaction, pid, advance, timing) { const needMsg = await this.tenderNeedMsg(pid, advance.id, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: advance.tid, advance_id: advance.id, timing }); } async addMaterialMsg(transaction, pid, material, timing) { const needMsg = await this.tenderNeedMsg(pid, material.tid, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: material.tid, material_id: material.id, timing }); } async addSettleMsg(transaction, pid, settle, timing) { const needMsg = await this.tenderNeedMsg(pid, settle.tid, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: settle.tid, settle_id: settle.id, timing }); } async addChangeMsg(transaction, pid, change, timing) { const needMsg = await this.tenderNeedMsg(pid, change.tid, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: change.tid, cid: change.cid, timing }); } async addChangeApplyMsg(transaction, pid, change_apply, timing) { const needMsg = await this.tenderNeedMsg(pid, change_apply.tid, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: change_apply.tid, c_apply_id: change_apply.id, timing }); } async addChangeProjectMsg(transaction, pid, change_project, timing) { const needMsg = await this.tenderNeedMsg(pid, change_project.tid, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: change_project.tid, c_proj_id: change_project.id, timing }); } async addChangePlanMsg(transaction, pid, change_plan, timing) { const needMsg = await this.tenderNeedMsg(pid, change_plan.tid, timing); if (!needMsg) return; await transaction.insert(this.tableName, { pid, tid: change_plan.tid, c_plan_id: change_plan.id, timing }); } async addReportMsg(transaction, pid, tender, stage, timing, subInfo) { const needMsg = await this.tenderNeedMsg(pid, stage.tid, timing); if (!needMsg) return; if (transaction) { await transaction.insert(this.tableName, { pid, tid: tender.id, sid: stage ? stage.id : 0, timing, extra_info: JSON.stringify(subInfo || {}) }); } else { await this.db.insert(this.tableName, { pid, tid: tender.id, sid: stage ? stage.id : 0, timing, extra_info: JSON.stringify(subInfo || {}) }); } } async _getOtherMsgBaseData(id, timing) { switch(timing) { case pushOperate.report.change_file: const change = await this.ctx.service.change.getAllDataByCondition({ where: { cid: id } }); return change.map(x => { return { cid: x.cid } }); case pushOperate.report.change_plan_file: const changePlan = await this.ctx.service.changePlan.getAllDataByCondition({ where: { id } }); return changePlan.map(x => { return { c_plan_id: x.id } }); case pushOperate.report.change_apply_file: const changeApply = await this.ctx.service.changeApply.getAllDataByCondition({ where: { id } }); return changeApply.map(x => { return { c_apply_id: x.id } }); default: return []; } } async addOtherReportMsg(transaction, pid, tender, id, timing, subInfo) { const needMsg = await this.tenderNeedMsg(pid, tender.id, timing); if (!needMsg) return; const data = await this._getOtherMsgBaseData(id, timing); const insertMsg = data.map(x => { return { pid, tid: tender.id, timing, extra_info: JSON.stringify(subInfo || {}), ...x }; }); if (transaction) { await transaction.insert(this.tableName, insertMsg); } else { await this.db.insert(this.tableName, insertMsg); } } } return SpecPull; };