stage_audit.js 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/2/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const auditType = require('../const/audit').auditType;
  11. const smsTypeConst = require('../const/sms_type');
  12. const SMS = require('../lib/sms');
  13. const SmsAliConst = require('../const/sms_alitemplate');
  14. const wxConst = require('../const/wechat_template');
  15. const shenpiConst = require('../const/shenpi');
  16. const payConst = require('../const/deal_pay');
  17. const pushType = require('../const/audit').pushType;
  18. const syncApiConst = require('../const/sync_api');
  19. const measureType = require('../const/tender').measureType;
  20. const RevisePrice = require('../lib/revise_price');
  21. const pushOperate = require('../const/spec_3f').pushOperate;
  22. module.exports = app => {
  23. class StageAudit extends app.BaseService {
  24. /**
  25. * 构造函数
  26. *
  27. * @param {Object} ctx - egg全局变量
  28. * @return {void}
  29. */
  30. constructor(ctx) {
  31. super(ctx);
  32. this.tableName = 'stage_audit';
  33. }
  34. /**
  35. * 获取 审核人信息
  36. *
  37. * @param {Number} stageId - 期id
  38. * @param {Number} auditorId - 审核人id
  39. * @param {Number} times - 第几次审批
  40. * @return {Promise<*>}
  41. */
  42. async getAuditor(stageId, auditorId, times = 1) {
  43. const sql =
  44. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  45. 'FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  46. 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?';
  47. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
  48. return await this.db.queryOne(sql, sqlParam);
  49. }
  50. async getAuditorByOrder(stageId, order, times) {
  51. const sql =
  52. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  53. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  54. ' FROM ' + this.tableName + ' AS la' +
  55. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  56. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
  57. ' ORDER BY `order` DESC';
  58. const sqlParam = [stageId, order, times ? times: 1];
  59. return await this.db.queryOne(sql, sqlParam);
  60. }
  61. async getAuditorsByOrder(stageId, order, times) {
  62. const sql =
  63. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  64. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  65. ' FROM ' + this.tableName + ' AS la' +
  66. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  67. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
  68. ' ORDER BY `order` DESC';
  69. const sqlParam = [stageId, order, times ? times: 1];
  70. return await this.db.query(sql, sqlParam);
  71. }
  72. async getLastestAuditor(stageId, times, status) {
  73. const sql =
  74. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  75. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  76. ' FROM ' + this.tableName + ' AS la' +
  77. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  78. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  79. ' ORDER BY `order` DESC';
  80. const sqlParam = [stageId, status, times ? times: 1];
  81. return await this.db.queryOne(sql, sqlParam);
  82. }
  83. async getLastestAuditors(stageId, times, status) {
  84. const sql =
  85. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  86. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  87. ' FROM ' + this.tableName + ' AS la' +
  88. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  89. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  90. ' ORDER BY `order` DESC';
  91. const sqlParam = [stageId, status, times ? times: 1];
  92. const result = await this.db.query(sql, sqlParam);
  93. if (result.length === 0) return [];
  94. return result.filter(x => { x.order === result[result.length - 1].order });
  95. }
  96. /**
  97. * 获取 审核列表信息
  98. *
  99. * @param {Number} stageId - 期id
  100. * @param {Number} times - 第几次审批
  101. * @param {Number} order_sort - 列表排序方式
  102. * @return {Promise<*>}
  103. */
  104. async getAuditors(stageId, times = 1, order_sort = 'asc') {
  105. const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
  106. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pa.sign_path' +
  107. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
  108. ' WHERE la.sid = ? AND la.times = ?' +
  109. ' ORDER BY la.order ' + order_sort;
  110. const sqlParam = [stageId, times];
  111. const result = await this.db.query(sql, sqlParam);
  112. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  113. for (const i in result) {
  114. result[i].max_sort = max_sort;
  115. }
  116. return result;
  117. }
  118. async getAllAuditors(tenderId) {
  119. const sql =
  120. 'SELECT sa.aid, sa.tid FROM ' + this.tableName + ' sa' +
  121. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On sa.tid = t.id' +
  122. ' WHERE t.id = ?' +
  123. ' GROUP BY sa.aid';
  124. const sqlParam = [tenderId];
  125. return this.db.query(sql, sqlParam);
  126. }
  127. /**
  128. * 获取标段审核人最后一位的名称
  129. *
  130. * @param {Number} tenderId - 标段id
  131. * @param {Number} auditorId - 审核人id
  132. * @param {Number} times - 第几次审批
  133. * @return {Promise<*>}
  134. */
  135. async getStatusName(stageId) {
  136. const sql = 'SELECT pa.`name` FROM ?? AS sa Left Join ?? AS pa On sa.`aid` = pa.`id`' +
  137. ' WHERE sa.`sid` = ? and sa.`status` != ? ORDER BY sa.`times` DESC, sa.`order` DESC';
  138. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.uncheck];
  139. return await this.db.queryOne(sql, sqlParam);
  140. }
  141. /**
  142. * 获取 当前审核人
  143. *
  144. * @param {Number} stageId - 期id
  145. * @param {Number} times - 第几次审批
  146. * @return {Promise<*>}
  147. */
  148. async getCurAuditor(stageId, times = 1) {
  149. const sql =
  150. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  151. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  152. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
  153. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  154. return await this.db.queryOne(sql, sqlParam);
  155. }
  156. async getCurAuditors(stageId, times = 1) {
  157. const sql =
  158. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  159. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  160. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
  161. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  162. return await this.db.query(sql, sqlParam);
  163. }
  164. /**
  165. * 获取 最新审核顺序
  166. *
  167. * @param {Number} stageId - 期id
  168. * @param {Number} times - 第几次审批
  169. * @return {Promise<number>}
  170. */
  171. async getNewOrder(stageId, times = 1) {
  172. const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ' + this.tableName + ' Where `sid` = ? and `times` = ?';
  173. const sqlParam = [stageId, times];
  174. const result = await this.db.queryOne(sql, sqlParam);
  175. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  176. }
  177. /**
  178. * 新增审核人
  179. *
  180. * @param {Number} stageId - 期id
  181. * @param {Number} auditorId - 审核人id
  182. * @param {Number} times - 第几次审批
  183. * @return {Promise<number>}
  184. */
  185. async addAuditor(stageId, auditorId, times = 1, is_gdzs = 0) {
  186. const transaction = await this.db.beginTransaction();
  187. try {
  188. let [newOrder, newAuditOrder] = await this.getNewOrder(stageId, times);
  189. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  190. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  191. if (is_gdzs) await this._syncOrderByDelete(transaction, stageId, newOrder, times, '+');
  192. const data = {
  193. tid: this.ctx.tender.id,
  194. sid: stageId,
  195. aid: auditorId,
  196. times,
  197. order: newOrder,
  198. status: auditConst.status.uncheck,
  199. audit_order: newAuditOrder,
  200. };
  201. const result = await transaction.insert(this.tableName, data);
  202. await transaction.commit();
  203. return result.effectRows = 1;
  204. } catch (err) {
  205. await transaction.rollback();
  206. throw err;
  207. }
  208. return false;
  209. }
  210. /**
  211. * 移除审核人时,同步其后审核人order
  212. * @param transaction - 事务
  213. * @param {Number} stageId - 标段id
  214. * @param {Number} auditorId - 审核人id
  215. * @param {Number} times - 第几次审批
  216. * @return {Promise<*>}
  217. * @private
  218. */
  219. async _syncOrderByDelete(transaction, stageId, order, times, selfOperate = '-') {
  220. this.initSqlBuilder();
  221. this.sqlBuilder.setAndWhere('sid', {
  222. value: stageId,
  223. operate: '=',
  224. });
  225. this.sqlBuilder.setAndWhere('order', {
  226. value: order,
  227. operate: '>=',
  228. });
  229. this.sqlBuilder.setAndWhere('times', {
  230. value: times,
  231. operate: '=',
  232. });
  233. this.sqlBuilder.setUpdateData('order', {
  234. value: 1,
  235. selfOperate: selfOperate,
  236. });
  237. this.sqlBuilder.setUpdateData('audit_order', {
  238. value: 1,
  239. selfOperate: selfOperate,
  240. });
  241. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  242. const data = await transaction.query(sql, sqlParam);
  243. return data;
  244. }
  245. /**
  246. * 移除审核人
  247. *
  248. * @param {Number} stageId - 期id
  249. * @param {Number} auditorId - 审核人id
  250. * @param {Number} times - 第几次审批
  251. * @return {Promise<boolean>}
  252. */
  253. async deleteAuditor(stageId, auditorId, times = 1) {
  254. const transaction = await this.db.beginTransaction();
  255. try {
  256. const condition = { sid: stageId, aid: auditorId, times };
  257. const auditor = await this.getDataByCondition(condition);
  258. if (!auditor) {
  259. throw '该审核人不存在';
  260. }
  261. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  262. await transaction.delete(this.tableName, condition);
  263. await transaction.commit();
  264. } catch (err) {
  265. await transaction.rollback();
  266. throw err;
  267. }
  268. return true;
  269. }
  270. async _updateTender(transaction) {
  271. if (!this.ctx.tender) return;
  272. if (this.ctx.tender.data.measure_type !== measureType.gcl.value) return;
  273. const sum = await this.ctx.service.ledger.addUp({ tender_id: this.ctx.tender.id/* , is_leaf: true*/ });
  274. await transaction.update(this.ctx.service.tender.tableName, {
  275. id: this.ctx.tender.id, total_price: sum.total_price, deal_tp: sum.deal_tp
  276. });
  277. return sum;
  278. }
  279. /**
  280. * 开始审批
  281. *
  282. * @param {Number} stageId - 期id
  283. * @param {Number} times - 第几次审批
  284. * @return {Promise<boolean>}
  285. */
  286. async start(stageId, times = 1) {
  287. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: 1 } });
  288. if (audits.length === 0) {
  289. if(this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
  290. throw '请联系管理员添加审批人';
  291. } else {
  292. throw '请先选择审批人,再上报数据';
  293. }
  294. }
  295. const transaction = await this.db.beginTransaction();
  296. try {
  297. const ledgerTp = await this._updateTender(transaction);
  298. const begin_time = new Date();
  299. const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time } });
  300. await transaction.updateRows(this.tableName, updateData);
  301. // 计算原报最终数据
  302. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  303. // 复制一份下一审核人数据
  304. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  305. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  306. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  307. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  308. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  309. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  310. // 更新期数据
  311. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  312. const stageTp = {
  313. contract_tp: tpData.contract_tp,
  314. qc_tp: tpData.qc_tp,
  315. positive_qc_tp: tpData.positive_qc_tp,
  316. negative_qc_tp: tpData.negative_qc_tp,
  317. yf_tp: yfPay.tp,
  318. sf_tp: sfPay.tp,
  319. };
  320. this.ctx.stage.tp_history.push({ times: this.ctx.stage.curTimes, order: 0, ...stageTp });
  321. await this.ctx.service.tenderCache.updateStageCache4Start(transaction, this.ctx.stage, auditConst.status.checking, audits, ledgerTp, stageTp);
  322. await transaction.update(this.ctx.service.stage.tableName, {
  323. id: stageId,
  324. status: auditConst.status.checking,
  325. ...stageTp,
  326. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  327. cache_time_r: this.ctx.stage.cache_time_l,
  328. });
  329. // 多人协同,取消下一审批人存在的锁定
  330. await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, audits.map(x => { return x.aid; }), transaction);
  331. // 添加短信通知-需要审批提醒功能
  332. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  333. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  334. const auditIds = this._.map(audits, 'aid');
  335. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  336. users.push(...auditIds);
  337. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  338. qi: stageInfo.order,
  339. code: shenpiUrl,
  340. });
  341. // 微信模板通知
  342. const wechatData = {
  343. wap_url: shenpiUrl,
  344. qi: stageInfo.order,
  345. status: wxConst.status.check,
  346. tips: wxConst.tips.check,
  347. code: this.ctx.session.sessionProject.code,
  348. };
  349. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  350. // 上报/审批 - 检查三方特殊推送
  351. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  352. await transaction.commit();
  353. // 通知发送 - 第三方更新
  354. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  355. const base_data = {
  356. tid: this.ctx.tender.id,
  357. sid: stageId,
  358. op: 'update',
  359. };
  360. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  361. base_data.op = 'update';
  362. base_data.sid = -1;
  363. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  364. }
  365. } catch (err) {
  366. await transaction.rollback();
  367. throw err;
  368. }
  369. return true;
  370. }
  371. async _checked(pid, stageId, checkData, times) {
  372. const accountId = this.ctx.session.sessionUser.accountId;
  373. const time = new Date();
  374. // 整理当前流程审核人状态更新
  375. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  376. if (audits.length === 0) throw '审核数据错误';
  377. const selfAudit = audits.find(x => { return x.aid === accountId; });
  378. if (!selfAudit) throw '当前标段您无权审批';
  379. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: selfAudit.order } });
  380. const nextAudits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: selfAudit.order + 1 } });
  381. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  382. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  383. const transaction = await this.db.beginTransaction();
  384. try {
  385. const ledgerTp = await this._updateTender(transaction);
  386. // 添加通知
  387. const noticeContent = await this.getNoticeContent(pid, stageInfo.tid, stageId, accountId, checkData.opinion);
  388. const auditors = await this.getAuditGroupByListWithOwner(stageId, times);
  389. const defaultNoticeRecord = {
  390. pid,
  391. type: pushType.stage,
  392. status: auditConst.status.checked,
  393. content: noticeContent,
  394. };
  395. const records = [];
  396. auditors.forEach(audit => {
  397. records.push({
  398. uid: audit.aid,
  399. ...defaultNoticeRecord,
  400. });
  401. });
  402. this.ctx.stage.userAssists.forEach(u => {
  403. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  404. });
  405. this.ctx.stage.auditAssists.forEach(u => {
  406. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  407. });
  408. await transaction.insert('zh_notice', records);
  409. // 更新本人审批状态
  410. await transaction.update(this.tableName, {
  411. id: selfAudit.id,
  412. status: checkData.checkType,
  413. opinion: checkData.opinion,
  414. end_time: time,
  415. });
  416. // 计算并合同支付最终数据
  417. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  418. const stageTp = {
  419. contract_tp: tpData.contract_tp,
  420. qc_tp: tpData.qc_tp,
  421. positive_qc_tp: tpData.positive_qc_tp,
  422. negative_qc_tp: tpData.negative_qc_tp,
  423. yf_tp: yfPay.tp,
  424. sf_tp: sfPay.tp,
  425. };
  426. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  427. if (audits.length === 1 || selfAudit.audit_type !== auditType.key.and) {
  428. // 或签更新他人审批状态
  429. if (selfAudit.audit_type === auditType.key.or) {
  430. const updateOther = [];
  431. for (const audit of audits) {
  432. if (audit.aid === selfAudit.aid) continue;
  433. updateOther.push({
  434. id: audit.id,
  435. status: auditConst.status.checkSkip,
  436. opinion: '',
  437. end_time: time,
  438. });
  439. }
  440. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  441. }
  442. // 无下一审核人表示,审核结束
  443. if (nextAudits.length > 0) {
  444. // 复制一份下一审核人数据
  445. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudits[0].order, transaction);
  446. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  447. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  448. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  449. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  450. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  451. // 流程至下一审批人
  452. const updateData = nextAudits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time: time }; });
  453. await transaction.updateRows(this.tableName, updateData);
  454. // 同步 期信息
  455. await transaction.update(this.ctx.service.stage.tableName, {
  456. id: stageId,
  457. status: auditConst.status.checking,
  458. ...stageTp,
  459. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  460. cache_time_r: this.ctx.stage.cache_time_l,
  461. });
  462. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, nextAudits, flowAudits, ledgerTp, stageTp);
  463. // 多人协同,取消下一审批人存在的锁定
  464. await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, nextAudits.map(x => { return x.aid; }), transaction);
  465. // 添加短信通知-需要审批提醒功能
  466. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  467. const nextAuditIds = this._.map(nextAudits, 'aid');
  468. const users = this._.map(this._.filter(this.ctx.auditAssists, function (x) { return nextAuditIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  469. users.push(...nextAuditIds);
  470. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  471. qi: stageInfo.order,
  472. code: shenpiUrl,
  473. });
  474. // 微信模板通知
  475. const wechatData = {
  476. wap_url: shenpiUrl,
  477. qi: stageInfo.order,
  478. status: wxConst.status.check,
  479. tips: wxConst.tips.check,
  480. code: this.ctx.session.sessionProject.code,
  481. };
  482. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  483. } else {
  484. await this.ctx.service.tenderTag.saveTenderTag(this.ctx.tender.id, {stage_time: new Date()}, transaction);
  485. const his_id = await this.ctx.service.ledgerHistory.checkBackupLedgerHistory(this.ctx.stage.tid, this.ctx.stage.id);
  486. // 本期结束
  487. // 生成截止本期数据 final数据
  488. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  489. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  490. await this.ctx.service.stageChangeFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  491. // 同步 期信息
  492. await transaction.update(this.ctx.service.stage.tableName, {
  493. id: stageId,
  494. status: checkData.checkType,
  495. ...stageTp,
  496. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  497. cache_time_r: this.ctx.stage.cache_time_l,
  498. his_id,
  499. });
  500. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, nextAudits, flowAudits, ledgerTp, stageTp);
  501. await this.ctx.service.stagePay.cacheOrder(this.ctx.stage, transaction);
  502. // 添加短信通知-审批通过提醒功能
  503. const auditList = await this.getAuditors(stageId, stageInfo.times);
  504. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id, this._.map(this.ctx.stage.userAssists, 'ass_user_id'), this._.map(this.ctx.stage.auditAssists, 'ass_user_id')));
  505. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  506. qi: stageInfo.order,
  507. status: SmsAliConst.status.success,
  508. });
  509. // 微信模板通知
  510. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  511. const wechatData = {
  512. wap_url: shenpiUrl,
  513. qi: stageInfo.order,
  514. status: wxConst.status.success,
  515. tips: wxConst.tips.success,
  516. code: this.ctx.session.sessionProject.code,
  517. };
  518. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  519. // 审批通过 - 检查三方特殊推送
  520. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.checked);
  521. }
  522. // 上报/审批 - 检查三方特殊推送
  523. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  524. }
  525. await transaction.commit();
  526. } catch (err) {
  527. await transaction.rollback();
  528. throw err;
  529. }
  530. }
  531. async _checkNo(pid, stageId, checkData, times) {
  532. const accountId = this.ctx.session.sessionUser.accountId;
  533. const time = new Date();
  534. // 整理当前流程审核人状态更新
  535. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  536. if (!audits) throw '审核数据错误';
  537. const selfAudit = audits.find(x => { return x.aid === accountId; });
  538. if (!selfAudit) throw '当前标段您无权审批';
  539. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
  540. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  541. const auditors = await this.getUniqAuditor(stageId, times); // 全部参与的审批人
  542. const newAuditors = auditors.map(x => {
  543. return {
  544. aid: x.aid, tid: selfAudit.tid, sid: selfAudit.sid,
  545. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  546. audit_type: x.audit_type, audit_order: x.audit_order,
  547. }
  548. });
  549. const transaction = await this.db.beginTransaction();
  550. try {
  551. const ledgerTp = await this._updateTender(transaction);
  552. // 添加推送
  553. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
  554. const defaultNoticeRecord = {
  555. pid,
  556. type: pushType.stage,
  557. status: auditConst.status.checkNo,
  558. content: noticeContent,
  559. };
  560. const records = [
  561. {
  562. uid: this.ctx.stage.user_id,
  563. ...defaultNoticeRecord,
  564. },
  565. ];
  566. auditors.forEach(audit => {
  567. records.push({
  568. uid: audit.aid,
  569. ...defaultNoticeRecord,
  570. });
  571. });
  572. this.ctx.stage.userAssists.forEach(u => {
  573. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  574. });
  575. this.ctx.stage.auditAssists.forEach(u => {
  576. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  577. });
  578. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  579. // 计算并合同支付最终数据
  580. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  581. const stageTp = {
  582. contract_tp: tpData.contract_tp,
  583. qc_tp: tpData.qc_tp,
  584. positive_qc_tp: tpData.positive_qc_tp,
  585. negative_qc_tp: tpData.negative_qc_tp,
  586. yf_tp: yfPay.tp,
  587. sf_tp: sfPay.tp,
  588. };
  589. const orgHisIndex = this.ctx.stage.tp_history.findIndex(x => { return x.times === times && x.order === selfAudit.order; });
  590. if (orgHisIndex) this.ctx.stage.tp_history.splice(orgHisIndex, 1);
  591. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  592. const updateData = [];
  593. audits.forEach(x => {
  594. updateData.push({
  595. id: x.id,
  596. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  597. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  598. end_time: x.aid === selfAudit.aid ? time : null,
  599. })
  600. });
  601. await transaction.updateRows(this.tableName, updateData);
  602. // 同步 期信息
  603. await transaction.update(this.ctx.service.stage.tableName, {
  604. id: stageId,
  605. status: checkData.checkType,
  606. ...stageTp,
  607. times: times + 1,
  608. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  609. cache_time_r: this.ctx.stage.cache_time_l,
  610. });
  611. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType,
  612. [{ aid: this.ctx.stage.user_id, audit_order: 0, audit_type: auditType.key.common, status: auditConst.status.uncheck }], flowAudits, ledgerTp, stageTp);
  613. // 拷贝新一次审核流程列表
  614. await transaction.insert(this.tableName, newAuditors);
  615. // 计算该审批人最终数据
  616. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  617. // 复制一份最新数据给原报
  618. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  619. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  620. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  621. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  622. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  623. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  624. // 锁定本人数据,保留锁定数据相关确认状态
  625. await this.ctx.service.stageAuditAss.lockConfirm4CheckNo(this.ctx.stage, audits.map(x => { return x.aid; }), auditors, transaction);
  626. // 添加短信通知-审批退回提醒功能
  627. // const mobile_array = [];
  628. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  629. const auditList = await this.getAuditors(stageId, stageInfo.times);
  630. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id, this._.map(this.ctx.stage.userAssists, 'ass_user_id'), this._.map(this.ctx.stage.auditAssists, 'ass_user_id')));
  631. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  632. qi: stageInfo.order,
  633. status: SmsAliConst.status.back,
  634. });
  635. // 微信模板通知
  636. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  637. const wechatData = {
  638. wap_url: shenpiUrl,
  639. qi: stageInfo.order,
  640. status: wxConst.status.back,
  641. tips: wxConst.tips.back,
  642. code: this.ctx.session.sessionProject.code,
  643. };
  644. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  645. // 上报/审批 - 检查三方特殊推送
  646. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  647. await transaction.commit();
  648. } catch (err) {
  649. await transaction.rollback();
  650. throw err;
  651. }
  652. }
  653. async _checkNoPre(pid, stageId, checkData, times) {
  654. const accountId = this.ctx.session.sessionUser.accountId;
  655. const time = new Date();
  656. // 整理当前流程审核人状态更新
  657. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  658. if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
  659. const selfAudit = audits.find(x => { return x.aid === accountId; });
  660. if (!selfAudit) throw '当前标段您无权审批';
  661. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
  662. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  663. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  664. const auditors2 = await this.getAuditGroupByList(stageId, times);
  665. const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
  666. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  667. const transaction = await this.db.beginTransaction();
  668. try {
  669. const ledgerTp = await this._updateTender(transaction);
  670. // 添加通知
  671. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
  672. const defaultNoticeRecord = {
  673. pid,
  674. type: pushType.stage,
  675. status: auditConst.status.checkNoPre,
  676. content: noticeContent,
  677. };
  678. const records = [
  679. {
  680. uid: this.ctx.stage.user_id,
  681. ...defaultNoticeRecord
  682. },
  683. ];
  684. auditors2.forEach(audit => {
  685. records.push({
  686. uid: audit.aid,
  687. ...defaultNoticeRecord
  688. });
  689. });
  690. this.ctx.stage.userAssists.forEach(u => {
  691. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  692. });
  693. this.ctx.stage.auditAssists.forEach(u => {
  694. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  695. });
  696. await transaction.insert('zh_notice', records);
  697. // 计算并合同支付最终数据
  698. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  699. const stageTp = {
  700. contract_tp: tpData.contract_tp,
  701. qc_tp: tpData.qc_tp,
  702. positive_qc_tp: tpData.positive_qc_tp,
  703. negative_qc_tp: tpData.negative_qc_tp,
  704. yf_tp: yfPay.tp,
  705. sf_tp: sfPay.tp,
  706. };
  707. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  708. // 同步 期信息
  709. await transaction.update(this.ctx.service.stage.tableName, {
  710. id: stageId,
  711. times,
  712. ...stageTp,
  713. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  714. cache_time_r: this.ctx.stage.cache_time_l,
  715. });
  716. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, preAuditors, flowAudits, ledgerTp, stageTp);
  717. // 更新同一流程所有审批人状态
  718. const updateData = [];
  719. for (const audit of audits) {
  720. if (audit.aid === selfAudit.aid) {
  721. updateData.push({
  722. id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time,
  723. });
  724. } else {
  725. updateData.push({
  726. id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null,
  727. });
  728. }
  729. }
  730. await transaction.updateRows(this.tableName, updateData);
  731. // 顺移其后审核人流程顺序
  732. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  733. await transaction.query(sql, [stageId, selfAudit.times, selfAudit.order]);
  734. // 上一审批人,当前审批人 再次添加至流程
  735. const newAuditors = [];
  736. preAuditors.forEach(x => {
  737. newAuditors.push({
  738. tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
  739. times: x.times, order: selfAudit.order + 1,
  740. status: auditConst.status.checking, begin_time: time,
  741. audit_type: x.audit_type, audit_order: x.audit_order,
  742. });
  743. });
  744. audits.forEach(x => {
  745. newAuditors.push({
  746. tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
  747. times: x.times, order: selfAudit.order + 2,
  748. status: auditConst.status.uncheck,
  749. audit_type: x.audit_type, audit_order: x.audit_order,
  750. });
  751. });
  752. await transaction.insert(this.tableName, newAuditors);
  753. // 计算该审批人最终数据
  754. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  755. // 复制一份最新数据给下一人
  756. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, selfAudit.order + 1, transaction);
  757. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  758. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  759. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  760. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  761. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  762. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  763. // 锁定本人数据,保留锁定数据相关确认状态
  764. await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, audits.map(x => { return x.aid; }), preAuditorIds, transaction);
  765. // 同步 期信息
  766. await transaction.update(this.ctx.service.stage.tableName, {
  767. id: stageId,
  768. status: checkData.checkType,
  769. cache_time_r: this.ctx.stage.cache_time_l,
  770. });
  771. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  772. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  773. const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return preAuditorIds.indexOf(x.user_id) >= 0 }), 'ass_user_id');
  774. users.push(...preAuditorIds);
  775. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  776. qi: stageInfo.order,
  777. code: shenpiUrl,
  778. });
  779. // 微信模板通知
  780. const wechatData = {
  781. wap_url: shenpiUrl,
  782. qi: stageInfo.order,
  783. status: wxConst.status.check,
  784. tips: wxConst.tips.check,
  785. code: this.ctx.session.sessionProject.code,
  786. };
  787. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  788. // 上报/审批 - 检查三方特殊推送
  789. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  790. await transaction.commit();
  791. } catch (err) {
  792. await transaction.rollback();
  793. throw err;
  794. }
  795. }
  796. /**
  797. * 审批
  798. * @param {Number} stageId - 标段id
  799. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  800. * @param {Number} times - 第几次审批
  801. * @return {Promise<void>}
  802. */
  803. async check(stageId, checkData, times = 1) {
  804. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  805. throw '提交数据错误';
  806. }
  807. // // 整理当前流程审核人状态更新
  808. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  809. // if (!audit) {
  810. // throw '审核数据错误';
  811. // }
  812. // const time = new Date();
  813. const pid = this.ctx.session.sessionProject.id;
  814. switch (checkData.checkType) {
  815. case auditConst.status.checked:
  816. await this._checked(pid, stageId, checkData, times);
  817. break;
  818. case auditConst.status.checkNo:
  819. await this._checkNo(pid, stageId, checkData, times);
  820. break;
  821. case auditConst.status.checkNoPre:
  822. await this._checkNoPre(pid, stageId, checkData, times);
  823. break;
  824. default:
  825. throw '无效审批操作';
  826. }
  827. // 通知发送 - 第三方更新
  828. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  829. const base_data = {
  830. tid: this.ctx.tender.id,
  831. sid: stageId,
  832. op: 'update',
  833. };
  834. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  835. base_data.op = 'update';
  836. base_data.sid = -1;
  837. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  838. }
  839. }
  840. /**
  841. * 审批
  842. * @param {Number} stageId - 标段id
  843. * @param {Number} times - 第几次审批
  844. * @return {Promise<void>}
  845. */
  846. async checkAgain(stageId, times = 1) {
  847. const time = new Date();
  848. // 整理当前流程审核人状态更新
  849. const auditors = await this.getAllDataByCondition({
  850. where: { sid: stageId, times },
  851. orders: [['order', 'desc']],
  852. });
  853. const maxOrder = auditors[auditors.length - 1].order;
  854. const audits = auditors.filter(x => { return x.order === maxOrder});
  855. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  856. const selfAudit = audits.find(x => { return x.aid === accountId; });
  857. if (!selfAudit) throw '当前标段您无权审批';
  858. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  859. const transaction = await this.db.beginTransaction();
  860. try {
  861. // 当前审批人2次添加至流程中
  862. const checkAgainAuditors = [], checkingAuditors = [];
  863. audit.forEach(x => {
  864. checkAgainAuditors.push({
  865. tid: x.tid, sid: x.sid, aid: x.aid,
  866. times: x.times, order: x.order + 1,
  867. status: x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  868. begin_time: time, end_time: time, opinion: '',
  869. audit_type: x.audit_type, audit_order: x.audit_order,
  870. });
  871. });
  872. audit.forEach(x => {
  873. checkingAuditors.push({
  874. tid: x.tid, sid: x.sid, aid: x.aid,
  875. times: x.times, order: x.order + 2,
  876. status: auditConst.status.checking,
  877. begin_time: time, end_time: time, opinion: '',
  878. audit_type: x.audit_type, audit_order: x.audit_order,
  879. });
  880. });
  881. await transaction.insert(this.tableName, checkAgainAuditors);
  882. await transaction.insert(this.tableName, checkingAuditors);
  883. // 复制一份最新数据给下一人
  884. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  885. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  886. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  887. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  888. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  889. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  890. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  891. // 同步 期信息
  892. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === selfAudit.order });
  893. const stageTp = {
  894. contract_tp: his.contract_tp,
  895. qc_tp: his.qc_tp,
  896. positive_qc_tp: his.positive_qc_tp,
  897. negative_qc_tp: his.negative_qc_tp,
  898. yf_tp: his.tp,
  899. sf_tp: his.tp,
  900. };
  901. this.ctx.stage.tp_history.push({ times, order: selfAudit.order + 1, ...stageTp });
  902. await transaction.update(this.ctx.service.stage.tableName, {
  903. id: stageId,
  904. status: auditConst.status.checking,
  905. cache_time_r: this.ctx.stage.cache_time_l,
  906. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  907. });
  908. // 已经引用到本期的单价变更,全部取消
  909. await this.ctx.service.revisePrice.cancelPriceUsed(this.ctx.stage, transaction);
  910. // 重算所有单价变更
  911. const priceCalc = new RevisePrice(this.ctx);
  912. const pcTp = await priceCalc.stageCheckAgainPriceChange(this.ctx.stage, audit.order + 2, transaction);
  913. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, checkingAuditors, checkAgainAuditors, null, stageTp, pcTp);
  914. // 添加短信通知-需要审批提醒功能
  915. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  916. const auditorIds = audits.map(x => { return x.aid; });
  917. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditorIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  918. users.push(...auditorIds);
  919. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  920. qi: stageInfo.order,
  921. code: shenpiUrl,
  922. });
  923. // 微信模板通知
  924. const wechatData = {
  925. wap_url: shenpiUrl,
  926. qi: stageInfo.order,
  927. status: wxConst.status.check,
  928. tips: wxConst.tips.check,
  929. code: this.ctx.session.sessionProject.code,
  930. };
  931. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  932. // 上报/审批 - 检查三方特殊推送
  933. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  934. await transaction.commit();
  935. // 通知发送 - 第三方更新
  936. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  937. const base_data = {
  938. tid: this.ctx.tender.id,
  939. sid: stageId,
  940. op: 'update',
  941. };
  942. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  943. base_data.op = 'update';
  944. base_data.sid = -1;
  945. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  946. }
  947. } catch (err) {
  948. await transaction.rollback();
  949. throw err;
  950. }
  951. }
  952. /**
  953. * 原报撤回,直接改动审批人状态
  954. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  955. *
  956. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  957. * ...
  958. *
  959. * @param stage
  960. * @returns {Promise<void>}
  961. * @private
  962. */
  963. async _userCheckCancel(stage) {
  964. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  965. const transaction = await this.db.beginTransaction();
  966. try {
  967. const ledgerTp = await this._updateTender(transaction);
  968. // 整理当前流程审核人状态更新
  969. // 审批人变成待审批状态
  970. const updateData = stage.curAuditors.map(x => {
  971. return {
  972. id: x.id,
  973. status: auditConst.status.uncheck,
  974. begin_time: null,
  975. opinion: null,
  976. }
  977. });
  978. await transaction.updateRows(this.tableName, updateData);
  979. // 计算并合同支付最终数据
  980. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  981. const stageTp = {
  982. contract_tp: tpData.contract_tp,
  983. qc_tp: tpData.qc_tp,
  984. positive_qc_tp: tpData.positive_qc_tp,
  985. negative_qc_tp: tpData.negative_qc_tp,
  986. yf_tp: yfPay.tp,
  987. sf_tp: sfPay.tp,
  988. };
  989. await transaction.update(this.ctx.service.stage.tableName, {
  990. id: stage.id,
  991. times: stage.times,
  992. ...stageTp,
  993. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times < stage.times; })),
  994. cache_time_r: this.ctx.stage.cache_time_l,
  995. status: stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  996. });
  997. // 计量明细:重复数据删除原报,新增数据修改为原报
  998. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, stage.times, 0, stage.time, 1, transaction);
  999. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, stage.times, 0, stage.time, 1, transaction);
  1000. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, stage.times, 0, stage.time, 1, transaction);
  1001. await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, stage.times, 0, stage.time, 1, transaction);
  1002. // 合同支付明细:保留审批人数据,覆盖到原报中并删除该审批人合同支付数据
  1003. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
  1004. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, 0, transaction, this.ctx.stage.times, 1);
  1005. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 1, transaction);
  1006. // 其他台账明细:不需更新历史
  1007. // 更新标段计量数据缓存
  1008. const preFlow = stage.times > 1 ? await this.getLastestAuditors(stage.id, stage.times, auditConst.status.checkNo): null;
  1009. const curFlow = [{ aid: this.ctx.stage.user_id, audit_type: auditType.key.common, audit_order: 1, status: auditConst.status.uncheck}];
  1010. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  1011. curFlow, preFlow, ledgerTp, stageTp);
  1012. // 上报/审批 - 检查三方特殊推送
  1013. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  1014. await transaction.commit();
  1015. } catch(err) {
  1016. await transaction.rollback();
  1017. throw err;
  1018. }
  1019. }
  1020. /**
  1021. * 审批人撤回审批通过,插入两条数据
  1022. *
  1023. * 一审 1 A checked 一审 1 A checked
  1024. * 二审 2 B checked pre -> 二审 2 B checked
  1025. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  1026. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  1027. * 三审 5 C uncheck order、status改
  1028. * 四审 6 D uncheck order改
  1029. *
  1030. * @param stage
  1031. * @returns {Promise<void>}
  1032. * @private
  1033. */
  1034. async _auditCheckCancel(stage) {
  1035. if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
  1036. throw '撤回用户数据错误';
  1037. }
  1038. const accountId = this.ctx.session.sessionUser.accountId;
  1039. const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
  1040. if (!selfAuditor) throw '撤回用户数据错误';
  1041. const time = new Date();
  1042. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1043. const transaction = await this.db.beginTransaction();
  1044. try {
  1045. const ledgerTp = await this._updateTender(transaction);
  1046. // 整理当前流程审核人状态更新
  1047. // 顺移其后审核人流程顺序
  1048. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  1049. await transaction.query(sql, [stage.id, selfAuditor.times, stage.curAuditors[0].order]);
  1050. // 当前审批人2次添加至流程中
  1051. const checkCancelAuditors = [], checkingAuditors = [];
  1052. stage.preAuditors.forEach(x => {
  1053. checkCancelAuditors.push({
  1054. tid: stage.tid, sid: stage.id, aid: x.aid,
  1055. times: x.times, order: x.order + 1,
  1056. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1057. begin_time: time, end_time: time, opinion: '',
  1058. audit_type: x.audit_type, audit_order: x.audit_order,
  1059. });
  1060. });
  1061. stage.preAuditors.forEach(x => {
  1062. checkingAuditors.push({
  1063. tid: stage.tid, sid: stage.id, aid: x.aid,
  1064. times: x.times, order: x.order + 2,
  1065. status: auditConst.status.checking,
  1066. begin_time: time, end_time: time, opinion: '',
  1067. audit_type: x.audit_type, audit_order: x.audit_order,
  1068. });
  1069. });
  1070. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  1071. // 当前审批人变成待审批
  1072. await transaction.update(this.tableName, { begin_time: null, status: auditConst.status.uncheck, order: stage.curAuditors[0].order + 2 },
  1073. { where: { sid: stage.id, times: stage.curAuditors[0].times, order: stage.curAuditors[0].order } });
  1074. // 计算并合同支付最终数据
  1075. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1076. const stageTp = {
  1077. contract_tp: tpData.contract_tp,
  1078. qc_tp: tpData.qc_tp,
  1079. positive_qc_tp: tpData.positive_qc_tp,
  1080. negative_qc_tp: tpData.negative_qc_tp,
  1081. yf_tp: yfPay.tp,
  1082. sf_tp: sfPay.tp,
  1083. };
  1084. this.ctx.stage.tp_history.push({ times: stage.times, order: selfAuditor.order + 1, ...stageTp });
  1085. // 同步 期信息
  1086. await transaction.update(this.ctx.service.stage.tableName, {
  1087. id: stage.id,
  1088. times: stage.times,
  1089. ...stageTp,
  1090. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1091. cache_time_r: this.ctx.stage.cache_time_l,
  1092. });
  1093. // 计量明细:勿需修改任何数据
  1094. // 合同支付明细:复制一份最新数据给下一人
  1095. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
  1096. // 其他台账明细:更新两份历史数据
  1097. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1098. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1099. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1100. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1101. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1102. // 更新标段计量数据缓存
  1103. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
  1104. // 上报/审批 - 检查三方特殊推送
  1105. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1106. await transaction.commit();
  1107. } catch(err) {
  1108. await transaction.rollback();
  1109. throw err;
  1110. }
  1111. }
  1112. /**
  1113. * 审批人撤回审批退回上一人,插入两条数据
  1114. *
  1115. * 一审 1 A checked 一审 1 A checked
  1116. * 二审 2 B checked 二审 2 B checked
  1117. * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre
  1118. * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his
  1119. * 三审 3 C uncheck 三审 5 C checking status改 增pay_cur
  1120. * 四审 6 D uncheck 四审 6 D uncheck
  1121. *
  1122. * @param stage
  1123. * @returns {Promise<void>}
  1124. * @private
  1125. */
  1126. async _auditCheckCancelNoPre(stage) {
  1127. if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
  1128. throw '撤回用户数据错误';
  1129. }
  1130. const accountId = this.ctx.session.sessionUser.accountId;
  1131. const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
  1132. if (!selfAuditor) throw '撤回用户数据错误';
  1133. const time = new Date();
  1134. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1135. const transaction = await this.db.beginTransaction();
  1136. try {
  1137. const ledgerTp = await this._updateTender(transaction);
  1138. // 整理当前流程审核人状态更新
  1139. // 删除当前审批人
  1140. await transaction.delete(this.tableName, { id: stage.curAuditors.map(x => { return x.id; }) });
  1141. // 添加撤回人到审批流程中
  1142. const newAuditors = [];
  1143. stage.preAuditors.forEach(x => {
  1144. newAuditors.push({
  1145. tid: stage.tid, sid: stage.id, aid: x.aid,
  1146. times: x.times, order: x.order + 1,
  1147. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1148. begin_time: time, end_time: time, opinion: '',
  1149. audit_type: x.audit_type, audit_order: x.audit_order,
  1150. });
  1151. });
  1152. await transaction.insert(this.tableName, newAuditors);
  1153. // 更新上一个人,最新审批状态为审批中
  1154. await transaction.update(this.tableName, { begin_time: time, status: auditConst.status.checking }, {
  1155. where: { sid: selfAuditor.sid, times: selfAuditor.times, order: selfAuditor.order + 2 }
  1156. });
  1157. // 计算并合同支付最终数据
  1158. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1159. const stageTp = {
  1160. contract_tp: tpData.contract_tp,
  1161. qc_tp: tpData.qc_tp,
  1162. positive_qc_tp: tpData.positive_qc_tp,
  1163. negative_qc_tp: tpData.negative_qc_tp,
  1164. yf_tp: yfPay.tp,
  1165. sf_tp: sfPay.tp,
  1166. };
  1167. stage.tp_history.push({ times, order: selfAuditor.order + 1, ...stageTp });
  1168. // 同步 期信息
  1169. await transaction.update(this.ctx.service.stage.tableName, {
  1170. id: stage.id,
  1171. times: stage.times,
  1172. ...stageTp,
  1173. status: auditConst.status.checking,
  1174. tp_history: JSON.stringify(stage.tp_history),
  1175. cache_time_r: stage.cache_time_l,
  1176. });
  1177. // 计量明细:勿需修改任何数据
  1178. // 合同支付明细:复制一份最新数据给下一人
  1179. await this.ctx.service.stagePay.copyAuditStagePays(stage, times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
  1180. // 其他台账明细:更新一份历史数据
  1181. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1182. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1183. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1184. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1185. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1186. // 更新标段计量数据缓存
  1187. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, newAuditors, stage.preAuditors, ledgerTp, stageTp);
  1188. // 上报/审批 - 检查三方特殊推送
  1189. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1190. await transaction.commit();
  1191. } catch(err) {
  1192. await transaction.rollback();
  1193. throw err;
  1194. }
  1195. }
  1196. /**
  1197. * 审批人撤回审批退回原报
  1198. *
  1199. * 1# 一审 1 A checked 1# 一审 1 A checked
  1200. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  1201. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  1202. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  1203. * 三审 5 C uncheck order改
  1204. *
  1205. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  1206. * 二审 2 B uncheck
  1207. * 三审 3 C uncheck
  1208. *
  1209. * @param stage
  1210. * @returns {Promise<void>}
  1211. * @private
  1212. */
  1213. async _auditCheckCancelNo(stage) {
  1214. const accountId = this.ctx.session.sessionUser.accountId;
  1215. const selfAuditor = stage.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
  1216. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  1217. const time = new Date();
  1218. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  1219. const transaction = await this.db.beginTransaction();
  1220. try {
  1221. const ledgerTp = await this._updateTender(transaction);
  1222. // 整理上一个流程审核人状态更新
  1223. // 顺移其后审核人流程顺序
  1224. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  1225. await transaction.query(sql, [stage.id, selfAuditor.times, selfAuditor.order]);
  1226. // 当前审批人2次添加至流程中
  1227. const checkCancelAuditors = [], checkingAuditors = [];
  1228. stage.preAuditors.forEach(x => {
  1229. checkCancelAuditors.push({
  1230. tid: stage.tid, sid: stage.id, aid: x.aid,
  1231. times: x.times, order: x.order + 1,
  1232. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1233. begin_time: time, end_time: time, opinion: '',
  1234. audit_type: x.audit_type, audit_order: x.audit_order,
  1235. });
  1236. });
  1237. stage.preAuditors.forEach(x => {
  1238. checkingAuditors.push({
  1239. tid: stage.tid, sid: stage.id, aid: x.aid,
  1240. times: x.times, order: x.order + 2,
  1241. status: auditConst.status.checking,
  1242. begin_time: time, end_time: time, opinion: '',
  1243. audit_type: x.audit_type, audit_order: x.audit_order,
  1244. });
  1245. });
  1246. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  1247. // 删除当前次审批流
  1248. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1249. // 计算并合同支付最终数据
  1250. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1251. const stageTp = {
  1252. contract_tp: tpData.contract_tp,
  1253. qc_tp: tpData.qc_tp,
  1254. positive_qc_tp: tpData.positive_qc_tp,
  1255. negative_qc_tp: tpData.negative_qc_tp,
  1256. yf_tp: yfPay.tp,
  1257. sf_tp: sfPay.tp,
  1258. };
  1259. stage.tp_history.push({ times: selfAuditor.times, order: selfAuditor.order + 1, ...stageTp });
  1260. await transaction.update(this.ctx.service.stage.tableName, {
  1261. id: stage.id,
  1262. times: stage.times - 1,
  1263. ...stageTp,
  1264. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1265. cache_time_r: stage.cache_time_l,
  1266. status: auditConst.status.checking,
  1267. });
  1268. // 计量明细:重复数据删除原报,新增数据修改为原报
  1269. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1270. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1271. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1272. await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1273. // 合同支付明细:复制一份最新数据新增的2个审批流,删除最新一次审批数据
  1274. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 1, transaction, stage.times, 0);
  1275. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 2, transaction, stage.times, 0);
  1276. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
  1277. // 其他台账明细:更新一份历史数据
  1278. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1279. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1280. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1281. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1282. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1283. // 更新标段计量数据缓存
  1284. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, auditConst.status.checking, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
  1285. // 上报/审批 - 检查三方特殊推送
  1286. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1287. await transaction.commit();
  1288. } catch(err) {
  1289. await transaction.rollback();
  1290. throw err;
  1291. }
  1292. }
  1293. /**
  1294. * 会签未全部审批通过时,撤回仅修改本人状态
  1295. *
  1296. * @param stage
  1297. * @returns {Promise<void>}
  1298. * @private
  1299. */
  1300. async _auditCheckCancelAnd(stage) {
  1301. const accountId = this.ctx.session.sessionUser.accountId;
  1302. const selfAuditor = stage.flowAuditors.find(x => { return x.aid === accountId; });
  1303. if (!selfAuditor || selfAuditor.status !== auditConst.status.checked) throw '不可撤回';
  1304. const transaction = await this.db.beginTransaction();
  1305. try {
  1306. await transaction.update(this.tableName, {
  1307. id: selfAuditor.id, status: auditConst.status.checking, opinion: '', end_time: null,
  1308. });
  1309. await transaction.commit();
  1310. } catch(err) {
  1311. await transaction.rollback();
  1312. throw err;
  1313. }
  1314. }
  1315. /**
  1316. * 审批撤回
  1317. * @param {Number} stageId - 标段id
  1318. * @param {Number} times - 第几次审批
  1319. * @return {Promise<void>}
  1320. */
  1321. async checkCancel(stage) {
  1322. // 分5种情况,根据ctx.cancancel值判断:
  1323. // 1.原报发起撤回,当前流程删除,并回到待上报
  1324. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  1325. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  1326. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  1327. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  1328. if (stage.cancancel === 5) {
  1329. await this._auditCheckCancelAnd(stage);
  1330. } else {
  1331. switch (this.ctx.stage.cancancel) {
  1332. case 1: await this._userCheckCancel(stage); break;
  1333. case 2: await this._auditCheckCancel(stage); break;
  1334. case 3: await this._auditCheckCancelNoPre(stage); break;
  1335. case 4: await this._auditCheckCancelNo(stage); break;
  1336. default: throw '不可撤回,请刷新页面重试';
  1337. }
  1338. // 通知发送 - 第三方更新
  1339. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1340. const base_data = {
  1341. tid: stage.tid,
  1342. sid: stage.id,
  1343. op: 'update',
  1344. };
  1345. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1346. base_data.op = 'update';
  1347. base_data.sid = -1;
  1348. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1349. }
  1350. }
  1351. }
  1352. /**
  1353. * 获取审核人需要审核的期列表
  1354. *
  1355. * @param auditorId
  1356. * @return {Promise<*>}
  1357. */
  1358. async getAuditStage(auditorId) {
  1359. const sql =
  1360. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1361. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  1362. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  1363. ' FROM ?? AS sa ' +
  1364. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  1365. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  1366. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  1367. ' ORDER BY sa.`begin_time` DESC';
  1368. const sqlParam = [
  1369. this.tableName,
  1370. this.ctx.service.stage.tableName,
  1371. this.ctx.service.tender.tableName,
  1372. auditorId,
  1373. auditConst.status.checking,
  1374. auditorId,
  1375. auditConst.status.checkNo,
  1376. auditConst.status.checkNo,
  1377. ];
  1378. return await this.db.query(sql, sqlParam);
  1379. }
  1380. /**
  1381. * 获取审核人审核的次数
  1382. *
  1383. * @param auditorId
  1384. * @return {Promise<*>}
  1385. */
  1386. async getCountByChecked(auditorId) {
  1387. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  1388. }
  1389. /**
  1390. * 获取最近一次审批结束时间
  1391. *
  1392. * @param auditorId
  1393. * @return {Promise<*>}
  1394. */
  1395. async getLastEndTimeByChecked(auditorId) {
  1396. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  1397. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
  1398. const sqlParam = [this.tableName, auditorId];
  1399. const result = await this.db.queryOne(sql, sqlParam);
  1400. return result ? result.end_time : null;
  1401. }
  1402. /**
  1403. * 获取 某时间后 审批进度 更新的期
  1404. * @param {Number} pid - 查询标段
  1405. * @param {Number} uid - 查询人
  1406. * @param {Date} time - 查询时间
  1407. * @return {Promise<*>}
  1408. */
  1409. async getNoticeStage(pid, uid, time) {
  1410. let notice = await this.db.select('zh_notice', {
  1411. where: { pid, type: pushType.stage, uid },
  1412. orders: [['create_time', 'desc']],
  1413. limit: 10,
  1414. offset: 0,
  1415. });
  1416. notice = notice.map(v => {
  1417. const extra = JSON.parse(v.content);
  1418. delete v.content;
  1419. return { ...v, ...extra };
  1420. });
  1421. return notice;
  1422. }
  1423. /**
  1424. * 用于添加推送所需的content内容
  1425. * @param {Number} pid 项目id
  1426. * @param {Number} tid 台账id
  1427. * @param {Number} sid 期id
  1428. * @param {Number} uid 审核人id
  1429. */
  1430. async getNoticeContent(pid, tid, sid, uid, opinion = '') {
  1431. const noticeSql =
  1432. 'SELECT * FROM (SELECT ' +
  1433. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1434. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1435. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1436. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1437. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1438. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1439. const content = await this.db.query(noticeSql, noticeSqlParam);
  1440. if (content.length) {
  1441. content[0].opinion = opinion;
  1442. }
  1443. return content.length ? JSON.stringify(content[0]) : '';
  1444. }
  1445. /**
  1446. * 获取审核人流程列表
  1447. *
  1448. * @param auditorId
  1449. * @return {Promise<*>}
  1450. */
  1451. async getAuditGroupByList(stageId, times, transaction = false) {
  1452. // const sql =
  1453. // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order`, la.`status`' +
  1454. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1455. // ' WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = 0 GROUP BY la.`aid` ORDER BY la.`order`';
  1456. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1457. const sql =
  1458. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.`status`, la.audit_type, la.audit_order' +
  1459. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `sid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  1460. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  1461. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  1462. const sqlParam = [this.tableName, stageId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, stageId, times, 0];
  1463. return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  1464. }
  1465. /**
  1466. * 获取审核人流程列表
  1467. *
  1468. * @param auditorId
  1469. * @return {Promise<*>}
  1470. */
  1471. async getAuditGroupByListWithOwner(stageId, times) {
  1472. const result = await this.getAuditGroupByList(stageId, times);
  1473. const sql =
  1474. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1475. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1476. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1477. ' ON s.user_id = pa.id' +
  1478. ' WHERE s.id = ?';
  1479. const sqlParam = [times, stageId, stageId];
  1480. const user = await this.db.queryOne(sql, sqlParam);
  1481. result.unshift(user);
  1482. return result;
  1483. }
  1484. /**
  1485. * 复制上一期的审批人列表给最新一期
  1486. *
  1487. * @param transaction - 新增一期的事务
  1488. * @param {Object} preStage - 上一期
  1489. * @param {Object} newStage - 最新一期
  1490. * @return {Promise<*>}
  1491. */
  1492. async copyPreStageAuditors(transaction, preStage, newStage) {
  1493. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1494. const newAuditors = [];
  1495. for (const a of auditors) {
  1496. const na = {
  1497. tid: preStage.tid,
  1498. sid: newStage.id,
  1499. aid: a.aid,
  1500. times: newStage.times,
  1501. order: a.audit_order,
  1502. status: auditConst.status.uncheck,
  1503. audit_type: a.audit_type,
  1504. audit_order: a.audit_order,
  1505. };
  1506. newAuditors.push(na);
  1507. }
  1508. const result = await transaction.insert(this.tableName, newAuditors);
  1509. return (result.effectRows = auditors.length);
  1510. }
  1511. /**
  1512. * 移除审核人
  1513. *
  1514. * @param {Number} stageId - 期id
  1515. * @param {Number} status - 期状态
  1516. * @param {Number} status - 期次数
  1517. * @return {Promise<boolean>}
  1518. */
  1519. async getAuditorByStatus(stageId, status, times = 1) {
  1520. let auditor = null;
  1521. let sql = '';
  1522. let sqlParam = '';
  1523. switch (status) {
  1524. case auditConst.status.checking:
  1525. case auditConst.status.checked:
  1526. case auditConst.status.checkNoPre:
  1527. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1528. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1529. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1530. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1531. auditor = await this.db.queryOne(sql, sqlParam);
  1532. break;
  1533. case auditConst.status.checkNo:
  1534. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1535. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1536. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1537. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1538. auditor = await this.db.queryOne(sql, sqlParam);
  1539. break;
  1540. case auditConst.status.uncheck:
  1541. default:
  1542. break;
  1543. }
  1544. return auditor;
  1545. }
  1546. async getAuditorsByStatus(stageId, status, times = 1) {
  1547. let auditor = [];
  1548. let sql = '';
  1549. let sqlParam = '';
  1550. let cur;
  1551. switch (status) {
  1552. case auditConst.status.checking:
  1553. case auditConst.status.checked:
  1554. case auditConst.status.checkNoPre:
  1555. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, times, status]);
  1556. if (!cur) return [];
  1557. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order, la.audit_type ' +
  1558. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1559. ' WHERE la.`sid` = ? and la.`order` = ?';
  1560. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order];
  1561. auditor = await this.db.query(sql, sqlParam);
  1562. break;
  1563. case auditConst.status.checkNo:
  1564. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, parseInt(times) - 1, status]);
  1565. if (!cur) return [];
  1566. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order, la.audit_type ' +
  1567. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1568. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?';
  1569. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order, parseInt(times) - 1];
  1570. auditor = await this.db.queryOne(sql, sqlParam);
  1571. break;
  1572. case auditConst.status.uncheck:
  1573. default:
  1574. break;
  1575. }
  1576. return auditor;
  1577. }
  1578. /**
  1579. * 取某一期已批准审核信息(报表用)
  1580. *
  1581. * @param {Number} stageId - 期id
  1582. * @param {Number} times - 期次数
  1583. * @return {Promise<boolean>}
  1584. */
  1585. async getStageAudit(stageId, times = 1) {
  1586. const sql = 'SELECT a1.aid, a1.begin_time, a1.end_time, a1.status, a1.opinion ' + 'FROM ?? AS a1 ' + 'WHERE a1.`sid` = ? and a1.`times` = ? ' + 'ORDER BY a1.order';
  1587. const sqlParam = [this.tableName, stageId, times];
  1588. const rst = await this.db.query(sql, sqlParam);
  1589. return rst;
  1590. }
  1591. /**
  1592. * 取待审批期列表(wap用)
  1593. *
  1594. * @param auditorId
  1595. * @return {Promise<*>}
  1596. */
  1597. async getAuditStageByWap(auditorId) {
  1598. const sql =
  1599. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1600. // ' s.`order` As `sorder`, s.`status` As `sstatus`, s.`s_time`, s.`contract_tp`, s.`qc_tp`, s.`pre_contract_tp`, s.`pre_qc_tp`, s.`yf_tp`, s.`pre_yf_tp`, ' +
  1601. ' s.*,' +
  1602. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1603. ' ti.`deal_info` ' +
  1604. ' FROM ?? AS sa' +
  1605. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1606. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1607. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1608. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1609. const sqlParam = [
  1610. this.tableName,
  1611. this.ctx.service.stage.tableName,
  1612. this.ctx.service.tender.tableName,
  1613. this.ctx.service.tenderInfo.tableName,
  1614. auditorId,
  1615. auditConst.status.checking,
  1616. ];
  1617. return await this.db.query(sql, sqlParam);
  1618. }
  1619. /**
  1620. * 删除 某期 某次 全审批流程
  1621. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1622. * @param {Number} sid - 标段id
  1623. * @param {Number} times - 第几次审批
  1624. * @param transaction - 删除事务
  1625. * @return {Promise<void>}
  1626. */
  1627. async _timesDelete(sid, times, transaction) {
  1628. // 审批流程
  1629. await transaction.delete(this.tableName, { sid, times });
  1630. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1631. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1632. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1633. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1634. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1635. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1636. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1637. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid, times });
  1638. // 其他台账
  1639. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1640. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1641. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1642. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1643. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1644. }
  1645. /**
  1646. * 删除本次审批流程
  1647. * @param {Number} stageId - 标段id
  1648. * @param {Number} times - 第几次审批
  1649. * @return {Promise<void>}
  1650. */
  1651. async timesDelete() {
  1652. const transaction = await this.db.beginTransaction();
  1653. try {
  1654. // 删除最新一次数据
  1655. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1656. // 审批退回,未重新上报时,需删除最新两次数据
  1657. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1658. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1659. if (isCheckNo) {
  1660. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1661. }
  1662. // 添加上一次审批人
  1663. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1664. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1665. const auditors = await this.db.query(sql, sqlParam);
  1666. let order = 1;
  1667. for (const a of auditors) {
  1668. a.times = nowTimes;
  1669. a.order = order;
  1670. a.status = auditConst.status.uncheck;
  1671. order++;
  1672. }
  1673. const ledgerTp = await this._updateTender(transaction);
  1674. // 拷贝新一次审核流程列表
  1675. await transaction.insert(this.tableName, auditors);
  1676. // 计算缓存
  1677. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1678. // 计算并合同支付最终数据
  1679. const lastAudit = await this.getDataByCondition({
  1680. sid: this.ctx.stage.id,
  1681. times: nowTimes - 1,
  1682. status: auditConst.status.checkNo,
  1683. });
  1684. if (!lastAudit) throw '审批数据错误';
  1685. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1686. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1687. const yfPay = stagePay.find(function(x) {
  1688. return x.ptype === payConst.payType.yf;
  1689. });
  1690. const sfPay = stagePay.find(function(x) {
  1691. return x.ptype === payConst.payType.sf;
  1692. });
  1693. // 同步 期信息
  1694. const time = new Date();
  1695. await transaction.update(this.ctx.service.stage.tableName, {
  1696. id: this.ctx.stage.id,
  1697. status: auditConst.status.checkNo,
  1698. contract_tp: tpData.contract_tp,
  1699. qc_tp: tpData.qc_tp,
  1700. positive_qc_tp: tpData.positive_qc_tp,
  1701. negative_qc_tp: tpData.negative_qc_tp,
  1702. times: nowTimes,
  1703. yf_tp: yfPay ? yfPay.tp : null,
  1704. sf_tp: sfPay ? sfPay.tp : null,
  1705. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times <= nowTimes; })),
  1706. cache_time_l: time,
  1707. cache_time_r: time,
  1708. });
  1709. await transaction.commit();
  1710. // 通知发送 - 第三方更新
  1711. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1712. const base_data = {
  1713. tid: this.ctx.tender.id,
  1714. sid: this.ctx.stage.id,
  1715. op: 'update',
  1716. };
  1717. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1718. base_data.op = 'update';
  1719. base_data.sid = -1;
  1720. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1721. }
  1722. } catch (err) {
  1723. await transaction.rollback();
  1724. throw err;
  1725. }
  1726. }
  1727. // 固定审批流-更新
  1728. async updateNewAuditList(stage, newList) {
  1729. const transaction = await this.db.beginTransaction();
  1730. try {
  1731. // 先删除旧的审批流,再添加新的
  1732. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1733. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times });
  1734. const newAuditors = [];
  1735. for (const auditor of newList) {
  1736. newAuditors.push({
  1737. tid: stage.tid, sid: stage.id, aid: auditor.audit_id,
  1738. times: stage.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1739. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1740. });
  1741. }
  1742. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1743. await transaction.commit();
  1744. } catch (err) {
  1745. await transaction.rollback();
  1746. throw err;
  1747. }
  1748. }
  1749. // 固定终审-更新
  1750. async updateLastAudit(stage, auditList, lastId) {
  1751. const transaction = await this.db.beginTransaction();
  1752. try {
  1753. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1754. const existAudit = auditList.find(x => { return x.aid === lastId });
  1755. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order)}, 0) + 1 : 1; // 最大值 + 1
  1756. if (existAudit) {
  1757. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1758. const sameOrder = auditList.find(x => { return x.order === existAudit.order });
  1759. if (!sameOrder) {
  1760. const updateData = [];
  1761. auditList.forEach(x => {
  1762. if (x.order <= existAudit.order) return;
  1763. updateData.push({id: x.id, order: x.order - 1, audit_order: x.audit_order - 1});
  1764. });
  1765. if (updateData.length > 0) {
  1766. await transaction.updateRows(updateData);
  1767. order = order - 1;
  1768. }
  1769. }
  1770. }
  1771. // 添加终审
  1772. const newAuditor = {
  1773. tid: stage.tid, sid: stage.id, aid: lastId,
  1774. times: stage.times, order, status: auditConst.status.uncheck,
  1775. audit_type: auditType.key.common, audit_order: order,
  1776. };
  1777. await transaction.insert(this.tableName, newAuditor);
  1778. await transaction.commit();
  1779. } catch (err) {
  1780. await transaction.rollback();
  1781. throw err;
  1782. }
  1783. }
  1784. async getFinalAuditGroup(stageId, times) {
  1785. const sql =
  1786. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`sid`, Max(la.`order`) as max_order ' +
  1787. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1788. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1789. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1790. const result = await this.db.query(sql, sqlParam);
  1791. for (const r of result) {
  1792. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1793. r.status = auditor.status;
  1794. r.opinion = auditor.opinion;
  1795. r.begin_time = auditor.begin_time;
  1796. r.end_time = auditor.end_time;
  1797. }
  1798. return result;
  1799. }
  1800. async getNumByMonth(tid, startMonth, endMonth) {
  1801. const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT MAX(id) FROM ?? WHERE tid = ? GROUP BY sid) AND status = ? AND end_time between ? and ?';
  1802. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1803. const result = await this.db.queryOne(sql, sqlParam);
  1804. return result ? result.num : 0;
  1805. }
  1806. /**
  1807. * 删除本次审批流程
  1808. * @param {Number} stageId - 标段id
  1809. * @param {Number} times - 第几次审批
  1810. * @param {Object} data - 更改参数
  1811. * @return {Promise<void>}
  1812. */
  1813. async saveAudit(stageId, times, data) {
  1814. const transaction = await this.db.beginTransaction();
  1815. try {
  1816. const auditors = await this.getAuditGroupByList(stageId, times);
  1817. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1818. if (data.operate === 'add') {
  1819. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1820. throw '当前人下无法操作新增';
  1821. }
  1822. const newAudit = {
  1823. tid: this.ctx.tender.id,
  1824. sid: stageId,
  1825. aid: data.new_aid,
  1826. order: now_audit.order+1,
  1827. times: times,
  1828. status: 1
  1829. };
  1830. // order+1
  1831. await this._syncOrderByDelete(transaction, stageId, now_audit.order+1, times, '+');
  1832. await transaction.insert(this.tableName, newAudit);
  1833. // 更新审批流程页数据,如果存在
  1834. } else if (data.operate === 'del') {
  1835. if (now_audit.status !== auditConst.status.uncheck) {
  1836. throw '当前人无法操作删除';
  1837. }
  1838. await transaction.delete(this.tableName, { sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  1839. await this._syncOrderByDelete(transaction, stageId, now_audit.order, times);
  1840. // 旧的更新为is_old为1
  1841. await transaction.update(this.tableName, { is_old: 1 }, {
  1842. where: {
  1843. sid: stageId,
  1844. times,
  1845. aid: data.old_aid,
  1846. }
  1847. });
  1848. } else if (data.operate === 'change') {
  1849. const nowAudit = await this.getDataByCondition({ sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  1850. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1851. throw '当前人无法操作替换';
  1852. }
  1853. nowAudit.aid = data.new_aid;
  1854. await transaction.update(this.tableName, nowAudit);
  1855. // 旧的更新为is_old为1
  1856. await transaction.update(this.tableName, { is_old: 1 }, {
  1857. where: {
  1858. sid: stageId,
  1859. times,
  1860. aid: data.old_aid,
  1861. }
  1862. });
  1863. }
  1864. if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl || this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdzs) {
  1865. const newAuditors = await this.getAuditGroupByList(stageId, times, transaction);
  1866. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.stage, shenpiConst.sp_type.stage, this._.map(newAuditors, 'aid'));
  1867. }
  1868. // 更新到审批流程方法
  1869. await transaction.commit();
  1870. } catch (err) {
  1871. await transaction.rollback();
  1872. throw err;
  1873. }
  1874. }
  1875. async getAuditorGroup(stageId, times) {
  1876. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
  1877. return this.ctx.helper.groupAuditors(auditors);
  1878. }
  1879. async getUserGroup(stageId, times) {
  1880. const group = await this.getAuditorGroup(stageId, times);
  1881. const sql =
  1882. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1883. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1884. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1885. ' ON s.user_id = pa.id' +
  1886. ' WHERE s.id = ?';
  1887. const sqlParam = [times, stageId, stageId];
  1888. const user = await this.db.queryOne(sql, sqlParam);
  1889. group.unshift([ user ]);
  1890. return group;
  1891. }
  1892. async getAuditorHistory(stageId, times) {
  1893. const history = [];
  1894. if (times >= 1) {
  1895. for (let i = 1; i <= times; i++) {
  1896. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, i);
  1897. const group = this.ctx.helper.groupAuditors(auditors);
  1898. const historyGroup = [];
  1899. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  1900. for (const g of group) {
  1901. const his = {
  1902. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  1903. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  1904. auditors: g
  1905. };
  1906. if (his.audit_type === auditType.key.common) {
  1907. his.name = g[0].name;
  1908. } else {
  1909. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  1910. }
  1911. his.is_final = his.audit_order === max_order;
  1912. if (g[0].begin_time) {
  1913. his.begin_time = g[0].begin_time;
  1914. const beginTime = this.ctx.moment(g[0].begin_time);
  1915. his.beginYear = beginTime.format('YYYY');
  1916. his.beginDate = beginTime.format('MM-DD');
  1917. his.beginTime = beginTime.format('HH:mm:ss');
  1918. }
  1919. let end_time;
  1920. g.forEach(x => {
  1921. if (x.status === auditConst.status.checkSkip) return;
  1922. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  1923. if (x.end_time && (!end_time || x.end_time > end_time)) {
  1924. end_time = x.end_time;
  1925. if (his.status !== auditConst.status.checking) his.status = x.status;
  1926. }
  1927. });
  1928. if (end_time) {
  1929. his.end_time = end_time;
  1930. const endTime = this.ctx.moment(end_time);
  1931. his.endYear = endTime.format('YYYY');
  1932. his.endDate = endTime.format('MM-DD');
  1933. his.endTime = endTime.format('HH:mm:ss');
  1934. }
  1935. historyGroup.push(his);
  1936. }
  1937. history.push(historyGroup);
  1938. }
  1939. }
  1940. return history;
  1941. }
  1942. async getUniqAuditor(stageId, times) {
  1943. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
  1944. const result = [];
  1945. auditors.forEach(x => {
  1946. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === x.audit_order; }) < 0) {
  1947. result.push(x);
  1948. }
  1949. });
  1950. return result;
  1951. }
  1952. }
  1953. return StageAudit;
  1954. };