stage_audit.js 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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, audit, nextAudit, 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, audit, nextAudit, 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, { id: this.ctx.stage.user_id }, audit, ledgerTp, stageTp);
  556. // 拷贝新一次审核流程列表
  557. await transaction.insert(this.tableName, auditors);
  558. // 计算该审批人最终数据
  559. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  560. // 复制一份最新数据给原报
  561. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  562. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  563. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  564. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  565. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  566. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  567. // 锁定本人数据,保留锁定数据相关确认状态
  568. await this.ctx.service.stageAuditAss.lockConfirm4CheckNo(this.ctx.stage, audit.aid, auditors, transaction);
  569. // 添加短信通知-审批退回提醒功能
  570. // const mobile_array = [];
  571. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  572. const auditList = await this.getAuditors(stageId, stageInfo.times);
  573. 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')));
  574. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  575. qi: stageInfo.order,
  576. status: SmsAliConst.status.back,
  577. });
  578. // 微信模板通知
  579. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  580. const wechatData = {
  581. wap_url: shenpiUrl,
  582. qi: stageInfo.order,
  583. status: wxConst.status.back,
  584. tips: wxConst.tips.back,
  585. code: this.ctx.session.sessionProject.code,
  586. };
  587. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  588. // 上报/审批 - 检查三方特殊推送
  589. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  590. await transaction.commit();
  591. } catch (err) {
  592. await transaction.rollback();
  593. throw err;
  594. }
  595. }
  596. async _checkNoPre(pid, stageId, checkData, times) {
  597. const time = new Date();
  598. // 整理当前流程审核人状态更新
  599. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  600. if (!audit || audit.order <= 1) {
  601. throw '审核数据错误';
  602. }
  603. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  604. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  605. const auditors2 = await this.getAuditGroupByList(stageId, times);
  606. const auditorIndex = await auditors2.findIndex(function(item) {
  607. return item.aid === audit.aid;
  608. });
  609. const preAuditor = auditors2[auditorIndex - 1];
  610. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  611. const transaction = await this.db.beginTransaction();
  612. try {
  613. const ledgerTp = await this._updateTender(transaction);
  614. // 添加推送
  615. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  616. const defaultNoticeRecord = {
  617. pid,
  618. type: pushType.stage,
  619. status: auditConst.status.checkNoPre,
  620. content: noticeContent,
  621. };
  622. const records = [
  623. {
  624. uid: this.ctx.stage.user_id,
  625. ...defaultNoticeRecord
  626. },
  627. ];
  628. auditors2.forEach(audit => {
  629. records.push({
  630. uid: audit.aid,
  631. ...defaultNoticeRecord
  632. });
  633. });
  634. this.ctx.stage.userAssists.forEach(u => {
  635. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  636. });
  637. this.ctx.stage.auditAssists.forEach(u => {
  638. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  639. });
  640. await transaction.insert('zh_notice', records);
  641. // 计算并合同支付最终数据
  642. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  643. const stageTp = {
  644. contract_tp: tpData.contract_tp,
  645. qc_tp: tpData.qc_tp,
  646. positive_qc_tp: tpData.positive_qc_tp,
  647. negative_qc_tp: tpData.negative_qc_tp,
  648. yf_tp: yfPay.tp,
  649. sf_tp: sfPay.tp,
  650. };
  651. this.ctx.stage.tp_history.push({ times, order: audit.order, ...stageTp });
  652. // 同步 期信息
  653. await transaction.update(this.ctx.service.stage.tableName, {
  654. id: stageId,
  655. times,
  656. ...stageTp,
  657. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  658. cache_time_r: this.ctx.stage.cache_time_l,
  659. });
  660. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, audit, preAuditor, ledgerTp, stageTp);
  661. await transaction.update(this.tableName, {
  662. id: audit.id,
  663. status: checkData.checkType,
  664. opinion: checkData.opinion,
  665. end_time: time,
  666. });
  667. // 顺移其后审核人流程顺序
  668. this.initSqlBuilder();
  669. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  670. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  671. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  672. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  673. const data = await transaction.query(sql, sqlParam);
  674. // 上一审批人,当前审批人 再次添加至流程
  675. const newAuditors = [];
  676. newAuditors.push({
  677. tid: audit.tid,
  678. sid: audit.sid,
  679. aid: preAuditor.aid,
  680. times: audit.times,
  681. order: audit.order + 1,
  682. status: auditConst.status.checking,
  683. begin_time: time,
  684. });
  685. newAuditors.push({
  686. tid: audit.tid,
  687. sid: audit.sid,
  688. aid: audit.aid,
  689. times: audit.times,
  690. order: audit.order + 2,
  691. status: auditConst.status.uncheck,
  692. });
  693. await transaction.insert(this.tableName, newAuditors);
  694. // 计算该审批人最终数据
  695. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  696. // 复制一份最新数据给下一人
  697. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  698. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  699. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  700. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  701. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  702. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  703. // 锁定本人数据,保留锁定数据相关确认状态
  704. await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, audit.aid, preAuditor.aid, transaction);
  705. // 同步 期信息
  706. await transaction.update(this.ctx.service.stage.tableName, {
  707. id: stageId,
  708. status: checkData.checkType,
  709. cache_time_r: this.ctx.stage.cache_time_l,
  710. });
  711. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  712. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  713. const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return x.user_id === preAuditor.aid}), 'ass_user_id');
  714. users.push(preAuditor.aid);
  715. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  716. qi: stageInfo.order,
  717. code: shenpiUrl,
  718. });
  719. // 微信模板通知
  720. const wechatData = {
  721. wap_url: shenpiUrl,
  722. qi: stageInfo.order,
  723. status: wxConst.status.check,
  724. tips: wxConst.tips.check,
  725. code: this.ctx.session.sessionProject.code,
  726. };
  727. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  728. // 上报/审批 - 检查三方特殊推送
  729. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  730. await transaction.commit();
  731. } catch (err) {
  732. await transaction.rollback();
  733. throw err;
  734. }
  735. }
  736. /**
  737. * 审批
  738. * @param {Number} stageId - 标段id
  739. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  740. * @param {Number} times - 第几次审批
  741. * @return {Promise<void>}
  742. */
  743. async check(stageId, checkData, times = 1) {
  744. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  745. throw '提交数据错误';
  746. }
  747. // // 整理当前流程审核人状态更新
  748. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  749. // if (!audit) {
  750. // throw '审核数据错误';
  751. // }
  752. // const time = new Date();
  753. const pid = this.ctx.session.sessionProject.id;
  754. switch (checkData.checkType) {
  755. case auditConst.status.checked:
  756. await this._checked(pid, stageId, checkData, times);
  757. break;
  758. case auditConst.status.checkNo:
  759. await this._checkNo(pid, stageId, checkData, times);
  760. break;
  761. case auditConst.status.checkNoPre:
  762. await this._checkNoPre(pid, stageId, checkData, times);
  763. break;
  764. default:
  765. throw '无效审批操作';
  766. }
  767. // 通知发送 - 第三方更新
  768. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  769. const base_data = {
  770. tid: this.ctx.tender.id,
  771. sid: stageId,
  772. op: 'update',
  773. };
  774. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  775. base_data.op = 'update';
  776. base_data.sid = -1;
  777. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  778. }
  779. }
  780. /**
  781. * 审批
  782. * @param {Number} stageId - 标段id
  783. * @param {Number} times - 第几次审批
  784. * @return {Promise<void>}
  785. */
  786. async checkAgain(stageId, times = 1) {
  787. const time = new Date();
  788. // 整理当前流程审核人状态更新
  789. const audit = (
  790. await this.getAllDataByCondition({
  791. where: { sid: stageId, times },
  792. orders: [['order', 'desc']],
  793. limit: 1,
  794. offset: 0,
  795. })
  796. )[0];
  797. if (!audit || audit.order < 1) {
  798. throw '审核数据错误';
  799. }
  800. const transaction = await this.db.beginTransaction();
  801. try {
  802. // 当前审批人2次添加至流程中
  803. const newAuditors = [];
  804. newAuditors.push({
  805. tid: audit.tid,
  806. sid: audit.sid,
  807. aid: audit.aid,
  808. times: audit.times,
  809. order: audit.order + 1,
  810. status: auditConst.status.checkAgain,
  811. begin_time: time,
  812. end_time: time,
  813. opinion: '',
  814. });
  815. newAuditors.push({
  816. tid: audit.tid,
  817. sid: audit.sid,
  818. aid: audit.aid,
  819. times: audit.times,
  820. order: audit.order + 2,
  821. status: auditConst.status.checking,
  822. begin_time: time,
  823. });
  824. await transaction.insert(this.tableName, newAuditors);
  825. // 复制一份最新数据给下一人
  826. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  827. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  828. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  829. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  830. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  831. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  832. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  833. // 同步 期信息
  834. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === audit.order });
  835. const stageTp = {
  836. contract_tp: his.contract_tp,
  837. qc_tp: his.qc_tp,
  838. positive_qc_tp: his.positive_qc_tp,
  839. negative_qc_tp: his.negative_qc_tp,
  840. yf_tp: his.tp,
  841. sf_tp: his.tp,
  842. };
  843. this.ctx.stage.tp_history.push({ times, order: audit.order + 1, ...stageTp });
  844. await transaction.update(this.ctx.service.stage.tableName, {
  845. id: stageId,
  846. status: auditConst.status.checking,
  847. cache_time_r: this.ctx.stage.cache_time_l,
  848. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  849. });
  850. // 已经引用到本期的单价变更,全部取消
  851. await this.ctx.service.revisePrice.cancelPriceUsed(this.ctx.stage, transaction);
  852. // 重算所有单价变更
  853. const priceCalc = new RevisePrice(this.ctx);
  854. const pcTp = await priceCalc.stageCheckAgainPriceChange(this.ctx.stage, audit.order + 2, transaction);
  855. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, audit, audit, null, stageTp, pcTp);
  856. // 添加短信通知-需要审批提醒功能
  857. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  858. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  859. // const smsType = JSON.parse(smsUser.sms_type);
  860. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  861. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  862. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  863. // const sms = new SMS(this.ctx);
  864. // const tenderName = await sms.contentChange(tenderInfo.name);
  865. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  866. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  867. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  868. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  869. // sms.send(smsUser.auth_mobile, content);
  870. // }
  871. // }
  872. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  873. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  874. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return x.user_id == audit.aid; }), 'ass_user_id');
  875. users.push(audit.aid);
  876. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  877. qi: stageInfo.order,
  878. code: shenpiUrl,
  879. });
  880. // 微信模板通知
  881. const wechatData = {
  882. wap_url: shenpiUrl,
  883. qi: stageInfo.order,
  884. status: wxConst.status.check,
  885. tips: wxConst.tips.check,
  886. code: this.ctx.session.sessionProject.code,
  887. };
  888. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  889. // 上报/审批 - 检查三方特殊推送
  890. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  891. await transaction.commit();
  892. // 通知发送 - 第三方更新
  893. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  894. const base_data = {
  895. tid: this.ctx.tender.id,
  896. sid: stageId,
  897. op: 'update',
  898. };
  899. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  900. base_data.op = 'update';
  901. base_data.sid = -1;
  902. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  903. }
  904. } catch (err) {
  905. await transaction.rollback();
  906. throw err;
  907. }
  908. }
  909. /**
  910. * 审批撤回
  911. * @param {Number} stageId - 标段id
  912. * @param {Number} times - 第几次审批
  913. * @return {Promise<void>}
  914. */
  915. async checkCancel(stageId, times = 1) {
  916. // 分4种情况,根据ctx.cancancel值判断:
  917. // 1.原报发起撤回,当前流程删除,并回到待上报
  918. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  919. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  920. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  921. const transaction = await this.db.beginTransaction();
  922. const time = new Date();
  923. try {
  924. const ledgerTp = await this._updateTender(transaction);
  925. let newTimes, newOrder, newSaid, oldTimes, oldOrder, oldSaid = '';
  926. if (this.ctx.stage.cancancel === 1) {
  927. // 原报撤回,判断是否为多次,多次则为退回状态
  928. // 整理当前流程审核人状态更新
  929. const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  930. // // 审批人变成待审批状态
  931. await transaction.update(this.tableName, {
  932. id: curAudit.id,
  933. status: auditConst.status.uncheck,
  934. begin_time: null,
  935. opinion: null,
  936. });
  937. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  938. // 计算并合同支付最终数据
  939. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  940. const stageTp = {
  941. contract_tp: tpData.contract_tp,
  942. qc_tp: tpData.qc_tp,
  943. positive_qc_tp: tpData.positive_qc_tp,
  944. negative_qc_tp: tpData.negative_qc_tp,
  945. yf_tp: yfPay.tp,
  946. sf_tp: sfPay.tp,
  947. };
  948. this.ctx.stage.tp_history.push({ times, order: curAudit.order, ...stageTp });
  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),
  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 });
  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: times - 1, order: curAudit.order, ...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. };