change.js 80 KB

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