change.js 78 KB

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