shenpi_audit.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. /**
  3. * 版本数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. const shenpiConst = require('../const/shenpi');
  10. module.exports = app => {
  11. class ShenpiAudit extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'shenpi_audit';
  21. }
  22. async getAudit(tid, type, status) {
  23. const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  24. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ?';
  25. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  26. return await this.db.queryOne(sql, sqlParam);
  27. }
  28. async getAuditList(tid, type, status) {
  29. const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  30. ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? ORDER BY sp.id ASC';
  31. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];
  32. return await this.db.query(sql, sqlParam);
  33. }
  34. async addAudit(data) {
  35. const insertData = {
  36. tid: this.ctx.tender.id,
  37. sp_type: data.code,
  38. sp_status: data.status,
  39. audit_id: data.audit_id,
  40. };
  41. const result = await this.db.insert(this.tableName, insertData);
  42. return result.effectRows === 1;
  43. }
  44. async removeAudit(data) {
  45. const delData = {
  46. tid: this.ctx.tender.id,
  47. sp_type: data.code,
  48. sp_status: data.status,
  49. audit_id: data.audit_id,
  50. };
  51. return await this.db.delete(this.tableName, delData);
  52. }
  53. async copyAudit2otherTender(data) {
  54. const transaction = await this.db.beginTransaction();
  55. try {
  56. const shenpi_status = parseInt(data.status);
  57. // 1.复制修改当前审批到其他的tender_info里
  58. // 2.删除其他的shenpiAudit
  59. // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)
  60. const tenderInfoUpdateList = [];
  61. const tenders = [];
  62. for (const tid of data.tidList.split(',')) {
  63. // 获取原报
  64. const tender = await this.ctx.service.tender.getDataById(tid);
  65. if (tender) {
  66. tenders.push({ id: parseInt(tid), user_id: tender.user_id });
  67. const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tid);
  68. // 把当前期状态复制到其他标段里
  69. if (shenpiInfo[data.code] !== shenpi_status) {
  70. shenpiInfo[data.code] = shenpi_status;
  71. tenderInfoUpdateList.push({ row: { shenpi: JSON.stringify(shenpiInfo) }, where: { tid: parseInt(tid) } });
  72. }
  73. }
  74. }
  75. if (tenderInfoUpdateList.length > 0) await transaction.updateRows(this.ctx.service.tenderInfo.tableName, tenderInfoUpdateList);
  76. const insertList = [];
  77. const needYB = ['ledger', 'revise', 'change'];
  78. const canYB = needYB.indexOf(data.code) !== -1;
  79. for (const t of tenders) {
  80. if (shenpi_status !== shenpiConst.sp_status.sqspr) {
  81. await transaction.delete(this.tableName, { tid: t.id, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status });
  82. for (const aid of data.aidList.split(',')) {
  83. if (aid !== t.user_id || (aid === t.user_id && canYB)) {
  84. const insertData = {
  85. tid: t.id,
  86. sp_type: shenpiConst.sp_type[data.code],
  87. sp_status: shenpi_status,
  88. audit_id: parseInt(aid),
  89. };
  90. insertList.push(insertData);
  91. }
  92. }
  93. }
  94. }
  95. // console.log(tenderInfoUpdateList, insertList);
  96. if (insertList.length > 0) await transaction.insert(this.tableName, insertList);
  97. await transaction.commit();
  98. return true;
  99. } catch (err) {
  100. await transaction.rollback();
  101. throw err;
  102. }
  103. }
  104. }
  105. return ShenpiAudit;
  106. };