Explorar o código

增加发送文件功能

boweniac hai 10 meses
pai
achega
7f092861bc

+ 16 - 0
ent/custom_types/types.go

@@ -0,0 +1,16 @@
+package custom_types
+
+type Condition struct {
+	Equal       int      `json:"equal"`
+	LabelIdList []uint64 `json:"labelIdList"`
+}
+
+type Action struct {
+	Type    int    `json:"type"`
+	Content string `json:"content"`
+	Meta    *Meta  `json:"meta,omitempty"`
+}
+
+type Meta struct {
+	Filename string `json:"filename,omitempty"`
+}

+ 17 - 0
ent/messagerecords.go

@@ -3,12 +3,14 @@
 package ent
 
 import (
+	"encoding/json"
 	"fmt"
 	"strings"
 	"time"
 
 	"entgo.io/ent"
 	"entgo.io/ent/dialect/sql"
+	"github.com/suyuan32/simple-admin-job/ent/custom_types"
 	"github.com/suyuan32/simple-admin-job/ent/messagerecords"
 )
 
@@ -35,6 +37,8 @@ type MessageRecords struct {
 	ContentType int `json:"content_type,omitempty"`
 	// 发送内容
 	Content string `json:"content,omitempty"`
+	// 元数据
+	Meta custom_types.Meta `json:"meta,omitempty"`
 	// 异常原因
 	ErrorDetail string `json:"error_detail,omitempty"`
 	// 发送时间
@@ -53,6 +57,8 @@ func (*MessageRecords) scanValues(columns []string) ([]any, error) {
 	values := make([]any, len(columns))
 	for i := range columns {
 		switch columns[i] {
+		case messagerecords.FieldMeta:
+			values[i] = new([]byte)
 		case messagerecords.FieldID, messagerecords.FieldStatus, messagerecords.FieldContactID, messagerecords.FieldContactType, messagerecords.FieldContentType, messagerecords.FieldSourceType, messagerecords.FieldSourceID, messagerecords.FieldSubSourceID:
 			values[i] = new(sql.NullInt64)
 		case messagerecords.FieldBotWxid, messagerecords.FieldContactWxid, messagerecords.FieldContent, messagerecords.FieldErrorDetail:
@@ -134,6 +140,14 @@ func (mr *MessageRecords) assignValues(columns []string, values []any) error {
 			} else if value.Valid {
 				mr.Content = value.String
 			}
+		case messagerecords.FieldMeta:
+			if value, ok := values[i].(*[]byte); !ok {
+				return fmt.Errorf("unexpected type %T for field meta", values[i])
+			} else if value != nil && len(*value) > 0 {
+				if err := json.Unmarshal(*value, &mr.Meta); err != nil {
+					return fmt.Errorf("unmarshal field meta: %w", err)
+				}
+			}
 		case messagerecords.FieldErrorDetail:
 			if value, ok := values[i].(*sql.NullString); !ok {
 				return fmt.Errorf("unexpected type %T for field error_detail", values[i])
@@ -227,6 +241,9 @@ func (mr *MessageRecords) String() string {
 	builder.WriteString("content=")
 	builder.WriteString(mr.Content)
 	builder.WriteString(", ")
+	builder.WriteString("meta=")
+	builder.WriteString(fmt.Sprintf("%v", mr.Meta))
+	builder.WriteString(", ")
 	builder.WriteString("error_detail=")
 	builder.WriteString(mr.ErrorDetail)
 	builder.WriteString(", ")

+ 3 - 0
ent/messagerecords/messagerecords.go

@@ -31,6 +31,8 @@ const (
 	FieldContentType = "content_type"
 	// FieldContent holds the string denoting the content field in the database.
 	FieldContent = "content"
+	// FieldMeta holds the string denoting the meta field in the database.
+	FieldMeta = "meta"
 	// FieldErrorDetail holds the string denoting the error_detail field in the database.
 	FieldErrorDetail = "error_detail"
 	// FieldSendTime holds the string denoting the send_time field in the database.
@@ -57,6 +59,7 @@ var Columns = []string{
 	FieldContactWxid,
 	FieldContentType,
 	FieldContent,
+	FieldMeta,
 	FieldErrorDetail,
 	FieldSendTime,
 	FieldSourceType,

+ 10 - 0
ent/messagerecords/where.go

@@ -579,6 +579,16 @@ func ContentContainsFold(v string) predicate.MessageRecords {
 	return predicate.MessageRecords(sql.FieldContainsFold(FieldContent, v))
 }
 
+// MetaIsNil applies the IsNil predicate on the "meta" field.
+func MetaIsNil() predicate.MessageRecords {
+	return predicate.MessageRecords(sql.FieldIsNull(FieldMeta))
+}
+
+// MetaNotNil applies the NotNil predicate on the "meta" field.
+func MetaNotNil() predicate.MessageRecords {
+	return predicate.MessageRecords(sql.FieldNotNull(FieldMeta))
+}
+
 // ErrorDetailEQ applies the EQ predicate on the "error_detail" field.
 func ErrorDetailEQ(v string) predicate.MessageRecords {
 	return predicate.MessageRecords(sql.FieldEQ(FieldErrorDetail, v))

+ 19 - 0
ent/messagerecords_create.go

@@ -10,6 +10,7 @@ import (
 
 	"entgo.io/ent/dialect/sql/sqlgraph"
 	"entgo.io/ent/schema/field"
+	"github.com/suyuan32/simple-admin-job/ent/custom_types"
 	"github.com/suyuan32/simple-admin-job/ent/messagerecords"
 )
 
@@ -138,6 +139,20 @@ func (mrc *MessageRecordsCreate) SetNillableContent(s *string) *MessageRecordsCr
 	return mrc
 }
 
+// SetMeta sets the "meta" field.
+func (mrc *MessageRecordsCreate) SetMeta(ct custom_types.Meta) *MessageRecordsCreate {
+	mrc.mutation.SetMeta(ct)
+	return mrc
+}
+
+// SetNillableMeta sets the "meta" field if the given value is not nil.
+func (mrc *MessageRecordsCreate) SetNillableMeta(ct *custom_types.Meta) *MessageRecordsCreate {
+	if ct != nil {
+		mrc.SetMeta(*ct)
+	}
+	return mrc
+}
+
 // SetErrorDetail sets the "error_detail" field.
 func (mrc *MessageRecordsCreate) SetErrorDetail(s string) *MessageRecordsCreate {
 	mrc.mutation.SetErrorDetail(s)
@@ -392,6 +407,10 @@ func (mrc *MessageRecordsCreate) createSpec() (*MessageRecords, *sqlgraph.Create
 		_spec.SetField(messagerecords.FieldContent, field.TypeString, value)
 		_node.Content = value
 	}
+	if value, ok := mrc.mutation.Meta(); ok {
+		_spec.SetField(messagerecords.FieldMeta, field.TypeJSON, value)
+		_node.Meta = value
+	}
 	if value, ok := mrc.mutation.ErrorDetail(); ok {
 		_spec.SetField(messagerecords.FieldErrorDetail, field.TypeString, value)
 		_node.ErrorDetail = value

+ 53 - 0
ent/messagerecords_update.go

@@ -11,6 +11,7 @@ import (
 	"entgo.io/ent/dialect/sql"
 	"entgo.io/ent/dialect/sql/sqlgraph"
 	"entgo.io/ent/schema/field"
+	"github.com/suyuan32/simple-admin-job/ent/custom_types"
 	"github.com/suyuan32/simple-admin-job/ent/messagerecords"
 	"github.com/suyuan32/simple-admin-job/ent/predicate"
 )
@@ -172,6 +173,26 @@ func (mru *MessageRecordsUpdate) SetNillableContent(s *string) *MessageRecordsUp
 	return mru
 }
 
+// SetMeta sets the "meta" field.
+func (mru *MessageRecordsUpdate) SetMeta(ct custom_types.Meta) *MessageRecordsUpdate {
+	mru.mutation.SetMeta(ct)
+	return mru
+}
+
+// SetNillableMeta sets the "meta" field if the given value is not nil.
+func (mru *MessageRecordsUpdate) SetNillableMeta(ct *custom_types.Meta) *MessageRecordsUpdate {
+	if ct != nil {
+		mru.SetMeta(*ct)
+	}
+	return mru
+}
+
+// ClearMeta clears the value of the "meta" field.
+func (mru *MessageRecordsUpdate) ClearMeta() *MessageRecordsUpdate {
+	mru.mutation.ClearMeta()
+	return mru
+}
+
 // SetErrorDetail sets the "error_detail" field.
 func (mru *MessageRecordsUpdate) SetErrorDetail(s string) *MessageRecordsUpdate {
 	mru.mutation.SetErrorDetail(s)
@@ -373,6 +394,12 @@ func (mru *MessageRecordsUpdate) sqlSave(ctx context.Context) (n int, err error)
 	if value, ok := mru.mutation.Content(); ok {
 		_spec.SetField(messagerecords.FieldContent, field.TypeString, value)
 	}
+	if value, ok := mru.mutation.Meta(); ok {
+		_spec.SetField(messagerecords.FieldMeta, field.TypeJSON, value)
+	}
+	if mru.mutation.MetaCleared() {
+		_spec.ClearField(messagerecords.FieldMeta, field.TypeJSON)
+	}
 	if value, ok := mru.mutation.ErrorDetail(); ok {
 		_spec.SetField(messagerecords.FieldErrorDetail, field.TypeString, value)
 	}
@@ -570,6 +597,26 @@ func (mruo *MessageRecordsUpdateOne) SetNillableContent(s *string) *MessageRecor
 	return mruo
 }
 
+// SetMeta sets the "meta" field.
+func (mruo *MessageRecordsUpdateOne) SetMeta(ct custom_types.Meta) *MessageRecordsUpdateOne {
+	mruo.mutation.SetMeta(ct)
+	return mruo
+}
+
+// SetNillableMeta sets the "meta" field if the given value is not nil.
+func (mruo *MessageRecordsUpdateOne) SetNillableMeta(ct *custom_types.Meta) *MessageRecordsUpdateOne {
+	if ct != nil {
+		mruo.SetMeta(*ct)
+	}
+	return mruo
+}
+
+// ClearMeta clears the value of the "meta" field.
+func (mruo *MessageRecordsUpdateOne) ClearMeta() *MessageRecordsUpdateOne {
+	mruo.mutation.ClearMeta()
+	return mruo
+}
+
 // SetErrorDetail sets the "error_detail" field.
 func (mruo *MessageRecordsUpdateOne) SetErrorDetail(s string) *MessageRecordsUpdateOne {
 	mruo.mutation.SetErrorDetail(s)
@@ -801,6 +848,12 @@ func (mruo *MessageRecordsUpdateOne) sqlSave(ctx context.Context) (_node *Messag
 	if value, ok := mruo.mutation.Content(); ok {
 		_spec.SetField(messagerecords.FieldContent, field.TypeString, value)
 	}
+	if value, ok := mruo.mutation.Meta(); ok {
+		_spec.SetField(messagerecords.FieldMeta, field.TypeJSON, value)
+	}
+	if mruo.mutation.MetaCleared() {
+		_spec.ClearField(messagerecords.FieldMeta, field.TypeJSON)
+	}
 	if value, ok := mruo.mutation.ErrorDetail(); ok {
 		_spec.SetField(messagerecords.FieldErrorDetail, field.TypeString, value)
 	}

+ 1 - 0
ent/migrate/schema.go

@@ -21,6 +21,7 @@ var (
 		{Name: "contact_wxid", Type: field.TypeString, Comment: "接收方微信 id", Default: ""},
 		{Name: "content_type", Type: field.TypeInt, Comment: "内容类型 1 文本 2 文件", Default: 1},
 		{Name: "content", Type: field.TypeString, Comment: "发送内容", Default: ""},
+		{Name: "meta", Type: field.TypeJSON, Nullable: true, Comment: "元数据"},
 		{Name: "error_detail", Type: field.TypeString, Comment: "异常原因", Default: ""},
 		{Name: "send_time", Type: field.TypeTime, Nullable: true, Comment: "发送时间"},
 		{Name: "source_type", Type: field.TypeInt, Comment: "源类型 1 点发 2 群发 3 SOP", Default: 1},

+ 75 - 1
ent/mutation.go

@@ -11,6 +11,7 @@ import (
 
 	"entgo.io/ent"
 	"entgo.io/ent/dialect/sql"
+	"github.com/suyuan32/simple-admin-job/ent/custom_types"
 	"github.com/suyuan32/simple-admin-job/ent/messagerecords"
 	"github.com/suyuan32/simple-admin-job/ent/predicate"
 	"github.com/suyuan32/simple-admin-job/ent/task"
@@ -50,6 +51,7 @@ type MessageRecordsMutation struct {
 	content_type     *int
 	addcontent_type  *int
 	content          *string
+	meta             *custom_types.Meta
 	error_detail     *string
 	send_time        *time.Time
 	source_type      *int
@@ -600,6 +602,55 @@ func (m *MessageRecordsMutation) ResetContent() {
 	m.content = nil
 }
 
+// SetMeta sets the "meta" field.
+func (m *MessageRecordsMutation) SetMeta(ct custom_types.Meta) {
+	m.meta = &ct
+}
+
+// Meta returns the value of the "meta" field in the mutation.
+func (m *MessageRecordsMutation) Meta() (r custom_types.Meta, exists bool) {
+	v := m.meta
+	if v == nil {
+		return
+	}
+	return *v, true
+}
+
+// OldMeta returns the old "meta" field's value of the MessageRecords entity.
+// If the MessageRecords object wasn't provided to the builder, the object is fetched from the database.
+// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
+func (m *MessageRecordsMutation) OldMeta(ctx context.Context) (v custom_types.Meta, err error) {
+	if !m.op.Is(OpUpdateOne) {
+		return v, errors.New("OldMeta is only allowed on UpdateOne operations")
+	}
+	if m.id == nil || m.oldValue == nil {
+		return v, errors.New("OldMeta requires an ID field in the mutation")
+	}
+	oldValue, err := m.oldValue(ctx)
+	if err != nil {
+		return v, fmt.Errorf("querying old value for OldMeta: %w", err)
+	}
+	return oldValue.Meta, nil
+}
+
+// ClearMeta clears the value of the "meta" field.
+func (m *MessageRecordsMutation) ClearMeta() {
+	m.meta = nil
+	m.clearedFields[messagerecords.FieldMeta] = struct{}{}
+}
+
+// MetaCleared returns if the "meta" field was cleared in this mutation.
+func (m *MessageRecordsMutation) MetaCleared() bool {
+	_, ok := m.clearedFields[messagerecords.FieldMeta]
+	return ok
+}
+
+// ResetMeta resets all changes to the "meta" field.
+func (m *MessageRecordsMutation) ResetMeta() {
+	m.meta = nil
+	delete(m.clearedFields, messagerecords.FieldMeta)
+}
+
 // SetErrorDetail sets the "error_detail" field.
 func (m *MessageRecordsMutation) SetErrorDetail(s string) {
 	m.error_detail = &s
@@ -915,7 +966,7 @@ func (m *MessageRecordsMutation) Type() string {
 // order to get all numeric fields that were incremented/decremented, call
 // AddedFields().
 func (m *MessageRecordsMutation) Fields() []string {
-	fields := make([]string, 0, 14)
+	fields := make([]string, 0, 15)
 	if m.created_at != nil {
 		fields = append(fields, messagerecords.FieldCreatedAt)
 	}
@@ -943,6 +994,9 @@ func (m *MessageRecordsMutation) Fields() []string {
 	if m.content != nil {
 		fields = append(fields, messagerecords.FieldContent)
 	}
+	if m.meta != nil {
+		fields = append(fields, messagerecords.FieldMeta)
+	}
 	if m.error_detail != nil {
 		fields = append(fields, messagerecords.FieldErrorDetail)
 	}
@@ -984,6 +1038,8 @@ func (m *MessageRecordsMutation) Field(name string) (ent.Value, bool) {
 		return m.ContentType()
 	case messagerecords.FieldContent:
 		return m.Content()
+	case messagerecords.FieldMeta:
+		return m.Meta()
 	case messagerecords.FieldErrorDetail:
 		return m.ErrorDetail()
 	case messagerecords.FieldSendTime:
@@ -1021,6 +1077,8 @@ func (m *MessageRecordsMutation) OldField(ctx context.Context, name string) (ent
 		return m.OldContentType(ctx)
 	case messagerecords.FieldContent:
 		return m.OldContent(ctx)
+	case messagerecords.FieldMeta:
+		return m.OldMeta(ctx)
 	case messagerecords.FieldErrorDetail:
 		return m.OldErrorDetail(ctx)
 	case messagerecords.FieldSendTime:
@@ -1103,6 +1161,13 @@ func (m *MessageRecordsMutation) SetField(name string, value ent.Value) error {
 		}
 		m.SetContent(v)
 		return nil
+	case messagerecords.FieldMeta:
+		v, ok := value.(custom_types.Meta)
+		if !ok {
+			return fmt.Errorf("unexpected type %T for field %s", value, name)
+		}
+		m.SetMeta(v)
+		return nil
 	case messagerecords.FieldErrorDetail:
 		v, ok := value.(string)
 		if !ok {
@@ -1261,6 +1326,9 @@ func (m *MessageRecordsMutation) ClearedFields() []string {
 	if m.FieldCleared(messagerecords.FieldContactID) {
 		fields = append(fields, messagerecords.FieldContactID)
 	}
+	if m.FieldCleared(messagerecords.FieldMeta) {
+		fields = append(fields, messagerecords.FieldMeta)
+	}
 	if m.FieldCleared(messagerecords.FieldSendTime) {
 		fields = append(fields, messagerecords.FieldSendTime)
 	}
@@ -1290,6 +1358,9 @@ func (m *MessageRecordsMutation) ClearField(name string) error {
 	case messagerecords.FieldContactID:
 		m.ClearContactID()
 		return nil
+	case messagerecords.FieldMeta:
+		m.ClearMeta()
+		return nil
 	case messagerecords.FieldSendTime:
 		m.ClearSendTime()
 		return nil
@@ -1334,6 +1405,9 @@ func (m *MessageRecordsMutation) ResetField(name string) error {
 	case messagerecords.FieldContent:
 		m.ResetContent()
 		return nil
+	case messagerecords.FieldMeta:
+		m.ResetMeta()
+		return nil
 	case messagerecords.FieldErrorDetail:
 		m.ResetErrorDetail()
 		return nil

+ 4 - 4
ent/runtime.go

@@ -53,19 +53,19 @@ func init() {
 	// messagerecords.DefaultContent holds the default value on creation for the content field.
 	messagerecords.DefaultContent = messagerecordsDescContent.Default.(string)
 	// messagerecordsDescErrorDetail is the schema descriptor for error_detail field.
-	messagerecordsDescErrorDetail := messagerecordsFields[6].Descriptor()
+	messagerecordsDescErrorDetail := messagerecordsFields[7].Descriptor()
 	// messagerecords.DefaultErrorDetail holds the default value on creation for the error_detail field.
 	messagerecords.DefaultErrorDetail = messagerecordsDescErrorDetail.Default.(string)
 	// messagerecordsDescSourceType is the schema descriptor for source_type field.
-	messagerecordsDescSourceType := messagerecordsFields[8].Descriptor()
+	messagerecordsDescSourceType := messagerecordsFields[9].Descriptor()
 	// messagerecords.DefaultSourceType holds the default value on creation for the source_type field.
 	messagerecords.DefaultSourceType = messagerecordsDescSourceType.Default.(int)
 	// messagerecordsDescSourceID is the schema descriptor for source_id field.
-	messagerecordsDescSourceID := messagerecordsFields[9].Descriptor()
+	messagerecordsDescSourceID := messagerecordsFields[10].Descriptor()
 	// messagerecords.DefaultSourceID holds the default value on creation for the source_id field.
 	messagerecords.DefaultSourceID = messagerecordsDescSourceID.Default.(uint64)
 	// messagerecordsDescSubSourceID is the schema descriptor for sub_source_id field.
-	messagerecordsDescSubSourceID := messagerecordsFields[10].Descriptor()
+	messagerecordsDescSubSourceID := messagerecordsFields[11].Descriptor()
 	// messagerecords.DefaultSubSourceID holds the default value on creation for the sub_source_id field.
 	messagerecords.DefaultSubSourceID = messagerecordsDescSubSourceID.Default.(uint64)
 	taskMixin := schema.Task{}.Mixin()

+ 4 - 0
ent/schema/message_records.go

@@ -6,6 +6,7 @@ import (
 	"entgo.io/ent/schema"
 	"entgo.io/ent/schema/field"
 	"github.com/suyuan32/simple-admin-common/orm/ent/mixins"
+	"github.com/suyuan32/simple-admin-job/ent/custom_types"
 )
 
 type MessageRecords struct {
@@ -32,6 +33,9 @@ func (MessageRecords) Fields() []ent.Field {
 		field.String("content").Default("").
 			Annotations(entsql.WithComments(true)).
 			Comment("发送内容"),
+		field.JSON("meta", custom_types.Meta{}).Optional().
+			Annotations(entsql.WithComments(true)).
+			Comment("元数据"),
 		field.String("error_detail").Default("").
 			Annotations(entsql.WithComments(true)).
 			Comment("异常原因"),

+ 29 - 1
ent/set_not_nil.go

@@ -2,7 +2,11 @@
 
 package ent
 
-import "time"
+import (
+	"time"
+
+	"github.com/suyuan32/simple-admin-job/ent/custom_types"
+)
 
 // set field if value's pointer is not nil.
 func (mr *MessageRecordsUpdate) SetNotNilUpdatedAt(value *time.Time) *MessageRecordsUpdate {
@@ -197,6 +201,30 @@ func (mr *MessageRecordsCreate) SetNotNilContent(value *string) *MessageRecordsC
 }
 
 // set field if value's pointer is not nil.
+func (mr *MessageRecordsUpdate) SetNotNilMeta(value *custom_types.Meta) *MessageRecordsUpdate {
+	if value != nil {
+		return mr.SetMeta(*value)
+	}
+	return mr
+}
+
+// set field if value's pointer is not nil.
+func (mr *MessageRecordsUpdateOne) SetNotNilMeta(value *custom_types.Meta) *MessageRecordsUpdateOne {
+	if value != nil {
+		return mr.SetMeta(*value)
+	}
+	return mr
+}
+
+// set field if value's pointer is not nil.
+func (mr *MessageRecordsCreate) SetNotNilMeta(value *custom_types.Meta) *MessageRecordsCreate {
+	if value != nil {
+		return mr.SetMeta(*value)
+	}
+	return mr
+}
+
+// set field if value's pointer is not nil.
 func (mr *MessageRecordsUpdate) SetNotNilErrorDetail(value *string) *MessageRecordsUpdate {
 	if value != nil {
 		return mr.SetErrorDetail(*value)

+ 4 - 3
internal/mqs/amq/handler/amq/wxhook/send_wx.go

@@ -98,9 +98,10 @@ func (l *SendWxHandler) ProcessTask(ctx context.Context, t *asynq.Task) error {
 			}).Post("http://" + p.Ip + ":" + wxPort + "/SendTextMsg")
 		} else {
 			_, err = client.R().SetBody(&SendFileMsgReq{
-				Wxid:     v.ContactWxid,
-				Filepath: v.Content,
-			}).Post("http://" + p.Ip + ":" + wxPort + "/SendTextMsg")
+				Wxid:        v.ContactWxid,
+				Filepath:    v.Content,
+				Diyfilename: v.Meta.Filename,
+			}).Post("http://" + p.Ip + ":" + wxPort + "/SendFileMsg")
 		}
 
 		if err != nil {