stage_audit.js 124 KB

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