change.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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 smsTypeConst = require('../const/sms_type')
  13. const SMS = require('../lib/sms')
  14. const SmsAliConst = require('../const/sms_alitemplate')
  15. const pushType = require('../const/audit').pushType
  16. module.exports = app => {
  17. class Change extends app.BaseService {
  18. /**
  19. * 构造函数
  20. *
  21. * @param {Object} ctx - egg全局变量
  22. * @return {void}
  23. */
  24. constructor(ctx) {
  25. super(ctx)
  26. this.tableName = 'change'
  27. }
  28. /**
  29. * 查找数据
  30. *
  31. * @param {Object} data - 筛选表单中的get数据
  32. * @return {void}
  33. */
  34. searchFilter(data) {
  35. this.initSqlBuilder()
  36. // this.sqlBuilder.columns = ['id', 'username', 'real_name', 'create_time', 'last_login', 'login_ip',
  37. // 'group_id', 'token', 'can_login'];
  38. data.type = parseInt(data.status)
  39. if (data.keyword !== undefined) {
  40. switch (data.type) {
  41. // 用户名
  42. case 1:
  43. this.sqlBuilder.setAndWhere('username', {
  44. value: this.db.escape(data.keyword + '%'),
  45. operate: 'like'
  46. })
  47. break
  48. // 姓名
  49. case 2:
  50. this.sqlBuilder.setAndWhere('real_name', {
  51. value: this.db.escape(data.keyword + '%'),
  52. operate: 'like'
  53. })
  54. break
  55. // 联系电话
  56. case 3:
  57. this.sqlBuilder.setAndWhere('telephone', {
  58. value: this.db.escape(data.keyword + '%'),
  59. operate: 'like'
  60. })
  61. break
  62. default:
  63. break
  64. }
  65. }
  66. // 办事处筛选
  67. if (data.office !== undefined && data.office !== '') {
  68. this.sqlBuilder.setAndWhere('office', {
  69. value: this.db.escape(data.office),
  70. operate: '='
  71. })
  72. }
  73. }
  74. async add(tenderId, userId, code, name) {
  75. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?))'
  76. const sqlParam = [this.tableName, tenderId, code, audit.flow.status.checked, code, audit.flow.status.checked]
  77. const codeCount = await this.db.queryOne(sql, sqlParam)
  78. const count = codeCount.count
  79. if (count > 0) {
  80. throw '变更令号重复'
  81. }
  82. // 初始化事务
  83. this.transaction = await this.db.beginTransaction()
  84. let result = false
  85. try {
  86. const cid = this.uuid.v4()
  87. const change = {
  88. cid,
  89. tid: tenderId,
  90. uid: userId,
  91. status: audit.flow.status.uncheck,
  92. times: 1,
  93. valid: true,
  94. in_time: new Date(),
  95. code,
  96. name
  97. }
  98. const operate = await this.transaction.insert(this.tableName, change)
  99. if (operate.affectedRows <= 0) {
  100. throw '新建变更令数据失败'
  101. }
  102. // 把提交人信息添加到zh_change_audit
  103. const userInfo = await this.ctx.service.projectAccount.getDataById(userId)
  104. const changeaudit = [
  105. {
  106. tid: tenderId,
  107. cid,
  108. uid: userId,
  109. name: userInfo.name,
  110. jobs: userInfo.role,
  111. company: userInfo.company,
  112. times: 1,
  113. usite: 0,
  114. usort: 0,
  115. status: 2
  116. }
  117. ]
  118. // 并把之前存在的变更令审批人添加到zh_change_audit
  119. // 先找出标段最近存在的变更令审批人的变更令info
  120. const changeInfo = await this.ctx.service.change.getHaveAuditLastInfo(tenderId)
  121. if (changeInfo) {
  122. // 再获取非原报审批人
  123. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times)
  124. let sort = 1
  125. for (const audit of auditList) {
  126. if (audit.usite !== 0) {
  127. const oneaudit = {
  128. tid: tenderId,
  129. cid,
  130. uid: audit.uid,
  131. name: audit.name,
  132. jobs: audit.jobs,
  133. company: audit.company,
  134. times: 1,
  135. usite: audit.usite,
  136. usort: sort++,
  137. status: 1
  138. }
  139. changeaudit.push(oneaudit)
  140. }
  141. }
  142. }
  143. await this.transaction.insert(this.ctx.service.changeAudit.tableName, changeaudit)
  144. result = change
  145. this.transaction.commit()
  146. } catch (error) {
  147. console.log(error)
  148. // 回滚
  149. await this.transaction.rollback()
  150. }
  151. return result
  152. }
  153. async getHaveAuditLastInfo(tenderId) {
  154. const sql = 'SELECT * 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'
  155. const sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId]
  156. return await this.db.queryOne(sql, sqlParam)
  157. }
  158. async pendingDatas(tenderId, userId) {
  159. return await this.getAllDataByCondition({
  160. tid: tenderId,
  161. uid: userId,
  162. status: audit.flow.status.checking
  163. })
  164. }
  165. async uncheckDatas(tenderId, userId) {
  166. return await this.getAllDataByCondition({
  167. tid: tenderId,
  168. uid: userId,
  169. status: audit.flow.status.uncheck
  170. })
  171. }
  172. async checkingDatas(tenderId, userId) {
  173. return await this.getAllDataByCondition({
  174. tid: tenderId,
  175. uid: userId,
  176. status: audit.flow.status.checking
  177. })
  178. }
  179. async checkedDatas(tenderId, userId) {
  180. return await this.getAllDataByCondition({
  181. tid: tenderId,
  182. uid: userId,
  183. status: audit.flow.status.checked
  184. })
  185. }
  186. async checkNoDatas(tenderId, userId) {
  187. return await this.getAllDataByCondition({
  188. tid: tenderId,
  189. uid: userId,
  190. status: audit.flow.status.checkNo
  191. })
  192. }
  193. async checkNoCount(tenderId, userId) {
  194. return await this.count({
  195. tid: tenderId,
  196. uid: userId,
  197. status: audit.flow.status.checkNo
  198. })
  199. }
  200. /**
  201. * 获取变更令列表
  202. * @param {int} tenderId - 标段id
  203. * @param {int} status - 状态
  204. * @param {int} hadlimit - 分页
  205. * @return {object} list - 列表
  206. */
  207. async getListByStatus(tenderId, status = 0, hadlimit = 1) {
  208. let sql = ''
  209. let sqlParam = ''
  210. switch (status) {
  211. case 0: // 包含你的所有变更令
  212. sql =
  213. 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND ' +
  214. '(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 = ? ) ORDER BY a.in_time DESC'
  215. sqlParam = [
  216. this.tableName,
  217. tenderId,
  218. this.ctx.session.sessionUser.accountId,
  219. audit.flow.status.uncheck,
  220. this.ctx.service.changeAudit.tableName,
  221. this.ctx.session.sessionUser.accountId,
  222. audit.flow.status.checked
  223. ]
  224. break
  225. case 1: // 待处理(你的)
  226. sql = 'SELECT a.* FROM ?? as a WHERE cid in(SELECT b.cid FROM ?? as b WHERE tid = ? AND uid = ? AND status = ?) ORDER BY in_time DESC'
  227. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.flow.auditStatus.checking]
  228. break
  229. case 5: // 待上报(所有的)PS:取未上报和退回的变更令
  230. sql =
  231. 'SELECT a.* FROM ?? AS a WHERE ' +
  232. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
  233. '(a.status = ? OR a.status = ?) AND a.tid = ? ORDER BY a.in_time DESC'
  234. sqlParam = [
  235. this.tableName,
  236. this.ctx.service.changeAudit.tableName,
  237. this.ctx.session.sessionUser.accountId,
  238. audit.flow.status.uncheck,
  239. audit.flow.status.back,
  240. tenderId
  241. ]
  242. break
  243. case 2: // 进行中(所有的)
  244. case 4: // 终止(所有的)
  245. sql =
  246. 'SELECT a.* FROM ?? AS a WHERE ' +
  247. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) AND ' +
  248. 'a.status = ? AND a.tid = ? ORDER BY a.in_time DESC'
  249. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, status, tenderId]
  250. break
  251. case 3: // 已完成(所有的)
  252. sql = 'SELECT a.* FROM ?? AS a WHERE ' + 'a.status = ? AND a.tid = ? ORDER BY a.in_time DESC'
  253. sqlParam = [this.tableName, status, tenderId]
  254. break
  255. default:
  256. break
  257. }
  258. if (hadlimit) {
  259. const limit = this.app.config.pageSize
  260. const offset = limit * (this.ctx.page - 1)
  261. const limitString = offset >= 0 ? offset + ',' + limit : limit
  262. sql += ' LIMIT ' + limitString
  263. }
  264. const list = await this.db.query(sql, sqlParam)
  265. return list
  266. }
  267. /**
  268. * 获取变更令个数
  269. * @param {int} tenderId - 标段id
  270. * @param {int} status - 状态
  271. * @return {void}
  272. */
  273. async getCountByStatus(tenderId, status) {
  274. switch (status) {
  275. case 0: // 包含你的所有变更令
  276. const sql =
  277. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  278. '(a.uid = ? OR a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid))'
  279. const sqlParam = [
  280. this.tableName,
  281. tenderId,
  282. this.ctx.session.sessionUser.accountId,
  283. this.ctx.service.changeAudit.tableName,
  284. this.ctx.session.sessionUser.accountId
  285. ]
  286. const result = await this.db.query(sql, sqlParam)
  287. return result[0].count
  288. case 1: // 待处理(你的)
  289. return await this.ctx.service.changeAudit.count({
  290. tid: tenderId,
  291. uid: this.ctx.session.sessionUser.accountId,
  292. status: 2
  293. })
  294. case 5: // 待上报(所有的)PS:取未上报和退回的变更令
  295. const sql2 =
  296. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  297. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) ' +
  298. 'AND (a.status = ? OR a.status = ?) AND a.tid = ?'
  299. const sqlParam2 = [
  300. this.tableName,
  301. this.ctx.service.changeAudit.tableName,
  302. this.ctx.session.sessionUser.accountId,
  303. audit.flow.status.uncheck,
  304. audit.flow.status.back,
  305. tenderId
  306. ]
  307. const result2 = await this.db.query(sql2, sqlParam2)
  308. return result2[0].count
  309. case 2: // 进行中(所有的)
  310. case 4: // 终止(所有的)
  311. const sql3 =
  312. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  313. '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 = ?'
  314. const sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, status, tenderId]
  315. const result3 = await this.db.query(sql3, sqlParam3)
  316. return result3[0].count
  317. case 3: // 已完成(所有的)
  318. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?'
  319. const sqlParam4 = [this.tableName, status, tenderId]
  320. const result4 = await this.db.query(sql4, sqlParam4)
  321. return result4[0].count
  322. default:
  323. break
  324. }
  325. }
  326. /**
  327. * 上报或重新上报或保存修改功能
  328. * @param {int} postData - 表单提交的数据
  329. * @param {int} tenderId - 标段id
  330. * @return {void}
  331. */
  332. async save(postData, tenderId) {
  333. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tenderId)
  334. // 初始化事务
  335. this.transaction = await this.db.beginTransaction()
  336. let result = false
  337. try {
  338. // 变更令信息
  339. const changeInfo = await this.getDataByCondition({ cid: postData.cid })
  340. // 该变更令原报人信息
  341. const lastUser = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 0)
  342. // 先删除本次原有的变更审批人和清单
  343. await this.ctx.service.changeAudit.deleteAuditData(this.transaction, changeInfo.cid, changeInfo.times)
  344. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid: changeInfo.cid })
  345. let change_status = false
  346. // 获取变更令提交状态
  347. if (postData.changestatus !== undefined && parseInt(postData.changestatus) === 1) {
  348. change_status = true
  349. // 更新原报人审批状态
  350. await this.transaction.update(this.ctx.service.changeAudit.tableName, {
  351. id: lastUser.id,
  352. status: audit.flow.auditStatus.checked,
  353. sin_time: new Date()
  354. })
  355. }
  356. // 再插入postData里的变更审批人和清单
  357. if (postData.changeaudit !== undefined && postData.changeaudit !== '') {
  358. const changeAudit = postData.changeaudit.split(',')
  359. const insertCA = []
  360. let uSite = 1
  361. let uSort = parseInt(lastUser.usort) + 1
  362. for (const [index, ca] of changeAudit.entries()) {
  363. const auditInfo = ca.split('/%/')
  364. const uStatus = change_status && index === 0 ? audit.flow.auditStatus.checking : audit.flow.auditStatus.uncheck
  365. const sin_time = change_status && index === 0 ? new Date() : null
  366. const caArray = {
  367. tid: tenderId,
  368. cid: changeInfo.cid,
  369. uid: auditInfo[0],
  370. name: auditInfo[1],
  371. jobs: auditInfo[2],
  372. company: auditInfo[3],
  373. times: changeInfo.times,
  374. usite: uSite,
  375. usort: uSort,
  376. status: uStatus,
  377. sin_time
  378. }
  379. uSite++
  380. uSort++
  381. insertCA.push(caArray)
  382. console.log(change_status, index)
  383. // 添加短信通知-需要审批提醒功能
  384. if (change_status && index === 0) {
  385. // const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo[0]);
  386. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  387. // const smsType = JSON.parse(smsUser.sms_type);
  388. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  389. // const sms = new SMS(this.ctx);
  390. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi');
  391. // const content = '【纵横计量支付】' + changeInfo.code + '变更需要您审批。' + result;
  392. // sms.send(smsUser.auth_mobile, content);
  393. // }
  394. // }
  395. const sms = new SMS(this.ctx)
  396. const code = await sms.contentChange(changeInfo.code)
  397. const shenpiUrl = await this.ctx.helper.urlToShort(
  398. 'http://' + this.ctx.request.header.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi'
  399. )
  400. await this.ctx.helper.sendAliSms(auditInfo[0], smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  401. biangeng: code,
  402. code: shenpiUrl
  403. })
  404. }
  405. }
  406. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insertCA)
  407. }
  408. let changeList = []
  409. if (postData.changelist !== undefined && postData.changelist !== '') {
  410. changeList = postData.changelist.split('^_^')
  411. }
  412. let changeWhiteList = []
  413. if (postData.changewhitelist !== undefined && postData.changewhitelist !== '') {
  414. changeWhiteList = postData.changewhitelist.split('^_^')
  415. }
  416. changeList.push.apply(changeList, changeWhiteList)
  417. const insertCL = []
  418. let total_price = 0
  419. if (changeList.length > 0) {
  420. for (const cl of changeList) {
  421. const clInfo = cl.split(';')
  422. const clArray = {
  423. tid: tenderId,
  424. cid: changeInfo.cid,
  425. lid: clInfo[8],
  426. code: clInfo[0],
  427. name: clInfo[1],
  428. bwmx: clInfo[2],
  429. unit: clInfo[3],
  430. unit_price: clInfo[4],
  431. oamount: clInfo[5],
  432. camount: clInfo[6],
  433. samount: '',
  434. detail: clInfo[7],
  435. spamount: clInfo[6]
  436. }
  437. if (clInfo[4] === '') {
  438. delete clArray.unit_price
  439. }
  440. insertCL.push(clArray)
  441. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(clArray.unit_price, clArray.spamount, tenderInfo.decimal.tp))
  442. }
  443. await this.transaction.insert(this.ctx.service.changeAuditList.tableName, insertCL)
  444. }
  445. // 修改变更令基本数据
  446. const cArray = {
  447. code: postData.code,
  448. name: postData.name,
  449. peg: postData.peg,
  450. org_name: postData.org_name,
  451. org_code: postData.org_code,
  452. new_name: postData.new_name,
  453. new_code: postData.new_code,
  454. content: postData.content,
  455. basis: postData.basis,
  456. expr: postData.expr,
  457. memo: postData.memo,
  458. type: postData.type.join(','),
  459. class: postData.class,
  460. quality: postData.quality,
  461. company: postData.company,
  462. charge: postData.charge,
  463. total_price
  464. }
  465. const options = {
  466. where: {
  467. cid: changeInfo.cid
  468. }
  469. }
  470. if (change_status) {
  471. cArray.status = audit.flow.status.checking
  472. cArray.cin_time = Date.parse(new Date()) / 1000
  473. }
  474. await this.transaction.update(this.tableName, cArray, options)
  475. await this.transaction.commit()
  476. result = true
  477. } catch (error) {
  478. await this.transaction.rollback()
  479. result = false
  480. }
  481. return result
  482. }
  483. /**
  484. * 审批通过
  485. * @param {Number} pid 项目id
  486. * @param {int} postData - 表单提交的数据
  487. * @param {int} changeData - 变更令的数据
  488. * @return {void}
  489. */
  490. async approvalSuccess(pid, postData, changeData) {
  491. let tenderInfo
  492. // 初始化事务
  493. this.transaction = await this.db.beginTransaction()
  494. let result = false
  495. try {
  496. // 获取所有审核人列表
  497. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid)
  498. console.log('auditors', auditors)
  499. console.log('postData', postData)
  500. // 添加到消息推送表
  501. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId)
  502. const records = []
  503. auditors.forEach(auditor => {
  504. records.push({
  505. pid,
  506. type: pushType.change,
  507. uid: auditor.uid,
  508. status: audit.flow.status.checked,
  509. content: noticeContent
  510. })
  511. })
  512. await this.transaction.insert('zh_notice', records)
  513. // 设置审批人通过
  514. const audit_update = {
  515. id: postData.audit_id,
  516. sdesc: postData.sdesc,
  517. status: audit.flow.auditStatus.checked,
  518. sin_time: new Date()
  519. }
  520. const change_update = {
  521. w_code: postData.w_code,
  522. status: audit.flow.status.checking,
  523. cin_time: Date.parse(new Date()) / 1000
  524. }
  525. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update)
  526. // 清单数据更新
  527. const bills_list = postData.bills_list.split(',')
  528. let total_price = 0
  529. for (const bl of bills_list) {
  530. const listInfo = bl.split('_')
  531. const lid = listInfo[0]
  532. const amount = listInfo[1]
  533. const changeListInfo = await this.ctx.service.changeAuditList.getDataById(lid)
  534. if (!tenderInfo) {
  535. tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeListInfo.tid)
  536. }
  537. if (changeListInfo !== undefined) {
  538. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(changeListInfo.unit_price, amount, tenderInfo.decimal.tp))
  539. const audit_amount = changeListInfo.audit_amount !== null && changeListInfo.audit_amount !== '' ? changeListInfo.audit_amount.split(',') : []
  540. audit_amount.push(amount)
  541. const list_update = {
  542. id: lid,
  543. audit_amount: audit_amount.join(','),
  544. spamount: parseFloat(amount)
  545. }
  546. if (postData.audit_next_id === undefined) {
  547. list_update.samount = amount
  548. }
  549. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update)
  550. }
  551. }
  552. if (postData.audit_next_id === undefined) {
  553. // 变更令审批完成
  554. change_update.status = audit.flow.status.checked
  555. change_update.p_code = postData.p_code
  556. change_update.sin_time = Date.parse(new Date()) / 1000
  557. // 添加短信通知-审批通过提醒功能
  558. // const mobile_array = [];
  559. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeData.cid, changeData.times)
  560. // for (const user of auditList) {
  561. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.uid);
  562. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  563. // const smsType = JSON.parse(smsUser.sms_type);
  564. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  565. // mobile_array.push(smsUser.auth_mobile);
  566. // }
  567. // }
  568. // }
  569. // if (mobile_array.length > 0) {
  570. // const sms = new SMS(this.ctx);
  571. // const content = '【纵横计量支付】' + changeData.code + '变更,审批通过。';
  572. // sms.send(mobile_array, content);
  573. // }
  574. const users = this._.map(auditList, 'uid')
  575. const sms = new SMS(this.ctx)
  576. const code = await sms.contentChange(changeData.code)
  577. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), SmsAliConst.template.change_result, {
  578. biangeng: code,
  579. status: SmsAliConst.status.success
  580. })
  581. } else {
  582. // 设置下一个审批人为审批状态
  583. const nextAudit_update = {
  584. id: postData.audit_next_id,
  585. status: audit.flow.auditStatus.checking
  586. }
  587. await this.transaction.update(this.ctx.service.changeAudit.tableName, nextAudit_update)
  588. // 添加短信通知-需要审批提醒功能
  589. const nextAuditData = await this.ctx.service.changeAudit.getDataById(postData.audit_next_id)
  590. const sms = new SMS(this.ctx)
  591. const code = await sms.contentChange(changeData.code)
  592. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAuditData.uid);
  593. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  594. // const smsType = JSON.parse(smsUser.sms_type);
  595. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  596. // const sms = new SMS(this.ctx);
  597. // const code = await sms.contentChange(changeData.code);
  598. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi');
  599. // const content = '【纵横计量支付】' + code + '变更需要您审批。' + result;
  600. // sms.send(smsUser.auth_mobile, content);
  601. // }
  602. // }
  603. const shenpiUrl = await this.ctx.helper.urlToShort(
  604. 'http://' + this.ctx.request.header.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi'
  605. )
  606. await this.ctx.helper.sendAliSms(nextAuditData.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  607. biangeng: code,
  608. code: shenpiUrl
  609. })
  610. }
  611. change_update.total_price = total_price
  612. const options = {
  613. where: {
  614. cid: postData.change_id
  615. }
  616. }
  617. await this.transaction.update(this.tableName, change_update, options)
  618. await this.transaction.commit()
  619. result = true
  620. } catch (error) {
  621. console.log(error)
  622. await this.transaction.rollback()
  623. result = false
  624. }
  625. return result
  626. }
  627. /**
  628. * 审批终止
  629. * @param {int} postData - 表单提交的数据
  630. * @return {void}
  631. */
  632. async approvalStop(postData) {
  633. // 初始化事务
  634. this.transaction = await this.db.beginTransaction()
  635. let result = false
  636. try {
  637. // 设置审批人终止
  638. const audit_update = {
  639. id: postData.audit_id,
  640. sdesc: postData.sdesc,
  641. status: 4,
  642. sin_time: new Date()
  643. }
  644. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update)
  645. // 设置变更令终止
  646. const change_update = {
  647. w_code: postData.w_code,
  648. status: 4,
  649. cin_time: Date.parse(new Date()) / 1000
  650. }
  651. const options = {
  652. where: {
  653. cid: postData.change_id
  654. }
  655. }
  656. await this.transaction.update(this.tableName, change_update, options)
  657. await this.transaction.commit()
  658. result = true
  659. } catch (error) {
  660. await this.transaction.rollback()
  661. result = false
  662. }
  663. return result
  664. }
  665. /**
  666. * 审批退回到原报人
  667. * @param {Number} pid 项目id
  668. * @param {int} postData - 表单提交的数据
  669. * @param {int} changeData - 变更令的数据
  670. * @return {void}
  671. */
  672. async approvalBack(pid, postData, changeData) {
  673. // 初始化事务
  674. this.transaction = await this.db.beginTransaction()
  675. let result = false
  676. try {
  677. // 获取所有审核人列表
  678. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid)
  679. // 添加到消息推送表
  680. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId)
  681. const records = []
  682. auditors.forEach(auditor => {
  683. records.push({
  684. pid,
  685. type: pushType.change,
  686. uid: auditor.uid,
  687. status: audit.flow.status.backnew,
  688. content: noticeContent
  689. })
  690. })
  691. await this.transaction.insert('zh_notice', records)
  692. const changeInfo = await this.getDataByCondition({ cid: postData.change_id })
  693. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid)
  694. // 设置审批人退回
  695. const audit_update = {
  696. id: postData.audit_id,
  697. sdesc: postData.sdesc,
  698. status: audit.flow.auditStatus.back,
  699. sin_time: new Date()
  700. }
  701. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update)
  702. // 新增新一次的审批人列表
  703. // 获取当前次数审批人列表
  704. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times)
  705. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 1, 0)
  706. let usort = lastauditInfo.usort + 1
  707. const newTimes = changeInfo.times + 1
  708. const insert_audit_array = []
  709. for (const al of auditList) {
  710. const insert_audit = {
  711. tid: al.tid,
  712. cid: al.cid,
  713. uid: al.uid,
  714. name: al.name,
  715. jobs: al.jobs,
  716. company: al.company,
  717. times: newTimes,
  718. usite: al.usite,
  719. usort,
  720. status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking
  721. }
  722. insert_audit_array.push(insert_audit)
  723. usort++
  724. }
  725. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array)
  726. // 变更金额也退回
  727. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  728. where: { cid: changeInfo.cid }
  729. })
  730. let total_price = 0
  731. for (const cl of changeList) {
  732. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, tenderInfo.decimal.tp))
  733. }
  734. // 设置变更令退回
  735. const change_update = {
  736. w_code: postData.w_code,
  737. status: audit.flow.status.back,
  738. times: newTimes,
  739. cin_time: Date.parse(new Date()) / 1000,
  740. total_price
  741. }
  742. const options = {
  743. where: {
  744. cid: postData.change_id
  745. }
  746. }
  747. await this.transaction.update(this.tableName, change_update, options)
  748. await this.transaction.commit()
  749. result = true
  750. // 添加短信通知-审批退回提醒功能
  751. // const mobile_array = [];
  752. // for (const user of insert_audit_array) {
  753. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.uid);
  754. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  755. // const smsType = JSON.parse(smsUser.sms_type);
  756. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  757. // mobile_array.push(smsUser.auth_mobile);
  758. // }
  759. // }
  760. // }
  761. // if (mobile_array.length > 0) {
  762. // const sms = new SMS(this.ctx);
  763. // const code = await sms.contentChange(changeData.code);
  764. // const content = '【纵横计量支付】' + code + '变更,审批退回。';
  765. // sms.send(mobile_array, content);
  766. // }
  767. const users = this._.map(insert_audit_array, 'uid')
  768. const sms = new SMS(this.ctx)
  769. const code = await sms.contentChange(changeData.code)
  770. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), SmsAliConst.template.change_result, {
  771. biangeng: code,
  772. status: SmsAliConst.status.back
  773. })
  774. } catch (error) {
  775. await this.transaction.rollback()
  776. result = false
  777. }
  778. return result
  779. }
  780. /**
  781. * 审批退回到上一个审批人
  782. * @param {Number} pid 项目id
  783. * @param {int} postData - 表单提交的数据
  784. * @param {int} changeData - 变更令的数据
  785. * @return {void}
  786. */
  787. async approvalBackNew(pid, postData, changeData) {
  788. // 初始化事务
  789. this.transaction = await this.db.beginTransaction()
  790. let result = false
  791. try {
  792. // 获取所有审核人列表
  793. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid)
  794. // 添加到消息推送表
  795. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, this.ctx.session.sessionUser.accountId)
  796. const records = []
  797. auditors.forEach(auditor => {
  798. records.push({
  799. pid,
  800. type: pushType.change,
  801. uid: auditor.uid,
  802. status: audit.flow.status.back,
  803. content: noticeContent
  804. })
  805. })
  806. await this.transaction.insert('zh_notice', records)
  807. const changeInfo = await this.getDataByCondition({ cid: postData.change_id })
  808. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid)
  809. // 设置审批人退回
  810. const audit_update = {
  811. id: postData.audit_id,
  812. sdesc: postData.sdesc,
  813. status: audit.flow.auditStatus.backnew,
  814. sin_time: new Date()
  815. }
  816. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update)
  817. // 获取当前审批人信息
  818. const auditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_id)
  819. // 获取当前次数审批人列表
  820. const auditList = await this.ctx.service.changeAudit.getNextAuditList(changeInfo.cid, auditInfo.usort)
  821. let usort = auditInfo.usort + 1
  822. // 获取上一个审批人信息
  823. const lastauditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_last_id)
  824. // 新增2个审批人到审批列表中
  825. const insert_audit1 = {
  826. tid: lastauditInfo.tid,
  827. cid: lastauditInfo.cid,
  828. uid: lastauditInfo.uid,
  829. name: lastauditInfo.name,
  830. jobs: lastauditInfo.jobs,
  831. company: lastauditInfo.company,
  832. times: lastauditInfo.times,
  833. usite: lastauditInfo.usite,
  834. usort,
  835. status: audit.flow.auditStatus.checking
  836. }
  837. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1)
  838. usort++
  839. // 新增2个审批人到审批列表中
  840. const insert_audit2 = {
  841. tid: auditInfo.tid,
  842. cid: auditInfo.cid,
  843. uid: auditInfo.uid,
  844. name: auditInfo.name,
  845. jobs: auditInfo.jobs,
  846. company: auditInfo.company,
  847. times: auditInfo.times,
  848. usite: auditInfo.usite,
  849. usort,
  850. status: audit.flow.auditStatus.uncheck
  851. }
  852. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2)
  853. // 把接下未审批的审批人排序都加2
  854. for (const al of auditList) {
  855. const audit_update = {
  856. id: al.id,
  857. usort: al.usort + 2
  858. }
  859. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update)
  860. }
  861. // 审批列表数据也要回退
  862. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  863. where: { cid: changeInfo.cid }
  864. })
  865. let total_price = 0
  866. for (const cl of changeList) {
  867. const audit_amount = cl.audit_amount.split(',')
  868. const last_amount = audit_amount[audit_amount.length - 1]
  869. audit_amount.splice(-1, 1)
  870. const list_update = {
  871. id: cl.id,
  872. audit_amount: audit_amount.join(','),
  873. spamount: parseFloat(last_amount)
  874. }
  875. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tenderInfo.decimal.tp))
  876. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update)
  877. }
  878. // 设置变更令退回
  879. const change_update = {
  880. w_code: postData.w_code,
  881. status: audit.flow.status.backnew,
  882. cin_time: Date.parse(new Date()) / 1000,
  883. total_price
  884. }
  885. const options = {
  886. where: {
  887. cid: postData.change_id
  888. }
  889. }
  890. await this.transaction.update(this.tableName, change_update, options)
  891. await this.transaction.commit()
  892. result = true
  893. // 添加短信通知-需要审批提醒功能
  894. // const smsUser = await this.ctx.service.projectAccount.getDataById(lastauditInfo.uid);
  895. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  896. // const smsType = JSON.parse(smsUser.sms_type);
  897. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  898. // const sms = new SMS(this.ctx);
  899. // const code = await sms.contentChange(changeData.code);
  900. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi');
  901. // const content = '【纵横计量支付】' + code + '变更需要您审批。' + result;
  902. // sms.send(smsUser.auth_mobile, content);
  903. // }
  904. // }
  905. const sms = new SMS(this.ctx)
  906. const code = await sms.contentChange(changeData.code)
  907. const shenpiUrl = await this.ctx.helper.urlToShort(
  908. 'http://' + this.ctx.request.header.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi'
  909. )
  910. await this.ctx.helper.sendAliSms(lastauditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  911. biangeng: code,
  912. code: shenpiUrl
  913. })
  914. } catch (error) {
  915. await this.transaction.rollback()
  916. result = false
  917. }
  918. return result
  919. }
  920. /**
  921. * 查询可用的变更令
  922. * @param bills - 查询的清单
  923. * @param pos - 查询的部位
  924. * @return {Promise<*>} - 可用的变更令列表
  925. */
  926. async getValidChanges(tid, bills, pos) {
  927. const timesLen = 100
  928. const filter =
  929. 'cb.`code` = ' +
  930. this.db.escape(bills.b_code) +
  931. ' And cb.`name` = ' +
  932. this.db.escape(bills.name) +
  933. ' And cb.`unit` = ' +
  934. this.db.escape(bills.unit) +
  935. ' And cb.`unit_price` = ' +
  936. this.db.escape(bills.unit_price) +
  937. (pos ? ' And cb.`bwmx` = ' + this.db.escape(pos.name) : '')
  938. const sql =
  939. '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,' +
  940. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  941. ' 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, ' +
  942. ' scb.used_amount' +
  943. ' FROM ' +
  944. this.tableName +
  945. ' As c ' +
  946. ' Left Join ' +
  947. this.ctx.service.changeAuditList.tableName +
  948. ' As cb On c.cid = cb.cid ' +
  949. ' Left Join (' +
  950. ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
  951. ' FROM ' +
  952. this.ctx.service.stageChange.tableName +
  953. ' As sc' +
  954. ' INNER JOIN (SELECT MAX(`stimes` * ' +
  955. timesLen +
  956. ' + `sorder`) As `flow`, cbid, sid ' +
  957. ' FROM ' +
  958. this.ctx.service.stageChange.tableName +
  959. ' WHERE tid = ?' +
  960. ' GROUP BY cbid, sid' +
  961. ' ) As MF' +
  962. ' ON (sc.stimes * ' +
  963. timesLen +
  964. ' + sc.sorder) = MF.flow And sc.cbid = MF.cbid And sc.sid = MF.sid' +
  965. ' GROUP BY sc.cbid' +
  966. ' ) As scb ON cb.id = scb.cbid' +
  967. ' WHERE c.tid = ? And c.status = ? And c.valid And ' +
  968. filter +
  969. ' ORDER BY c.in_time'
  970. const sqlParam = [tid, tid, audit.flow.status.checked]
  971. const changes = await this.db.query(sql, sqlParam)
  972. for (const c of changes) {
  973. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' + ' FROM ?? As ca ' + ' Left Join ?? As pa ' + ' On ca.uid = pa.id ' + ' Where ca.cid = ?'
  974. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid]
  975. c.attachments = await this.db.query(aSql, aSqlParam)
  976. }
  977. return changes
  978. }
  979. /**
  980. * 查询变更令 + 变更令执行
  981. * @param tid
  982. * @return {Promise<void>}
  983. */
  984. async getChangeAndUsedInfo(tid) {
  985. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true)
  986. let filter
  987. if (lastStage.id === this.ctx.stage.id) {
  988. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))', [
  989. lastStage.order,
  990. lastStage.order,
  991. this.ctx.stage.curTimes,
  992. this.ctx.stage.curTimes,
  993. this.ctx.stage.curOrder
  994. ])
  995. } else {
  996. if (lastStage.status === audit.stage.status.uncheck) {
  997. filter = ' And s.order < ' + lastStage.order
  998. } else if (lastStage.status === audit.stage.status.checked) {
  999. filter = ''
  1000. } else if (lastStage.status === audit.stage.status.checkNo) {
  1001. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And sChange.`stimes` <= ?))', [lastStage.order, lastStage.order, lastStage.times])
  1002. } else {
  1003. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times)
  1004. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))', [
  1005. lastStage.order,
  1006. lastStage.order,
  1007. lastStage.times,
  1008. lastStage.times,
  1009. curAuditor.order - 1
  1010. ])
  1011. }
  1012. }
  1013. const sql =
  1014. 'SELECT C.*, Sum(U.utp) As used_tp, Round(Sum(U.utp) / C.total_price * 100, 2) As used_pt' +
  1015. ' FROM ' +
  1016. this.tableName +
  1017. ' As C' +
  1018. ' LEFT JOIN (SELECT sc.tid, sc.cid, sc.cbid, Round(SUM(sc.qty) * cb.unit_price, ?) As utp' +
  1019. ' FROM ' +
  1020. this.ctx.service.stageChange.tableName +
  1021. ' As sc' +
  1022. ' INNER JOIN (' +
  1023. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid` ' +
  1024. ' FROM ' +
  1025. this.ctx.service.stageChange.tableName +
  1026. ' As sChange ' +
  1027. ' LEFT JOIN ' +
  1028. this.ctx.service.stage.tableName +
  1029. ' As s' +
  1030. ' ON sChange.sid = s.id' +
  1031. ' WHERE sChange.tid = ?' +
  1032. filter +
  1033. ' GROUP By `lid`, `pid`, `cbid`, `sid`' +
  1034. ' ) As m' +
  1035. ' 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`' +
  1036. ' LEFT JOIN ' +
  1037. this.ctx.service.changeAuditList.tableName +
  1038. ' As cb ON sc.cbid = cb.id' +
  1039. ' GROUP By sc.`cbid`' +
  1040. ' ) As U ON C.cid = U.cid' +
  1041. ' WHERE C.tid = ? And C.status = ? And C.valid' +
  1042. ' GROUP By C.cid' +
  1043. ' ORDER By in_time'
  1044. const sqlParam = [this.ctx.tender.info.decimal.tp, tid, tid, audit.flow.status.checked]
  1045. return await this.db.query(sql, sqlParam)
  1046. }
  1047. /**
  1048. * 查询可用的变更令
  1049. * @param { string } cid - 查询的清单
  1050. * @return {Promise<*>} - 可用的变更令列表
  1051. */
  1052. async delete(cid) {
  1053. // 初始化事务
  1054. this.transaction = await this.db.beginTransaction()
  1055. let result = false
  1056. try {
  1057. // 先删除清单,审批人列表
  1058. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid })
  1059. await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid })
  1060. // 再删除附件和附件文件ni zuo
  1061. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } })
  1062. if (attList.length !== 0) {
  1063. for (const att of attList) {
  1064. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath))
  1065. }
  1066. await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid })
  1067. }
  1068. // 最后删除变更令
  1069. await this.transaction.delete(this.tableName, { cid })
  1070. await this.transaction.commit()
  1071. result = true
  1072. } catch (e) {
  1073. await this.transaction.rollback()
  1074. result = false
  1075. }
  1076. return result
  1077. }
  1078. /**
  1079. * 重新审批变更令
  1080. * @param { string } cid - 查询的清单
  1081. * @return {Promise<*>} - 可用的变更令列表
  1082. */
  1083. async checkAgain(cid) {
  1084. // 初始化事务
  1085. this.transaction = await this.db.beginTransaction()
  1086. let result = false
  1087. try {
  1088. const changeInfo = await this.getDataByCondition({ cid })
  1089. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid)
  1090. // 获取终审
  1091. const auditInfo = (
  1092. await this.ctx.service.changeAudit.getAllDataByCondition({
  1093. where: { cid },
  1094. orders: [['usort', 'desc']],
  1095. limit: 1,
  1096. offset: 0
  1097. })
  1098. )[0]
  1099. let usort = auditInfo.usort + 1
  1100. // 新增2个审批状态到审批列表中
  1101. const insert_audit1 = {
  1102. tid: auditInfo.tid,
  1103. cid: auditInfo.cid,
  1104. uid: auditInfo.uid,
  1105. name: auditInfo.name,
  1106. jobs: auditInfo.jobs,
  1107. company: auditInfo.company,
  1108. times: auditInfo.times,
  1109. usite: auditInfo.usite,
  1110. usort,
  1111. sin_time: new Date(),
  1112. status: audit.flow.auditStatus.checkAgain
  1113. }
  1114. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1)
  1115. usort++
  1116. // 新增2个审批人到审批列表中
  1117. const insert_audit2 = {
  1118. tid: auditInfo.tid,
  1119. cid: auditInfo.cid,
  1120. uid: auditInfo.uid,
  1121. name: auditInfo.name,
  1122. jobs: auditInfo.jobs,
  1123. company: auditInfo.company,
  1124. times: auditInfo.times,
  1125. usite: auditInfo.usite,
  1126. usort,
  1127. status: audit.flow.auditStatus.checking
  1128. }
  1129. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2)
  1130. // 审批列表数据也要回退
  1131. let total_price = 0
  1132. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({
  1133. where: { cid: changeInfo.cid }
  1134. })
  1135. for (const cl of changeList) {
  1136. const audit_amount = cl.audit_amount.split(',')
  1137. const last_amount = audit_amount[audit_amount.length - 1]
  1138. audit_amount.splice(-1, 1)
  1139. const list_update = {
  1140. id: cl.id,
  1141. audit_amount: audit_amount.join(','),
  1142. samount: ''
  1143. }
  1144. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tenderInfo.decimal.tp))
  1145. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update)
  1146. }
  1147. // 设置变更令审批中
  1148. const change_update = {
  1149. p_code: null,
  1150. status: audit.flow.status.checking,
  1151. cin_time: Date.parse(new Date()) / 1000,
  1152. sin_time: null,
  1153. total_price
  1154. }
  1155. const options = {
  1156. where: {
  1157. cid: changeInfo.cid
  1158. }
  1159. }
  1160. await this.transaction.update(this.tableName, change_update, options)
  1161. await this.transaction.commit()
  1162. result = true
  1163. // 添加短信通知-需要审批提醒功能
  1164. // const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo.uid);
  1165. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  1166. // const smsType = JSON.parse(smsUser.sms_type);
  1167. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  1168. // const sms = new SMS(this.ctx);
  1169. // const code = await sms.contentChange(changeInfo.code);
  1170. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi');
  1171. // const content = '【纵横计量支付】' + code + '变更需要您审批。' + result;
  1172. // sms.send(smsUser.auth_mobile, content);
  1173. // }
  1174. // }
  1175. const sms = new SMS(this.ctx)
  1176. const code = await sms.contentChange(changeInfo.code)
  1177. const shenpiUrl = await this.ctx.helper.urlToShort(
  1178. 'http://' + this.ctx.request.header.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi'
  1179. )
  1180. await this.ctx.helper.sendAliSms(auditInfo.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, {
  1181. biangeng: code,
  1182. code: shenpiUrl
  1183. })
  1184. } catch (error) {
  1185. await this.transaction.rollback()
  1186. result = false
  1187. }
  1188. return result
  1189. }
  1190. /**
  1191. * 判断是否有重名的变更令
  1192. * @param cid
  1193. * @param code
  1194. * @param tid
  1195. * @return {Promise<void>}
  1196. */
  1197. async isRepeat(cid, code, tid) {
  1198. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?)) AND `cid` != ? AND `tid` = ?'
  1199. const sqlParam = [this.tableName, code, audit.flow.status.checked, code, audit.flow.status.checked, cid, tid]
  1200. const result = await this.db.queryOne(sql, sqlParam)
  1201. return result.count !== 0
  1202. }
  1203. async getAllCheckedChanges(tid) {
  1204. return await this.getAllDataByCondition({
  1205. where: { tid, status: audit.flow.status.checked },
  1206. orders: [['in_time', 'desc']]
  1207. })
  1208. }
  1209. /**
  1210. * 用于添加推送所需的content内容
  1211. * @param {Number} pid 项目id
  1212. * @param {Number} tid 台账id
  1213. * @param {Number} cid 变更id
  1214. * @param {Number} uid 审核人id
  1215. */
  1216. async getNoticeContent(pid, tid, cid, uid) {
  1217. const noticeSql =
  1218. 'SELECT * FROM (SELECT ' +
  1219. ' t.`id` As `tid`, t.`name`, c.`cid`, c.`code` As `c_code`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1220. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1221. ' LEFT JOIN ?? As c ON c.`cid` = ?' +
  1222. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1223. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`'
  1224. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.change.tableName, cid, this.ctx.service.projectAccount.tableName, uid, pid]
  1225. const content = await this.db.query(noticeSql, noticeSqlParam)
  1226. return content.length ? JSON.stringify(content[0]) : ''
  1227. }
  1228. }
  1229. return Change
  1230. }