stage_audit.js 78 KB

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