change.js 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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, SUM(`positive_tp`) AS p_tp, SUM(`negative_tp`) AS n_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, result.p_tp, result.n_tp] : [0, 0, 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 + '/information#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 + '/information#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 + '/information#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 + '/information#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 + '/information#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 + '/information#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. async getAllChangeHasMinus(tenderId) {
  1625. const sql = `SELECT * FROM ${this.tableName} WHERE tid = ? AND negative_tp < 0 ORDER BY code asc`;
  1626. return this.db.query(sql, [tenderId]);
  1627. }
  1628. }
  1629. return Change;
  1630. };