work_order_master.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "kpt-pasture/config"
  6. "kpt-pasture/http/util"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/hibiken/asynq"
  11. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  12. )
  13. const (
  14. QueueWorkOrder = "workOrder"
  15. TaskWorkOrder = "event:workOrder"
  16. )
  17. func AsynqQueueWorkOrder() asynq.Option {
  18. return asynq.Queue(QueueWorkOrder)
  19. }
  20. type WorkOrderMaster struct {
  21. Id int64 `json:"id"`
  22. Name string `json:"name"`
  23. CategoryId pasturePb.WorkOrderCategory_Kind `json:"categoryId"`
  24. CategoryName string `json:"categoryName"`
  25. Priority pasturePb.Priority_Kind `json:"priority"`
  26. Frequency pasturePb.WorkOrderFrequency_Kind `json:"frequency"`
  27. ExecTime string `json:"execTime"`
  28. SubscribeUnit pasturePb.WorkOrderSubscribeUnit_Kind `json:"subscribeUnit"`
  29. ExecPersons string `json:"execPersons"`
  30. ExecPersonNames string `json:"execPersonsNames"`
  31. WeekMonthValue string `json:"WeekMonthValue"`
  32. IsShow pasturePb.IsShow_Kind `json:"isShow"`
  33. Photos string `json:"photos"`
  34. Remarks string `json:"remarks"`
  35. OperationId int64 `json:"operationId"`
  36. OperationName string `json:"operationName"`
  37. CreatedAt int64 `json:"createdAt"`
  38. UpdatedAt int64 `json:"updatedAt"`
  39. }
  40. func (w *WorkOrderMaster) TableName() string {
  41. return "work_order_master"
  42. }
  43. func NewWorkOrderMaster(
  44. req *pasturePb.WorkOrderList,
  45. curUser *SystemUser,
  46. systemUserList []*SystemUser,
  47. workOrderCategoryMap map[pasturePb.WorkOrderCategory_Kind]string,
  48. ) *WorkOrderMaster {
  49. execPersonNames := make([]string, 0)
  50. if len(req.ExecPersons) > 0 {
  51. for _, vv := range systemUserList {
  52. for _, v := range req.ExecPersons {
  53. if int32(vv.Id) != v {
  54. continue
  55. }
  56. execPersonNames = append(execPersonNames, vv.Name)
  57. }
  58. for _, depId := range req.ExecDepartmental {
  59. if int64(depId) != vv.DeptId {
  60. continue
  61. }
  62. execPersonNames = append(execPersonNames, vv.Name)
  63. }
  64. }
  65. }
  66. weekMonthValue := ""
  67. if req.Frequency == pasturePb.WorkOrderFrequency_Weekly || req.Frequency == pasturePb.WorkOrderFrequency_Monthly {
  68. weekMonthValue = util.Int32SliceToString(req.WeekMonthValue, ",")
  69. }
  70. return &WorkOrderMaster{
  71. Name: req.Name,
  72. CategoryId: req.CategoryId,
  73. CategoryName: workOrderCategoryMap[req.CategoryId],
  74. Priority: req.Priority,
  75. ExecTime: req.ExecTime,
  76. SubscribeUnit: req.SubscribeUnit,
  77. ExecPersons: util.Int32SliceToString(req.ExecPersons, ","),
  78. ExecPersonNames: strings.Join(execPersonNames, ","),
  79. WeekMonthValue: weekMonthValue,
  80. IsShow: req.IsShow,
  81. Photos: strings.Join(req.Photos, ","),
  82. Frequency: req.Frequency,
  83. Remarks: req.Remarks,
  84. OperationId: curUser.Id,
  85. OperationName: curUser.Name,
  86. }
  87. }
  88. type TaskWorkOrderPayload struct {
  89. WorkOrderId int64 `json:"workOrderId"`
  90. }
  91. func NewTaskWorkOrderOption(processAt int64) []asynq.Option {
  92. return []asynq.Option{
  93. asynq.MaxRetry(3),
  94. AsynqQueueWorkOrder(),
  95. asynq.ProcessAt(time.Unix(processAt, 64)),
  96. }
  97. }
  98. func NewTaskWorkOrderPayload(id int64, execTime time.Duration) *asynq.Task {
  99. payload, _ := json.Marshal(TaskWorkOrderPayload{
  100. WorkOrderId: id,
  101. })
  102. processAt := time.Now().Add(execTime).Unix()
  103. return asynq.NewTask(fmt.Sprintf("%s:%s", config.Options().FarmName, TaskWorkOrder), payload, NewTaskWorkOrderOption(processAt)...)
  104. }
  105. type WorkOrderMasterSlice []*WorkOrderMaster
  106. func (w WorkOrderMasterSlice) ToPB(
  107. priorityMap map[pasturePb.Priority_Kind]string,
  108. frequencyMap map[pasturePb.WorkOrderFrequency_Kind]string,
  109. subscribeUnitMap map[pasturePb.WorkOrderSubscribeUnit_Kind]string,
  110. weekMap map[pasturePb.Week_Kind]string,
  111. monthMap map[int32]string,
  112. ) []*pasturePb.WorkOrderList {
  113. res := make([]*pasturePb.WorkOrderList, len(w))
  114. for i, v := range w {
  115. execPersons, weekMonthValue := make([]int32, 0), make([]int32, 0)
  116. weekMonthValueName := make([]string, 0)
  117. if len(v.ExecPersons) > 0 {
  118. for _, personId := range strings.Split(v.ExecPersons, ",") {
  119. p, _ := strconv.Atoi(personId)
  120. execPersons = append(execPersons, int32(p))
  121. }
  122. }
  123. if len(v.WeekMonthValue) > 0 {
  124. for _, week := range strings.Split(v.WeekMonthValue, ",") {
  125. k, _ := strconv.Atoi(week)
  126. if v.Frequency == pasturePb.WorkOrderFrequency_Weekly {
  127. weekMonthValueName = append(weekMonthValueName, weekMap[pasturePb.Week_Kind(k)])
  128. }
  129. if v.Frequency == pasturePb.WorkOrderFrequency_Monthly {
  130. weekMonthValueName = append(weekMonthValueName, monthMap[int32(k)])
  131. }
  132. weekMonthValue = append(weekMonthValue, int32(k))
  133. }
  134. }
  135. res[i] = &pasturePb.WorkOrderList{
  136. Id: int32(v.Id),
  137. Name: v.Name,
  138. CategoryId: v.CategoryId,
  139. CategoryName: v.CategoryName,
  140. Priority: v.Priority,
  141. PriorityName: priorityMap[v.Priority],
  142. Frequency: v.Frequency,
  143. FrequencyName: frequencyMap[v.Frequency],
  144. ExecTime: v.ExecTime,
  145. SubscribeUnit: v.SubscribeUnit,
  146. SubscribeUnitName: subscribeUnitMap[v.SubscribeUnit],
  147. ExecPersons: execPersons,
  148. ExecPersonsNames: strings.Split(v.ExecPersonNames, ","),
  149. IsShow: v.IsShow,
  150. Photos: strings.Split(v.Photos, ","),
  151. WeekMonthValue: weekMonthValue,
  152. WeekMonthValueName: weekMonthValueName,
  153. Remarks: v.Remarks,
  154. OperatorId: int32(v.OperationId),
  155. OperatorName: v.OperationName,
  156. CreatedAt: int32(v.CreatedAt),
  157. UpdatedAt: int32(v.UpdatedAt),
  158. }
  159. }
  160. return res
  161. }