12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package data_import
- import (
- "adm-management/consts"
- "adm-management/parser"
- v1 "adm-management/pb/v1"
- "context"
- "encoding/json"
- "fmt"
- "strconv"
- "strings"
- "time"
- "git.getensh.com/common/gopkgsv2/database"
- "git.getensh.com/common/gopkgsv2/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- type OdsMessage struct {
- MsgType string `json:"msg_type"`
- SourceCode string `json:"source_code"` // 来源编码
- OfflineTaskId int64 `json:"offline_task_id"` // 离线消息任务id
- TaskList []int `json:"task_list"` // 任务列表
- From int `json:"from"` // 消息来源类型 1 数据库 2 excel
- Content string `json:"content"`
- Timestamp int64 `json:"timestamp"`
- }
- func DataImport(ctx context.Context, req *v1.DataImportRequest) (reply *v1.DataImportReply, err error) {
- reply = &v1.DataImportReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- db := database.DB()
- var taskIds string
- err = db.Raw("SELECT task_ids FROM t_adm_data_template where id = ? ", req.Id).Find(&taskIds).Error
- ids := strings.Split(taskIds, ",")
- // 构建消息
- odsMsg := OdsMessage{MsgType: consts.ODSMANUALAMENDMENT, From: 0}
- if len(ids) > 0 {
- for _, v := range ids {
- taskId, _ := strconv.Atoi(v)
- odsMsg.TaskList = append(odsMsg.TaskList, taskId)
- }
- }
- odsMsg.Timestamp = time.Now().Unix()
- requestList := make(map[string]string)
- for i := 0; i < len(req.RequestList); i++ {
- requestList[req.RequestList[i].Request] = req.RequestList[i].Value
- }
- nContent := make(map[string]interface{})
- nContent["new_content"] = requestList
- c, _ := json.Marshal(nContent)
- content := string(c)
- odsMsg.Content = content
- // 发送消息
- odsMsgByte, err := json.Marshal(odsMsg)
- if err != nil {
- return reply, err
- }
- err = parser.OdsMq.PublishMsg(odsMsgByte)
- if err != nil {
- return reply, err
- }
- return reply, err
- }
|