change.js 77 KB

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