作者 tangxvhui

暂存

... ... @@ -655,7 +655,7 @@ func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand)
if principalId == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "请选择任务负责人")
}
err := projectTaskService.CreateTask(transactionContext, &taskCommand.CreateTaskCommand{
err := projectTaskService.CreateTaskByProject(transactionContext, &taskCommand.CreateTaskByProjectCommand{
Name: content.Name,
LeaderId: principalId,
})
... ...
package command
type CreateTaskCommand struct {
type CreateTaskByProjectCommand struct {
Name string `json:"name"` //任务名称
LeaderId int `json:"leaderId,string"` //赋值人id
}
type CreateTaskCommand struct {
CompanyId int `json:"-"`
LeaderId int `json:"leaderId,string"`
Alias string `json:"alias"` //任务名称
LevelName string `json:"levelName"` //优先级
RelatedUserId []string `json:"relatedUserId"` //相关人员id
SortBy int `json:"sortBy"` // 优先级排序;值越小优先级越高
EndTime int64 `json:"endTime"` // 任务截止的时间戳,单位:秒;等于0时表示未设置时间
}
... ...
package service
import (
"encoding/json"
"fmt"
"strconv"
"strings"
... ... @@ -36,28 +35,29 @@ func dayEndTime(t time.Time) time.Time {
// return t2
// }
func (srv TaskService) CreateTaskTest(param *command.CreateTaskCommand) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
err = srv.CreateTask(transactionContext, param)
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
// func (srv TaskService) CreateTaskTest(param *command.CreateTaskCommand) error {
// transactionContext, err := factory.CreateTransactionContext(nil)
// if err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// _ = transactionContext.RollbackTransaction()
// }()
// err = srv.CreateTask(transactionContext, param)
// if err != nil {
// return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
// }
// if err := transactionContext.CommitTransaction(); err != nil {
// return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// return nil
// }
func (srv TaskService) CreateTask(transactionContext application.TransactionContext, param *command.CreateTaskCommand) error {
// CreateTask 重评估模板中创建
func (srv TaskService) CreateTaskByProject(transactionContext application.TransactionContext, param *command.CreateTaskByProjectCommand) error {
taskRepo := factory.CreateTaskRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
... ... @@ -65,9 +65,10 @@ func (srv TaskService) CreateTask(transactionContext application.TransactionCont
"transactionContext": transactionContext,
})
cnt, _, err := taskRepo.Find(map[string]interface{}{
"name": param.Name,
"leaderId": strconv.Itoa(param.LeaderId),
"limit": 1,
"name": param.Name,
"leaderId": strconv.Itoa(param.LeaderId),
"createdBy": 0,
"limit": 1,
})
if err != nil {
return application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务失败:"+err.Error())
... ... @@ -112,6 +113,7 @@ func (srv TaskService) CreateTask(transactionContext application.TransactionCont
RunAt: nowTime.Unix(),
StopAt: 0,
SortBy: domain.TaskSortNull,
CreatedBy: 0,
}
if int(leaderData.ParentId) != 0 {
newTask.RelatedUser = append(newTask.RelatedUser, int(leaderData.ParentId))
... ... @@ -160,6 +162,109 @@ func (srv TaskService) CreateTask(transactionContext application.TransactionCont
return nil
}
// 创建任务
func (srv TaskService) CreateTask(param *command.CreateTaskCommand) (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,
})
cnt, _, err := taskRepo.Find(map[string]interface{}{
"name": param.Alias,
"leaderId": strconv.Itoa(param.LeaderId),
"createdBy": 1,
"limit": 1,
})
if err != nil {
return map[string]interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
}
if cnt > 0 {
return map[string]interface{}{}, application.ThrowError(application.BUSINESS_ERROR, "已经存在相同的任务")
}
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, leaderList, err := userRepo.Find(map[string]interface{}{
"id": param.LeaderId,
"companyId": param.CompanyId,
})
if err != nil {
return map[string]interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询人员失败:"+err.Error())
}
if len(leaderList) == 0 {
return map[string]interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有查询到人员")
}
relatedUserIds := []int{}
for _, val := range param.RelatedUserId {
id, _ := strconv.Atoi(val)
relatedUserIds = append(relatedUserIds, id)
}
var relatedUser []*domain.User
if len(relatedUserIds) > 0 {
_, relatedUser, err = userRepo.Find(map[string]interface{}{
"ids": relatedUserIds,
"companyId": param.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取任务的相关人员"+err.Error())
}
}
leaderData := leaderList[0]
nowTime := time.Now()
newTask := domain.Task{
Id: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
CompanyId: int(leaderData.CompanyId),
Name: param.Alias,
Alias: param.Alias,
Leader: domain.TaskLeader{
Id: leaderData.Id,
Account: leaderData.Account,
Name: leaderData.Name,
},
Status: domain.TaskRunning,
Level: 0,
LevelName: "",
RelatedUser: []int{int(leaderData.Id)},
RunAt: nowTime.Unix(),
StopAt: 0,
SortBy: domain.TaskSortBy(param.SortBy),
CreatedBy: 1,
AssistFlagMax: 0,
}
//处理endTime
if param.EndTime == 0 {
newTask.EndTime = 0
} else {
newTask.EndTime = dayEndTime(time.Unix(param.EndTime, 10)).Unix()
}
newTask.ApplyLevelName(param.LevelName)
newTask.RelatedUser = []int{}
for _, val := range relatedUser {
newTask.RelatedUser = append(newTask.RelatedUser, int(val.Id))
}
err = taskRepo.Save(&newTask)
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": newTask.Id}, nil
}
// 更新任务
func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
... ... @@ -232,7 +337,7 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]
lastPlanCompletedAt := time.Time{}
newStageList := []*domain.TaskStage{}
//标记 变更完成时间的 里程碑
markStage := []domain.TaskStage{}
//markStage := []domain.TaskStage{}
for i, val2 := range param.StageList {
t, err := time.ParseInLocation("2006-01-02", val2.PlanCompletedAt, time.Local)
if err != nil {
... ... @@ -264,9 +369,10 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]
oldStage.DeletedAt = nil
oldStage.SortBy = i + 1
oldStage.Name = val2.Name
if ok := oldStage.SetPlanCompleted(t); ok {
markStage = append(markStage, *oldStage)
}
oldStage.SetPlanCompleted(t)
//if ok := oldStage.SetPlanCompleted(t); ok {
//markStage = append(markStage, *oldStage)
// }
}
stageList = append(stageList, newStageList...)
... ... @@ -310,35 +416,35 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
// messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// })
//根据markStage 记录的里程碑数据生成通知
for _, val := range markStage {
for _, val2 := range taskData.RelatedUser {
t1 := time.Unix(val.PlanCompletedAt, 0).Format("2006-01-02")
s := fmt.Sprintf(`%s 负责的任务 %s 的里程碑 %s 计划完成时间变更为 %s,请知晓!`, taskData.Leader.Name, taskData.Alias, val.Name, t1)
payload := map[string]string{
"id": strconv.Itoa(val.Id),
"taskName": taskData.Name,
"taskStageName": val.Name,
}
payloadStr, _ := json.Marshal(payload)
newMessage := domain.MessagePersonal{
Id: 0,
Types: domain.MessageTypesTaskModify,
TargetUserId: val2,
ReadFlag: domain.MessageUnread,
Title: s,
Content: s,
Payload: string(payloadStr),
}
err = messageRepo.Save(&newMessage)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
}
// for _, val := range markStage {
// for _, val2 := range taskData.RelatedUser {
// t1 := time.Unix(val.PlanCompletedAt, 0).Format("2006-01-02")
// s := fmt.Sprintf(`%s 负责的任务 %s 的里程碑 %s 计划完成时间变更为 %s,请知晓!`, taskData.Leader.Name, taskData.Alias, val.Name, t1)
// payload := map[string]string{
// "id": strconv.Itoa(val.Id),
// "taskName": taskData.Name,
// "taskStageName": val.Name,
// }
// payloadStr, _ := json.Marshal(payload)
// newMessage := domain.MessagePersonal{
// Id: 0,
// Types: domain.MessageTypesTaskModify,
// TargetUserId: val2,
// ReadFlag: domain.MessageUnread,
// Title: s,
// Content: s,
// Payload: string(payloadStr),
// }
// err = messageRepo.Save(&newMessage)
// 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())
}
... ...
... ... @@ -30,6 +30,7 @@ type Task struct {
SortBy int `pg:"sort_by,use_zero"` // 优先级排序;值越小优先级越高
EndTime int64 `pg:"end_time,use_zero"` // 任务截止的时间戳,单位:秒;等于0时表示未设置时间
UseEndTime int `pg:"use_end_time,use_zero"` // 是否应用任务截止的时间;默认值0:不应用,1:应用
AssistFlag int `pg:"assist_flag"` // 辅导异常标记
AssistFlagMax int `pg:"assist_flag_max"` // 辅导异常标记最大值
AssistFlag int `pg:"assist_flag,use_zero"` // 辅导异常标记
AssistFlagMax int `pg:"assist_flag_max,use_zero"` // 辅导异常标记最大值
CreatedBy int `pg:"created_by,use_zero"`
}
... ...
... ... @@ -49,6 +49,7 @@ func (repo *TaskRepository) TransformToDomain(d *models.Task) *domain.Task {
AssistFlagMax: d.AssistFlagMax,
CurrentStage: d.CurrentStage,
LastStage: d.LastStage,
CreatedBy: d.CreatedBy,
}
}
... ... @@ -81,6 +82,7 @@ func (repo *TaskRepository) Save(param *domain.Task) error {
UseEndTime: param.UseEndTime,
AssistFlag: param.AssistFlag,
AssistFlagMax: param.AssistFlagMax,
CreatedBy: param.CreatedBy,
}
db := repo.transactionContext.PgTx
if m.Id == 0 {
... ... @@ -152,6 +154,9 @@ func (repo *TaskRepository) Find(queryOptions map[string]interface{}) (int, []*d
if val, ok := queryOptions["status"]; ok {
query.Where("task.status=?", val)
}
if val, ok := queryOptions["createdBy"]; ok {
query.Where("created_by = ?", val)
}
if val, ok := queryOptions["limit"]; ok {
if limit, err := strconv.Atoi(fmt.Sprintf("%v", val)); err == nil {
query.Limit(limit)
... ...
... ... @@ -56,8 +56,8 @@ func (c *TaskController) CreateTask() {
return
}
err = srv.CreateTaskTest(paramReq)
c.Response(nil, err)
resp, err := srv.CreateTask(paramReq)
c.Response(resp, err)
}
// 启动任务
... ...
... ... @@ -12,6 +12,7 @@ func init() {
taskAdminNS := web.NewNamespace("/v1/admin/task",
web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()),
web.NSCtrlPost("/info", (*controllers.TaskController).GetTaskInfo),
web.NSCtrlPost("/", (*controllers.TaskController).CreateTask),
web.NSCtrlPut("/", (*controllers.TaskController).UpdateTask),
web.NSCtrlPut("/delete", (*controllers.TaskController).DeleteTask),
web.NSCtrlPost("/run", (*controllers.TaskController).RunTask),
... ...