change.js 79 KB

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