change.js 77 KB

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