change.js 79 KB

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