measure.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. /**
  3. * 中间计量数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/6/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').flow;
  10. const moment = require('moment');
  11. module.exports = app => {
  12. class Measure extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'measure';
  22. }
  23. /**
  24. * 新增中间计量
  25. * @param code - 编号
  26. * @param date - 年月
  27. * @param stage - 所属期
  28. * @returns {Promise<void>}
  29. */
  30. async add(tenderId, code, userId, date, stage) {
  31. const count = await this.ctx.service.measure.count({
  32. tender_id: tenderId, code: code,
  33. });
  34. if (count === 0) {
  35. const newData = {
  36. tender_id: tenderId,
  37. mid: this.uuid.v4(),
  38. code: code,
  39. in_time: new Date(),
  40. user_id: userId,
  41. status: auditConst.status.uncheck,
  42. valid: true,
  43. };
  44. const result = await this.ctx.service.measure.db.insert(this.tableName, newData);
  45. if (result.affectedRow === 0) {
  46. throw '新增中间计量失败';
  47. }
  48. } else {
  49. throw '您输入的编号已存在';
  50. }
  51. }
  52. async invalid(mid, userId) {
  53. const measure = this.getDataByCondition({mid: mid});
  54. if (measure.user_id === userId) {
  55. const result = await this.update({valid: false}, {mid: mid});
  56. return result.affectedRow === 0;
  57. } else {
  58. throw '数据错误';
  59. }
  60. }
  61. async _getAssistData(m) {
  62. m.in_time_str = moment(m.in_time_str).format('YYYYMM');
  63. if (m.times > 1 && m.status === auditConst.status.checkNo) {
  64. m.curAuditor = await this.ctx.service.measureAudit.getAuditor(m.mid, m.user_id, m.times);
  65. } else {
  66. m.curAuditor = await this.ctx.service.measureAudit.getCurAuditor(m.mid, m.times);
  67. }
  68. m.user = await this.ctx.service.projectAccount.getAccountInfoById(m.user_id);
  69. }
  70. async joinMeasure(tenderId, audit_id) {
  71. const sql = 'SELECT M.* FROM ?? As M, ?? As MA ' +
  72. ' WHERE M.`tender_id` = ? and MA.`audit_id` = ? and M.times = MA.times and MA.`order` > 0 and M.valid = true';
  73. const sqlParam = [this.tableName, this.ctx.service.measureAudit.tableName, tenderId, audit_id];
  74. const measures = await this.db.query(sql, sqlParam);
  75. for (const m of measures) {
  76. await this._getAssistData(m);
  77. }
  78. return measures;
  79. }
  80. async measureList(condition) {
  81. const measures = await this.getAllDataByCondition({where: condition});
  82. for (const m of measures) {
  83. await this._getAssistData(m);
  84. }
  85. return measures;
  86. }
  87. async measureData (condition) {
  88. const measure = await this.getDataByCondition(condition);
  89. await this._getAssistData(measure);
  90. return measure;
  91. }
  92. }
  93. return Measure;
  94. };