stage_audit.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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`, g.`sort` ' +
  82. 'FROM ?? AS la, ?? AS pa, (SELECT `aid`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `sid` = ? AND `times` = ? GROUP BY `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];
  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. }
  242. /**
  243. * 开始审批
  244. *
  245. * @param {Number} stageId - 期id
  246. * @param {Number} times - 第几次审批
  247. * @return {Promise<boolean>}
  248. */
  249. async start(stageId, times = 1) {
  250. const audit = await this.getDataByCondition({ sid: stageId, times, order: 1 });
  251. if (!audit) {
  252. if(this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
  253. throw '请联系管理员添加审批人';
  254. } else {
  255. throw '请先选择审批人,再上报数据';
  256. }
  257. }
  258. const transaction = await this.db.beginTransaction();
  259. try {
  260. await this._updateTender(transaction);
  261. await transaction.update(this.tableName, {
  262. id: audit.id,
  263. status: auditConst.status.checking,
  264. begin_time: new Date(),
  265. });
  266. // 计算原报最终数据
  267. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  268. // 复制一份下一审核人数据
  269. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  270. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  271. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  272. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  273. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  274. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  275. // 更新期数据
  276. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  277. this.ctx.stage.tp_history.push({
  278. times: this.ctx.stage.curTimes,
  279. order: 0,
  280. contract_tp: tpData.contract_tp,
  281. qc_tp: tpData.qc_tp,
  282. positive_qc_tp: tpData.positive_qc_tp,
  283. negative_qc_tp: tpData.negative_qc_tp,
  284. yf_tp: yfPay.tp,
  285. sf_tp: sfPay.tp,
  286. });
  287. await transaction.update(this.ctx.service.stage.tableName, {
  288. id: stageId,
  289. status: auditConst.status.checking,
  290. contract_tp: tpData.contract_tp,
  291. qc_tp: tpData.qc_tp,
  292. positive_qc_tp: tpData.positive_qc_tp,
  293. negative_qc_tp: tpData.negative_qc_tp,
  294. yf_tp: yfPay.tp,
  295. sf_tp: sfPay.tp,
  296. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  297. cache_time_r: this.ctx.stage.cache_time_l,
  298. });
  299. // 多人协同,取消下一审批人存在的锁定
  300. await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, audit.aid, transaction);
  301. // 添加短信通知-需要审批提醒功能
  302. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  303. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  304. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return x.user_id === audit.aid }), 'ass_user_id');
  305. users.push(audit.aid);
  306. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  307. qi: stageInfo.order,
  308. code: shenpiUrl,
  309. });
  310. // 微信模板通知
  311. const wechatData = {
  312. wap_url: shenpiUrl,
  313. qi: stageInfo.order,
  314. status: wxConst.status.check,
  315. tips: wxConst.tips.check,
  316. code: this.ctx.session.sessionProject.code,
  317. };
  318. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  319. // 上报/审批 - 检查三方特殊推送
  320. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  321. // todo 更新标段tender状态 ?
  322. await transaction.commit();
  323. // 通知发送 - 第三方更新
  324. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  325. const base_data = {
  326. tid: this.ctx.tender.id,
  327. sid: stageId,
  328. op: 'update',
  329. };
  330. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  331. base_data.op = 'update';
  332. base_data.sid = -1;
  333. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  334. }
  335. } catch (err) {
  336. await transaction.rollback();
  337. throw err;
  338. }
  339. return true;
  340. }
  341. async _checked(pid, stageId, checkData, times) {
  342. const time = new Date();
  343. // 整理当前流程审核人状态更新
  344. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  345. if (!audit) {
  346. throw '审核数据错误';
  347. }
  348. const nextAudit = await this.getDataByCondition({ sid: stageId, times, order: audit.order + 1 });
  349. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  350. const transaction = await this.db.beginTransaction();
  351. try {
  352. await this._updateTender(transaction);
  353. // 添加推送
  354. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  355. const auditors = await this.getAuditGroupByListWithOwner(stageId, times);
  356. const defaultNoticeRecord = {
  357. pid,
  358. type: pushType.stage,
  359. status: auditConst.status.checked,
  360. content: noticeContent,
  361. };
  362. const records = [];
  363. auditors.forEach(audit => {
  364. records.push({
  365. uid: audit.aid,
  366. ...defaultNoticeRecord,
  367. });
  368. });
  369. this.ctx.stage.userAssists.forEach(u => {
  370. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  371. });
  372. this.ctx.stage.auditAssists.forEach(u => {
  373. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  374. });
  375. await transaction.insert('zh_notice', records);
  376. await transaction.update(this.tableName, {
  377. id: audit.id,
  378. status: checkData.checkType,
  379. opinion: checkData.opinion,
  380. end_time: time,
  381. });
  382. // 计算并合同支付最终数据
  383. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  384. this.ctx.stage.tp_history.push({
  385. times,
  386. order: audit.order,
  387. contract_tp: tpData.contract_tp,
  388. qc_tp: tpData.qc_tp,
  389. positive_qc_tp: tpData.positive_qc_tp,
  390. negative_qc_tp: tpData.negative_qc_tp,
  391. yf_tp: yfPay.tp,
  392. sf_tp: sfPay.tp,
  393. });
  394. // 无下一审核人表示,审核结束
  395. if (nextAudit) {
  396. // 复制一份下一审核人数据
  397. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  398. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  399. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  400. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  401. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  402. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  403. // 流程至下一审批人
  404. await transaction.update(this.tableName, {
  405. id: nextAudit.id,
  406. status: auditConst.status.checking,
  407. begin_time: time,
  408. });
  409. // 同步 期信息
  410. await transaction.update(this.ctx.service.stage.tableName, {
  411. id: stageId,
  412. status: auditConst.status.checking,
  413. contract_tp: tpData.contract_tp,
  414. qc_tp: tpData.qc_tp,
  415. positive_qc_tp: tpData.positive_qc_tp,
  416. negative_qc_tp: tpData.negative_qc_tp,
  417. yf_tp: yfPay.tp,
  418. sf_tp: sfPay.tp,
  419. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  420. cache_time_r: this.ctx.stage.cache_time_l,
  421. });
  422. // 多人协同,取消下一审批人存在的锁定
  423. await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, nextAudit.aid, transaction);
  424. // 添加短信通知-需要审批提醒功能
  425. const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  426. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  427. const users = this._.map(this.ctx.auditAssists.filter(x => { return x.user_id === nextAudit.id; }), 'ass_user_id');
  428. users.push(nextAudit.aid);
  429. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  430. qi: stageInfo.order,
  431. code: shenpiUrl,
  432. });
  433. // 微信模板通知
  434. const wechatData = {
  435. wap_url: shenpiUrl,
  436. qi: stageInfo.order,
  437. status: wxConst.status.check,
  438. tips: wxConst.tips.check,
  439. code: this.ctx.session.sessionProject.code,
  440. };
  441. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  442. } else {
  443. await this.ctx.service.tenderTag.saveTenderTag(this.ctx.tender.id, {stage_time: new Date()}, transaction);
  444. const his_id = await this.ctx.service.ledgerHistory.checkBackupLedgerHistory(this.ctx.stage.tid, this.ctx.stage.id);
  445. // 本期结束
  446. // 生成截止本期数据 final数据
  447. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  448. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  449. await this.ctx.service.stageChangeFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  450. // 同步 期信息
  451. await transaction.update(this.ctx.service.stage.tableName, {
  452. id: stageId,
  453. status: checkData.checkType,
  454. contract_tp: tpData.contract_tp,
  455. qc_tp: tpData.qc_tp,
  456. positive_qc_tp: tpData.positive_qc_tp,
  457. negative_qc_tp: tpData.negative_qc_tp,
  458. yf_tp: yfPay.tp,
  459. sf_tp: sfPay.tp,
  460. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  461. cache_time_r: this.ctx.stage.cache_time_l,
  462. his_id,
  463. });
  464. // 添加短信通知-审批通过提醒功能
  465. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  466. const auditList = await this.getAuditors(stageId, stageInfo.times);
  467. 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')));
  468. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  469. qi: stageInfo.order,
  470. status: SmsAliConst.status.success,
  471. });
  472. // 微信模板通知
  473. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  474. const wechatData = {
  475. wap_url: shenpiUrl,
  476. qi: stageInfo.order,
  477. status: wxConst.status.success,
  478. tips: wxConst.tips.success,
  479. code: this.ctx.session.sessionProject.code,
  480. };
  481. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  482. // 审批通过 - 检查三方特殊推送
  483. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.checked);
  484. }
  485. // 上报/审批 - 检查三方特殊推送
  486. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  487. await transaction.commit();
  488. } catch (err) {
  489. await transaction.rollback();
  490. throw err;
  491. }
  492. }
  493. async _checkNo(pid, stageId, checkData, times) {
  494. const time = new Date();
  495. // 整理当前流程审核人状态更新
  496. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  497. if (!audit) {
  498. throw '审核数据错误';
  499. }
  500. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  501. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  502. const sqlParam = [this.tableName, stageId, times];
  503. const auditors = await this.db.query(sql, sqlParam);
  504. let order = 1;
  505. for (const a of auditors) {
  506. a.times = times + 1;
  507. a.order = order;
  508. a.status = auditConst.status.uncheck;
  509. order++;
  510. }
  511. const transaction = await this.db.beginTransaction();
  512. try {
  513. await this._updateTender(transaction);
  514. // 添加推送
  515. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  516. const defaultNoticeRecord = {
  517. pid,
  518. type: pushType.stage,
  519. status: auditConst.status.checkNo,
  520. content: noticeContent,
  521. };
  522. const records = [
  523. {
  524. uid: this.ctx.stage.user_id,
  525. ...defaultNoticeRecord,
  526. },
  527. ];
  528. auditors.forEach(audit => {
  529. records.push({
  530. uid: audit.aid,
  531. ...defaultNoticeRecord,
  532. });
  533. });
  534. this.ctx.stage.userAssists.forEach(u => {
  535. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  536. });
  537. this.ctx.stage.auditAssists.forEach(u => {
  538. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  539. });
  540. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  541. // 计算并合同支付最终数据
  542. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  543. this.ctx.stage.tp_history.push({
  544. times,
  545. order: audit.order,
  546. contract_tp: tpData.contract_tp,
  547. qc_tp: tpData.qc_tp,
  548. positive_qc_tp: tpData.positive_qc_tp,
  549. negative_qc_tp: tpData.negative_qc_tp,
  550. yf_tp: yfPay.tp,
  551. sf_tp: sfPay.tp,
  552. });
  553. await transaction.update(this.tableName, {
  554. id: audit.id,
  555. status: checkData.checkType,
  556. opinion: checkData.opinion,
  557. end_time: time,
  558. });
  559. // 同步 期信息
  560. await transaction.update(this.ctx.service.stage.tableName, {
  561. id: stageId,
  562. status: checkData.checkType,
  563. contract_tp: tpData.contract_tp,
  564. qc_tp: tpData.qc_tp,
  565. positive_qc_tp: tpData.positive_qc_tp,
  566. negative_qc_tp: tpData.negative_qc_tp,
  567. times: times + 1,
  568. yf_tp: yfPay.tp,
  569. sf_tp: sfPay.tp,
  570. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  571. cache_time_r: this.ctx.stage.cache_time_l,
  572. });
  573. // 拷贝新一次审核流程列表
  574. await transaction.insert(this.tableName, auditors);
  575. // 计算该审批人最终数据
  576. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  577. // 复制一份最新数据给原报
  578. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  579. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  580. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  581. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  582. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  583. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  584. // 锁定本人数据,保留锁定数据相关确认状态
  585. await this.ctx.service.stageAuditAss.lockConfirm4CheckNo(this.ctx.stage, audit.aid, auditors, transaction);
  586. // 添加短信通知-审批退回提醒功能
  587. // const mobile_array = [];
  588. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  589. const auditList = await this.getAuditors(stageId, stageInfo.times);
  590. 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')));
  591. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  592. qi: stageInfo.order,
  593. status: SmsAliConst.status.back,
  594. });
  595. // 微信模板通知
  596. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  597. const wechatData = {
  598. wap_url: shenpiUrl,
  599. qi: stageInfo.order,
  600. status: wxConst.status.back,
  601. tips: wxConst.tips.back,
  602. code: this.ctx.session.sessionProject.code,
  603. };
  604. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  605. // 上报/审批 - 检查三方特殊推送
  606. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  607. await transaction.commit();
  608. } catch (err) {
  609. await transaction.rollback();
  610. throw err;
  611. }
  612. }
  613. async _checkNoPre(pid, stageId, checkData, times) {
  614. const time = new Date();
  615. // 整理当前流程审核人状态更新
  616. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  617. if (!audit || audit.order <= 1) {
  618. throw '审核数据错误';
  619. }
  620. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  621. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  622. const auditors2 = await this.getAuditGroupByList(stageId, times);
  623. const auditorIndex = await auditors2.findIndex(function(item) {
  624. return item.aid === audit.aid;
  625. });
  626. const preAuditor = auditors2[auditorIndex - 1];
  627. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  628. const transaction = await this.db.beginTransaction();
  629. try {
  630. await this._updateTender(transaction);
  631. // 添加推送
  632. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  633. const defaultNoticeRecord = {
  634. pid,
  635. type: pushType.stage,
  636. status: auditConst.status.checkNoPre,
  637. content: noticeContent,
  638. };
  639. const records = [
  640. {
  641. uid: this.ctx.stage.user_id,
  642. ...defaultNoticeRecord
  643. },
  644. ];
  645. auditors2.forEach(audit => {
  646. records.push({
  647. uid: audit.aid,
  648. ...defaultNoticeRecord
  649. });
  650. });
  651. this.ctx.stage.userAssists.forEach(u => {
  652. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  653. });
  654. this.ctx.stage.auditAssists.forEach(u => {
  655. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  656. });
  657. await transaction.insert('zh_notice', records);
  658. // 计算并合同支付最终数据
  659. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  660. this.ctx.stage.tp_history.push({
  661. times,
  662. order: audit.order,
  663. contract_tp: tpData.contract_tp,
  664. qc_tp: tpData.qc_tp,
  665. positive_qc_tp: tpData.positive_qc_tp,
  666. negative_qc_tp: tpData.negative_qc_tp,
  667. yf_tp: yfPay.tp,
  668. sf_tp: sfPay.tp,
  669. });
  670. // 同步 期信息
  671. await transaction.update(this.ctx.service.stage.tableName, {
  672. id: stageId,
  673. contract_tp: tpData.contract_tp,
  674. qc_tp: tpData.qc_tp,
  675. positive_qc_tp: tpData.positive_qc_tp,
  676. negative_qc_tp: tpData.negative_qc_tp,
  677. times,
  678. yf_tp: yfPay.tp,
  679. sf_tp: sfPay.tp,
  680. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  681. cache_time_r: this.ctx.stage.cache_time_l,
  682. });
  683. await transaction.update(this.tableName, {
  684. id: audit.id,
  685. status: checkData.checkType,
  686. opinion: checkData.opinion,
  687. end_time: time,
  688. });
  689. // 顺移气候审核人流程顺序
  690. this.initSqlBuilder();
  691. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  692. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  693. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  694. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  695. const data = await transaction.query(sql, sqlParam);
  696. // 上一审批人,当前审批人 再次添加至流程
  697. const newAuditors = [];
  698. newAuditors.push({
  699. tid: audit.tid,
  700. sid: audit.sid,
  701. aid: preAuditor.aid,
  702. times: audit.times,
  703. order: audit.order + 1,
  704. status: auditConst.status.checking,
  705. begin_time: time,
  706. });
  707. newAuditors.push({
  708. tid: audit.tid,
  709. sid: audit.sid,
  710. aid: audit.aid,
  711. times: audit.times,
  712. order: audit.order + 2,
  713. status: auditConst.status.uncheck,
  714. });
  715. await transaction.insert(this.tableName, newAuditors);
  716. // 计算该审批人最终数据
  717. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  718. // 复制一份最新数据给下一人
  719. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  720. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  721. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  722. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  723. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  724. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  725. // 锁定本人数据,保留锁定数据相关确认状态
  726. await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, audit.aid, preAuditor.aid, transaction);
  727. // 同步 期信息
  728. await transaction.update(this.ctx.service.stage.tableName, {
  729. id: stageId,
  730. status: checkData.checkType,
  731. cache_time_r: this.ctx.stage.cache_time_l,
  732. });
  733. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  734. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  735. const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return x.user_id === preAuditor.aid}), 'ass_user_id');
  736. user.push(preAuditor.aid);
  737. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  738. qi: stageInfo.order,
  739. code: shenpiUrl,
  740. });
  741. // 微信模板通知
  742. const wechatData = {
  743. wap_url: shenpiUrl,
  744. qi: stageInfo.order,
  745. status: wxConst.status.check,
  746. tips: wxConst.tips.check,
  747. code: this.ctx.session.sessionProject.code,
  748. };
  749. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  750. // 上报/审批 - 检查三方特殊推送
  751. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  752. await transaction.commit();
  753. } catch (err) {
  754. await transaction.rollback();
  755. throw err;
  756. }
  757. }
  758. /**
  759. * 审批
  760. * @param {Number} stageId - 标段id
  761. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  762. * @param {Number} times - 第几次审批
  763. * @return {Promise<void>}
  764. */
  765. async check(stageId, checkData, times = 1) {
  766. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  767. throw '提交数据错误';
  768. }
  769. // // 整理当前流程审核人状态更新
  770. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  771. // if (!audit) {
  772. // throw '审核数据错误';
  773. // }
  774. // const time = new Date();
  775. const pid = this.ctx.session.sessionProject.id;
  776. switch (checkData.checkType) {
  777. case auditConst.status.checked:
  778. await this._checked(pid, stageId, checkData, times);
  779. break;
  780. case auditConst.status.checkNo:
  781. await this._checkNo(pid, stageId, checkData, times);
  782. break;
  783. case auditConst.status.checkNoPre:
  784. await this._checkNoPre(pid, stageId, checkData, times);
  785. break;
  786. default:
  787. throw '无效审批操作';
  788. }
  789. // 通知发送 - 第三方更新
  790. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  791. const base_data = {
  792. tid: this.ctx.tender.id,
  793. sid: stageId,
  794. op: 'update',
  795. };
  796. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  797. base_data.op = 'update';
  798. base_data.sid = -1;
  799. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  800. }
  801. }
  802. /**
  803. * 审批
  804. * @param {Number} stageId - 标段id
  805. * @param {Number} times - 第几次审批
  806. * @return {Promise<void>}
  807. */
  808. async checkAgain(stageId, times = 1) {
  809. const time = new Date();
  810. // 整理当前流程审核人状态更新
  811. const audit = (
  812. await this.getAllDataByCondition({
  813. where: { sid: stageId, times },
  814. orders: [['order', 'desc']],
  815. limit: 1,
  816. offset: 0,
  817. })
  818. )[0];
  819. if (!audit || audit.order < 1) {
  820. throw '审核数据错误';
  821. }
  822. const transaction = await this.db.beginTransaction();
  823. try {
  824. // 当前审批人2次添加至流程中
  825. const newAuditors = [];
  826. newAuditors.push({
  827. tid: audit.tid,
  828. sid: audit.sid,
  829. aid: audit.aid,
  830. times: audit.times,
  831. order: audit.order + 1,
  832. status: auditConst.status.checkAgain,
  833. begin_time: time,
  834. end_time: time,
  835. opinion: '',
  836. });
  837. newAuditors.push({
  838. tid: audit.tid,
  839. sid: audit.sid,
  840. aid: audit.aid,
  841. times: audit.times,
  842. order: audit.order + 2,
  843. status: auditConst.status.checking,
  844. begin_time: time,
  845. });
  846. await transaction.insert(this.tableName, newAuditors);
  847. // 复制一份最新数据给下一人
  848. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  849. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  850. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  851. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  852. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  853. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  854. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  855. // 同步 期信息
  856. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === audit.order });
  857. this.ctx.stage.tp_history.push({
  858. times,
  859. order: audit.order + 1,
  860. contract_tp: his.contract_tp,
  861. qc_tp: his.qc_tp,
  862. yf_tp: his.yf_tp,
  863. sf_tp: his.sf_tp,
  864. });
  865. await transaction.update(this.ctx.service.stage.tableName, {
  866. id: stageId,
  867. status: auditConst.status.checking,
  868. cache_time_r: this.ctx.stage.cache_time_l,
  869. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  870. });
  871. // 已经引用到本期的单价变更,全部取消
  872. await this.ctx.service.revisePrice.cancelPriceUsed(this.ctx.stage, transaction);
  873. // 重算所有单价变更
  874. const priceCalc = new RevisePrice(this.ctx);
  875. await priceCalc.stageCheckAgainPriceChange(this.ctx.stage, audit.order + 2, transaction);
  876. // 添加短信通知-需要审批提醒功能
  877. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  878. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  879. // const smsType = JSON.parse(smsUser.sms_type);
  880. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  881. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  882. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  883. // const sms = new SMS(this.ctx);
  884. // const tenderName = await sms.contentChange(tenderInfo.name);
  885. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  886. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  887. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  888. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  889. // sms.send(smsUser.auth_mobile, content);
  890. // }
  891. // }
  892. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  893. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  894. const users = this._.map(this.ctx.stage.auditAssist.filter(x => { return x.user_id == audit.aid; }), 'ass_user_id');
  895. users.push(audit.aid);
  896. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  897. qi: stageInfo.order,
  898. code: shenpiUrl,
  899. });
  900. // 微信模板通知
  901. const wechatData = {
  902. wap_url: shenpiUrl,
  903. qi: stageInfo.order,
  904. status: wxConst.status.check,
  905. tips: wxConst.tips.check,
  906. code: this.ctx.session.sessionProject.code,
  907. };
  908. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  909. // 上报/审批 - 检查三方特殊推送
  910. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  911. await transaction.commit();
  912. // 通知发送 - 第三方更新
  913. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  914. const base_data = {
  915. tid: this.ctx.tender.id,
  916. sid: stageId,
  917. op: 'update',
  918. };
  919. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  920. base_data.op = 'update';
  921. base_data.sid = -1;
  922. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  923. }
  924. } catch (err) {
  925. await transaction.rollback();
  926. throw err;
  927. }
  928. }
  929. /**
  930. * 获取审核人需要审核的期列表
  931. *
  932. * @param auditorId
  933. * @return {Promise<*>}
  934. */
  935. async getAuditStage(auditorId) {
  936. const sql =
  937. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  938. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  939. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  940. ' FROM ?? AS sa ' +
  941. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  942. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  943. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  944. ' ORDER BY sa.`begin_time` DESC';
  945. const sqlParam = [
  946. this.tableName,
  947. this.ctx.service.stage.tableName,
  948. this.ctx.service.tender.tableName,
  949. auditorId,
  950. auditConst.status.checking,
  951. auditorId,
  952. auditConst.status.checkNo,
  953. auditConst.status.checkNo,
  954. ];
  955. return await this.db.query(sql, sqlParam);
  956. }
  957. /**
  958. * 获取审核人审核的次数
  959. *
  960. * @param auditorId
  961. * @return {Promise<*>}
  962. */
  963. async getCountByChecked(auditorId) {
  964. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  965. }
  966. /**
  967. * 获取最近一次审批结束时间
  968. *
  969. * @param auditorId
  970. * @return {Promise<*>}
  971. */
  972. async getLastEndTimeByChecked(auditorId) {
  973. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  974. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
  975. const sqlParam = [this.tableName, auditorId];
  976. const result = await this.db.queryOne(sql, sqlParam);
  977. return result ? result.end_time : null;
  978. }
  979. /**
  980. * 获取 某时间后 审批进度 更新的期
  981. * @param {Number} pid - 查询标段
  982. * @param {Number} uid - 查询人
  983. * @param {Date} time - 查询时间
  984. * @return {Promise<*>}
  985. */
  986. async getNoticeStage(pid, uid, time) {
  987. let notice = await this.db.select('zh_notice', {
  988. where: { pid, type: pushType.stage, uid },
  989. orders: [['create_time', 'desc']],
  990. limit: 10,
  991. offset: 0,
  992. });
  993. notice = notice.map(v => {
  994. const extra = JSON.parse(v.content);
  995. delete v.content;
  996. return { ...v, ...extra };
  997. });
  998. return notice;
  999. }
  1000. /**
  1001. * 用于添加推送所需的content内容
  1002. * @param {Number} pid 项目id
  1003. * @param {Number} tid 台账id
  1004. * @param {Number} sid 期id
  1005. * @param {Number} uid 审核人id
  1006. */
  1007. async getNoticeContent(pid, tid, sid, uid, opinion = '') {
  1008. const noticeSql =
  1009. 'SELECT * FROM (SELECT ' +
  1010. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1011. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1012. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1013. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1014. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1015. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1016. const content = await this.db.query(noticeSql, noticeSqlParam);
  1017. if (content.length) {
  1018. content[0].opinion = opinion;
  1019. }
  1020. return content.length ? JSON.stringify(content[0]) : '';
  1021. }
  1022. /**
  1023. * 获取审核人流程列表
  1024. *
  1025. * @param auditorId
  1026. * @return {Promise<*>}
  1027. */
  1028. async getAuditGroupByList(stageId, times) {
  1029. const sql =
  1030. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  1031. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1032. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1033. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1034. return await this.db.query(sql, sqlParam);
  1035. }
  1036. /**
  1037. * 获取审核人流程列表
  1038. *
  1039. * @param auditorId
  1040. * @return {Promise<*>}
  1041. */
  1042. async getAuditGroupByListWithOwner(stageId, times) {
  1043. const result = await this.getAuditGroupByList(stageId, times);
  1044. const sql =
  1045. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1046. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1047. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1048. ' ON s.user_id = pa.id' +
  1049. ' WHERE s.id = ?';
  1050. const sqlParam = [times, stageId, stageId];
  1051. const user = await this.db.queryOne(sql, sqlParam);
  1052. result.unshift(user);
  1053. return result;
  1054. }
  1055. /**
  1056. * 复制上一期的审批人列表给最新一期
  1057. *
  1058. * @param transaction - 新增一期的事务
  1059. * @param {Object} preStage - 上一期
  1060. * @param {Object} newStage - 最新一期
  1061. * @return {Promise<*>}
  1062. */
  1063. async copyPreStageAuditors(transaction, preStage, newStage) {
  1064. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1065. const newAuditors = [];
  1066. for (const a of auditors) {
  1067. const na = {
  1068. tid: preStage.tid,
  1069. sid: newStage.id,
  1070. aid: a.aid,
  1071. times: newStage.times,
  1072. order: newAuditors.length + 1,
  1073. status: auditConst.status.uncheck,
  1074. };
  1075. newAuditors.push(na);
  1076. }
  1077. const result = await transaction.insert(this.tableName, newAuditors);
  1078. return (result.effectRows = auditors.length);
  1079. }
  1080. /**
  1081. * 移除审核人
  1082. *
  1083. * @param {Number} stageId - 期id
  1084. * @param {Number} status - 期状态
  1085. * @param {Number} status - 期次数
  1086. * @return {Promise<boolean>}
  1087. */
  1088. async getAuditorByStatus(stageId, status, times = 1) {
  1089. let auditor = null;
  1090. let sql = '';
  1091. let sqlParam = '';
  1092. switch (status) {
  1093. case auditConst.status.checking:
  1094. case auditConst.status.checked:
  1095. case auditConst.status.checkNoPre:
  1096. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1097. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1098. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1099. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1100. auditor = await this.db.queryOne(sql, sqlParam);
  1101. break;
  1102. case auditConst.status.checkNo:
  1103. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1104. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1105. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1106. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1107. auditor = await this.db.queryOne(sql, sqlParam);
  1108. break;
  1109. case auditConst.status.uncheck:
  1110. default:
  1111. break;
  1112. }
  1113. return auditor;
  1114. }
  1115. /**
  1116. * 取某一期已批准审核信息(报表用)
  1117. *
  1118. * @param {Number} stageId - 期id
  1119. * @param {Number} times - 期次数
  1120. * @return {Promise<boolean>}
  1121. */
  1122. async getStageAudit(stageId, times = 1) {
  1123. 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';
  1124. const sqlParam = [this.tableName, stageId, times];
  1125. const rst = await this.db.query(sql, sqlParam);
  1126. return rst;
  1127. }
  1128. /**
  1129. * 取待审批期列表(wap用)
  1130. *
  1131. * @param auditorId
  1132. * @return {Promise<*>}
  1133. */
  1134. async getAuditStageByWap(auditorId) {
  1135. const sql =
  1136. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1137. // ' 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`, ' +
  1138. ' s.*,' +
  1139. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1140. ' ti.`deal_info` ' +
  1141. ' FROM ?? AS sa' +
  1142. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1143. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1144. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1145. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1146. const sqlParam = [
  1147. this.tableName,
  1148. this.ctx.service.stage.tableName,
  1149. this.ctx.service.tender.tableName,
  1150. this.ctx.service.tenderInfo.tableName,
  1151. auditorId,
  1152. auditConst.status.checking,
  1153. ];
  1154. return await this.db.query(sql, sqlParam);
  1155. }
  1156. /**
  1157. * 删除 某期 某次 全审批流程
  1158. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1159. * @param {Number} sid - 标段id
  1160. * @param {Number} times - 第几次审批
  1161. * @param transaction - 删除事务
  1162. * @return {Promise<void>}
  1163. */
  1164. async _timesDelete(sid, times, transaction) {
  1165. // 审批流程
  1166. await transaction.delete(this.tableName, { sid, times });
  1167. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1168. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1169. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1170. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1171. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1172. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1173. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1174. await transaction.delete(this.ctx.serivce.stageAuditAss.tableName, { sid, times });
  1175. // 其他台账
  1176. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1177. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1178. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1179. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1180. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1181. }
  1182. /**
  1183. * 删除本次审批流程
  1184. * @param {Number} stageId - 标段id
  1185. * @param {Number} times - 第几次审批
  1186. * @return {Promise<void>}
  1187. */
  1188. async timesDelete() {
  1189. const transaction = await this.db.beginTransaction();
  1190. try {
  1191. // 删除最新一次数据
  1192. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1193. // 审批退回,未重新上报时,需删除最新两次数据
  1194. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1195. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1196. if (isCheckNo) {
  1197. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1198. }
  1199. // 添加上一次审批人
  1200. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1201. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1202. const auditors = await this.db.query(sql, sqlParam);
  1203. let order = 1;
  1204. for (const a of auditors) {
  1205. a.times = nowTimes;
  1206. a.order = order;
  1207. a.status = auditConst.status.uncheck;
  1208. order++;
  1209. }
  1210. await this._updateTender(transaction);
  1211. // 拷贝新一次审核流程列表
  1212. await transaction.insert(this.tableName, auditors);
  1213. // 计算缓存
  1214. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1215. // 计算并合同支付最终数据
  1216. const lastAudit = await this.getDataByCondition({
  1217. sid: this.ctx.stage.id,
  1218. times: nowTimes - 1,
  1219. status: auditConst.status.checkNo,
  1220. });
  1221. if (!lastAudit) throw '审批数据错误';
  1222. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1223. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1224. const yfPay = stagePay.find(function(x) {
  1225. return x.ptype === payConst.payType.yf;
  1226. });
  1227. const sfPay = stagePay.find(function(x) {
  1228. return x.ptype === payConst.payType.sf;
  1229. });
  1230. // 同步 期信息
  1231. const time = new Date();
  1232. await transaction.update(this.ctx.service.stage.tableName, {
  1233. id: this.ctx.stage.id,
  1234. status: auditConst.status.checkNo,
  1235. contract_tp: tpData.contract_tp,
  1236. qc_tp: tpData.qc_tp,
  1237. positive_qc_tp: tpData.positive_qc_tp,
  1238. negative_qc_tp: tpData.negative_qc_tp,
  1239. times: nowTimes,
  1240. yf_tp: yfPay ? yfPay.tp : null,
  1241. sf_tp: sfPay ? sfPay.tp : null,
  1242. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1243. cache_time_l: time,
  1244. cache_time_r: time,
  1245. });
  1246. await transaction.commit();
  1247. // 通知发送 - 第三方更新
  1248. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1249. const base_data = {
  1250. tid: this.ctx.tender.id,
  1251. sid: this.ctx.stage.id,
  1252. op: 'update',
  1253. };
  1254. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1255. base_data.op = 'update';
  1256. base_data.sid = -1;
  1257. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1258. }
  1259. } catch (err) {
  1260. await transaction.rollback();
  1261. throw err;
  1262. }
  1263. }
  1264. // 固定审批流-更新
  1265. async updateNewAuditList(stage, newIdList) {
  1266. const transaction = await this.db.beginTransaction();
  1267. try {
  1268. // 先删除旧的审批流,再添加新的
  1269. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1270. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times });
  1271. const newAuditors = [];
  1272. let order = 1;
  1273. for (const aid of newIdList) {
  1274. newAuditors.push({
  1275. tid: stage.tid, sid: stage.id, aid,
  1276. times: stage.times, order, status: auditConst.status.uncheck,
  1277. });
  1278. order++;
  1279. }
  1280. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1281. await transaction.commit();
  1282. } catch (err) {
  1283. await transaction.rollback();
  1284. throw err;
  1285. }
  1286. }
  1287. // 固定终审-更新
  1288. async updateLastAudit(stage, auditList, lastId) {
  1289. const transaction = await this.db.beginTransaction();
  1290. try {
  1291. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1292. const idList = this._.map(auditList, 'aid');
  1293. let order = idList.length + 1;
  1294. if (idList.indexOf(lastId) !== -1) {
  1295. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1296. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times, user_id: lastId });
  1297. const audit = this._.find(auditList, { 'aid': lastId });
  1298. // 顺移之后审核人流程顺序
  1299. await this._syncOrderByDelete(transaction, stage.id, audit.order, stage.times);
  1300. order = order - 1;
  1301. }
  1302. // 添加终审
  1303. const newAuditor = {
  1304. tid: stage.tid, sid: stage.id, aid: lastId,
  1305. times: stage.times, order, status: auditConst.status.uncheck,
  1306. };
  1307. await transaction.insert(this.tableName, newAuditor);
  1308. await transaction.commit();
  1309. } catch (err) {
  1310. await transaction.rollback();
  1311. throw err;
  1312. }
  1313. }
  1314. async getFinalAuditGroup(stageId, times) {
  1315. const sql =
  1316. '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 ' +
  1317. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1318. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1319. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1320. const result = await this.db.query(sql, sqlParam);
  1321. for (const r of result) {
  1322. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1323. r.status = auditor.status;
  1324. r.opinion = auditor.opinion;
  1325. r.begin_time = auditor.begin_time;
  1326. r.end_time = auditor.end_time;
  1327. }
  1328. return result;
  1329. }
  1330. async getNumByMonth(tid, startMonth, endMonth) {
  1331. 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 ?';
  1332. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1333. const result = await this.db.queryOne(sql, sqlParam);
  1334. return result ? result.num : 0;
  1335. }
  1336. }
  1337. return StageAudit;
  1338. };