measure.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. /**
  3. * 中间计量数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/6/27
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Measure extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'measure';
  20. }
  21. /**
  22. * 新增中间计量
  23. * @param code - 编号
  24. * @param date - 年月
  25. * @param stage - 所属期
  26. * @returns {Promise<void>}
  27. */
  28. async add(tenderId, code, userId, date, stage) {
  29. const count = await this.ctx.service.measure.count({
  30. tender_id: tenderId, code: code,
  31. });
  32. if (count === 0) {
  33. const newData = {
  34. tender_id: tenderId,
  35. mid: this.uuid.v4(),
  36. code: code,
  37. in_time: new Date(),
  38. userId: userId,
  39. };
  40. const result = await this.ctx.service.measure.db.insert(this.tableName, newData);
  41. if (result.affectedRow === 0) {
  42. throw '新增中间计量失败';
  43. }
  44. } else {
  45. throw '您输入的编号已存在';
  46. }
  47. }
  48. async joinMeasure(tenderId, audit_id) {
  49. const sql = 'SELECT M.* FROM ?? As M, ?? As MA ' +
  50. ' WHERE M.`tender_id` = ? and MA.`audit_id` = ? and M.times = MA.times and MA.`order` > 0';
  51. const sqlParam = [this.tableName, this.ctx.service.measureAudit.tableName, tenderId, audit_id];
  52. return await this.db.query(sql, sqlParam);
  53. }
  54. }
  55. return Measure;
  56. };