schedule_audit.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. const scheduleConst = require('../const/schedule');
  3. module.exports = app => {
  4. class ScheduleAudit extends app.BaseService {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.tableName = 'schedule_audit';
  8. }
  9. async addAudit(data) {
  10. const transaction = await this.db.beginTransaction();
  11. try {
  12. const insertData = {
  13. tid: this.ctx.tender.id,
  14. audit_id: data.audit_id,
  15. in_time: new Date(),
  16. };
  17. const result = await transaction.insert(this.tableName, insertData);
  18. await transaction.commit();
  19. return await this.getDataById(result.insertId);
  20. } catch (err) {
  21. await transaction.rollback();
  22. throw err;
  23. }
  24. }
  25. async removeAudit(data) {
  26. const transaction = await this.db.beginTransaction();
  27. try {
  28. await transaction.delete(this.tableName, { id: data.id });
  29. await transaction.commit();
  30. return true;
  31. } catch (err) {
  32. await transaction.rollback();
  33. throw err;
  34. }
  35. }
  36. async editAudit(data) {
  37. const transaction = await this.db.beginTransaction();
  38. try {
  39. if (!this._.includes(this._.values(scheduleConst.permission), data.permission)) {
  40. throw '修改失败';
  41. }
  42. await transaction.update(this.tableName, { id: data.id, permission: data.permission });
  43. await transaction.commit();
  44. return true;
  45. } catch (err) {
  46. await transaction.rollback();
  47. throw err;
  48. }
  49. }
  50. async setOtherTender(tidList, userList) {
  51. // 根据标段找出创建人去除,已存在的先删除再插入
  52. const transaction = await this.db.beginTransaction();
  53. try {
  54. const tenderList = await this.ctx.service.tender.getAllDataByCondition({
  55. columns: ['id', 'user_id'],
  56. where: { id: tidList.split(',') },
  57. });
  58. const oldTouristList = await this.getAllDataByCondition({ where: { tid: tidList.split(',') } });
  59. const insertData = [];
  60. const deleteIdData = [];
  61. for (const user of userList) {
  62. for (const t of tenderList) {
  63. const delId = this._.find(oldTouristList, { tid: t.id, user_id: user.uid });
  64. if (delId) deleteIdData.push(delId.id);
  65. if (user.uid !== t.user_id) {
  66. insertData.push({
  67. tid: t.id,
  68. audit_id: user.uid,
  69. permission: user.permission,
  70. in_time: new Date(),
  71. });
  72. }
  73. }
  74. }
  75. if (deleteIdData.length > 0) await transaction.delete(this.tableName, { id: deleteIdData });
  76. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  77. await transaction.commit();
  78. return true;
  79. } catch (err) {
  80. await transaction.rollback();
  81. throw err;
  82. }
  83. }
  84. }
  85. return ScheduleAudit;
  86. };