change.js 64 KB

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