change.js 77 KB

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