|
|
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,
|
|
|
})
|
...
|
...
|
@@ -67,6 +67,7 @@ func (srv TaskService) CreateTask(transactionContext application.TransactionCont |
|
|
cnt, _, err := taskRepo.Find(map[string]interface{}{
|
|
|
"name": param.Name,
|
|
|
"leaderId": strconv.Itoa(param.LeaderId),
|
|
|
"createdBy": 0,
|
|
|
"limit": 1,
|
|
|
})
|
|
|
if err != nil {
|
...
|
...
|
@@ -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())
|
|
|
}
|
...
|
...
|
|