stage_audit.js 94 KB

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