change.js 80 KB

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