作者 郑周

Merge branch 'dev-zhengzhou' into test

# Conflicts:
#	pkg/port/beego/controllers/task_controller.go
@@ -263,3 +263,11 @@ func CreateTaskAnomalyRepository(options map[string]interface{}) domain.TaskAnom @@ -263,3 +263,11 @@ func CreateTaskAnomalyRepository(options map[string]interface{}) domain.TaskAnom
263 } 263 }
264 return repository.NewTaskAnomalyRepository(transactionContext) 264 return repository.NewTaskAnomalyRepository(transactionContext)
265 } 265 }
  266 +
  267 +func CreateLogOptRepository(options map[string]interface{}) domain.LogOptRepository {
  268 + var transactionContext *pg.TransactionContext
  269 + if value, ok := options["transactionContext"]; ok {
  270 + transactionContext = value.(*pg.TransactionContext)
  271 + }
  272 + return repository.NewLogOptRepository(transactionContext)
  273 +}
  1 +package command
  2 +
  3 +import "github.com/beego/beego/v2/core/validation"
  4 +
  5 +type QueryLogCommand struct {
  6 + CompanyId int64 `cname:"公司ID" json:"-"`
  7 + TaskId int `cname:"任务ID" json:"taskId,string" valid:"Required"`
  8 + PageNumber int64 `cname:"分页页码" json:"pageNumber" valid:"Required"`
  9 + PageSize int64 `cname:"分页数量" json:"pageSize" valid:"Required"`
  10 +}
  11 +
  12 +func (in *QueryLogCommand) Valid(*validation.Validation) {
  13 +
  14 +}
  1 +package log_opt
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
  9 + "strconv"
  10 + "time"
  11 +)
  12 +
  13 +func CreateTask(u *domain.UserAuth, task domain.Task) error {
  14 + transactionContext, err := factory.StartTransaction()
  15 + if err != nil {
  16 + return err
  17 + }
  18 + defer func() {
  19 + _ = transactionContext.RollbackTransaction()
  20 + if err := recover(); err != nil {
  21 + log.Logger.Error(application.ThrowError(application.BUSINESS_ERROR, fmt.Sprintf("创建任务日志异常:%s", err)).Error())
  22 + }
  23 + }()
  24 + logOptRepo := factory.CreateLogOptRepository(map[string]interface{}{"transactionContext": transactionContext})
  25 +
  26 + logOpt := createdLogOpt(u, strconv.Itoa(task.Id))
  27 + logOpt.OptMethod = domain.CREATE
  28 + logOpt.OptField = "创建了任务"
  29 + logOpt.OptValue = task.Alias
  30 +
  31 + _, err = logOptRepo.Insert(logOpt)
  32 + if err != nil {
  33 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  34 + }
  35 + if err := transactionContext.CommitTransaction(); err != nil {
  36 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  37 + }
  38 + return nil
  39 +}
  40 +
  41 +func UpdateTask(u *domain.UserAuth,
  42 + srcTask *domain.Task,
  43 + newTask *domain.Task,
  44 + srcStage []*domain.TaskStage,
  45 + newStage []*domain.TaskStage,
  46 +) error {
  47 + transactionContext, err := factory.StartTransaction()
  48 + if err != nil {
  49 + return err
  50 + }
  51 + defer func() {
  52 + _ = transactionContext.RollbackTransaction()
  53 + if err := recover(); err != nil {
  54 + log.Logger.Error(application.ThrowError(application.BUSINESS_ERROR, fmt.Sprintf("更新任务日志异常:%s", err)).Error())
  55 + }
  56 + }()
  57 + logOptRepo := factory.CreateLogOptRepository(map[string]interface{}{"transactionContext": transactionContext})
  58 +
  59 + var logArray = compareTask(u, srcTask, newTask)
  60 + var logArray2 = compareStage(u, newTask.Id, srcStage, newStage)
  61 + logArray = append(logArray, logArray2...)
  62 + if len(logArray) == 0 {
  63 + return nil
  64 + }
  65 + for _, v := range logArray {
  66 + _, err = logOptRepo.Insert(v)
  67 + if err != nil {
  68 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  69 + }
  70 + }
  71 + if err := transactionContext.CommitTransaction(); err != nil {
  72 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  73 + }
  74 + return nil
  75 +}
  76 +
  77 +func compareTask(u *domain.UserAuth, srcTask *domain.Task, newTask *domain.Task) []*domain.LogOpt {
  78 + var taskId = strconv.Itoa(newTask.Id)
  79 + logArray := make([]*domain.LogOpt, 0)
  80 + if srcTask.Alias != newTask.Alias {
  81 + logOpt := createdLogOpt(u, taskId)
  82 + logOpt.OptField = "修改了任务名称"
  83 + logOpt.OptValue = newTask.Alias
  84 + logArray = append(logArray, logOpt)
  85 + }
  86 +
  87 + if srcTask.EndTime != newTask.EndTime {
  88 + logOpt := createdLogOpt(u, taskId)
  89 + logOpt.OptField = "修改了任务截止时间"
  90 + if newTask.EndTime == 0 {
  91 + logOpt.OptValue = "未设置时间"
  92 + } else {
  93 + logOpt.OptValue = time.Unix(newTask.EndTime, 0).Local().Format("2006-01-02 15:04:05")
  94 + }
  95 + logArray = append(logArray, logOpt)
  96 + }
  97 +
  98 + if srcTask.UseEndTime != newTask.UseEndTime {
  99 + logOpt := createdLogOpt(u, taskId)
  100 + logOpt.OptField = "修改了任务截止时间应用到日评中"
  101 + if newTask.UseEndTime == 0 {
  102 + logOpt.OptValue = "不应用"
  103 + } else {
  104 + logOpt.OptValue = "应用"
  105 + }
  106 + logArray = append(logArray, logOpt)
  107 + }
  108 +
  109 + if srcTask.LevelName != newTask.LevelName {
  110 + logOpt := createdLogOpt(u, taskId)
  111 + logOpt.OptField = "修改了任务类型"
  112 + logOpt.OptValue = newTask.LevelName
  113 + logArray = append(logArray, logOpt)
  114 + }
  115 +
  116 + if srcTask.SortBy != newTask.SortBy {
  117 + logOpt := createdLogOpt(u, taskId)
  118 + logOpt.OptField = "修改了任务优先级"
  119 + logOpt.OptValue = newTask.SortBy.Named()
  120 + logArray = append(logArray, logOpt)
  121 + }
  122 +
  123 + if srcTask.AssistFlagMax != newTask.AssistFlagMax {
  124 + logOpt := createdLogOpt(u, taskId)
  125 + logOpt.OptField = "修改了上级辅导时间"
  126 + logOpt.OptValue = strconv.Itoa(newTask.AssistFlagMax)
  127 + logArray = append(logArray, logOpt)
  128 + }
  129 +
  130 + // 新的任务相关方
  131 + uidNewMap := map[int]int{}
  132 + for _, id := range newTask.RelatedUser {
  133 + uidNewMap[id] = id
  134 + }
  135 + // 被移除的任务相关方
  136 + removedIds := make([]int, 0)
  137 + for _, id := range srcTask.RelatedUser {
  138 + if _, ok := uidNewMap[id]; ok {
  139 + delete(uidNewMap, id) // 删除后,剩余的都是新增的
  140 + } else {
  141 + removedIds = append(removedIds, id)
  142 + }
  143 + }
  144 +
  145 + // 变动的任务相关方
  146 + changeIds := make([]int, 0)
  147 + for _, v := range removedIds {
  148 + changeIds = append(changeIds, v)
  149 + }
  150 + for _, v := range uidNewMap {
  151 + changeIds = append(changeIds, v)
  152 + }
  153 + userMap, _ := getUserMap(changeIds)
  154 +
  155 + for _, v := range removedIds {
  156 + logOpt := createdLogOpt(u, taskId)
  157 + logOpt.OptMethod = domain.DELETE
  158 + logOpt.OptField = "移除了任务相关方"
  159 + if v, ok := userMap[v]; ok {
  160 + logOpt.OptValue = v.Name
  161 + }
  162 + logArray = append(logArray, logOpt)
  163 + }
  164 +
  165 + for _, v := range uidNewMap {
  166 + logOpt := createdLogOpt(u, taskId)
  167 + logOpt.OptMethod = domain.CREATE
  168 + logOpt.OptField = "添加了任务相关方"
  169 + if v, ok := userMap[v]; ok {
  170 + logOpt.OptValue = v.Name
  171 + }
  172 + logArray = append(logArray, logOpt)
  173 + }
  174 +
  175 + return logArray
  176 +}
  177 +
  178 +func compareStage(u *domain.UserAuth, id int, srcStage []*domain.TaskStage, newStage []*domain.TaskStage) []*domain.LogOpt {
  179 + taskId := strconv.Itoa(id)
  180 + logArray := make([]*domain.LogOpt, 0)
  181 + srcMap := map[int]*domain.TaskStage{}
  182 + for i := range srcStage {
  183 + it := srcStage[i]
  184 + srcMap[it.Id] = it
  185 + }
  186 +
  187 + for i := range newStage {
  188 + it := newStage[i]
  189 + if it.DeletedAt != nil {
  190 + logOpt := createdLogOpt(u, taskId)
  191 + logOpt.OptMethod = domain.DELETE
  192 + logOpt.OptField = "删除了里程碑"
  193 + logOpt.OptValue = it.Name
  194 + logArray = append(logArray, logOpt)
  195 + continue
  196 + }
  197 +
  198 + // 新旧比对
  199 + if v, ok := srcMap[it.Id]; ok {
  200 + if it.Name != v.Name {
  201 + logOpt := createdLogOpt(u, taskId)
  202 + logOpt.OptField = "修改了里程碑名称"
  203 + logOpt.OptValue = it.Name
  204 + logArray = append(logArray, logOpt)
  205 + }
  206 + if it.PlanCompletedAt != v.PlanCompletedAt {
  207 + logOpt := createdLogOpt(u, taskId)
  208 + logOpt.OptField = "修改了里程碑" + it.Name + "计划完成时间"
  209 + if it.PlanCompletedAt == 0 {
  210 + logOpt.OptValue = "未设置时间"
  211 + } else {
  212 + logOpt.OptValue = time.Unix(it.PlanCompletedAt, 0).Local().Format("2006-01-02 15:04:05")
  213 + }
  214 + logArray = append(logArray, logOpt)
  215 + }
  216 + } else {
  217 + logOpt := createdLogOpt(u, taskId)
  218 + logOpt.OptMethod = domain.CREATE
  219 + logOpt.OptField = "创建了里程碑"
  220 + logOpt.OptValue = it.Name
  221 + logArray = append(logArray, logOpt)
  222 + }
  223 + }
  224 +
  225 + return logArray
  226 +}
  227 +
  228 +func getUserMap(ids []int) (map[int]*domain.User, error) {
  229 + userMap := map[int]*domain.User{}
  230 + if len(ids) == 0 {
  231 + return userMap, nil
  232 + }
  233 + transactionContext, err := factory.CreateTransactionContext(nil)
  234 + if err != nil {
  235 + return userMap, err
  236 + }
  237 + userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  238 + _, users, err := userRepo.Find(map[string]interface{}{"ids": ids, "limit": len(ids)})
  239 + if err != nil {
  240 + return userMap, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  241 + }
  242 + for i := range users {
  243 + user := users[i]
  244 + userMap[int(user.Id)] = user
  245 + }
  246 + return userMap, nil
  247 +}
  248 +
  249 +func createdLogOpt(u *domain.UserAuth, taskId string) *domain.LogOpt {
  250 + var createdAt = time.Now()
  251 + var updatedAt = createdAt
  252 + operator := domain.StaffDesc{
  253 + UserId: int(u.UserId),
  254 + CompanyName: u.CompanyName,
  255 + Account: u.Phone,
  256 + UserName: u.Name,
  257 + }
  258 + return &domain.LogOpt{
  259 + Id: 0,
  260 + CompanyId: int(u.CompanyId),
  261 + Operator: operator,
  262 + OptMethod: domain.UPDATE,
  263 + OptTargetId: taskId,
  264 + OptField: "",
  265 + OptValue: "",
  266 + CreatedAt: createdAt,
  267 + UpdatedAt: updatedAt,
  268 + }
  269 +}
  1 +package log_opt
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/utils/tool_funs"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/log_opt/command"
  8 +)
  9 +
  10 +type LogOptService struct {
  11 +}
  12 +
  13 +func NewLogOptService() *LogOptService {
  14 + newRoleService := &LogOptService{}
  15 + return newRoleService
  16 +}
  17 +
  18 +func (rs *LogOptService) List(in *command.QueryLogCommand) (interface{}, error) {
  19 + transactionContext, err := factory.ValidateStartTransaction(in)
  20 + if err != nil {
  21 + return nil, err
  22 + }
  23 + defer func() {
  24 + transactionContext.RollbackTransaction()
  25 + }()
  26 +
  27 + ssMap := tool_funs.SimpleStructToMap(in)
  28 + ssMap["companyId"] = in.CompanyId
  29 + ssMap["optTargetId"] = in.TaskId
  30 +
  31 + logRepository := factory.CreateLogOptRepository(map[string]interface{}{"transactionContext": transactionContext})
  32 + total, logList, err := logRepository.Find(ssMap)
  33 + if err != nil {
  34 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  35 + }
  36 +
  37 + if err := transactionContext.CommitTransaction(); err != nil {
  38 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  39 + }
  40 +
  41 + return tool_funs.SimpleWrapGridMap(total, logList), nil
  42 +}
