change.js 82 KB

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