作者 tangxvhui

暂存

package adapter
type TaskInfoAdapter struct {
Id int `json:"id"`
Name string `json:"name"` // 任务名称
Alias string `json:"alias"` // 任务别名
Leader UserData `json:"leader"` // 任务负责人
Status int `json:"status"` // 任务的状态
Level int `json:"level"` // 优先级,值越小优先级越高
LevalName string `json:"levalName"` // 优先级名称
RelatedUserId []string `json:"relatedUserId"` // 相关的员工id
RelatedUser []UserData `json:"relatedUser"` // 相关的员工id
StageList []TaskStage `json:"stageList"`
}
type UserData struct {
Id int64 `json:"id,string"` // 用户Id
Account string `json:"account"` // 用户账号
Name string `json:"name"` // 用户姓名
}
type TaskStage struct {
Id int `json:"id"`
Name string `json:"name"` //里程碑名称
PlanCompletedAt string `json:"planCompletedAt"` //计划完成时间, 例:2006-01-02
}
... ...
package adapter
type TaskItem struct {
Id int `json:"id"`
Name string `json:"name"` // 任务名称
Alias string `json:"alias"` // 任务别名
Leader UserData `json:"leader"` // 任务负责人
Status int `json:"status"` // 任务的状态
Level int `json:"level"` // 优先级,值越小优先级越高
LevalName string `json:"levalName"` // 优先级名称
StageA string `json:"stageA"` // 里程碑1
StageB string `json:"stageB"` // 里程碑2
StageC string `json:"stageC"` // 里程碑3
StageD string `json:"stageD"` // 里程碑4
StageE string `json:"stageE"` // 里程碑5
}
... ...
package command
// 获取任务
type GetTaskCommand struct {
TaskId int `json:"taskId"`
CompanyId int `json:"-"`
}
... ...
package command
type ListTaskCommand struct {
PageSize int `json:"pageSize"`
PageNumber int `json:"pageNumber"`
CompanyId int `json:"-"`
SearchWord string `json:"searchWord"`
}
... ...
package command
type RunTaskCommand struct{}
type RunTaskCommand struct {
TaskId int `json:"taskId"`
CompanyId int `json:"-"`
}
... ...
package command
type StopTaskCommand struct{}
type StopTaskCommand struct {
TaskId int `json:"taskId"`
CompanyId int `json:"-"`
}
... ...
... ... @@ -2,7 +2,7 @@ package command
type UpdateTaskCommand struct {
Id int `json:"id"`
Name string `json:"name"` //任务名称
Alias string `json:"alias"` //任务名称
LeaderId int `json:"leaderId"` //赋值人id
LevelName string `json:"levelName"` //优先级
StageList []struct {
... ...
package service
import (
"strconv"
"time"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/adapter"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
... ... @@ -107,6 +110,9 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]
taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
taskData, err := taskRepo.FindOne(map[string]interface{}{
"id": param.Id,
})
... ... @@ -116,19 +122,300 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]
_, stageList, err := taskStageRepo.Find(map[string]interface{}{
"taskId": param.Id,
})
_ = taskData
_ = stageList
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
leaderData, err := userRepo.FindOne(map[string]interface{}{
"id": param.LeaderId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
taskData.Alias = param.Alias
taskData.Leader = domain.TaskLeader{
Id: leaderData.Id,
Account: leaderData.Account,
Name: leaderData.Name,
}
taskData.ApplyLevelName(param.LevelName)
nowTime := time.Now()
stageMap := map[int]*domain.TaskStage{}
for _, val := range stageList {
val.DeletedAt = &nowTime
stageMap[val.Id] = val
}
lastPlanCompletedAt := time.Time{}
newStageList := []*domain.TaskStage{}
for i, val2 := range param.StageList {
t, err := time.ParseInLocation("2006-01-02", val2.PlanCompletedAt, time.Local)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "请填写里程碑完成时间")
}
if i > 0 {
if lastPlanCompletedAt.After(t) {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "里程碑完成时间填写错误")
}
}
lastPlanCompletedAt = t
if val2.Id == 0 {
newStage := domain.TaskStage{
Id: 0,
TaskId: taskData.Id,
Name: val2.Name,
SortBy: i + 1,
Status: domain.TaskStageUncompleted,
PlanCompletedAt: t.Unix(),
RealCompletedAt: 0,
}
newStageList = append(newStageList, &newStage)
continue
}
oldStage, ok := stageMap[val2.Id]
if !ok {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "里程碑数据被其他人变更")
}
oldStage.DeletedAt = nil
oldStage.SortBy = i + 1
oldStage.PlanCompletedAt = t.Unix()
oldStage.Name = val2.Name
}
stageList = append(stageList, newStageList...)
err = taskStageRepo.Save(stageList)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
err = taskRepo.Save(taskData)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return nil, nil
return map[string]interface{}{
"id": param.Id,
}, nil
}
func (srv TaskService) RunTask() error {
return nil
// 获取任务详情
func (srv TaskService) GetTaskInfo(param *command.GetTaskCommand) (*adapter.TaskInfoAdapter, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
taskRepo := factory.CreateTaskRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
taskData, err := taskRepo.FindOne(map[string]interface{}{
"id": param.TaskId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
_, stageList, err := taskStageRepo.Find(map[string]interface{}{
"taskId": param.TaskId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
relatedUserId := taskData.RelatedUser
_, relatedUserList, err := userRepo.Find(map[string]interface{}{
"ids": relatedUserId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取相关人员"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
result := adapter.TaskInfoAdapter{
Id: taskData.Id,
Name: taskData.Name,
Alias: taskData.Alias,
Leader: adapter.UserData{
Id: taskData.Leader.Id,
Account: taskData.Leader.Account,
Name: taskData.Leader.Name,
},
Status: int(taskData.Status),
Level: taskData.Level,
LevalName: taskData.LevalName,
RelatedUserId: []string{},
RelatedUser: []adapter.UserData{},
StageList: []adapter.TaskStage{},
}
for _, val := range relatedUserList {
idStr := strconv.Itoa(int(val.Id))
result.RelatedUserId = append(result.RelatedUserId, idStr)
result.RelatedUser = append(result.RelatedUser, adapter.UserData{
Id: val.Id,
Account: val.Account,
Name: val.Name,
})
}
for _, val := range stageList {
var t string
if val.PlanCompletedAt > 0 {
t = time.Unix(val.PlanCompletedAt, 0).Local().Format("2006-01-02")
}
result.StageList = append(result.StageList, adapter.TaskStage{
Id: val.Id,
Name: val.Name,
PlanCompletedAt: t,
})
}
return &result, nil
}
func (srv TaskService) StopTask() error {
return nil
// RunTask 启动任务
func (srv TaskService) RunTask(param *command.RunTaskCommand) (map[string]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
taskRepo := factory.CreateTaskRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
taskData, err := taskRepo.FindOne(map[string]interface{}{
"id": param.TaskId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if taskData.Status == domain.TaskRunning {
return map[string]interface{}{"id": param.TaskId}, nil
}
taskData.Status = domain.TaskRunning
taskData.RunAt = time.Now().Unix()
err = taskRepo.Save(taskData)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存任务数据"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return map[string]interface{}{"id": param.TaskId}, nil
}
// RunTask 停止任务
func (srv TaskService) StopTask(param *command.StopTaskCommand) (map[string]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
taskRepo := factory.CreateTaskRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
taskData, err := taskRepo.FindOne(map[string]interface{}{
"id": param.TaskId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if taskData.Status == domain.TaskStop {
return map[string]interface{}{"id": param.TaskId}, nil
}
taskData.Status = domain.TaskStop
taskData.StopAt = time.Now().Unix()
err = taskRepo.Save(taskData)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存任务数据"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return map[string]interface{}{"id": param.TaskId}, nil
}
// ListTask 任务列表
func (srv TaskService) ListTask(param *command.ListTaskCommand) (map[string]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
taskRepo := factory.CreateTaskRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
//
condition := map[string]interface{}{
"offset": 0,
"limit": 20,
"companyId": param.CompanyId,
}
if len(param.SearchWord) > 0 {
condition["aliasOrLeader"] = "%" + param.SearchWord + "%"
}
if param.PageSize > 0 {
condition["limit"] = param.PageSize
if param.PageNumber > 0 {
condition["offset"] = (param.PageNumber - 1) * param.PageSize
}
}
taskCnt, taskList, err := taskRepo.Find(condition)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务数据"+err.Error())
}
taskIdList := []int{}
for _, val := range taskList {
taskIdList = append(taskIdList, val.Id)
}
_, stageList, err := taskStageRepo.Find(map[string]interface{}{
"taskIdList": taskIdList,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务阶段数据"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
taskResult := []*adapter.TaskItem{}
_ = stageList
// taskMapResult := map[int]*adapter.TaskItem{}
// for _, val := range taskList {
// }
result := tool_funs.SimpleWrapGridMap(int64(taskCnt), taskResult)
return result, nil
}
... ...
... ... @@ -15,6 +15,7 @@ type Task struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt"`
CompanyId int `json:"companyId,string"`
Name string `json:"name"` // 任务名称
Alias string `json:"alias"` // 任务别名
Leader TaskLeader `json:"leader"` // 任务负责人
... ...
... ... @@ -5,8 +5,8 @@ import "time"
type TaskStageState int
const (
TaskStageComplated TaskStageState = 1 //里程碑未完成
TaskStageUncompleted TaskStageState = 2 //里程碑完成
TaskStageUncompleted TaskStageState = 1 //里程碑未完成
TaskStageCompleted TaskStageState = 2 //里程碑完成
)
// 任务阶段
... ...
... ... @@ -52,6 +52,8 @@ func init() {
&models.Permission{},
&models.LogSms{},
&models.MessagePersonal{},
&models.Task{},
&models.TaskStage{},
}
for _, model := range tables {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
... ...
... ... @@ -9,12 +9,13 @@ import (
// 任务
type Task struct {
tableName struct{} `comment:"任务" pg:"task"`
Id int `pg:"id,pk"`
CreatedAt time.Time `pg:"created_at"`
UpdatedAt time.Time `pg:"updated_at"`
DeletedAt *time.Time `pg:"deleted_at"`
Name string `pg:"name"` // 任务名称
Alias string `pg:"alias"`
Id int `pg:"id,pk"` //
CreatedAt time.Time `pg:"created_at"` //
UpdatedAt time.Time `pg:"updated_at"` //
DeletedAt *time.Time `pg:"deleted_at"` //
Name string `pg:"name"` // 任务名称
Alias string `pg:"alias"` //
CompanyId int `pg:"company_id"` //
Leader domain.TaskLeader `pg:"leader"` // 任务负责人
Status int `pg:"status"` // 任务的状态
Level int `pg:"level"` // 优先级,值越小优先级越高
... ...
... ... @@ -38,6 +38,7 @@ func (repo *TaskRepository) TransformToDomain(d *models.Task) *domain.Task {
RelatedUser: d.RelatedUser,
RunAt: d.RunAt,
StopAt: d.StopAt,
CompanyId: d.CompanyId,
}
}
... ... @@ -60,6 +61,7 @@ func (repo *TaskRepository) Save(param *domain.Task) error {
LevalName: param.LevalName,
RelatedUser: param.RelatedUser,
RunAt: param.RunAt,
CompanyId: param.CompanyId,
StopAt: param.StopAt,
}
db := repo.transactionContext.PgTx
... ... @@ -117,9 +119,16 @@ func (repo *TaskRepository) Find(queryOptions map[string]interface{}) (int, []*d
if val, ok := queryOptions["name"]; ok {
query.Where("task.name like ?", val)
}
if val, ok := queryOptions["companyId"]; ok {
query.Where("task.company_id like ?", val)
}
if val, ok := queryOptions["leaderId"]; ok {
query.Where("task.leader->>'userId'='?'", val)
}
if val, ok := queryOptions["aliasOrLeader"]; ok {
query.Where(`task.alias like ? or task.leader->>'name' like ?`, val, val)
}
query.Order("id desc")
count, err := query.SelectAndCount()
if err != nil {
... ...
... ... @@ -106,9 +106,14 @@ func (repo *TaskStageRepository) Find(queryOptions map[string]interface{}) (int,
query := tx.Model(&m).
Where("deleted_at isnull").
Limit(20)
if val, ok := queryOptions["taskId"]; ok {
query.Where("task_stage.task_id = ?", val)
}
if val, ok := queryOptions["taskIdList"]; ok {
query.Where("task_stage.task_id in(?)", pg.In(val))
}
query.Order("sort_by")
count, err := query.SelectAndCount()
if err != nil {
... ...
package repository
import (
"errors"
"fmt"
"github.com/go-pg/pg/v10"
... ... @@ -92,8 +93,8 @@ func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domai
query.Where("company_id = ?", v)
}
err := query.First()
if err == pg.ErrNoRows {
return nil, ErrNoRows
if errors.Is(err, pg.ErrNoRows) {
return nil, fmt.Errorf("没有找到user数据")
}
if err != nil {
return nil, err
... ...