| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 | package modelimport (	"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 (	QueueWork     = "work"	TaskWorkOrder = "event:workOrder")func AsynqQueueWorkOrder() asynq.Option {	return asynq.Queue(QueueWork)}type WorkOrderMaster struct {	Id              int64                                 `json:"id"`	PastureId       int64                                 `json:"pastureId"`	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(	pastureId int64,	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{		PastureId:       pastureId,		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 []*WorkOrderMasterfunc (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}
 |