material_bills.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. /**
  3. * 期计量 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. module.exports = app => {
  11. class MaterialBills extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'material_bills';
  21. }
  22. /**
  23. * 添加工料
  24. * @return {void}
  25. */
  26. async add() {
  27. if (!this.ctx.tender || !this.ctx.material) {
  28. throw '数据错误';
  29. }
  30. const newBills = {
  31. tid: this.ctx.tender.id,
  32. mid: this.ctx.material.id,
  33. in_time: new Date(),
  34. };
  35. // 新增工料
  36. const result = await this.db.insert(this.tableName, newBills);
  37. if (result.affectedRows !== 1) {
  38. throw '新增工料数据失败';
  39. }
  40. return await this.getDataById(result.insertId);
  41. }
  42. /**
  43. * 删除工料
  44. * @param {int} id 工料id
  45. * @return {void}
  46. */
  47. async del(id) {
  48. if (!this.ctx.tender || !this.ctx.material) {
  49. throw '数据错误';
  50. }
  51. // 判断是否可删
  52. return await this.deleteById(id);
  53. }
  54. /**
  55. * 修改工料信息
  56. * @param {Object} data 工料内容
  57. * @return {void}
  58. */
  59. async save(data) {
  60. if (!this.ctx.tender || !this.ctx.material) {
  61. throw '数据错误';
  62. }
  63. delete data.in_time;
  64. // 判断是否可修改
  65. return await this.db.update(this.tableName, data);
  66. }
  67. }
  68. return MaterialBills;
  69. };