package smithy import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/event" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx" ) type modelTask struct { modules.MCompModel module *Smithy } func (this *modelTask) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = string(comm.TableSmithyTask) err = this.MCompModel.Init(service, module, comp, options) this.module = module.(*Smithy) this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, }) event.Register(comm.EventBuriedComplete, this.TCondFinishNotify) return } func (this *modelTask) getTaskRecord(uid string) (*pb.DBTujianTask, error) { tt := &pb.DBTujianTask{Uid: uid} if err := this.Get(uid, tt); err != nil { return tt, err } return tt, nil } func (this *modelTask) TCondFinishNotify(uid string, conds []*pb.ConIProgress) { this.module.Debug("铁匠铺任务通知", log.Field{Key: "uid", Value: uid}, log.Field{Key: "conds", Value: conds}) confList, err := this.module.configure.GetSmithyTasks() if err != nil { this.module.Error(err.Error()) return } condMap := make(map[int32]*pb.ConIProgress) for _, conf := range confList { for _, cond := range conds { if cond.Conid == conf.TypeId { condMap[conf.Key] = cond } } } update := make(map[string]interface{}) dt, err := this.getTaskRecord(uid) if err != nil { if err == mongo.ErrNoDocuments { for k, v := range condMap { tt := &pb.TujianTask{ TaskId: k, Cond: v, } if v.State == pb.BuriedItemFinishState_buried_finish { tt.Received = 1 } dt.Tasks = append(dt.Tasks, tt) if err := this.Add(uid, dt); err != nil { this.module.Error(err.Error()) return } return } } else { this.module.Error(err.Error()) return } } if dt.Tasks == nil { for k, v := range condMap { tt := &pb.TujianTask{ TaskId: k, Cond: v, } if v.State == pb.BuriedItemFinishState_buried_finish { tt.Received = 1 } dt.Tasks = append(dt.Tasks, tt) update["tasks"] = dt.Tasks if err := this.Change(uid, update); err != nil { this.module.Error(err.Error()) return } return } } for _, t := range dt.Tasks { for k, v := range condMap { // update if t.TaskId == k { t.Cond = v } else { //add dt.Tasks = append(dt.Tasks, &pb.TujianTask{ TaskId: k, Cond: v, }) } } } update["tasks"] = dt.Tasks if len(update) > 0 { if err := this.Change(uid, update); err != nil { this.module.Error(err.Error()) return } } } func (this *modelTask) updateTaskRecord(uid string, taskId int32) error { // if !this.checkTaskStatus(uid, taskId) { // return comm.NewCustomError(pb.ErrorCode_SmithyTaskNoFinished) // } dt, err := this.getTaskRecord(uid) if err != nil { if err == mongo.ErrNoDocuments { // tj := &pb.DBTujianTask{Uid: uid} // tj.Tasks = append(tj.Tasks, &pb.TujianTask{TaskId: taskId, Received: 2}) // return this.Add(uid, tj) return comm.NewCustomError(pb.ErrorCode_SmithyTaskNoFinished) } return err } // taskMap := make(map[int32]int32) update := make(map[string]interface{}) if dt.Uid != "" { for _, v := range dt.Tasks { if v.TaskId == taskId { if v.Cond != nil && v.Cond.State == pb.BuriedItemFinishState_buried_finish { v.Received = 2 } } else { return comm.NewCustomError(pb.ErrorCode_SmithyTaskNoFinished) } // taskMap[v.TaskId] = v.Received // } } // if t, ok := taskMap[taskId]; ok { // if t == 2 { // return comm.NewCustomError(pb.ErrorCode_SmithyTaskReceived) // } else { // update["received"] = 2 // } // } else { // dt.Tasks = append(dt.Tasks, &pb.TujianTask{TaskId: taskId, Received: 2}) update["tasks"] = dt.Tasks } if len(update) > 0 { if err := this.Change(uid, update); err != nil { return err } } return nil } // 检查任务状态 // func (this *modelTask) checkTaskStatus(uid string, taskId int32) bool { // conf, _ := this.module.configure.GetSmithyTask(taskId) // if conf == nil { // return false // } // // if ec := this.module.ModuleRtask.CheckCondi(uid, conf.TypeId); ec != nil { // // return false // // } // return true // }