lrf vor 2 Jahren
Ursprung
Commit
eb01970df9

+ 26 - 2
app/service/relation/relationCoachSchool.js

@@ -3,12 +3,15 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const _ = require('lodash');
 const assert = require('assert');
 const assert = require('assert');
+const Transaction = require('mongoose-transactions');
 
 
 //
 //
 class RelationCoachSchoolService extends CrudService {
 class RelationCoachSchoolService extends CrudService {
   constructor(ctx) {
   constructor(ctx) {
     super(ctx, 'relationcoachschool');
     super(ctx, 'relationcoachschool');
     this.model = this.ctx.model.Relation.RelationCoachSchool;
     this.model = this.ctx.model.Relation.RelationCoachSchool;
+    this.coachModel = this.ctx.model.User.Coach;
+    this.tran = new Transaction();
   }
   }
 
 
   // 创建关系前,先查下有没有,有就别创建了
   // 创建关系前,先查下有没有,有就别创建了
@@ -22,11 +25,32 @@ class RelationCoachSchoolService extends CrudService {
   }
   }
 
 
   async afterQuery(filter, data) {
   async afterQuery(filter, data) {
-    // console.log(data);
     data = JSON.parse(JSON.stringify(data));
     data = JSON.parse(JSON.stringify(data));
     return data;
     return data;
   }
   }
-
+  // 删除关系,同时将用户变为不同用户
+  async delete(filter) {
+    assert(filter);
+    filter = await this.beforeDelete(filter);
+    const { _id, id } = filter;
+    try {
+      const data = await this.model.findById(_id || id);
+      if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要删除的数据');
+      const { coach_id } = data;
+      const coach = await this.coachModel.findById(coach_id);
+      const { user_id } = coach;
+      this.tran.remove('RelationCoachSchool', _id || id);
+      this.tran.remove('Coach', coach_id);
+      this.tran.update('User', user_id, { type: '0' });
+      await this.tran.run();
+    } catch (error) {
+      await this.tran.rollback();
+      throw new Error(error);
+    } finally {
+      this.tran.clean();
+    }
+    return 'deleted';
+  }
 }
 }
 
 
 module.exports = RelationCoachSchoolService;
 module.exports = RelationCoachSchoolService;

+ 30 - 1
app/service/relation/relationStudentSchool.js

@@ -3,20 +3,49 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const _ = require('lodash');
 const assert = require('assert');
 const assert = require('assert');
+const Transaction = require('mongoose-transactions');
 
 
 //
 //
 class RelationStudentSchoolService extends CrudService {
 class RelationStudentSchoolService extends CrudService {
   constructor(ctx) {
   constructor(ctx) {
     super(ctx, 'relationstudentschool');
     super(ctx, 'relationstudentschool');
     this.model = this.ctx.model.Relation.RelationStudentSchool;
     this.model = this.ctx.model.Relation.RelationStudentSchool;
+    this.studentModel = this.ctx.model.User.Student;
+    this.tran = new Transaction();
   }
   }
   // 创建关系前,先查下有没有,有就别创建了
   // 创建关系前,先查下有没有,有就别创建了
   async beforeCreate(data) {
   async beforeCreate(data) {
     const { student_id, school_id } = data;
     const { student_id, school_id } = data;
     const num = await this.model.count({ student_id, school_id });
     const num = await this.model.count({ student_id, school_id });
-    if (num <= 0) { return data; }
+    if (num <= 0) {
+      return data;
+    }
     throw new BusinessError(ErrorCode.DATA_EXISTED, '学员已入学,无需重复添加');
     throw new BusinessError(ErrorCode.DATA_EXISTED, '学员已入学,无需重复添加');
   }
   }
+
+  // 删除关系,同时将用户变为不同用户
+  async delete(filter) {
+    assert(filter);
+    filter = await this.beforeDelete(filter);
+    const { _id, id } = filter;
+    try {
+      const data = await this.model.findById(_id || id);
+      if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要删除的数据');
+      const { student_id } = data;
+      const student = await this.studentModel.findById(student_id);
+      const { user_id } = student;
+      this.tran.remove('RelationStudentSchool', _id || id);
+      this.tran.remove('Student', student_id);
+      this.tran.update('User', user_id, { type: '0' });
+      await this.tran.run();
+    } catch (error) {
+      await this.tran.rollback();
+      throw new Error(error);
+    } finally {
+      this.tran.clean();
+    }
+    return 'deleted';
+  }
 }
 }
 
 
 module.exports = RelationStudentSchoolService;
 module.exports = RelationStudentSchoolService;

+ 1 - 0
package.json

@@ -14,6 +14,7 @@
     "egg-scripts": "^2.11.0",
     "egg-scripts": "^2.11.0",
     "lodash": "^4.17.21",
     "lodash": "^4.17.21",
     "moment": "^2.29.1",
     "moment": "^2.29.1",
+    "mongoose-transactions": "^1.1.4",
     "naf-framework-mongoose-free": "^0.0.32"
     "naf-framework-mongoose-free": "^0.0.32"
   },
   },
   "devDependencies": {
   "devDependencies": {