work_order_master.go 6.0 KB

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