116 lines
2.6 KiB
Go
116 lines
2.6 KiB
Go
package service
|
|
|
|
import (
|
|
json2 "encoding/json"
|
|
"github.com/tidwall/gjson"
|
|
"gorm.io/gorm"
|
|
"oasis/pkg/config"
|
|
httper2 "oasis/pkg/utils/httper"
|
|
loger2 "oasis/pkg/utils/loger"
|
|
"oasis/service/model"
|
|
"oasis/types"
|
|
"strconv"
|
|
)
|
|
|
|
type TaskService interface {
|
|
List(desc bool) []model.TaskDBModel
|
|
Delete(id string)
|
|
Add(m *model.TaskDBModel)
|
|
Update(m *model.TaskDBModel)
|
|
Info(id string) model.TaskDBModel
|
|
SyncTaskService()
|
|
}
|
|
|
|
type taskService struct {
|
|
db *gorm.DB
|
|
log loger2.OLog
|
|
}
|
|
|
|
func (s *taskService) List(desc bool) []model.TaskDBModel {
|
|
var list []model.TaskDBModel
|
|
var orderBy string
|
|
if !desc {
|
|
orderBy = "id"
|
|
} else {
|
|
orderBy = "id DESC"
|
|
}
|
|
s.db.Order(orderBy).Where("state=?", types.TASK_STATE_UNCOMPLETE).Find(&list)
|
|
return list
|
|
}
|
|
|
|
func (s *taskService) Delete(id string) {
|
|
var m model.TaskDBModel
|
|
s.db.Where("id = ?", id).Delete(&m)
|
|
}
|
|
|
|
func (s *taskService) Add(m *model.TaskDBModel) {
|
|
s.db.Save(m)
|
|
}
|
|
func (s *taskService) Update(m *model.TaskDBModel) {
|
|
s.db.Model(&m).Update("state", m.State)
|
|
}
|
|
func (s *taskService) taskDirService(id string) model.TaskDBModel {
|
|
var m model.TaskDBModel
|
|
s.db.Where("id = ?", id).First(&m)
|
|
return m
|
|
}
|
|
|
|
func (s *taskService) Info(id string) model.TaskDBModel {
|
|
var m model.TaskDBModel
|
|
s.db.Where("id = ?", id).Delete(&m)
|
|
return m
|
|
}
|
|
|
|
func (s *taskService) SyncTaskService() {
|
|
var count int64
|
|
s.db.Model(&model.TaskDBModel{}).Count(&count)
|
|
head := make(map[string]string)
|
|
|
|
t := make(chan string)
|
|
|
|
go func() {
|
|
str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil)
|
|
|
|
t <- gjson.Get(str, "data").String()
|
|
}()
|
|
head["Authorization"] = <-t
|
|
|
|
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/task/list/"+strconv.Itoa(int(count)), head)
|
|
|
|
list := []model.TaskDBModel{}
|
|
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
|
|
|
|
go func(list []model.TaskDBModel) {
|
|
for _, dbModel := range list {
|
|
s.db.Create(&dbModel)
|
|
}
|
|
}(list)
|
|
}
|
|
func SyncTask(db *gorm.DB) {
|
|
var count int64
|
|
db.Model(&model.TaskDBModel{}).Count(&count)
|
|
head := make(map[string]string)
|
|
|
|
t := make(chan string)
|
|
|
|
go func() {
|
|
str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil)
|
|
|
|
t <- gjson.Get(str, "data").String()
|
|
}()
|
|
head["Authorization"] = <-t
|
|
|
|
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/task/list/"+strconv.Itoa(int(count)), head)
|
|
|
|
list := []model.TaskDBModel{}
|
|
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
|
|
|
|
go func(list []model.TaskDBModel) {
|
|
for _, dbModel := range list {
|
|
db.Create(&dbModel)
|
|
}
|
|
}(list)
|
|
}
|
|
func NewTaskService(db *gorm.DB, log loger2.OLog) TaskService {
|
|
return &taskService{db: db, log: log}
|
|
}
|