change.js 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. const fs = require('fs');
  11. const path = require('path');
  12. const changeConst = require('../const/change');
  13. const smsTypeConst = require('../const/sms_type');
  14. const SMS = require('../lib/sms');
  15. const SmsAliConst = require('../const/sms_alitemplate');
  16. const wxConst = require('../const/wechat_template');
  17. const pushType = require('../const/audit').pushType;
  18. const projectLogConst = require('../const/project_log');
  19. module.exports = app => {
  20. class Change extends app.BaseService {
  21. /**
  22. * 构造函数
  23. *
  24. * @param {Object} ctx - egg全局变量
  25. * @return {void}
  26. */
  27. constructor(ctx) {
  28. super(ctx);
  29. this.tableName = 'change';
  30. }
  31. /**
  32. * 查找数据
  33. *
  34. * @param {Object} data - 筛选表单中的get数据
  35. * @return {void}
  36. */
  37. searchFilter(data) {
  38. this.initSqlBuilder();
  39. // this.sqlBuilder.columns = ['id', 'username', 'real_name', 'create_time', 'last_login', 'login_ip',
  40. // 'group_id', 'token', 'can_login'];
  41. data.type = parseInt(data.status);
  42. if (data.keyword !== undefined) {
  43. switch (data.type) {
  44. // 用户名
  45. case 1:
  46. this.sqlBuilder.setAndWhere('username', {
  47. value: this.db.escape(data.keyword + '%'),
  48. operate: 'like',
  49. });
  50. break;
  51. // 姓名
  52. case 2:
  53. this.sqlBuilder.setAndWhere('real_name', {
  54. value: this.db.escape(data.keyword + '%'),
  55. operate: 'like',
  56. });
  57. break;
  58. // 联系电话
  59. case 3:
  60. this.sqlBuilder.setAndWhere('telephone', {
  61. value: this.db.escape(data.keyword + '%'),
  62. operate: 'like',
  63. });
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. // 办事处筛选
  70. if (data.office !== undefined && data.office !== '') {
  71. this.sqlBuilder.setAndWhere('office', {
  72. value: this.db.escape(data.office),
  73. operate: '=',
  74. });
  75. }
  76. }
  77. async add(tenderId, userId, code, plan_code, name) {
  78. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?))';
  79. const sqlParam = [this.tableName, tenderId, code, audit.flow.status.checked, code, audit.flow.status.checked];
  80. const codeCount = await this.db.queryOne(sql, sqlParam);
  81. const count = codeCount.count;
  82. if (count > 0) {
  83. throw '变更令号重复';
  84. }
  85. // 初始化事务
  86. this.transaction = await this.db.beginTransaction();
  87. let result = false;
  88. try {
  89. const cid = this.uuid.v4();
  90. const change = {
  91. cid,
  92. tid: tenderId,
  93. uid: userId,
  94. status: audit.flow.status.uncheck,
  95. times: 1,
  96. valid: true,
  97. in_time: new Date(),
  98. code,
  99. name,
  100. plan_code,
  101. // tp_decimal: this.ctx.tender.info.decimal.tp,
  102. // up_decimal: this.ctx.tender.info.decimal.up,
  103. };
  104. if (plan_code) {
  105. const planInfo = await this.ctx.service.changePlan.getDataByCondition({ tid: tenderId, code: plan_code });
  106. if (planInfo) {
  107. change.org_name = planInfo.org_name;
  108. change.peg = planInfo.peg;
  109. change.org_code = planInfo.new_code;
  110. change.new_code = planInfo.c_new_code;
  111. change.new_name = planInfo.design_name;
  112. const classIndex = planInfo.class && this._.indexOf(changeConst.className, planInfo.class) !== -1 ? this._.indexOf(changeConst.className, planInfo.class) : 0;
  113. change.class = classIndex ? classIndex : changeConst.class.A.value;
  114. const qualityIndex = planInfo.quality && this._.indexOf(changeConst.qualityName, planInfo.quality) !== -1 ? this._.indexOf(changeConst.qualityName, planInfo.quality) : 0;
  115. change.quality = qualityIndex ? qualityIndex : changeConst.quality.common.value;
  116. change.content = planInfo.reason.replace(/[\r\n]/g, '\r\n') + '\r\n' + planInfo.content.replace(/[\r\n]/g, '\r\n');
  117. }
  118. }
  119. const operate = await this.transaction.insert(this.tableName, change);
  120. if (operate.affectedRows <= 0) {
  121. throw '新建变更令数据失败';
  122. }
  123. // 把提交人信息添加到zh_change_audit
  124. const userInfo = await this.ctx.service.projectAccount.getDataById(userId);
  125. const changeaudit = [
  126. {
  127. tid: tenderId,
  128. cid,
  129. uid: userId,
  130. name: userInfo.name,
  131. jobs: userInfo.role,
  132. company: userInfo.company,
  133. times: 1,
  134. usite: 0,
  135. usort: 0,
  136. status: 2,
  137. },
  138. ];
  139. // 并把之前存在的变更令审批人添加到zh_change_audit
  140. // 先找出标段最近存在的变更令审批人的变更令info
  141. const changeInfo = await this.ctx.service.change.getHaveAuditLastInfo(tenderId);
  142. if (changeInfo) {
  143. // 再获取非原报审批人
  144. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
  145. let sort = 1;
  146. for (const audit of auditList) {
  147. if (audit.usite !== 0) {
  148. const oneaudit = {
  149. tid: tenderId,
  150. cid,
  151. uid: audit.uid,
  152. name: audit.name,
  153. jobs: audit.jobs,
  154. company: audit.company,
  155. times: 1,
  156. usite: audit.usite,
  157. usort: sort++,
  158. status: 1,
  159. };
  160. changeaudit.push(oneaudit);
  161. }
  162. }
  163. }
  164. await this.transaction.insert(this.ctx.service.changeAudit.tableName, changeaudit);
  165. result = change;
  166. this.transaction.commit();
  167. } catch (error) {
  168. console.log(error);
  169. // 回滚
  170. await this.transaction.rollback();
  171. }
  172. return result;
  173. }
  174. async getHaveAuditLastInfo(tenderId) {
  175. const sql = 'SELECT a.* FROM ?? as a LEFT JOIN ?? as b ON a.`cid` = b.`cid` WHERE a.`tid` = ? AND b.`usite` > 0 ORDER BY a.`in_time` DESC';
  176. const sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId];
  177. return await this.db.queryOne(sql, sqlParam);
  178. }
  179. async pendingDatas(tenderId, userId) {
  180. return await this.getAllDataByCondition({
  181. tid: tenderId,
  182. uid: userId,
  183. status: audit.flow.status.checking,
  184. });
  185. }
  186. async uncheckDatas(tenderId, userId) {
  187. return await this.getAllDataByCondition({
  188. tid: tenderId,
  189. uid: userId,
  190. status: audit.flow.status.uncheck,
  191. });
  192. }
  193. async checkingDatas(tenderId, userId) {
  194. return await this.getAllDataByCondition({
  195. tid: tenderId,
  196. uid: userId,
  197. status: audit.flow.status.checking,
  198. });
  199. }
  200. async checkedDatas(tenderId, userId) {
  201. return await this.getAllDataByCondition({
  202. tid: tenderId,
  203. uid: userId,
  204. status: audit.flow.status.checked,
  205. });
  206. }
  207. async checkNoDatas(tenderId, userId) {
  208. return await this.getAllDataByCondition({
  209. tid: tenderId,
  210. uid: userId,
  211. status: audit.flow.status.checkNo,
  212. });
  213. }
  214. async checkNoCount(tenderId, userId) {
  215. return await this.count({
  216. tid: tenderId,
  217. uid: userId,
  218. status: audit.flow.status.checkNo,
  219. });
  220. }
  221. /**
  222. * 获取变更令列表
  223. * @param {int} tenderId - 标段id
  224. * @param {int} status - 状态
  225. * @param {int} hadlimit - 分页
  226. * @return {object} list - 列表
  227. */
  228. async getListByStatus(tenderId, status = 0, hadlimit = 1, sortBy = '', orderBy = '') {
  229. let sql = '';
  230. let sqlParam = '';
  231. if (this.ctx.tender.isTourist && status === 0) {
  232. sql = 'SELECT a.* FROM ?? As a WHERE a.tid = ?';
  233. sqlParam = [this.tableName, tenderId];
  234. } else {
  235. switch (status) {
  236. case 0: // 包含你的所有变更令
  237. sql =
  238. 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND ' +
  239. '(a.uid = ? OR (a.status != ? AND a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid)) OR a.status = ? )';
  240. sqlParam = [
  241. this.tableName,
  242. tenderId,
  243. this.ctx.session.sessionUser.accountId,
  244. audit.flow.status.uncheck,
  245. this.ctx.service.changeAudit.tableName,
  246. this.ctx.session.sessionUser.accountId,
  247. audit.flow.status.checked,
  248. ];
  249. break;
  250. case 1: // 待处理(你的)
  251. sql = 'SELECT a.* FROM ?? as a WHERE cid in(SELECT b.cid FROM ?? as b WHERE tid = ? AND uid = ? AND status = ?)';
  252. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.flow.auditStatus.checking];
  253. break;
  254. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  255. sql =
  256. 'SELECT a.* FROM ?? AS a WHERE ' +
  257. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
  258. '(a.status = ? OR a.status = ? OR a.status = ?) AND a.tid = ?';
  259. sqlParam = [
  260. this.tableName,
  261. this.ctx.service.changeAudit.tableName,
  262. this.ctx.session.sessionUser.accountId,
  263. audit.flow.status.uncheck,
  264. audit.flow.status.back,
  265. audit.flow.status.revise,
  266. tenderId,
  267. ];
  268. break;
  269. case 2: // 进行中(所有的)
  270. case 4: // 终止(所有的)
  271. sql =
  272. 'SELECT a.* FROM ?? AS a WHERE ' +
  273. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) AND ' +
  274. 'a.status = ? AND a.tid = ?';
  275. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, status, tenderId];
  276. break;
  277. case 3: // 已完成(所有的)
  278. sql = 'SELECT a.* FROM ?? AS a WHERE a.status = ? AND a.tid = ?';
  279. sqlParam = [this.tableName, status, tenderId];
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. if (sortBy && orderBy) {
  286. if (sortBy === 'code') {
  287. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  288. } else {
  289. sql += ' ORDER BY a.in_time ' + orderBy;
  290. }
  291. } else {
  292. sql += ' ORDER BY a.in_time DESC';
  293. }
  294. if (hadlimit) {
  295. const limit = this.app.config.pageSize;
  296. const offset = limit * (this.ctx.page - 1);
  297. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  298. sql += ' LIMIT ' + limitString;
  299. }
  300. const list = await this.db.query(sql, sqlParam);
  301. return list;
  302. }
  303. /**
  304. * 获取变更令个数
  305. * @param {int} tenderId - 标段id
  306. * @param {int} status - 状态
  307. * @return {void}
  308. */
  309. async getCountByStatus(tenderId, status) {
  310. if (this.ctx.tender.isTourist && status === 0) {
  311. const sql5 = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
  312. const sqlParam5 = [this.tableName, tenderId];
  313. const result5 = await this.db.query(sql5, sqlParam5);
  314. return result5[0].count;
  315. }
  316. switch (status) {
  317. case 0: // 包含你的所有变更令
  318. const sql =
  319. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  320. '(a.uid = ? OR (a.status != ? AND a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid)) OR a.status = ? )';
  321. const sqlParam = [
  322. this.tableName,
  323. tenderId,
  324. this.ctx.session.sessionUser.accountId,
  325. audit.flow.status.uncheck,
  326. this.ctx.service.changeAudit.tableName,
  327. this.ctx.session.sessionUser.accountId,
  328. audit.flow.status.checked,
  329. ];
  330. const result = await this.db.query(sql, sqlParam);
  331. return result[0].count;
  332. case 1: // 待处理(你的)
  333. // return await this.ctx.service.changeAudit.count({
  334. // tid: tenderId,
  335. // uid: this.ctx.session.sessionUser.accountId,
  336. // status: 2,
  337. // });
  338. const sql6 = 'SELECT count(*) AS count FROM ?? as a WHERE cid in(SELECT b.cid FROM ?? as b WHERE tid = ? AND uid = ? AND status = ?)';
  339. const sqlParam6 = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.flow.auditStatus.checking];
  340. const result6 = await this.db.query(sql6, sqlParam6);
  341. return result6[0].count;
  342. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  343. const sql2 =
  344. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  345. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) ' +
  346. 'AND (a.status = ? OR a.status = ? OR a.status = ?) AND a.tid = ?';
  347. const sqlParam2 = [
  348. this.tableName,
  349. this.ctx.service.changeAudit.tableName,
  350. this.ctx.session.sessionUser.accountId,
  351. audit.flow.status.uncheck,
  352. audit.flow.status.back,
  353. audit.flow.status.revise,
  354. tenderId,
  355. ];
  356. const result2 = await this.db.query(sql2, sqlParam2);
  357. return result2[0].count;
  358. case 2: // 进行中(所有的)
  359. case 4: // 终止(所有的)
  360. const sql3 =
  361. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  362. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) AND a.status = ? AND a.tid = ?';
  363. const sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, status, tenderId];
  364. const result3 = await this.db.query(sql3, sqlParam3);
  365. return result3[0].count;
  366. case 3: // 已完成(所有的)
  367. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  368. const sqlParam4 = [this.tableName, status, tenderId];
  369. const result4 = await this.db.query(sql4, sqlParam4);
  370. return result4[0].count;
  371. default:
  372. break;
  373. }
  374. }
  375. async getTp(tenderId, status) {
  376. if (this.ctx.tender.isTourist && status === 0) {
  377. const sql5 = 'SELECT SUM(cast (total_price as decimal(18,6))) AS total_price FROM ?? WHERE tid = ?';
  378. const sqlParam5 = [this.tableName, tenderId];
  379. const result5 = await this.db.query(sql5, sqlParam5);
  380. return result5[0].total_price ? result5[0].total_price : 0;
  381. }
  382. switch (status) {
  383. case 0: // 包含你的所有变更令
  384. const sql =
  385. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE a.tid = ? AND ' +
  386. '(a.uid = ? OR (a.status != ? AND a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid)) OR a.status = ? )';
  387. const sqlParam = [
  388. this.tableName,
  389. tenderId,
  390. this.ctx.session.sessionUser.accountId,
  391. audit.flow.status.uncheck,
  392. this.ctx.service.changeAudit.tableName,
  393. this.ctx.session.sessionUser.accountId,
  394. audit.flow.status.checked,
  395. ];
  396. const result = await this.db.query(sql, sqlParam);
  397. return result[0].total_price ? result[0].total_price : 0;
  398. case 1: // 待处理(你的)
  399. const sql6 = 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? as a WHERE cid in(SELECT b.cid FROM ?? as b WHERE tid = ? AND uid = ? AND status = ?)';
  400. const sqlParam6 = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.flow.auditStatus.checking];
  401. const result6 = await this.db.query(sql6, sqlParam6);
  402. return result6[0].total_price ? result6[0].total_price : 0;
  403. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  404. const sql2 =
  405. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE ' +
  406. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) ' +
  407. 'AND (a.status = ? OR a.status = ? OR a.status = ?) AND a.tid = ?';
  408. const sqlParam2 = [
  409. this.tableName,
  410. this.ctx.service.changeAudit.tableName,
  411. this.ctx.session.sessionUser.accountId,
  412. audit.flow.status.uncheck,
  413. audit.flow.status.back,
  414. audit.flow.status.revise,
  415. tenderId,
  416. ];
  417. const result2 = await this.db.query(sql2, sqlParam2);
  418. return result2[0].total_price ? result2[0].total_price : 0;
  419. case 2: // 进行中(所有的)
  420. case 4: // 终止(所有的)
  421. const sql3 =
  422. 'SELECT SUM(cast (a.total_price as decimal(18,6))) AS total_price FROM ?? AS a WHERE ' +
  423. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) AND a.status = ? AND a.tid = ?';
  424. const sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, status, tenderId];
  425. const result3 = await this.db.query(sql3, sqlParam3);
  426. return result3[0].total_price ? result3[0].total_price : 0;
  427. case 3: // 已完成(所有的)
  428. const sql4 = 'SELECT SUM(cast (total_price as decimal(18,6))) AS total_price FROM ?? WHERE status = ? AND tid = ?';
  429. const sqlParam4 = [this.tableName, status, tenderId];
  430. const result4 = await this.db.query(sql4, sqlParam4);
  431. return result4[0].total_price ? result4[0].total_price : 0;
  432. default:
  433. break;
  434. }
  435. }
  436. /**
  437. * 获取变更令个数
  438. * @param {int} tenderId - 标段id
  439. * @param {int} quality - 变更性质
  440. * @return {void}
  441. */
  442. async getCountByQuality(tenderId, quality) {
  443. return await this.db.count(this.tableName, { tid: tenderId, quality });
  444. }
  445. /**
  446. * 获取变更令个数
  447. * @param {int} tenderId - 标段id
  448. * @param {int} status - 状态
  449. * @return {void}
  450. */
  451. async getCountByStatus2(tenderId, status) {
  452. if (status === audit.filter.status.uncheck) {
  453. const sql =
  454. 'SELECT count(*) AS count FROM ?? WHERE ' +
  455. 'tid = ? AND (status = ? OR status = ? OR status = ?)';
  456. const sqlParam = [
  457. this.tableName,
  458. tenderId,
  459. audit.flow.status.uncheck,
  460. audit.flow.status.back,
  461. audit.flow.status.revise,
  462. ];
  463. const result = await this.db.queryOne(sql, sqlParam);
  464. return result ? result.count : 0;
  465. }
  466. return await this.db.count(this.tableName, { tid: tenderId, status });
  467. }
  468. /**
  469. * 获取已批复变更令的总金额
  470. * @param {int} tenderId - 标段id
  471. * @param {int} quality - 变更性质
  472. * @return {void}
  473. */
  474. async getChangeTp(tenderId) {
  475. const sql = 'SELECT SUM(`total_price`) AS tp FROM ?? WHERE tid = ? AND status = ?';
  476. const sqlParam = [this.tableName, tenderId, audit.flow.status.checked];
  477. const result = await this.db.queryOne(sql, sqlParam);
  478. return result ? result.tp : 0;
  479. }
  480. /**
  481. * 上报或重新上报或保存修改功能
  482. * @param {int} postData - 表单提交的数据
  483. * @param {int} tenderId - 标段id
  484. * @return {void}
  485. */
  486. async save(postData, tenderId) {
  487. // 初始化事务
  488. this.transaction = await this.db.beginTransaction();
  489. let result = false;
  490. try {
  491. // 变更令信息
  492. const changeInfo = await this.getDataByCondition({ cid: postData.cid });
  493. // 该变更令原报人信息
  494. const lastUser = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 0);
  495. // 先删除本次原有的变更审批人和清单
  496. await this.ctx.service.changeAudit.deleteAuditData(this.transaction, changeInfo.cid, changeInfo.times);
  497. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid: changeInfo.cid });
  498. let change_status = false;
  499. // 获取变更令提交状态
  500. if (postData.changestatus !== undefined && parseInt(postData.changestatus) === 1) {
  501. change_status = true;
  502. // 更新原报人审批状态
  503. await this.transaction.update(this.ctx.service.changeAudit.tableName, {
  504. id: lastUser.id,
  505. status: audit.flow.auditStatus.checked,
  506. sin_time: new Date(),
  507. });
  508. }
  509. // 再插入postData里的变更审批人和清单
  510. if (postData.changeaudit !== undefined && postData.changeaudit !== '') {
  511. const changeAudit = postData.changeaudit.split(',');
  512. const insertCA = [];
  513. let uSite = 1;
  514. let uSort = parseInt(lastUser.usort) + 1;
  515. for (const [index, ca] of changeAudit.entries()) {
  516. const auditInfo = ca.split('/%/');
  517. const uStatus = change_status && index === 0 ? audit.flow.auditStatus.checking : audit.flow.auditStatus.uncheck;
  518. const sin_time = change_status && index === 0 ? new Date() : null;
  519. const caArray = {
  520. tid: tenderId,
  521. cid: changeInfo.cid,
  522. uid: auditInfo[0],
  523. name: auditInfo[1],
  524. jobs: auditInfo[2],
  525. company: auditInfo[3],
  526. times: changeInfo.times,
  527. usite: uSite,
  528. usort: uSort,
  529. status: uStatus,
  530. sin_time,
  531. };
  532. uSite++;
  533. uSort++;
  534. insertCA.push(caArray);
  535. // 添加短信通知-需要审批提醒功能
  536. if (change_status && index === 0) {
  537. const sms = new SMS(this.ctx);
  538. const code = await sms.contentChange(changeInfo.code);
  539. const shenpiUrl = await this.ctx.helper.urlToShort(
  540. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi'
  541. );
  542. await this.ctx.helper.sendAliSms(auditInfo[0], smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  543. biangeng: code,
  544. code: shenpiUrl,
  545. });
  546. // 微信模板通知
  547. const wechatData = {
  548. wap_url: shenpiUrl,
  549. status: wxConst.status.check,
  550. tips: wxConst.tips.check,
  551. code: this.ctx.session.sessionProject.code,
  552. c_name: changeInfo.name,
  553. };
  554. await this.ctx.helper.sendWechat(auditInfo[0], smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  555. }
  556. }
  557. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insertCA);
  558. }
  559. let changeList = [];
  560. if (postData.changelist !== undefined && postData.changelist !== '') {
  561. changeList = postData.changelist.split('^_^');
  562. }
  563. let changeWhiteList = [];
  564. if (postData.changewhitelist !== undefined && postData.changewhitelist !== '') {
  565. changeWhiteList = postData.changewhitelist.split('^_^');
  566. }
  567. changeList.push.apply(changeList, changeWhiteList);
  568. const insertCL = [];
  569. let total_price = 0;
  570. if (changeList.length > 0) {
  571. for (const cl of changeList) {
  572. const clInfo = cl.split('*;*');
  573. const clArray = {
  574. tid: tenderId,
  575. cid: changeInfo.cid,
  576. lid: clInfo[8],
  577. code: clInfo[0],
  578. name: clInfo[1],
  579. bwmx: clInfo[2],
  580. unit: clInfo[3],
  581. unit_price: clInfo[4],
  582. oamount: clInfo[5],
  583. camount: clInfo[6],
  584. samount: '',
  585. detail: clInfo[7],
  586. spamount: clInfo[6],
  587. xmj_code: clInfo[9] !== '' ? clInfo[9] : null,
  588. xmj_jldy: clInfo[10] !== '' ? clInfo[10] : null,
  589. gcl_id: clInfo[11] !== '' ? clInfo[11] : '',
  590. };
  591. if (clInfo[4] === '') {
  592. delete clArray.unit_price;
  593. }
  594. insertCL.push(clArray);
  595. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(clArray.unit_price, clArray.spamount, this.ctx.tender.info.decimal.tp));
  596. }
  597. await this.transaction.insert(this.ctx.service.changeAuditList.tableName, insertCL);
  598. }
  599. // 修改变更令基本数据
  600. const cArray = {
  601. code: postData.code,
  602. name: postData.name,
  603. peg: postData.peg,
  604. org_name: postData.org_name,
  605. org_code: postData.org_code,
  606. new_name: postData.new_name,
  607. new_code: postData.new_code,
  608. content: postData.content,
  609. basis: postData.basis,
  610. expr: postData.expr,
  611. memo: postData.memo,
  612. type: postData.type.join(','),
  613. class: postData.class,
  614. quality: postData.quality,
  615. company: postData.company,
  616. charge: postData.charge,
  617. w_code: postData.w_code,
  618. total_price,
  619. // tp_decimal: this.ctx.tender.info.decimal.tp,
  620. };
  621. const options = {
  622. where: {
  623. cid: changeInfo.cid,
  624. },
  625. };
  626. if (change_status) {
  627. cArray.tp_decimal = this.ctx.tender.info.decimal.tp;
  628. cArray.up_decimal = this.ctx.tender.info.decimal.up;
  629. cArray.status = audit.flow.status.checking;
  630. cArray.cin_time = Date.parse(new Date()) / 1000;
  631. }
  632. await this.transaction.update(this.tableName, cArray, options);
  633. await this.transaction.commit();
  634. result = true;
  635. } catch (error) {
  636. await this.transaction.rollback();
  637. result = false;
  638. }
  639. return result;
  640. }
  641. /**
  642. * 保存变更信息
  643. * @param {int} postData - 表单提交的数据
  644. * @param {int} tenderId - 标段id
  645. * @return {void}
  646. */
  647. async saveInfo(postData) {
  648. // 初始化事务
  649. const transaction = await this.db.beginTransaction();
  650. let result = false;
  651. try {
  652. const options = {
  653. where: {
  654. cid: this.ctx.change.cid,
  655. },
  656. };
  657. await transaction.update(this.tableName, postData, options);
  658. await transaction.commit();
  659. result = true;
  660. } catch (error) {
  661. await transaction.rollback();
  662. result = false;
  663. }
  664. return result;
  665. }
  666. /**
  667. * 审批通过
  668. * @param {Number} pid 项目id
  669. * @param {int} postData - 表单提交的数据
  670. * @param {int} changeData - 变更令的数据
  671. * @return {void}
  672. */
  673. async approvalSuccess(pid, postData, changeData) {
  674. // 初始化事务
  675. this.transaction = await this.db.beginTransaction();
  676. let result = false;
  677. try {
  678. // 获取所有审核人列表
  679. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  680. // console.log('auditors', auditors);
  681. // console.log('postData', postData);
  682. // 添加到消息推送表
  683. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId);
  684. const records = [];
  685. auditors.forEach(auditor => {
  686. records.push({
  687. pid,
  688. type: pushType.change,
  689. uid: auditor.uid,
  690. status: audit.flow.status.checked,
  691. content: noticeContent,
  692. });
  693. });
  694. await this.transaction.insert('zh_notice', records);
  695. // 设置审批人通过
  696. const audit_update = {
  697. id: postData.audit_id,
  698. sdesc: postData.sdesc,
  699. status: audit.flow.auditStatus.checked,
  700. sin_time: new Date(),
  701. };
  702. const change_update = {
  703. w_code: postData.w_code,
  704. status: audit.flow.status.checking,
  705. cin_time: Date.parse(new Date()) / 1000,
  706. };
  707. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  708. // 清单数据更新
  709. const bills_list = postData.bills_list.split(',');
  710. let total_price = 0;
  711. const tp_decimal = changeData.tp_decimal ? changeData.tp_decimal : this.ctx.tender.info.decimal.tp;
  712. for (const bl of bills_list) {
  713. const listInfo = bl.split('_');
  714. const lid = listInfo[0];
  715. const amount = listInfo[1];
  716. const changeListInfo = await this.ctx.service.changeAuditList.getDataById(lid);
  717. if (changeListInfo !== undefined) {
  718. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(changeListInfo.unit_price, amount, tp_decimal));
  719. const audit_amount = changeListInfo.audit_amount !== null && changeListInfo.audit_amount !== '' ? changeListInfo.audit_amount.split(',') : [];
  720. audit_amount.push(amount);
  721. const list_update = {
  722. id: lid,
  723. audit_amount: audit_amount.join(','),
  724. spamount: parseFloat(amount),
  725. };
  726. if (postData.audit_next_id === undefined) {
  727. list_update.samount = amount;
  728. }
  729. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  730. }
  731. }
  732. if (postData.audit_next_id === undefined) {
  733. // 变更令审批完成
  734. change_update.status = audit.flow.status.checked;
  735. change_update.p_code = postData.p_code;
  736. change_update.sin_time = Date.parse(new Date()) / 1000;
  737. change_update.is_revise = 0;
  738. await this.ctx.service.tenderTag.saveTenderTag(changeData.tid, { bgl_time: new Date() }, this.transaction);
  739. await this.ctx.service.changeAuditList.updateToLedger(this.transaction, changeData.tid, changeData.cid);
  740. // 添加短信通知-审批通过提醒功能
  741. // const mobile_array = [];
  742. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeData.cid, changeData.times);
  743. const users = this._.map(auditList, 'uid');
  744. const sms = new SMS(this.ctx);
  745. const code = await sms.contentChange(changeData.code);
  746. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), SmsAliConst.template.change_result, {
  747. biangeng: code,
  748. status: SmsAliConst.status.success,
  749. });
  750. // 微信模板通知
  751. const shenpiUrl = await this.ctx.helper.urlToShort(
  752. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi'
  753. );
  754. const wechatData = {
  755. wap_url: shenpiUrl,
  756. status: wxConst.status.success,
  757. tips: wxConst.tips.success,
  758. code: this.ctx.session.sessionProject.code,
  759. c_name: changeData.name,
  760. };
  761. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  762. } else {
  763. // 设置下一个审批人为审批状态
  764. const nextAudit_update = {
  765. id: postData.audit_next_id,
  766. status: audit.flow.auditStatus.checking,
  767. sin_time: new Date(),
  768. };
  769. await this.transaction.update(this.ctx.service.changeAudit.tableName, nextAudit_update);
  770. // 添加短信通知-需要审批提醒功能
  771. const nextAuditData = await this.ctx.service.changeAudit.getDataById(postData.audit_next_id);
  772. const sms = new SMS(this.ctx);
  773. const code = await sms.contentChange(changeData.code);
  774. const shenpiUrl = await this.ctx.helper.urlToShort(
  775. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi'
  776. );
  777. await this.ctx.helper.sendAliSms(nextAuditData.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  778. biangeng: code,
  779. code: shenpiUrl,
  780. });
  781. // 微信模板通知
  782. const wechatData = {
  783. wap_url: shenpiUrl,
  784. status: wxConst.status.check,
  785. tips: wxConst.tips.check,
  786. code: this.ctx.session.sessionProject.code,
  787. c_name: changeData.name,
  788. };
  789. await this.ctx.helper.sendWechat(nextAuditData.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  790. }
  791. change_update.total_price = total_price;
  792. const options = {
  793. where: {
  794. cid: postData.change_id,
  795. },
  796. };
  797. await this.transaction.update(this.tableName, change_update, options);
  798. await this.transaction.commit();
  799. result = true;
  800. } catch (error) {
  801. console.log(error);
  802. await this.transaction.rollback();
  803. result = false;
  804. }
  805. return result;
  806. }
  807. /**
  808. * 审批终止
  809. * @param {int} postData - 表单提交的数据
  810. * @return {void}
  811. */
  812. async approvalStop(postData) {
  813. // 初始化事务
  814. this.transaction = await this.db.beginTransaction();
  815. let result = false;
  816. try {
  817. // 设置审批人终止
  818. const audit_update = {
  819. id: postData.audit_id,
  820. sdesc: postData.sdesc,
  821. status: 4,
  822. sin_time: new Date(),
  823. };
  824. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  825. // 设置变更令终止
  826. const change_update = {
  827. w_code: postData.w_code,
  828. status: 4,
  829. cin_time: Date.parse(new Date()) / 1000,
  830. };
  831. const options = {
  832. where: {
  833. cid: postData.change_id,
  834. },
  835. };
  836. await this.transaction.update(this.tableName, change_update, options);
  837. await this.transaction.commit();
  838. result = true;
  839. } catch (error) {
  840. await this.transaction.rollback();
  841. result = false;
  842. }
  843. return result;
  844. }
  845. /**
  846. * 审批退回到原报人
  847. * @param {Number} pid 项目id
  848. * @param {int} postData - 表单提交的数据
  849. * @param {int} changeData - 变更令的数据
  850. * @return {void}
  851. */
  852. async approvalBack(pid, postData, changeData) {
  853. // 初始化事务
  854. this.transaction = await this.db.beginTransaction();
  855. let result = false;
  856. try {
  857. // 获取所有审核人列表
  858. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  859. // 添加到消息推送表
  860. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId);
  861. const records = [];
  862. auditors.forEach(auditor => {
  863. records.push({
  864. pid,
  865. type: pushType.change,
  866. uid: auditor.uid,
  867. status: audit.flow.status.backnew,
  868. content: noticeContent,
  869. });
  870. });
  871. await this.transaction.insert('zh_notice', records);
  872. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  873. // 设置审批人退回
  874. const audit_update = {
  875. id: postData.audit_id,
  876. sdesc: postData.sdesc,
  877. status: audit.flow.auditStatus.back,
  878. sin_time: new Date(),
  879. };
  880. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  881. // 新增新一次的审批人列表
  882. // 获取当前次数审批人列表
  883. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
  884. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 1, 0);
  885. let usort = lastauditInfo.usort + 1;
  886. const newTimes = changeInfo.times + 1;
  887. const insert_audit_array = [];
  888. for (const al of auditList) {
  889. const insert_audit = {
  890. tid: al.tid,
  891. cid: al.cid,
  892. uid: al.uid,
  893. name: al.name,
  894. jobs: al.jobs,
  895. company: al.company,
  896. times: newTimes,
  897. usite: al.usite,
  898. usort,
  899. status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking,
  900. };
  901. insert_audit_array.push(insert_audit);
  902. usort++;
  903. }
  904. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
  905. // 变更金额也退回
  906. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  907. where: { cid: changeInfo.cid },
  908. });
  909. let total_price = 0;
  910. const tp_decimal = changeData.tp_decimal ? changeData.tp_decimal : this.ctx.tender.info.decimal.tp;
  911. for (const cl of changeList) {
  912. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, tp_decimal));
  913. }
  914. // 设置变更令退回
  915. const change_update = {
  916. w_code: postData.w_code,
  917. status: audit.flow.status.back,
  918. times: newTimes,
  919. cin_time: Date.parse(new Date()) / 1000,
  920. total_price,
  921. tp_decimal: null,
  922. up_decimal: null,
  923. };
  924. const options = {
  925. where: {
  926. cid: postData.change_id,
  927. },
  928. };
  929. await this.transaction.update(this.tableName, change_update, options);
  930. await this.transaction.commit();
  931. result = true;
  932. const users = this._.map(insert_audit_array, 'uid');
  933. const sms = new SMS(this.ctx);
  934. const code = await sms.contentChange(changeData.code);
  935. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), SmsAliConst.template.change_result, {
  936. biangeng: code,
  937. status: SmsAliConst.status.back,
  938. });
  939. // 微信模板通知
  940. const shenpiUrl = await this.ctx.helper.urlToShort(
  941. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi'
  942. );
  943. const wechatData = {
  944. wap_url: shenpiUrl,
  945. status: wxConst.status.back,
  946. tips: wxConst.tips.back,
  947. code: this.ctx.session.sessionProject.code,
  948. c_name: changeInfo.name,
  949. };
  950. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  951. } catch (error) {
  952. await this.transaction.rollback();
  953. result = false;
  954. }
  955. return result;
  956. }
  957. /**
  958. * 审批退回到上一个审批人
  959. * @param {Number} pid 项目id
  960. * @param {int} postData - 表单提交的数据
  961. * @param {int} changeData - 变更令的数据
  962. * @return {void}
  963. */
  964. async approvalBackNew(pid, postData, changeData) {
  965. // 初始化事务
  966. this.transaction = await this.db.beginTransaction();
  967. let result = false;
  968. try {
  969. // 获取所有审核人列表
  970. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  971. // 添加到消息推送表
  972. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId);
  973. const records = [];
  974. auditors.forEach(auditor => {
  975. records.push({
  976. pid,
  977. type: pushType.change,
  978. uid: auditor.uid,
  979. status: audit.flow.status.back,
  980. content: noticeContent,
  981. });
  982. });
  983. await this.transaction.insert('zh_notice', records);
  984. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  985. // 设置审批人退回
  986. const audit_update = {
  987. id: postData.audit_id,
  988. sdesc: postData.sdesc,
  989. status: audit.flow.auditStatus.backnew,
  990. sin_time: new Date(),
  991. };
  992. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  993. // 获取当前审批人信息
  994. const auditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_id);
  995. // 获取当前次数审批人列表
  996. const auditList = await this.ctx.service.changeAudit.getNextAuditList(changeInfo.cid, auditInfo.usort);
  997. let usort = auditInfo.usort + 1;
  998. // 获取上一个审批人信息
  999. const lastauditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_last_id);
  1000. // 新增2个审批人到审批列表中
  1001. const insert_audit1 = {
  1002. tid: lastauditInfo.tid,
  1003. cid: lastauditInfo.cid,
  1004. uid: lastauditInfo.uid,
  1005. name: lastauditInfo.name,
  1006. jobs: lastauditInfo.jobs,
  1007. company: lastauditInfo.company,
  1008. times: lastauditInfo.times,
  1009. usite: lastauditInfo.usite,
  1010. usort,
  1011. status: audit.flow.auditStatus.checking,
  1012. sin_time: new Date(),
  1013. };
  1014. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  1015. usort++;
  1016. // 新增2个审批人到审批列表中
  1017. const insert_audit2 = {
  1018. tid: auditInfo.tid,
  1019. cid: auditInfo.cid,
  1020. uid: auditInfo.uid,
  1021. name: auditInfo.name,
  1022. jobs: auditInfo.jobs,
  1023. company: auditInfo.company,
  1024. times: auditInfo.times,
  1025. usite: auditInfo.usite,
  1026. usort,
  1027. status: audit.flow.auditStatus.uncheck,
  1028. };
  1029. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  1030. // 把接下未审批的审批人排序都加2
  1031. for (const al of auditList) {
  1032. const audit_update = {
  1033. id: al.id,
  1034. usort: al.usort + 2,
  1035. };
  1036. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  1037. }
  1038. // 审批列表数据也要回退
  1039. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  1040. where: { cid: changeInfo.cid },
  1041. });
  1042. let total_price = 0;
  1043. const tp_decimal = changeData.tp_decimal ? changeData.tp_decimal : this.ctx.tender.info.decimal.tp;
  1044. for (const cl of changeList) {
  1045. const audit_amount = cl.audit_amount.split(',');
  1046. const last_amount = audit_amount[audit_amount.length - 1];
  1047. audit_amount.splice(-1, 1);
  1048. const list_update = {
  1049. id: cl.id,
  1050. audit_amount: audit_amount.join(','),
  1051. spamount: parseFloat(last_amount),
  1052. };
  1053. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  1054. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  1055. }
  1056. // 设置变更令退回
  1057. const change_update = {
  1058. w_code: postData.w_code,
  1059. status: audit.flow.status.backnew,
  1060. cin_time: Date.parse(new Date()) / 1000,
  1061. total_price,
  1062. };
  1063. const options = {
  1064. where: {
  1065. cid: postData.change_id,
  1066. },
  1067. };
  1068. await this.transaction.update(this.tableName, change_update, options);
  1069. await this.transaction.commit();
  1070. result = true;
  1071. const sms = new SMS(this.ctx);
  1072. const code = await sms.contentChange(changeData.code);
  1073. const shenpiUrl = await this.ctx.helper.urlToShort(
  1074. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi'
  1075. );
  1076. await this.ctx.helper.sendAliSms(lastauditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  1077. biangeng: code,
  1078. code: shenpiUrl,
  1079. });
  1080. // 微信模板通知
  1081. const wechatData = {
  1082. wap_url: shenpiUrl,
  1083. status: wxConst.status.check,
  1084. tips: wxConst.tips.check,
  1085. code: this.ctx.session.sessionProject.code,
  1086. c_name: changeInfo.name,
  1087. };
  1088. await this.ctx.helper.sendWechat(lastauditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  1089. } catch (error) {
  1090. await this.transaction.rollback();
  1091. result = false;
  1092. }
  1093. return result;
  1094. }
  1095. /**
  1096. * 查询可用的变更令
  1097. * @param bills - 查询的清单
  1098. * @param pos - 查询的部位
  1099. * @return {Promise<*>} - 可用的变更令列表
  1100. */
  1101. async getValidChanges(tid, bills, pos) {
  1102. const self = this;
  1103. const getFilterPart = function(field, value) {
  1104. return value
  1105. ? field + ' = ' + self.db.escape(value)
  1106. : self.db.format("(?? = null or ?? = '')", [field, field]);
  1107. };
  1108. const timesLen = 100;
  1109. const filter = getFilterPart('cb.code', bills.b_code) +
  1110. ' And ' + getFilterPart('cb.name', bills.name) +
  1111. ' And ' + getFilterPart('cb.unit', bills.unit) +
  1112. ' And cb.`unit_price` = ' + this.db.escape(bills.unit_price) +
  1113. (pos ? getFilterPart('cb.bwmx', pos.name) : '');
  1114. const sql =
  1115. 'SELECT c.cid, c.code, c.name, c.w_code, c.p_code, c.peg, c.org_name, c.org_code, c.new_name, c.new_code,' +
  1116. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  1117. ' cb.id As cbid, cb.code As b_code, cb.name As b_name, cb.unit As b_unit, cb.samount As b_amount, cb.detail As b_detail, cb.bwmx As b_bwmx, cb.gcl_id, ' +
  1118. ' scb.used_amount' +
  1119. ' FROM ' + this.tableName + ' As c ' +
  1120. ' Left Join ' + this.ctx.service.changeAuditList.tableName + ' As cb On c.cid = cb.cid ' +
  1121. ' Left Join (' +
  1122. ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
  1123. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  1124. ' INNER JOIN (SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, cbid, sid ' +
  1125. ' FROM ' + this.ctx.service.stageChange.tableName +
  1126. ' WHERE tid = ?' +
  1127. ' GROUP BY cbid, sid' +
  1128. ' ) As MF' +
  1129. ' ON (sc.stimes * ' + timesLen + ' + sc.sorder) = MF.flow And sc.cbid = MF.cbid And sc.sid = MF.sid' +
  1130. ' GROUP BY sc.cbid' +
  1131. ' ) As scb ON cb.id = scb.cbid' +
  1132. ' WHERE c.tid = ? And c.status = ? And c.valid And ' + filter +
  1133. ' ORDER BY c.in_time';
  1134. const sqlParam = [tid, tid, audit.flow.status.checked];
  1135. const changes = await this.db.query(sql, sqlParam);
  1136. for (const c of changes) {
  1137. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  1138. ' FROM ?? As ca ' +
  1139. ' Left Join ?? As pa ' +
  1140. ' On ca.uid = pa.id ' +
  1141. ' Where ca.cid = ?';
  1142. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
  1143. c.attachments = await this.db.query(aSql, aSqlParam);
  1144. }
  1145. return changes;
  1146. }
  1147. /**
  1148. * 查询审批人可用的变更令
  1149. * @param bills - 查询的清单
  1150. * @param pos - 查询的部位
  1151. * @return {Promise<*>} - 可用的变更令列表
  1152. */
  1153. async getAuditValidChanges(tid, bills, pos, times, order) {
  1154. const timesLen = 100;
  1155. const filter =
  1156. 'cb.`code` = ' + this.db.escape(bills.b_code) +
  1157. ' And cb.`name` = ' + this.db.escape(bills.name) +
  1158. ' And cb.`unit` = ' + this.db.escape(bills.unit) +
  1159. ' And cb.`unit_price` = ' + this.db.escape(bills.unit_price) +
  1160. (pos ? ' And cb.`bwmx` = ' + this.db.escape(pos.name) : '');
  1161. const sql =
  1162. 'SELECT c.cid, c.code, c.name, c.w_code, c.p_code, c.peg, c.org_name, c.org_code, c.new_name, c.new_code,' +
  1163. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  1164. ' cb.id As cbid, cb.code As b_code, cb.name As b_name, cb.unit As b_unit, cb.samount As b_amount, cb.detail As b_detail, cb.bwmx As b_bwmx, cb.gcl_id, ' +
  1165. ' scb.used_amount' +
  1166. ' FROM ' + this.tableName + ' As c ' +
  1167. ' Left Join ' + this.ctx.service.changeAuditList.tableName + ' As cb On c.cid = cb.cid ' +
  1168. ' Left Join (' +
  1169. ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
  1170. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  1171. ' INNER JOIN (SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, cbid, sid ' +
  1172. ' FROM ' + this.ctx.service.stageChange.tableName +
  1173. ' WHERE tid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?)) ' +
  1174. ' GROUP BY cbid, sid' +
  1175. ' ) As MF' +
  1176. ' ON (sc.stimes * ' + timesLen + ' + sc.sorder) = MF.flow And sc.cbid = MF.cbid And sc.sid = MF.sid' +
  1177. ' GROUP BY sc.cbid' +
  1178. ' ) As scb ON cb.id = scb.cbid' +
  1179. ' WHERE c.tid = ? And c.status = ? And c.valid And ' + filter +
  1180. ' ORDER BY c.in_time';
  1181. const sqlParam = [tid, times, times, order, tid, audit.flow.status.checked];
  1182. const changes = await this.db.query(sql, sqlParam);
  1183. for (const c of changes) {
  1184. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  1185. ' FROM ?? As ca ' +
  1186. ' Left Join ?? As pa ' +
  1187. ' On ca.uid = pa.id ' +
  1188. ' Where ca.cid = ?';
  1189. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
  1190. c.attachments = await this.db.query(aSql, aSqlParam);
  1191. }
  1192. return changes;
  1193. }
  1194. /**
  1195. * 查询变更令 + 变更令执行
  1196. * @param tid
  1197. * @return {Promise<void>}
  1198. */
  1199. async getChangeAndUsedInfo(tid) {
  1200. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  1201. let filter;
  1202. if (lastStage.id === this.ctx.stage.id) {
  1203. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))', [
  1204. lastStage.order,
  1205. lastStage.order,
  1206. this.ctx.stage.curTimes,
  1207. this.ctx.stage.curTimes,
  1208. this.ctx.stage.curOrder,
  1209. ]);
  1210. } else {
  1211. if (lastStage.status === audit.stage.status.uncheck) {
  1212. filter = ' And s.order < ' + lastStage.order;
  1213. } else if (lastStage.status === audit.stage.status.checked) {
  1214. filter = '';
  1215. } else if (lastStage.status === audit.stage.status.checkNo) {
  1216. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And sChange.`stimes` <= ?))', [lastStage.order, lastStage.order, lastStage.times]);
  1217. } else {
  1218. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
  1219. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))', [
  1220. lastStage.order,
  1221. lastStage.order,
  1222. lastStage.times,
  1223. lastStage.times,
  1224. curAuditor.order - 1,
  1225. ]);
  1226. }
  1227. }
  1228. const sql =
  1229. 'SELECT C.*, Sum(U.utp) As used_tp, TRUNCATE(Sum(U.utp) / C.total_price * 100 + 0.005, 2) As used_pt' +
  1230. ' FROM ' + this.tableName + ' As C' +
  1231. ' LEFT JOIN (SELECT sc.tid, sc.cid, sc.cbid, IF(SUM(sc.qty) > 0, TRUNCATE(SUM(sc.qty) * cb.unit_price + ?, ?), TRUNCATE(SUM(sc.qty) * cb.unit_price - ?, ?)) As utp,' +
  1232. ' IF(SUM(sc.qty) > 0, TRUNCATE(SUM(sc.qty) * cb.unit_price + ?, ?), 0, ?)) As utp_po, IF(SUM(sc.qty) > 0, 0, TRUNCATE(SUM(sc.qty) * cb.unit_price - ?, ?)) As utp_ne' +
  1233. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  1234. ' INNER JOIN (' +
  1235. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid` ' +
  1236. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sChange ' +
  1237. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s' +
  1238. ' ON sChange.sid = s.id' +
  1239. ' WHERE sChange.tid = ?' + filter +
  1240. ' GROUP By `lid`, `pid`, `cbid`, `sid`' +
  1241. ' ) As m' +
  1242. ' ON sc.stimes = m.stimes And sc.sorder = m.sorder And sc.`cbid` = m.`cbid` AND sc.`sid` = m.`sid` And sc.`lid` = m.`lid` And sc.`pid` = m.`pid`' +
  1243. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName +
  1244. ' As cb ON sc.cbid = cb.id' +
  1245. ' GROUP By sc.`cbid`' +
  1246. ' ) As U ON C.cid = U.cid' +
  1247. ' WHERE C.tid = ? And C.status = ? And C.valid' +
  1248. ' GROUP By C.cid' +
  1249. ' ORDER By in_time';
  1250. // 舍入步长
  1251. const step = parseFloat('0.' + '0000000'.substr(0, this.ctx.tender.info.decimal.tp) + '5');
  1252. const sqlParam = [step, this.ctx.tender.info.decimal.tp, step, this.ctx.tender.info.decimal.tp, tid, tid, audit.flow.status.checked];
  1253. const data = await this.db.query(sql, sqlParam);
  1254. return data;
  1255. }
  1256. /**
  1257. * 查询可用的变更令
  1258. * @param { string } cid - 查询的清单
  1259. * @return {Promise<*>} - 可用的变更令列表
  1260. */
  1261. async delete(cid) {
  1262. // 初始化事务
  1263. this.transaction = await this.db.beginTransaction();
  1264. let result = false;
  1265. try {
  1266. const changeInfo = await this.getDataByCondition({ cid });
  1267. // 先删除清单,审批人列表
  1268. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
  1269. await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
  1270. // 再删除附件和附件文件ni zuo
  1271. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
  1272. await this.ctx.helper.delFiles(attList);
  1273. await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  1274. // if (attList.length !== 0) {
  1275. // for (const att of attList) {
  1276. // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  1277. // }
  1278. // await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  1279. // }
  1280. // 最后删除变更令
  1281. await this.transaction.delete(this.tableName, { cid });
  1282. // 记录删除日志
  1283. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.change, projectLogConst.status.delete, changeInfo.code);
  1284. await this.transaction.commit();
  1285. result = true;
  1286. } catch (e) {
  1287. await this.transaction.rollback();
  1288. result = false;
  1289. }
  1290. return result;
  1291. }
  1292. /**
  1293. * 重新审批变更令
  1294. * @param { string } cid - 查询的清单
  1295. * @return {Promise<*>} - 可用的变更令列表
  1296. */
  1297. async checkRevise(cid) {
  1298. // 初始化事务
  1299. this.transaction = await this.db.beginTransaction();
  1300. let result = false;
  1301. try {
  1302. const changeData = await this.getDataByCondition({ cid });
  1303. const pid = this.ctx.session.sessionProject.id;
  1304. // 获取所有审核人列表
  1305. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  1306. // 添加到消息推送表
  1307. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId);
  1308. const records = [];
  1309. auditors.forEach(auditor => {
  1310. records.push({
  1311. pid,
  1312. type: pushType.change,
  1313. uid: auditor.uid,
  1314. status: audit.flow.status.revise,
  1315. content: noticeContent,
  1316. });
  1317. });
  1318. await this.transaction.insert('zh_notice', records);
  1319. // 获取当前次数审批人列表
  1320. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeData.cid, changeData.times);
  1321. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeData.cid, changeData.times, 1, 0);
  1322. let usort = lastauditInfo.usort + 1;
  1323. const newTimes = changeData.times + 1;
  1324. const insert_audit_array = [];
  1325. // 新增一个发起修订状态到审批流程中
  1326. const revise_audit = {
  1327. tid: auditList[0].tid,
  1328. cid: auditList[0].cid,
  1329. uid: auditList[0].uid,
  1330. name: auditList[0].name,
  1331. jobs: auditList[0].jobs,
  1332. company: auditList[0].company,
  1333. times: changeData.times,
  1334. usite: lastauditInfo.usite + 1,
  1335. usort,
  1336. status: audit.flow.auditStatus.revise,
  1337. sin_time: new Date(),
  1338. };
  1339. insert_audit_array.push(revise_audit);
  1340. usort++;
  1341. // 新增新一次的审批人列表
  1342. for (const al of auditList) {
  1343. const insert_audit = {
  1344. tid: al.tid,
  1345. cid: al.cid,
  1346. uid: al.uid,
  1347. name: al.name,
  1348. jobs: al.jobs,
  1349. company: al.company,
  1350. times: newTimes,
  1351. usite: al.usite,
  1352. usort,
  1353. status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking,
  1354. };
  1355. insert_audit_array.push(insert_audit);
  1356. usort++;
  1357. }
  1358. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
  1359. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  1360. where: { cid: changeData.cid },
  1361. });
  1362. // 申请变更金额更新为上一次审批审批变更后数量,清空audit_amount值
  1363. const updateTpList = [];
  1364. for (const cl of changeList) {
  1365. updateTpList.push({ id: cl.id, camount: cl.spamount, audit_amount: null, samount: '' });
  1366. }
  1367. if (updateTpList.length > 0) {
  1368. await this.transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateTpList);
  1369. }
  1370. // let total_price = 0;
  1371. // const tp_decimal = changeData.tp_decimal ? changeData.tp_decimal : this.ctx.tender.info.decimal.tp;
  1372. // for (const cl of changeList) {
  1373. // total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, tp_decimal));
  1374. // }
  1375. // 设置变更令修订状态
  1376. const change_update = {
  1377. w_code: changeData.w_code,
  1378. status: audit.flow.status.revise,
  1379. times: newTimes,
  1380. cin_time: Date.parse(new Date()) / 1000,
  1381. // total_price,
  1382. tp_decimal: null,
  1383. up_decimal: null,
  1384. is_revise: 1,
  1385. };
  1386. const options = {
  1387. where: {
  1388. cid: changeData.cid,
  1389. },
  1390. };
  1391. await this.transaction.update(this.tableName, change_update, options);
  1392. await this.transaction.commit();
  1393. result = true;
  1394. } catch (error) {
  1395. await this.transaction.rollback();
  1396. result = false;
  1397. }
  1398. return result;
  1399. }
  1400. /**
  1401. * 重新审批变更令
  1402. * @param { string } cid - 查询的清单
  1403. * @return {Promise<*>} - 可用的变更令列表
  1404. */
  1405. async checkAgain(cid) {
  1406. // 初始化事务
  1407. this.transaction = await this.db.beginTransaction();
  1408. let result = false;
  1409. try {
  1410. const changeInfo = await this.getDataByCondition({ cid });
  1411. // 获取终审
  1412. const auditInfo = (
  1413. await this.ctx.service.changeAudit.getAllDataByCondition({
  1414. where: { cid },
  1415. orders: [['usort', 'desc']],
  1416. limit: 1,
  1417. offset: 0,
  1418. })
  1419. )[0];
  1420. let usort = auditInfo.usort + 1;
  1421. // 新增2个审批状态到审批列表中
  1422. const insert_audit1 = {
  1423. tid: auditInfo.tid,
  1424. cid: auditInfo.cid,
  1425. uid: auditInfo.uid,
  1426. name: auditInfo.name,
  1427. jobs: auditInfo.jobs,
  1428. company: auditInfo.company,
  1429. times: auditInfo.times,
  1430. usite: auditInfo.usite,
  1431. usort,
  1432. sin_time: new Date(),
  1433. status: audit.flow.auditStatus.checkAgain,
  1434. };
  1435. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  1436. usort++;
  1437. // 新增2个审批人到审批列表中
  1438. const insert_audit2 = {
  1439. tid: auditInfo.tid,
  1440. cid: auditInfo.cid,
  1441. uid: auditInfo.uid,
  1442. name: auditInfo.name,
  1443. jobs: auditInfo.jobs,
  1444. company: auditInfo.company,
  1445. times: auditInfo.times,
  1446. usite: auditInfo.usite,
  1447. usort,
  1448. status: audit.flow.auditStatus.checking,
  1449. sin_time: new Date(),
  1450. };
  1451. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  1452. // 审批列表数据也要回退
  1453. let total_price = 0;
  1454. const tp_decimal = changeInfo.tp_decimal ? changeInfo.tp_decimal : this.ctx.tender.info.decimal.tp;
  1455. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  1456. where: { cid: changeInfo.cid },
  1457. });
  1458. for (const cl of changeList) {
  1459. const audit_amount = cl.audit_amount.split(',');
  1460. const last_amount = audit_amount[audit_amount.length - 1];
  1461. audit_amount.splice(-1, 1);
  1462. const list_update = {
  1463. id: cl.id,
  1464. audit_amount: audit_amount.join(','),
  1465. samount: '',
  1466. };
  1467. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  1468. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  1469. }
  1470. // 设置变更令审批中
  1471. const change_update = {
  1472. p_code: null,
  1473. status: audit.flow.status.checking,
  1474. cin_time: Date.parse(new Date()) / 1000,
  1475. sin_time: null,
  1476. total_price,
  1477. };
  1478. const options = {
  1479. where: {
  1480. cid: changeInfo.cid,
  1481. },
  1482. };
  1483. await this.transaction.update(this.tableName, change_update, options);
  1484. await this.transaction.commit();
  1485. result = true;
  1486. const sms = new SMS(this.ctx);
  1487. const code = await sms.contentChange(changeInfo.code);
  1488. const shenpiUrl = await this.ctx.helper.urlToShort(
  1489. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi'
  1490. );
  1491. await this.ctx.helper.sendAliSms(auditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  1492. biangeng: code,
  1493. code: shenpiUrl,
  1494. });
  1495. // 微信模板通知
  1496. const wechatData = {
  1497. wap_url: shenpiUrl,
  1498. status: wxConst.status.check,
  1499. tips: wxConst.tips.check,
  1500. code: this.ctx.session.sessionProject.code,
  1501. c_name: changeInfo.name,
  1502. };
  1503. await this.ctx.helper.sendWechat(auditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  1504. } catch (error) {
  1505. await this.transaction.rollback();
  1506. result = false;
  1507. }
  1508. return result;
  1509. }
  1510. /**
  1511. * 判断是否有重名的变更令
  1512. * @param cid
  1513. * @param code
  1514. * @param tid
  1515. * @return {Promise<void>}
  1516. */
  1517. async isRepeat(cid, code, tid) {
  1518. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?)) AND `cid` != ? AND `tid` = ?';
  1519. const sqlParam = [this.tableName, code, audit.flow.status.checked, code, audit.flow.status.checked, cid, tid];
  1520. const result = await this.db.queryOne(sql, sqlParam);
  1521. return result.count !== 0;
  1522. }
  1523. async getAllCheckedChanges(tid) {
  1524. return await this.getAllDataByCondition({
  1525. where: { tid, status: audit.flow.status.checked },
  1526. orders: [['in_time', 'desc']],
  1527. });
  1528. }
  1529. /**
  1530. * 用于添加推送所需的content内容
  1531. * @param {Number} pid 项目id
  1532. * @param {Number} tid 台账id
  1533. * @param {Number} cid 变更id
  1534. * @param {Number} uid 审核人id
  1535. */
  1536. async getNoticeContent(pid, tid, cid, uid) {
  1537. const noticeSql =
  1538. 'SELECT * FROM (SELECT ' +
  1539. ' t.`id` As `tid`, t.`name`, c.`cid`, c.`code` As `c_code`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1540. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1541. ' LEFT JOIN ?? As c ON c.`cid` = ?' +
  1542. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1543. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1544. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.change.tableName, cid, this.ctx.service.projectAccount.tableName, uid, pid];
  1545. const content = await this.db.query(noticeSql, noticeSqlParam);
  1546. return content.length ? JSON.stringify(content[0]) : '';
  1547. }
  1548. /**
  1549. * 获取当前标段其他变更令
  1550. * @param {Number} tid - 标段id
  1551. * @param {Number} cid - 当前变更令
  1552. */
  1553. async getOthersChange(tid, cid) {
  1554. const sql = 'SELECT * FROM ?? WHERE tid = ? AND cid != ? ORDER By `in_time` desc ';
  1555. const sqlParam = [this.tableName, tid, cid];
  1556. const data = await this.db.query(sql, sqlParam);
  1557. const changeClassObj = {};
  1558. for (const c in changeConst.class) {
  1559. if (changeConst.class.hasOwnProperty(c)) {
  1560. const item = changeConst.class[c];
  1561. changeClassObj[item.value] = item.name;
  1562. }
  1563. }
  1564. for (let i = 0; i < data.length; i++) {
  1565. data[i].class = changeClassObj[data[i].class];
  1566. }
  1567. return data;
  1568. }
  1569. /**
  1570. * 拷贝变更令至当前变更令
  1571. * @param {String} cid - 当前变更令
  1572. * @param {String} copy_cid - 要拷贝的变更令
  1573. */
  1574. async handleCopyChange(cid, copy_cid) {
  1575. // const change = await this.getDataByCondition({ cid });
  1576. const copyChange = await this.getDataByCondition({ cid: copy_cid });
  1577. return await this.update({ peg: copyChange.peg, org_name: copyChange.org_name, org_code: copyChange.org_code, new_name: copyChange.new_name, content: copyChange.content, basis: copyChange.basis, expr: copyChange.expr, memo: copyChange.memo, type: copyChange.type,
  1578. class: copyChange.class, quality: copyChange.quality, company: copyChange.company, charge: copyChange.charge, new_code: copyChange.new_code }, {
  1579. cid,
  1580. });
  1581. }
  1582. async updateDecimalAndTp() {
  1583. // 判断是否可修改
  1584. // 判断t_type是否为费用
  1585. const transaction = await this.db.beginTransaction();
  1586. try {
  1587. await this.ctx.service.changeAuditList.calcCamountSum(transaction, true);
  1588. await transaction.commit();
  1589. return true;
  1590. } catch (err) {
  1591. await transaction.rollback();
  1592. throw err;
  1593. }
  1594. }
  1595. async updateChangeSelect(data) {
  1596. const conn = await this.db.beginTransaction();
  1597. try {
  1598. for (const d of data) {
  1599. await conn.update(this.tableName, { selected: d.selected }, { where: { cid: d.cid } });
  1600. }
  1601. await conn.commit();
  1602. } catch (err) {
  1603. await conn.rollback();
  1604. throw err;
  1605. }
  1606. }
  1607. }
  1608. return Change;
  1609. };