work_order_master.go 5.9 KB

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