change.js 78 KB

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