stage_audit.js 93 KB

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