134 lines
3.6 KiB
Go
134 lines
3.6 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"prompts-core/consts/public"
|
|
"prompts-core/model/entity"
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
var ProviderProtocol = &providerProtocolDao{}
|
|
|
|
type providerProtocolDao struct{}
|
|
|
|
// Insert 新增协议配置
|
|
func (d *providerProtocolDao) Insert(ctx context.Context, req *entity.ProviderProtocol) (id int64, err error) {
|
|
var m = new(entity.ProviderProtocol)
|
|
err = gconv.Struct(req, &m)
|
|
if err != nil {
|
|
return
|
|
}
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameProviderProtocol).
|
|
Insert(m)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
//// Get 查询协议配置
|
|
//func (d *providerProtocolDao) Get(ctx context.Context, req *entity.ProviderProtocol, fields ...string) (res *entity.ProviderProtocol, err error) {
|
|
// r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameProviderProtocol).
|
|
// NoTenantId(ctx).
|
|
// OmitEmpty().
|
|
// Where(entity.ProviderProtocolCol.Id, req.Id).
|
|
// Where(entity.ProviderProtocolCol.ProviderName, req.ProviderName). //主要是根据运营商查询
|
|
// Where(entity.ProviderProtocolCol.Status, 1).
|
|
// Fields(fields).One()
|
|
// if err != nil {
|
|
// return nil, err
|
|
// }
|
|
// if r.IsEmpty() {
|
|
// return nil, nil
|
|
// }
|
|
// err = r.Struct(&res)
|
|
// return
|
|
//}
|
|
|
|
// Get 获取协议配置
|
|
func (d *providerProtocolDao) Get(ctx context.Context, req *entity.ProviderProtocol, fields ...string) (*entity.ProviderProtocol, error) {
|
|
sql := fmt.Sprintf(`SELECT * FROM %s WHERE deleted_at IS NULL AND status = 1`, public.TableNameProviderProtocol)
|
|
args := make([]any, 0)
|
|
|
|
if req.Id != 0 {
|
|
sql += ` AND id = ?`
|
|
args = append(args, req.Id)
|
|
}
|
|
if req.ProviderName != "" {
|
|
sql += ` AND provider_name = ?`
|
|
args = append(args, req.ProviderName)
|
|
}
|
|
|
|
sql += ` LIMIT 1`
|
|
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).GetAll(ctx, sql, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if r.IsEmpty() {
|
|
return nil, nil
|
|
}
|
|
|
|
var list []*entity.ProviderProtocol
|
|
if err = r.Structs(&list); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(list) == 0 {
|
|
return nil, nil
|
|
}
|
|
return list[0], nil
|
|
}
|
|
|
|
// List 列表查询
|
|
func (d *providerProtocolDao) List(ctx context.Context, req *entity.ProviderProtocol, page, size int, fields ...string) (list []*entity.ProviderProtocol, total int, err error) {
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
if size <= 0 {
|
|
size = 10
|
|
}
|
|
model := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameProviderProtocol).
|
|
Fields(fields).
|
|
OmitEmpty()
|
|
model.Where(entity.ProviderProtocolCol.ProviderName, req.ProviderName)
|
|
model.Where(entity.ProviderProtocolCol.Status, req.Status)
|
|
model.OrderDesc(entity.ProviderProtocolCol.CreatedAt)
|
|
model.Page(page, size)
|
|
r, total, err := model.AllAndCount(false)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Structs(&list)
|
|
return
|
|
}
|
|
|
|
// Update 更新协议配置
|
|
func (d *providerProtocolDao) Update(ctx context.Context, req *entity.ProviderProtocol) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameProviderProtocol).
|
|
OmitEmpty().
|
|
Where(entity.ProviderProtocolCol.Id, req.Id).
|
|
Data(req).
|
|
Update()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Delete 软删除协议配置
|
|
func (d *providerProtocolDao) Delete(ctx context.Context, id int64) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameProviderProtocol).
|
|
Where(entity.ProviderProtocolCol.Id, id).
|
|
Data(map[string]any{
|
|
entity.ProviderProtocolCol.DeletedAt: "NOW()",
|
|
}).
|
|
Update()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.RowsAffected()
|
|
}
|