change.js 80 KB

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