@@ -9,7 +9,7 @@ type UpdateTaskCommand struct { @@ -9,7 +9,7 @@ type UpdateTaskCommand struct {
9 RelatedUserId []string `json:"relatedUserId"` // 相关人员id 9 RelatedUserId []string `json:"relatedUserId"` // 相关人员id
10 SortBy int `json:"sortBy"` // 优先级排序;值越小优先级越高 10 SortBy int `json:"sortBy"` // 优先级排序;值越小优先级越高
11 EndTime int64 `json:"endTime"` // 任务截止的时间戳,单位:秒;等于0时表示未设置时间 11 EndTime int64 `json:"endTime"` // 任务截止的时间戳,单位:秒;等于0时表示未设置时间
12 - UseEndtime int `json:"useEndTime"` // 是否应用任务截止的时间;默认值0:不应用,1:应用 12 + UseEndTime int `json:"useEndTime"` // 是否应用任务截止的时间;默认值0:不应用,1:应用
13 StageList []struct { 13 StageList []struct {
14 Id int `json:"id,string"` 14 Id int `json:"id,string"`
15 Name string `json:"name"` //里程碑名称 15 Name string `json:"name"` //里程碑名称
@@ -2,6 +2,7 @@ package service @@ -2,6 +2,7 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/log_opt"
5 "strconv" 6 "strconv"
6 "strings" 7 "strings"
7 "time" 8 "time"
@@ -116,8 +117,8 @@ func (srv TaskService) CreateTaskByProject(transactionContext application.Transa @@ -116,8 +117,8 @@ func (srv TaskService) CreateTaskByProject(transactionContext application.Transa
116 return nil 117 return nil
117 } 118 }
118 119
119 -// 创建任务  
120 -func (srv TaskService) CreateTask(param *command.CreateTaskCommand) (map[string]interface{}, error) { 120 +// CreateTask 创建任务
  121 +func (srv TaskService) CreateTask(param *command.CreateTaskCommand, userReq *domain.UserAuth) (map[string]interface{}, error) {
121 sortNamed := domain.TaskSortBy(param.SortBy) 122 sortNamed := domain.TaskSortBy(param.SortBy)
122 if sortNamed.Named() == "" { 123 if sortNamed.Named() == "" {
123 return nil, application.ThrowError(application.TRANSACTION_ERROR, "优先级设置错误") 124 return nil, application.ThrowError(application.TRANSACTION_ERROR, "优先级设置错误")
@@ -220,11 +221,15 @@ func (srv TaskService) CreateTask(param *command.CreateTaskCommand) (map[string] @@ -220,11 +221,15 @@ func (srv TaskService) CreateTask(param *command.CreateTaskCommand) (map[string]
220 if err := transactionContext.CommitTransaction(); err != nil { 221 if err := transactionContext.CommitTransaction(); err != nil {
221 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 222 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
222 } 223 }
  224 +
  225 + // 生成创建日志
  226 + _ = log_opt.CreateTask(userReq, newTask)
  227 +
223 return map[string]interface{}{"id": newTask.Id}, nil 228 return map[string]interface{}{"id": newTask.Id}, nil
224 } 229 }
225 230
226 -// 更新任务  
227 -func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]interface{}, error) { 231 +// UpdateTask 更新任务
  232 +func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand, userReq *domain.UserAuth) (map[string]interface{}, error) {
228 transactionContext, err := factory.CreateTransactionContext(nil) 233 transactionContext, err := factory.CreateTransactionContext(nil)
229 if err != nil { 234 if err != nil {
230 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 235 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -274,6 +279,14 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string] @@ -274,6 +279,14 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]
274 if leaderList[0].CompanyId != int64(param.CompanyId) { 279 if leaderList[0].CompanyId != int64(param.CompanyId) {
275 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "负责人数据验证不通过") 280 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "负责人数据验证不通过")
276 } 281 }
  282 +
  283 + // 拷贝数据
  284 + var copyTask = taskData.Copy()
  285 + var copyStageList = make([]*domain.TaskStage, 0)
  286 + for _, v := range stageList {
  287 + copyStageList = append(copyStageList, v)
  288 + }
  289 +
277 //更新任务的相关人员 290 //更新任务的相关人员
278 relatedUserIds := []int{} 291 relatedUserIds := []int{}
279 for _, val := range param.RelatedUserId { 292 for _, val := range param.RelatedUserId {
@@ -382,7 +395,7 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string] @@ -382,7 +395,7 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]
382 } else { 395 } else {
383 taskData.EndTime = dayEndTime(time.Unix(param.EndTime, 10)).Unix() 396 taskData.EndTime = dayEndTime(time.Unix(param.EndTime, 10)).Unix()
384 } 397 }
385 - taskData.UseEndTime = param.UseEndtime 398 + taskData.UseEndTime = param.UseEndTime
386 taskData.AssistFlagMax = param.AssistFlagMax 399 taskData.AssistFlagMax = param.AssistFlagMax
387 err = taskRepo.Save(taskData) 400 err = taskRepo.Save(taskData)
388 if err != nil { 401 if err != nil {
@@ -420,6 +433,10 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string] @@ -420,6 +433,10 @@ func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (map[string]
420 if err := transactionContext.CommitTransaction(); err != nil { 433 if err := transactionContext.CommitTransaction(); err != nil {
421 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 434 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
422 } 435 }
  436 +
  437 + // 生成更新日志
  438 + _ = log_opt.UpdateTask(userReq, &copyTask, taskData, copyStageList, stageList)
  439 +
423 return map[string]interface{}{ 440 return map[string]interface{}{
424 "id": param.Id, 441 "id": param.Id,
425 }, nil 442 }, nil
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +type OptMethod int
  6 +
  7 +const (
  8 + CREATE OptMethod = 1 // 创建
  9 + UPDATE OptMethod = 2 // 更新
  10 + DELETE OptMethod = 3 // 删除
  11 +)
  12 +
  13 +// LogOpt 操作日志
  14 +type LogOpt struct {
  15 + //OptModule string `json:"optModule" comment:"操作模块"`
  16 + Id int `json:"id,string" comment:"ID" pg:"id,pk"`
  17 + CompanyId int `json:"companyId" comment:"公司ID"`
  18 + Operator StaffDesc `json:"operator" comment:"操作人"`
  19 + OptMethod OptMethod `json:"optMethod" comment:"操作方法"`
  20 + OptTargetId string `json:"optTargetId" comment:"操作目标ID"`
  21 + OptField string `json:"optField" comment:"操作字段"`
  22 + OptValue string `json:"optValue" comment:"操作字段值"`
  23 + CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
  24 + UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
  25 + DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
  26 +}
  27 +
  28 +type LogOptRepository interface {
  29 + Insert(opt *LogOpt) (*LogOpt, error)
  30 + Remove(opt *LogOpt) (*LogOpt, error)
  31 + FindOne(queryOptions map[string]interface{}) (*LogOpt, error)
  32 + Find(queryOptions map[string]interface{}) (int64, []*LogOpt, error)
  33 + Count(queryOptions map[string]interface{}) (int64, error)
  34 +}
  1 +package models
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +// LogOpt 操作日志
  9 +type LogOpt struct {
  10 + //OptModule string `comment:"操作模块"`
  11 + tableName struct{} `comment:"操作日志" pg:"log_opt"`
  12 + Id int `comment:"ID" pg:"id,pk"`
  13 + CompanyId int `comment:"公司ID"`
  14 + Operator domain.StaffDesc `comment:"操作人"`
  15 + OptMethod domain.OptMethod `comment:"操作方法"`
  16 + OptTargetId string `comment:"操作目标ID"`
  17 + OptField string `comment:"操作字段"`
  18 + OptValue string `comment:"操作字段值"`
  19 + CreatedAt time.Time `comment:"创建时间"`
  20 + UpdatedAt time.Time `comment:"更新时间"`
  21 + DeletedAt *time.Time `comment:"删除时间"`
  22 +}
  1 +package repository
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "time"
  7 +
  8 + "github.com/go-pg/pg/v10"
  9 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  10 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  11 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  12 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
  13 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
  14 +)
  15 +
  16 +type LogOptRepository struct {
  17 + transactionContext *pgTransaction.TransactionContext
  18 +}
  19 +
  20 +func NewLogOptRepository(transactionContext *pgTransaction.TransactionContext) *LogOptRepository {
  21 + return &LogOptRepository{transactionContext: transactionContext}
  22 +}
  23 +
  24 +func (repo *LogOptRepository) TransformToDomain(m *models.LogOpt) domain.LogOpt {
  25 + return domain.LogOpt{
  26 + Id: m.Id,
  27 + CompanyId: m.CompanyId,
  28 + Operator: m.Operator,
  29 + OptMethod: m.OptMethod,
  30 + OptTargetId: m.OptTargetId,
  31 + OptField: m.OptField,
  32 + OptValue: m.OptValue,
  33 + CreatedAt: m.CreatedAt.Local(),
  34 + UpdatedAt: m.UpdatedAt.Local(),
  35 + DeletedAt: m.DeletedAt,
  36 + }
  37 +}
  38 +
  39 +func (repo *LogOptRepository) TransformToModel(d *domain.LogOpt) models.LogOpt {
  40 + return models.LogOpt{
  41 + Id: d.Id,
  42 + CompanyId: d.CompanyId,
  43 + Operator: d.Operator,
  44 + OptMethod: d.OptMethod,
  45 + OptTargetId: d.OptTargetId,
  46 + OptField: d.OptField,
  47 + OptValue: d.OptValue,
  48 + CreatedAt: d.CreatedAt,
  49 + UpdatedAt: d.UpdatedAt,
  50 + DeletedAt: d.DeletedAt,
  51 + }
  52 +}
  53 +
  54 +func (repo *LogOptRepository) Insert(d *domain.LogOpt) (*domain.LogOpt, error) {
  55 + var isCreate = d.Id == 0
  56 + if isCreate {
  57 + id, err := utils.NewSnowflakeId()
  58 + if err != nil {
  59 + return d, err
  60 + }
  61 + d.Id = int(id)
  62 + d.CreatedAt = time.Now()
  63 + d.UpdatedAt = d.CreatedAt
  64 + } else {
  65 + d.UpdatedAt = time.Now()
  66 + }
  67 + m := repo.TransformToModel(d)
  68 + tx := repo.transactionContext.PgTx
  69 + var err error
  70 + if isCreate {
  71 + _, err = tx.Model(&m).Returning("id").Insert()
  72 + } else {
  73 + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
  74 + }
  75 + if err != nil {
  76 + return nil, err
  77 + }
  78 + d.Id = m.Id
  79 + return d, nil
  80 +}
  81 +
  82 +func (repo *LogOptRepository) Remove(d *domain.LogOpt) (*domain.LogOpt, error) {
  83 + tx := repo.transactionContext.PgTx
  84 + nowTime := time.Now()
  85 + m := repo.TransformToModel(d)
  86 + m.DeletedAt = &nowTime
  87 + if _, err := tx.Model(&m).WherePK().Update(); err != nil {
  88 + return d, err
  89 + }
  90 + return d, nil
  91 +}
  92 +
  93 +func (repo *LogOptRepository) FindOne(queryOptions map[string]interface{}) (*domain.LogOpt, error) {
  94 + tx := repo.transactionContext.PgTx
  95 + m := new(models.LogOpt)
  96 + query := tx.Model(m)
  97 + query.Where("deleted_at isnull")
  98 + if id, ok := queryOptions["id"]; ok {
  99 + query.Where("id=?", id)
  100 + }
  101 + if err := query.First(); err != nil {
  102 + if errors.Is(err, pg.ErrNoRows) {
  103 + return nil, fmt.Errorf("没有此资源")
  104 + } else {
  105 + return nil, err
  106 + }
  107 + }
  108 + u := repo.TransformToDomain(m)
  109 + return &u, nil
  110 +}
  111 +
  112 +func (repo *LogOptRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.LogOpt, error) {
  113 + tx := repo.transactionContext.PgTx
  114 + var m []*models.LogOpt
  115 +
  116 + query := tx.Model(&m).Where("deleted_at isnull")
  117 +
  118 + if v, ok := queryOptions["ids"]; ok {
  119 + query.Where("id in (?)", pg.In(v))
  120 + }
  121 +
  122 + if v, ok := queryOptions["id"]; ok {
  123 + query.Where("id=?", v)
  124 + }
  125 +
  126 + if v, ok := queryOptions["companyId"]; ok {
  127 + query.Where("company_id = ?", v)
  128 + }
  129 +
  130 + if v, ok := queryOptions["optTargetId"]; ok {
  131 + query.Where("opt_target_id = ?", v)
  132 + }
  133 +
  134 + if v, ok := queryOptions["limit"].(int64); ok {
  135 + query.Limit(int(v))
  136 + }
  137 + if v, ok := queryOptions["offset"].(int64); ok {
  138 + query.Offset(int(v))
  139 + }
  140 +
  141 + // 按创建时间降序
  142 + query.Order("created_at DESC")
  143 +
  144 + count, err := query.SelectAndCount()
  145 + if err != nil {
  146 + return 0, nil, err
  147 + }
  148 + var arrays []*domain.LogOpt
  149 + for _, v := range m {
  150 + d := repo.TransformToDomain(v)
  151 + arrays = append(arrays, &d)
  152 + }
  153 + return int64(count), arrays, nil
  154 +}
  155 +
  156 +func (repo *LogOptRepository) Count(queryOptions map[string]interface{}) (int64, error) {
  157 + tx := repo.transactionContext.PgTx
  158 + m := new(models.LogOpt)
  159 + query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
  160 + query.Where("deleted_at isnull")
  161 +
  162 + if id, ok := queryOptions["id"]; ok {
  163 + query.Where("id = ?", id)
  164 + }
  165 +
  166 + if notId, ok := queryOptions["notId"]; ok {
  167 + query.Where("id != ?", notId)
  168 + }
  169 +
  170 + if v, ok := queryOptions["companyId"]; ok {
  171 + query.Where("company_id = ?", v)
  172 + }
  173 +
  174 + count, err := query.Count()
  175 + if err != nil {
  176 + return 0, err
  177 + }
  178 + return int64(count), nil
  179 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/web/beego"
  6 + service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/log_opt"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/log_opt/command"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
  9 +)
  10 +
  11 +type LogOptController struct {
  12 + beego.BaseController
  13 +}
  14 +
  15 +func (controller *LogOptController) ListLog() {
  16 + logService := service.NewLogOptService()
  17 + in := &command.QueryLogCommand{}
  18 + if err := controller.Unmarshal(in); err != nil {
  19 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  20 + } else {
  21 + ua := middlewares.GetUser(controller.Ctx)
  22 + in.CompanyId = ua.CompanyId
  23 + controller.Response(logService.List(in))
  24 + }
  25 +}
@@ -41,7 +41,7 @@ func (c *TaskController) UpdateTask() { @@ -41,7 +41,7 @@ func (c *TaskController) UpdateTask() {
41 } 41 }
42 userReq := middlewares.GetUser(c.Ctx) 42 userReq := middlewares.GetUser(c.Ctx)
43 paramReq.CompanyId = int(userReq.CompanyId) 43 paramReq.CompanyId = int(userReq.CompanyId)
44 - data, err := srv.UpdateTask(paramReq) 44 + data, err := srv.UpdateTask(paramReq, userReq)
45 c.Response(data, err) 45 c.Response(data, err)
46 } 46 }
47 47
@@ -57,7 +57,7 @@ func (c *TaskController) CreateTask() { @@ -57,7 +57,7 @@ func (c *TaskController) CreateTask() {
57 } 57 }
58 userReq := middlewares.GetUser(c.Ctx) 58 userReq := middlewares.GetUser(c.Ctx)
59 paramReq.CompanyId = int(userReq.CompanyId) 59 paramReq.CompanyId = int(userReq.CompanyId)
60 - resp, err := srv.CreateTask(paramReq) 60 + resp, err := srv.CreateTask(paramReq, userReq)
61 c.Response(resp, err) 61 c.Response(resp, err)
62 } 62 }
63 63
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "github.com/linmadan/egglib-go/web/beego/filters"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/controllers"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
  8 +)
  9 +
  10 +func init() {
  11 + ns := web.NewNamespace("/v1/log-opt",
  12 + web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()),
  13 + web.NSRouter("/list", &controllers.LogOptController{}, "Post:ListLog"),
  14 + )
  15 +
  16 + web.AddNamespace(ns)
  17 +}