package model import ( "encoding/json" "fmt" "kpt-pasture/config" "kpt-pasture/http/util" "strconv" "strings" "time" "github.com/hibiken/asynq" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) const ( QueueWorkOrder = "workOrder" TaskWorkOrder = "event:workOrder" ) func AsynqQueueWorkOrder() asynq.Option { return asynq.Queue(QueueWorkOrder) } type WorkOrderMaster struct { Id int64 `json:"id"` Name string `json:"name"` CategoryId pasturePb.WorkOrderCategory_Kind `json:"categoryId"` CategoryName string `json:"categoryName"` Priority pasturePb.Priority_Kind `json:"priority"` Frequency pasturePb.WorkOrderFrequency_Kind `json:"frequency"` ExecTime string `json:"execTime"` SubscribeUnit pasturePb.WorkOrderSubscribeUnit_Kind `json:"subscribeUnit"` ExecPersons string `json:"execPersons"` ExecPersonNames string `json:"execPersonsNames"` WeekMonthValue string `json:"WeekMonthValue"` IsShow pasturePb.IsShow_Kind `json:"isShow"` Photos string `json:"photos"` Remarks string `json:"remarks"` OperationId int64 `json:"operationId"` OperationName string `json:"operationName"` CreatedAt int64 `json:"createdAt"` UpdatedAt int64 `json:"updatedAt"` } func (w *WorkOrderMaster) TableName() string { return "work_order_master" } func NewWorkOrderMaster( req *pasturePb.WorkOrderList, curUser *SystemUser, systemUserList []*SystemUser, workOrderCategoryMap map[pasturePb.WorkOrderCategory_Kind]string, ) *WorkOrderMaster { execPersonNames := make([]string, 0) if len(req.ExecPersons) > 0 { for _, vv := range systemUserList { for _, v := range req.ExecPersons { if int32(vv.Id) != v { continue } execPersonNames = append(execPersonNames, vv.Name) } for _, depId := range req.ExecDepartmental { if int64(depId) != vv.DeptId { continue } execPersonNames = append(execPersonNames, vv.Name) } } } weekMonthValue := "" if req.Frequency == pasturePb.WorkOrderFrequency_Weekly || req.Frequency == pasturePb.WorkOrderFrequency_Monthly { weekMonthValue = util.Int32SliceToString(req.WeekMonthValue, ",") } return &WorkOrderMaster{ Name: req.Name, CategoryId: req.CategoryId, CategoryName: workOrderCategoryMap[req.CategoryId], Priority: req.Priority, ExecTime: req.ExecTime, SubscribeUnit: req.SubscribeUnit, ExecPersons: util.Int32SliceToString(req.ExecPersons, ","), ExecPersonNames: strings.Join(execPersonNames, ","), WeekMonthValue: weekMonthValue, IsShow: req.IsShow, Photos: strings.Join(req.Photos, ","), Frequency: req.Frequency, Remarks: req.Remarks, OperationId: curUser.Id, OperationName: curUser.Name, } } type TaskWorkOrderPayload struct { WorkOrderId int64 `json:"workOrderId"` } func NewTaskWorkOrderOption(processAt int64) []asynq.Option { return []asynq.Option{ asynq.MaxRetry(3), AsynqQueueWorkOrder(), asynq.ProcessAt(time.Unix(processAt, 64)), } } func NewTaskWorkOrderPayload(id int64, execTime time.Duration) *asynq.Task { payload, _ := json.Marshal(TaskWorkOrderPayload{ WorkOrderId: id, }) processAt := time.Now().Add(execTime).Unix() return asynq.NewTask(fmt.Sprintf("%s:%s", config.Options().FarmName, TaskWorkOrder), payload, NewTaskWorkOrderOption(processAt)...) } type WorkOrderMasterSlice []*WorkOrderMaster func (w WorkOrderMasterSlice) ToPB( priorityMap map[pasturePb.Priority_Kind]string, frequencyMap map[pasturePb.WorkOrderFrequency_Kind]string, subscribeUnitMap map[pasturePb.WorkOrderSubscribeUnit_Kind]string, weekMap map[pasturePb.Week_Kind]string, monthMap map[int32]string, ) []*pasturePb.WorkOrderList { res := make([]*pasturePb.WorkOrderList, len(w)) for i, v := range w { execPersons, weekMonthValue := make([]int32, 0), make([]int32, 0) weekMonthValueName := make([]string, 0) if len(v.ExecPersons) > 0 { for _, personId := range strings.Split(v.ExecPersons, ",") { p, _ := strconv.Atoi(personId) execPersons = append(execPersons, int32(p)) } } if len(v.WeekMonthValue) > 0 { for _, week := range strings.Split(v.WeekMonthValue, ",") { k, _ := strconv.Atoi(week) if v.Frequency == pasturePb.WorkOrderFrequency_Weekly { weekMonthValueName = append(weekMonthValueName, weekMap[pasturePb.Week_Kind(k)]) } if v.Frequency == pasturePb.WorkOrderFrequency_Monthly { weekMonthValueName = append(weekMonthValueName, monthMap[int32(k)]) } weekMonthValue = append(weekMonthValue, int32(k)) } } res[i] = &pasturePb.WorkOrderList{ Id: int32(v.Id), Name: v.Name, CategoryId: v.CategoryId, CategoryName: v.CategoryName, Priority: v.Priority, PriorityName: priorityMap[v.Priority], Frequency: v.Frequency, FrequencyName: frequencyMap[v.Frequency], ExecTime: v.ExecTime, SubscribeUnit: v.SubscribeUnit, SubscribeUnitName: subscribeUnitMap[v.SubscribeUnit], ExecPersons: execPersons, ExecPersonsNames: strings.Split(v.ExecPersonNames, ","), IsShow: v.IsShow, Photos: strings.Split(v.Photos, ","), WeekMonthValue: weekMonthValue, WeekMonthValueName: weekMonthValueName, Remarks: v.Remarks, OperatorId: int32(v.OperationId), OperatorName: v.OperationName, CreatedAt: int32(v.CreatedAt), UpdatedAt: int32(v.UpdatedAt), } } return res }