change.js 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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. await this.ctx.service.changeAuditList.updateToLedger(this.transaction, changeData.tid, changeData.cid);
  720. // 添加短信通知-审批通过提醒功能
  721. // const mobile_array = [];
  722. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeData.cid, changeData.times);
  723. const users = this._.map(auditList, 'uid');
  724. const sms = new SMS(this.ctx);
  725. const code = await sms.contentChange(changeData.code);
  726. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), SmsAliConst.template.change_result, {
  727. biangeng: code,
  728. status: SmsAliConst.status.success,
  729. });
  730. // 微信模板通知
  731. const shenpiUrl = await this.ctx.helper.urlToShort(
  732. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi'
  733. );
  734. const wechatData = {
  735. wap_url: shenpiUrl,
  736. status: wxConst.status.success,
  737. tips: wxConst.tips.success,
  738. code: this.ctx.session.sessionProject.code,
  739. c_name: changeData.name,
  740. };
  741. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  742. } else {
  743. // 设置下一个审批人为审批状态
  744. const nextAudit_update = {
  745. id: postData.audit_next_id,
  746. status: audit.flow.auditStatus.checking,
  747. sin_time: new Date(),
  748. };
  749. await this.transaction.update(this.ctx.service.changeAudit.tableName, nextAudit_update);
  750. // 添加短信通知-需要审批提醒功能
  751. const nextAuditData = await this.ctx.service.changeAudit.getDataById(postData.audit_next_id);
  752. const sms = new SMS(this.ctx);
  753. const code = await sms.contentChange(changeData.code);
  754. const shenpiUrl = await this.ctx.helper.urlToShort(
  755. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi'
  756. );
  757. await this.ctx.helper.sendAliSms(nextAuditData.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  758. biangeng: code,
  759. code: shenpiUrl,
  760. });
  761. // 微信模板通知
  762. const wechatData = {
  763. wap_url: shenpiUrl,
  764. status: wxConst.status.check,
  765. tips: wxConst.tips.check,
  766. code: this.ctx.session.sessionProject.code,
  767. c_name: changeData.name,
  768. };
  769. await this.ctx.helper.sendWechat(nextAuditData.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  770. }
  771. change_update.total_price = total_price;
  772. const options = {
  773. where: {
  774. cid: postData.change_id,
  775. },
  776. };
  777. await this.transaction.update(this.tableName, change_update, options);
  778. await this.transaction.commit();
  779. result = true;
  780. } catch (error) {
  781. console.log(error);
  782. await this.transaction.rollback();
  783. result = false;
  784. }
  785. return result;
  786. }
  787. /**
  788. * 审批终止
  789. * @param {int} postData - 表单提交的数据
  790. * @return {void}
  791. */
  792. async approvalStop(postData) {
  793. // 初始化事务
  794. this.transaction = await this.db.beginTransaction();
  795. let result = false;
  796. try {
  797. // 设置审批人终止
  798. const audit_update = {
  799. id: postData.audit_id,
  800. sdesc: postData.sdesc,
  801. status: 4,
  802. sin_time: new Date(),
  803. };
  804. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  805. // 设置变更令终止
  806. const change_update = {
  807. w_code: postData.w_code,
  808. status: 4,
  809. cin_time: Date.parse(new Date()) / 1000,
  810. };
  811. const options = {
  812. where: {
  813. cid: postData.change_id,
  814. },
  815. };
  816. await this.transaction.update(this.tableName, change_update, options);
  817. await this.transaction.commit();
  818. result = true;
  819. } catch (error) {
  820. await this.transaction.rollback();
  821. result = false;
  822. }
  823. return result;
  824. }
  825. /**
  826. * 审批退回到原报人
  827. * @param {Number} pid 项目id
  828. * @param {int} postData - 表单提交的数据
  829. * @param {int} changeData - 变更令的数据
  830. * @return {void}
  831. */
  832. async approvalBack(pid, postData, changeData) {
  833. // 初始化事务
  834. this.transaction = await this.db.beginTransaction();
  835. let result = false;
  836. try {
  837. // 获取所有审核人列表
  838. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  839. // 添加到消息推送表
  840. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId);
  841. const records = [];
  842. auditors.forEach(auditor => {
  843. records.push({
  844. pid,
  845. type: pushType.change,
  846. uid: auditor.uid,
  847. status: audit.flow.status.backnew,
  848. content: noticeContent,
  849. });
  850. });
  851. await this.transaction.insert('zh_notice', records);
  852. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  853. // 设置审批人退回
  854. const audit_update = {
  855. id: postData.audit_id,
  856. sdesc: postData.sdesc,
  857. status: audit.flow.auditStatus.back,
  858. sin_time: new Date(),
  859. };
  860. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  861. // 新增新一次的审批人列表
  862. // 获取当前次数审批人列表
  863. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
  864. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 1, 0);
  865. let usort = lastauditInfo.usort + 1;
  866. const newTimes = changeInfo.times + 1;
  867. const insert_audit_array = [];
  868. for (const al of auditList) {
  869. const insert_audit = {
  870. tid: al.tid,
  871. cid: al.cid,
  872. uid: al.uid,
  873. name: al.name,
  874. jobs: al.jobs,
  875. company: al.company,
  876. times: newTimes,
  877. usite: al.usite,
  878. usort,
  879. status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking,
  880. };
  881. insert_audit_array.push(insert_audit);
  882. usort++;
  883. }
  884. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
  885. // 变更金额也退回
  886. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  887. where: { cid: changeInfo.cid },
  888. });
  889. let total_price = 0;
  890. const tp_decimal = changeData.tp_decimal ? changeData.tp_decimal : this.ctx.tender.info.decimal.tp;
  891. for (const cl of changeList) {
  892. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, tp_decimal));
  893. }
  894. // 设置变更令退回
  895. const change_update = {
  896. w_code: postData.w_code,
  897. status: audit.flow.status.back,
  898. times: newTimes,
  899. cin_time: Date.parse(new Date()) / 1000,
  900. total_price,
  901. tp_decimal: null,
  902. };
  903. const options = {
  904. where: {
  905. cid: postData.change_id,
  906. },
  907. };
  908. await this.transaction.update(this.tableName, change_update, options);
  909. await this.transaction.commit();
  910. result = true;
  911. const users = this._.map(insert_audit_array, 'uid');
  912. const sms = new SMS(this.ctx);
  913. const code = await sms.contentChange(changeData.code);
  914. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), SmsAliConst.template.change_result, {
  915. biangeng: code,
  916. status: SmsAliConst.status.back,
  917. });
  918. // 微信模板通知
  919. const shenpiUrl = await this.ctx.helper.urlToShort(
  920. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi'
  921. );
  922. const wechatData = {
  923. wap_url: shenpiUrl,
  924. status: wxConst.status.back,
  925. tips: wxConst.tips.back,
  926. code: this.ctx.session.sessionProject.code,
  927. c_name: changeInfo.name,
  928. };
  929. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  930. } catch (error) {
  931. await this.transaction.rollback();
  932. result = false;
  933. }
  934. return result;
  935. }
  936. /**
  937. * 审批退回到上一个审批人
  938. * @param {Number} pid 项目id
  939. * @param {int} postData - 表单提交的数据
  940. * @param {int} changeData - 变更令的数据
  941. * @return {void}
  942. */
  943. async approvalBackNew(pid, postData, changeData) {
  944. // 初始化事务
  945. this.transaction = await this.db.beginTransaction();
  946. let result = false;
  947. try {
  948. // 获取所有审核人列表
  949. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  950. // 添加到消息推送表
  951. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId);
  952. const records = [];
  953. auditors.forEach(auditor => {
  954. records.push({
  955. pid,
  956. type: pushType.change,
  957. uid: auditor.uid,
  958. status: audit.flow.status.back,
  959. content: noticeContent,
  960. });
  961. });
  962. await this.transaction.insert('zh_notice', records);
  963. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  964. // 设置审批人退回
  965. const audit_update = {
  966. id: postData.audit_id,
  967. sdesc: postData.sdesc,
  968. status: audit.flow.auditStatus.backnew,
  969. sin_time: new Date(),
  970. };
  971. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  972. // 获取当前审批人信息
  973. const auditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_id);
  974. // 获取当前次数审批人列表
  975. const auditList = await this.ctx.service.changeAudit.getNextAuditList(changeInfo.cid, auditInfo.usort);
  976. let usort = auditInfo.usort + 1;
  977. // 获取上一个审批人信息
  978. const lastauditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_last_id);
  979. // 新增2个审批人到审批列表中
  980. const insert_audit1 = {
  981. tid: lastauditInfo.tid,
  982. cid: lastauditInfo.cid,
  983. uid: lastauditInfo.uid,
  984. name: lastauditInfo.name,
  985. jobs: lastauditInfo.jobs,
  986. company: lastauditInfo.company,
  987. times: lastauditInfo.times,
  988. usite: lastauditInfo.usite,
  989. usort,
  990. status: audit.flow.auditStatus.checking,
  991. sin_time: new Date(),
  992. };
  993. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  994. usort++;
  995. // 新增2个审批人到审批列表中
  996. const insert_audit2 = {
  997. tid: auditInfo.tid,
  998. cid: auditInfo.cid,
  999. uid: auditInfo.uid,
  1000. name: auditInfo.name,
  1001. jobs: auditInfo.jobs,
  1002. company: auditInfo.company,
  1003. times: auditInfo.times,
  1004. usite: auditInfo.usite,
  1005. usort,
  1006. status: audit.flow.auditStatus.uncheck,
  1007. };
  1008. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  1009. // 把接下未审批的审批人排序都加2
  1010. for (const al of auditList) {
  1011. const audit_update = {
  1012. id: al.id,
  1013. usort: al.usort + 2,
  1014. };
  1015. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  1016. }
  1017. // 审批列表数据也要回退
  1018. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  1019. where: { cid: changeInfo.cid },
  1020. });
  1021. let total_price = 0;
  1022. const tp_decimal = changeData.tp_decimal ? changeData.tp_decimal : this.ctx.tender.info.decimal.tp;
  1023. for (const cl of changeList) {
  1024. const audit_amount = cl.audit_amount.split(',');
  1025. const last_amount = audit_amount[audit_amount.length - 1];
  1026. audit_amount.splice(-1, 1);
  1027. const list_update = {
  1028. id: cl.id,
  1029. audit_amount: audit_amount.join(','),
  1030. spamount: parseFloat(last_amount),
  1031. };
  1032. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  1033. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  1034. }
  1035. // 设置变更令退回
  1036. const change_update = {
  1037. w_code: postData.w_code,
  1038. status: audit.flow.status.backnew,
  1039. cin_time: Date.parse(new Date()) / 1000,
  1040. total_price,
  1041. };
  1042. const options = {
  1043. where: {
  1044. cid: postData.change_id,
  1045. },
  1046. };
  1047. await this.transaction.update(this.tableName, change_update, options);
  1048. await this.transaction.commit();
  1049. result = true;
  1050. const sms = new SMS(this.ctx);
  1051. const code = await sms.contentChange(changeData.code);
  1052. const shenpiUrl = await this.ctx.helper.urlToShort(
  1053. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi'
  1054. );
  1055. await this.ctx.helper.sendAliSms(lastauditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  1056. biangeng: code,
  1057. code: shenpiUrl,
  1058. });
  1059. // 微信模板通知
  1060. const wechatData = {
  1061. wap_url: shenpiUrl,
  1062. status: wxConst.status.check,
  1063. tips: wxConst.tips.check,
  1064. code: this.ctx.session.sessionProject.code,
  1065. c_name: changeInfo.name,
  1066. };
  1067. await this.ctx.helper.sendWechat(lastauditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  1068. } catch (error) {
  1069. await this.transaction.rollback();
  1070. result = false;
  1071. }
  1072. return result;
  1073. }
  1074. /**
  1075. * 查询可用的变更令
  1076. * @param bills - 查询的清单
  1077. * @param pos - 查询的部位
  1078. * @return {Promise<*>} - 可用的变更令列表
  1079. */
  1080. async getValidChanges(tid, bills, pos) {
  1081. const self = this;
  1082. const getFilterPart = function(field, value) {
  1083. return value
  1084. ? field + ' = ' + self.db.escape(value)
  1085. : self.db.format("(?? = null or ?? = '')", [field, field]);
  1086. };
  1087. const timesLen = 100;
  1088. const filter = getFilterPart('cb.code', bills.b_code) +
  1089. ' And ' + getFilterPart('cb.name', bills.name) +
  1090. ' And ' + getFilterPart('cb.unit', bills.unit) +
  1091. ' And cb.`unit_price` = ' + this.db.escape(bills.unit_price) +
  1092. (pos ? getFilterPart('cb.bwmx', pos.name) : '');
  1093. const sql =
  1094. '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,' +
  1095. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  1096. ' 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, ' +
  1097. ' scb.used_amount' +
  1098. ' FROM ' + this.tableName + ' As c ' +
  1099. ' Left Join ' + this.ctx.service.changeAuditList.tableName + ' As cb On c.cid = cb.cid ' +
  1100. ' Left Join (' +
  1101. ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
  1102. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  1103. ' INNER JOIN (SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, cbid, sid ' +
  1104. ' FROM ' + this.ctx.service.stageChange.tableName +
  1105. ' WHERE tid = ?' +
  1106. ' GROUP BY cbid, sid' +
  1107. ' ) As MF' +
  1108. ' ON (sc.stimes * ' + timesLen + ' + sc.sorder) = MF.flow And sc.cbid = MF.cbid And sc.sid = MF.sid' +
  1109. ' GROUP BY sc.cbid' +
  1110. ' ) As scb ON cb.id = scb.cbid' +
  1111. ' WHERE c.tid = ? And c.status = ? And c.valid And ' + filter +
  1112. ' ORDER BY c.in_time';
  1113. const sqlParam = [tid, tid, audit.flow.status.checked];
  1114. const changes = await this.db.query(sql, sqlParam);
  1115. for (const c of changes) {
  1116. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  1117. ' FROM ?? As ca ' +
  1118. ' Left Join ?? As pa ' +
  1119. ' On ca.uid = pa.id ' +
  1120. ' Where ca.cid = ?';
  1121. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
  1122. c.attachments = await this.db.query(aSql, aSqlParam);
  1123. }
  1124. return changes;
  1125. }
  1126. /**
  1127. * 查询审批人可用的变更令
  1128. * @param bills - 查询的清单
  1129. * @param pos - 查询的部位
  1130. * @return {Promise<*>} - 可用的变更令列表
  1131. */
  1132. async getAuditValidChanges(tid, bills, pos, times, order) {
  1133. const timesLen = 100;
  1134. const filter =
  1135. 'cb.`code` = ' + this.db.escape(bills.b_code) +
  1136. ' And cb.`name` = ' + this.db.escape(bills.name) +
  1137. ' And cb.`unit` = ' + this.db.escape(bills.unit) +
  1138. ' And cb.`unit_price` = ' + this.db.escape(bills.unit_price) +
  1139. (pos ? ' And cb.`bwmx` = ' + this.db.escape(pos.name) : '');
  1140. const sql =
  1141. '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,' +
  1142. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  1143. ' 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, ' +
  1144. ' scb.used_amount' +
  1145. ' FROM ' + this.tableName + ' As c ' +
  1146. ' Left Join ' + this.ctx.service.changeAuditList.tableName + ' As cb On c.cid = cb.cid ' +
  1147. ' Left Join (' +
  1148. ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
  1149. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  1150. ' INNER JOIN (SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, cbid, sid ' +
  1151. ' FROM ' + this.ctx.service.stageChange.tableName +
  1152. ' WHERE tid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?)) ' +
  1153. ' GROUP BY cbid, sid' +
  1154. ' ) As MF' +
  1155. ' ON (sc.stimes * ' + timesLen + ' + sc.sorder) = MF.flow And sc.cbid = MF.cbid And sc.sid = MF.sid' +
  1156. ' GROUP BY sc.cbid' +
  1157. ' ) As scb ON cb.id = scb.cbid' +
  1158. ' WHERE c.tid = ? And c.status = ? And c.valid And ' + filter +
  1159. ' ORDER BY c.in_time';
  1160. const sqlParam = [tid, times, times, order, tid, audit.flow.status.checked];
  1161. const changes = await this.db.query(sql, sqlParam);
  1162. for (const c of changes) {
  1163. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  1164. ' FROM ?? As ca ' +
  1165. ' Left Join ?? As pa ' +
  1166. ' On ca.uid = pa.id ' +
  1167. ' Where ca.cid = ?';
  1168. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
  1169. c.attachments = await this.db.query(aSql, aSqlParam);
  1170. }
  1171. return changes;
  1172. }
  1173. /**
  1174. * 查询变更令 + 变更令执行
  1175. * @param tid
  1176. * @return {Promise<void>}
  1177. */
  1178. async getChangeAndUsedInfo(tid) {
  1179. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  1180. let filter;
  1181. if (lastStage.id === this.ctx.stage.id) {
  1182. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))', [
  1183. lastStage.order,
  1184. lastStage.order,
  1185. this.ctx.stage.curTimes,
  1186. this.ctx.stage.curTimes,
  1187. this.ctx.stage.curOrder,
  1188. ]);
  1189. } else {
  1190. if (lastStage.status === audit.stage.status.uncheck) {
  1191. filter = ' And s.order < ' + lastStage.order;
  1192. } else if (lastStage.status === audit.stage.status.checked) {
  1193. filter = '';
  1194. } else if (lastStage.status === audit.stage.status.checkNo) {
  1195. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And sChange.`stimes` <= ?))', [lastStage.order, lastStage.order, lastStage.times]);
  1196. } else {
  1197. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
  1198. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))', [
  1199. lastStage.order,
  1200. lastStage.order,
  1201. lastStage.times,
  1202. lastStage.times,
  1203. curAuditor.order - 1,
  1204. ]);
  1205. }
  1206. }
  1207. const sql =
  1208. 'SELECT C.*, Sum(U.utp) As used_tp, TRUNCATE(Sum(U.utp) / C.total_price * 100 + 0.005, 2) As used_pt' +
  1209. ' FROM ' + this.tableName + ' As C' +
  1210. ' 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' +
  1211. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  1212. ' INNER JOIN (' +
  1213. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid` ' +
  1214. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sChange ' +
  1215. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s' +
  1216. ' ON sChange.sid = s.id' +
  1217. ' WHERE sChange.tid = ?' + filter +
  1218. ' GROUP By `lid`, `pid`, `cbid`, `sid`' +
  1219. ' ) As m' +
  1220. ' 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`' +
  1221. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName +
  1222. ' As cb ON sc.cbid = cb.id' +
  1223. ' GROUP By sc.`cbid`' +
  1224. ' ) As U ON C.cid = U.cid' +
  1225. ' WHERE C.tid = ? And C.status = ? And C.valid' +
  1226. ' GROUP By C.cid' +
  1227. ' ORDER By in_time';
  1228. // 舍入步长
  1229. const step = parseFloat('0.' + '0000000'.substr(0, this.ctx.tender.info.decimal.tp) + '5');
  1230. const sqlParam = [step, this.ctx.tender.info.decimal.tp, step, this.ctx.tender.info.decimal.tp, tid, tid, audit.flow.status.checked];
  1231. const data = await this.db.query(sql, sqlParam);
  1232. return data;
  1233. }
  1234. /**
  1235. * 查询可用的变更令
  1236. * @param { string } cid - 查询的清单
  1237. * @return {Promise<*>} - 可用的变更令列表
  1238. */
  1239. async delete(cid) {
  1240. // 初始化事务
  1241. this.transaction = await this.db.beginTransaction();
  1242. let result = false;
  1243. try {
  1244. const changeInfo = await this.getDataByCondition({ cid });
  1245. // 先删除清单,审批人列表
  1246. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
  1247. await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
  1248. // 再删除附件和附件文件ni zuo
  1249. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
  1250. await this.ctx.helper.delFiles(attList);
  1251. await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  1252. // if (attList.length !== 0) {
  1253. // for (const att of attList) {
  1254. // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  1255. // }
  1256. // await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  1257. // }
  1258. // 最后删除变更令
  1259. await this.transaction.delete(this.tableName, { cid });
  1260. // 记录删除日志
  1261. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.change, projectLogConst.status.delete, changeInfo.code);
  1262. await this.transaction.commit();
  1263. result = true;
  1264. } catch (e) {
  1265. await this.transaction.rollback();
  1266. result = false;
  1267. }
  1268. return result;
  1269. }
  1270. /**
  1271. * 重新审批变更令
  1272. * @param { string } cid - 查询的清单
  1273. * @return {Promise<*>} - 可用的变更令列表
  1274. */
  1275. async checkRevise(cid) {
  1276. // 初始化事务
  1277. this.transaction = await this.db.beginTransaction();
  1278. let result = false;
  1279. try {
  1280. const changeData = await this.getDataByCondition({ cid });
  1281. const pid = this.ctx.session.sessionProject.id;
  1282. // 获取所有审核人列表
  1283. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  1284. // 添加到消息推送表
  1285. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId);
  1286. const records = [];
  1287. auditors.forEach(auditor => {
  1288. records.push({
  1289. pid,
  1290. type: pushType.change,
  1291. uid: auditor.uid,
  1292. status: audit.flow.status.revise,
  1293. content: noticeContent,
  1294. });
  1295. });
  1296. await this.transaction.insert('zh_notice', records);
  1297. // 获取当前次数审批人列表
  1298. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeData.cid, changeData.times);
  1299. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeData.cid, changeData.times, 1, 0);
  1300. let usort = lastauditInfo.usort + 1;
  1301. const newTimes = changeData.times + 1;
  1302. const insert_audit_array = [];
  1303. // 新增一个发起修订状态到审批流程中
  1304. const revise_audit = {
  1305. tid: auditList[0].tid,
  1306. cid: auditList[0].cid,
  1307. uid: auditList[0].uid,
  1308. name: auditList[0].name,
  1309. jobs: auditList[0].jobs,
  1310. company: auditList[0].company,
  1311. times: changeData.times,
  1312. usite: lastauditInfo.usite + 1,
  1313. usort,
  1314. status: audit.flow.auditStatus.revise,
  1315. sin_time: new Date(),
  1316. };
  1317. insert_audit_array.push(revise_audit);
  1318. usort++;
  1319. // 新增新一次的审批人列表
  1320. for (const al of auditList) {
  1321. const insert_audit = {
  1322. tid: al.tid,
  1323. cid: al.cid,
  1324. uid: al.uid,
  1325. name: al.name,
  1326. jobs: al.jobs,
  1327. company: al.company,
  1328. times: newTimes,
  1329. usite: al.usite,
  1330. usort,
  1331. status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking,
  1332. };
  1333. insert_audit_array.push(insert_audit);
  1334. usort++;
  1335. }
  1336. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
  1337. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  1338. where: { cid: changeData.cid },
  1339. });
  1340. // 申请变更金额更新为上一次审批审批变更后数量,清空audit_amount值
  1341. const updateTpList = [];
  1342. for (const cl of changeList) {
  1343. updateTpList.push({ id: cl.id, camount: cl.spamount, audit_amount: null, samount: '' });
  1344. }
  1345. if (updateTpList.length > 0) {
  1346. await this.transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateTpList);
  1347. }
  1348. // let total_price = 0;
  1349. // const tp_decimal = changeData.tp_decimal ? changeData.tp_decimal : this.ctx.tender.info.decimal.tp;
  1350. // for (const cl of changeList) {
  1351. // total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, tp_decimal));
  1352. // }
  1353. // 设置变更令修订状态
  1354. const change_update = {
  1355. w_code: changeData.w_code,
  1356. status: audit.flow.status.revise,
  1357. times: newTimes,
  1358. cin_time: Date.parse(new Date()) / 1000,
  1359. // total_price,
  1360. tp_decimal: null,
  1361. is_revise: 1,
  1362. };
  1363. const options = {
  1364. where: {
  1365. cid: changeData.cid,
  1366. },
  1367. };
  1368. await this.transaction.update(this.tableName, change_update, options);
  1369. await this.transaction.commit();
  1370. result = true;
  1371. } catch (error) {
  1372. await this.transaction.rollback();
  1373. result = false;
  1374. }
  1375. return result;
  1376. }
  1377. /**
  1378. * 重新审批变更令
  1379. * @param { string } cid - 查询的清单
  1380. * @return {Promise<*>} - 可用的变更令列表
  1381. */
  1382. async checkAgain(cid) {
  1383. // 初始化事务
  1384. this.transaction = await this.db.beginTransaction();
  1385. let result = false;
  1386. try {
  1387. const changeInfo = await this.getDataByCondition({ cid });
  1388. // 获取终审
  1389. const auditInfo = (
  1390. await this.ctx.service.changeAudit.getAllDataByCondition({
  1391. where: { cid },
  1392. orders: [['usort', 'desc']],
  1393. limit: 1,
  1394. offset: 0,
  1395. })
  1396. )[0];
  1397. let usort = auditInfo.usort + 1;
  1398. // 新增2个审批状态到审批列表中
  1399. const insert_audit1 = {
  1400. tid: auditInfo.tid,
  1401. cid: auditInfo.cid,
  1402. uid: auditInfo.uid,
  1403. name: auditInfo.name,
  1404. jobs: auditInfo.jobs,
  1405. company: auditInfo.company,
  1406. times: auditInfo.times,
  1407. usite: auditInfo.usite,
  1408. usort,
  1409. sin_time: new Date(),
  1410. status: audit.flow.auditStatus.checkAgain,
  1411. };
  1412. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  1413. usort++;
  1414. // 新增2个审批人到审批列表中
  1415. const insert_audit2 = {
  1416. tid: auditInfo.tid,
  1417. cid: auditInfo.cid,
  1418. uid: auditInfo.uid,
  1419. name: auditInfo.name,
  1420. jobs: auditInfo.jobs,
  1421. company: auditInfo.company,
  1422. times: auditInfo.times,
  1423. usite: auditInfo.usite,
  1424. usort,
  1425. status: audit.flow.auditStatus.checking,
  1426. sin_time: new Date(),
  1427. };
  1428. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  1429. // 审批列表数据也要回退
  1430. let total_price = 0;
  1431. const tp_decimal = changeInfo.tp_decimal ? changeInfo.tp_decimal : this.ctx.tender.info.decimal.tp;
  1432. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  1433. where: { cid: changeInfo.cid },
  1434. });
  1435. for (const cl of changeList) {
  1436. const audit_amount = cl.audit_amount.split(',');
  1437. const last_amount = audit_amount[audit_amount.length - 1];
  1438. audit_amount.splice(-1, 1);
  1439. const list_update = {
  1440. id: cl.id,
  1441. audit_amount: audit_amount.join(','),
  1442. samount: '',
  1443. };
  1444. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal));
  1445. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  1446. }
  1447. // 设置变更令审批中
  1448. const change_update = {
  1449. p_code: null,
  1450. status: audit.flow.status.checking,
  1451. cin_time: Date.parse(new Date()) / 1000,
  1452. sin_time: null,
  1453. total_price,
  1454. };
  1455. const options = {
  1456. where: {
  1457. cid: changeInfo.cid,
  1458. },
  1459. };
  1460. await this.transaction.update(this.tableName, change_update, options);
  1461. await this.transaction.commit();
  1462. result = true;
  1463. const sms = new SMS(this.ctx);
  1464. const code = await sms.contentChange(changeInfo.code);
  1465. const shenpiUrl = await this.ctx.helper.urlToShort(
  1466. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi'
  1467. );
  1468. await this.ctx.helper.sendAliSms(auditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  1469. biangeng: code,
  1470. code: shenpiUrl,
  1471. });
  1472. // 微信模板通知
  1473. const wechatData = {
  1474. wap_url: shenpiUrl,
  1475. status: wxConst.status.check,
  1476. tips: wxConst.tips.check,
  1477. code: this.ctx.session.sessionProject.code,
  1478. c_name: changeInfo.name,
  1479. };
  1480. await this.ctx.helper.sendWechat(auditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  1481. } catch (error) {
  1482. await this.transaction.rollback();
  1483. result = false;
  1484. }
  1485. return result;
  1486. }
  1487. /**
  1488. * 判断是否有重名的变更令
  1489. * @param cid
  1490. * @param code
  1491. * @param tid
  1492. * @return {Promise<void>}
  1493. */
  1494. async isRepeat(cid, code, tid) {
  1495. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?)) AND `cid` != ? AND `tid` = ?';
  1496. const sqlParam = [this.tableName, code, audit.flow.status.checked, code, audit.flow.status.checked, cid, tid];
  1497. const result = await this.db.queryOne(sql, sqlParam);
  1498. return result.count !== 0;
  1499. }
  1500. async getAllCheckedChanges(tid) {
  1501. return await this.getAllDataByCondition({
  1502. where: { tid, status: audit.flow.status.checked },
  1503. orders: [['in_time', 'desc']],
  1504. });
  1505. }
  1506. /**
  1507. * 用于添加推送所需的content内容
  1508. * @param {Number} pid 项目id
  1509. * @param {Number} tid 台账id
  1510. * @param {Number} cid 变更id
  1511. * @param {Number} uid 审核人id
  1512. */
  1513. async getNoticeContent(pid, tid, cid, uid) {
  1514. const noticeSql =
  1515. 'SELECT * FROM (SELECT ' +
  1516. ' t.`id` As `tid`, t.`name`, c.`cid`, c.`code` As `c_code`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1517. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1518. ' LEFT JOIN ?? As c ON c.`cid` = ?' +
  1519. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1520. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1521. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.change.tableName, cid, this.ctx.service.projectAccount.tableName, uid, pid];
  1522. const content = await this.db.query(noticeSql, noticeSqlParam);
  1523. return content.length ? JSON.stringify(content[0]) : '';
  1524. }
  1525. /**
  1526. * 获取当前标段其他变更令
  1527. * @param {Number} tid - 标段id
  1528. * @param {Number} cid - 当前变更令
  1529. */
  1530. async getOthersChange(tid, cid) {
  1531. const sql = 'SELECT * FROM ?? WHERE tid = ? AND cid != ? ORDER By `in_time` desc ';
  1532. const sqlParam = [this.tableName, tid, cid];
  1533. const data = await this.db.query(sql, sqlParam);
  1534. const changeClassObj = {};
  1535. for (const c in changeConst.class) {
  1536. if (changeConst.class.hasOwnProperty(c)) {
  1537. const item = changeConst.class[c];
  1538. changeClassObj[item.value] = item.name;
  1539. }
  1540. }
  1541. for (let i = 0; i < data.length; i++) {
  1542. data[i].class = changeClassObj[data[i].class];
  1543. }
  1544. return data;
  1545. }
  1546. /**
  1547. * 拷贝变更令至当前变更令
  1548. * @param {String} cid - 当前变更令
  1549. * @param {String} copy_cid - 要拷贝的变更令
  1550. */
  1551. async handleCopyChange(cid, copy_cid) {
  1552. // const change = await this.getDataByCondition({ cid });
  1553. const copyChange = await this.getDataByCondition({ cid: copy_cid });
  1554. 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,
  1555. class: copyChange.class, quality: copyChange.quality, company: copyChange.company, charge: copyChange.charge, new_code: copyChange.new_code }, {
  1556. cid,
  1557. });
  1558. }
  1559. }
  1560. return Change;
  1561. };