|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/TeaOSLab/EdgeAPI/internal/errors" |
| 6 | + _ "github.com/go-sql-driver/mysql" |
| 7 | + "github.com/iwind/TeaGo/Tea" |
| 8 | + "github.com/iwind/TeaGo/dbs" |
| 9 | + "github.com/iwind/TeaGo/maps" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + MessageMediaInstanceStateEnabled = 1 // 已启用 |
| 14 | + MessageMediaInstanceStateDisabled = 0 // 已禁用 |
| 15 | +) |
| 16 | + |
| 17 | +type MessageMediaInstanceDAO dbs.DAO |
| 18 | + |
| 19 | +func NewMessageMediaInstanceDAO() *MessageMediaInstanceDAO { |
| 20 | + return dbs.NewDAO(&MessageMediaInstanceDAO{ |
| 21 | + DAOObject: dbs.DAOObject{ |
| 22 | + DB: Tea.Env, |
| 23 | + Table: "edgeMessageMediaInstances", |
| 24 | + Model: new(MessageMediaInstance), |
| 25 | + PkName: "id", |
| 26 | + }, |
| 27 | + }).(*MessageMediaInstanceDAO) |
| 28 | +} |
| 29 | + |
| 30 | +var SharedMessageMediaInstanceDAO *MessageMediaInstanceDAO |
| 31 | + |
| 32 | +func init() { |
| 33 | + dbs.OnReady(func() { |
| 34 | + SharedMessageMediaInstanceDAO = NewMessageMediaInstanceDAO() |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +// 启用条目 |
| 39 | +func (this *MessageMediaInstanceDAO) EnableMessageMediaInstance(tx *dbs.Tx, id int64) error { |
| 40 | + _, err := this.Query(tx). |
| 41 | + Pk(id). |
| 42 | + Set("state", MessageMediaInstanceStateEnabled). |
| 43 | + Update() |
| 44 | + return err |
| 45 | +} |
| 46 | + |
| 47 | +// 禁用条目 |
| 48 | +func (this *MessageMediaInstanceDAO) DisableMessageMediaInstance(tx *dbs.Tx, id int64) error { |
| 49 | + _, err := this.Query(tx). |
| 50 | + Pk(id). |
| 51 | + Set("state", MessageMediaInstanceStateDisabled). |
| 52 | + Update() |
| 53 | + return err |
| 54 | +} |
| 55 | + |
| 56 | +// 查找启用中的条目 |
| 57 | +func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx, id int64) (*MessageMediaInstance, error) { |
| 58 | + result, err := this.Query(tx). |
| 59 | + Pk(id). |
| 60 | + Attr("state", MessageMediaInstanceStateEnabled). |
| 61 | + Find() |
| 62 | + if result == nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + return result.(*MessageMediaInstance), err |
| 66 | +} |
| 67 | + |
| 68 | +// 创建媒介实例 |
| 69 | +func (this *MessageMediaInstanceDAO) CreateMediaInstance(tx *dbs.Tx, name string, mediaType string, params maps.Map, description string) (int64, error) { |
| 70 | + op := NewMessageMediaInstanceOperator() |
| 71 | + op.Name = name |
| 72 | + op.MediaType = mediaType |
| 73 | + |
| 74 | + // 参数 |
| 75 | + if params == nil { |
| 76 | + params = maps.Map{} |
| 77 | + } |
| 78 | + paramsJSON, err := json.Marshal(params) |
| 79 | + if err != nil { |
| 80 | + return 0, err |
| 81 | + } |
| 82 | + op.Params = paramsJSON |
| 83 | + |
| 84 | + op.Description = description |
| 85 | + |
| 86 | + op.IsOn = true |
| 87 | + op.State = MessageMediaInstanceStateEnabled |
| 88 | + return this.SaveInt64(tx, op) |
| 89 | +} |
| 90 | + |
| 91 | +// 修改媒介实例 |
| 92 | +func (this *MessageMediaInstanceDAO) UpdateMediaInstance(tx *dbs.Tx, instanceId int64, name string, mediaType string, params maps.Map, description string, isOn bool) error { |
| 93 | + if instanceId <= 0 { |
| 94 | + return errors.New("invalid instanceId") |
| 95 | + } |
| 96 | + |
| 97 | + op := NewMessageMediaInstanceOperator() |
| 98 | + op.Id = instanceId |
| 99 | + op.Name = name |
| 100 | + op.MediaType = mediaType |
| 101 | + |
| 102 | + // 参数 |
| 103 | + if params == nil { |
| 104 | + params = maps.Map{} |
| 105 | + } |
| 106 | + paramsJSON, err := json.Marshal(params) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + op.Params = paramsJSON |
| 111 | + |
| 112 | + op.Description = description |
| 113 | + op.IsOn = isOn |
| 114 | + return this.Save(tx, op) |
| 115 | +} |
| 116 | + |
| 117 | +// 计算接收人数量 |
| 118 | +func (this *MessageMediaInstanceDAO) CountAllEnabledMediaInstances(tx *dbs.Tx, mediaType string, keyword string) (int64, error) { |
| 119 | + query := this.Query(tx) |
| 120 | + if len(mediaType) > 0 { |
| 121 | + query.Attr("mediaType", mediaType) |
| 122 | + } |
| 123 | + if len(keyword) > 0 { |
| 124 | + query.Where("(name LIKE :keyword OR description LIKE :keyword)"). |
| 125 | + Param("keyword", "%"+keyword+"%") |
| 126 | + } |
| 127 | + return query. |
| 128 | + State(MessageMediaInstanceStateEnabled). |
| 129 | + Where("mediaType IN (SELECT `type` FROM " + SharedMessageMediaDAO.Table + " WHERE state=1)"). |
| 130 | + Count() |
| 131 | +} |
| 132 | + |
| 133 | +// 列出单页接收人 |
| 134 | +func (this *MessageMediaInstanceDAO) ListAllEnabledMediaInstances(tx *dbs.Tx, mediaType string, keyword string, offset int64, size int64) (result []*MessageMediaInstance, err error) { |
| 135 | + query := this.Query(tx) |
| 136 | + if len(mediaType) > 0 { |
| 137 | + query.Attr("mediaType", mediaType) |
| 138 | + } |
| 139 | + if len(keyword) > 0 { |
| 140 | + query.Where("(name LIKE :keyword OR description LIKE :keyword)"). |
| 141 | + Param("keyword", "%"+keyword+"%") |
| 142 | + } |
| 143 | + _, err = query. |
| 144 | + State(MessageMediaInstanceStateEnabled). |
| 145 | + Where("mediaType IN (SELECT `type` FROM " + SharedMessageMediaDAO.Table + " WHERE state=1)"). |
| 146 | + DescPk(). |
| 147 | + Offset(offset). |
| 148 | + Limit(size). |
| 149 | + Slice(&result). |
| 150 | + FindAll() |
| 151 | + return |
| 152 | +} |
0 commit comments