@@ -0,0 +1,26 @@
|
||||
package sysconfser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"91porn-server/models/v/productmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
func TestShortDramaCardOptions(t *testing.T) {
|
||||
first := primitive.NewObjectID()
|
||||
second := primitive.NewObjectID()
|
||||
products := []productmod.Product{
|
||||
{ID: first, Name: "短剧月卡"},
|
||||
{ID: second, Name: "短剧季卡"},
|
||||
}
|
||||
options := shortDramaCardOptions(products)
|
||||
if len(options) != 2 {
|
||||
t.Fatalf("options length = %d, want 2", len(options))
|
||||
}
|
||||
if options[0].Key != "短剧月卡" || options[0].Value != first.Hex() ||
|
||||
options[1].Key != "短剧季卡" || options[1].Value != second.Hex() {
|
||||
t.Fatalf("options = %#v", options)
|
||||
}
|
||||
}
|
||||
Executable
+180
@@ -0,0 +1,180 @@
|
||||
package sysconfser
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"91porn-server/models/cache/sysconfdata"
|
||||
"91porn-server/models/v/productmod"
|
||||
"91porn-server/models/v/sysconfmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type WebListReq struct {
|
||||
GpCode *string `form:"gpCode" json:"gpCode"`
|
||||
}
|
||||
|
||||
var ensureSysConfInitOnce sync.Once
|
||||
|
||||
type WebListRes struct {
|
||||
Data map[string][]*sysconfmod.SysConf `json:"data"`
|
||||
}
|
||||
|
||||
// GetAll 获取所有配置
|
||||
func (q *WebListReq) GetAll() (res WebListRes, err error) {
|
||||
ensureSysConfInitOnce.Do(func() {
|
||||
sysconfmod.EnsureInitData()
|
||||
})
|
||||
|
||||
filter := bson.M{}
|
||||
if q.GpCode != nil {
|
||||
filter["gpCode"] = *q.GpCode
|
||||
}
|
||||
|
||||
sort := bson.D{{Key: "sort_order", Value: -1}}
|
||||
list, e := sysconfmod.GetAll(filter, sort)
|
||||
if e != nil {
|
||||
return res, e
|
||||
}
|
||||
if err = fillShortDramaCardOptions(list); err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Data = groupByField(list)
|
||||
return
|
||||
}
|
||||
|
||||
func fillShortDramaCardOptions(items []*sysconfmod.SysConf) error {
|
||||
var target *sysconfmod.SysConf
|
||||
for _, item := range items {
|
||||
if item != nil && item.VCode == string(sysconfmod.VCodeShortDramaCardID) {
|
||||
target = item
|
||||
break
|
||||
}
|
||||
}
|
||||
if target == nil {
|
||||
return nil
|
||||
}
|
||||
products, err := productmod.FindActiveDramaCards()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
target.SelectValues = shortDramaCardOptions(products)
|
||||
return nil
|
||||
}
|
||||
|
||||
func shortDramaCardOptions(products []productmod.Product) []sysconfmod.SelectItem {
|
||||
options := make([]sysconfmod.SelectItem, 0, len(products))
|
||||
for i := range products {
|
||||
options = append(options, sysconfmod.SelectItem{
|
||||
Key: products[i].Name, Value: products[i].ID.Hex(),
|
||||
})
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
func groupByField(items []*sysconfmod.SysConf) map[string][]*sysconfmod.SysConf {
|
||||
grouped := make(map[string][]*sysconfmod.SysConf)
|
||||
for _, item := range items {
|
||||
grouped[item.GpCode] = append(grouped[item.GpCode], item)
|
||||
}
|
||||
|
||||
return grouped
|
||||
}
|
||||
|
||||
type WebCreateReq struct {
|
||||
GroupName string `json:"groupName" form:"groupName" binding:"required"` // 分组名
|
||||
GpCode string `json:"gpCode" form:"gpCode" binding:"required"` // 分组编码
|
||||
VCode string `json:"vCode" form:"vCode" binding:"required"` // 变量名
|
||||
Title string `json:"title" form:"title" binding:"required"` // 变量标题
|
||||
Tip string `json:"tip" form:"tip"` // 变量描述
|
||||
Type string `json:"type" form:"type" binding:"required,oneof=text string img int float bool object text-array string-array select"` // 类型:text,string,img,int,float,bool,object,text-array,string-array,string-select
|
||||
Value string `json:"value" bson:"value"` // 变量值
|
||||
SelectValues []sysconfmod.SelectItem `json:"selectValues" form:"selectValues"`
|
||||
IsRequired bool `json:"is_required" form:"is_required"` // 是否必填
|
||||
SortOrder int `json:"sort_order" form:"sort_order" binding:"required"` // 排序值
|
||||
}
|
||||
|
||||
// Create 发布数据
|
||||
func (p *WebCreateReq) Create() error {
|
||||
data := sysconfmod.SysConf{
|
||||
GroupName: p.GroupName,
|
||||
GpCode: p.GpCode,
|
||||
VCode: p.VCode,
|
||||
Title: p.Title,
|
||||
Tip: p.Tip,
|
||||
Type: p.Type,
|
||||
Value: p.Value,
|
||||
IsRequired: p.IsRequired,
|
||||
SortOrder: p.SortOrder,
|
||||
SelectValues: p.SelectValues,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
// 创建数据
|
||||
if _, err := sysconfdata.InsertData(nil, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type WebUpdateReq struct {
|
||||
Configs []ConfigParams `json:"configs" binding:"required"`
|
||||
}
|
||||
type ConfigParams struct {
|
||||
ID primitive.ObjectID `json:"id" binding:"required"`
|
||||
Value string `json:"value"` // 变量值
|
||||
}
|
||||
|
||||
// Update 更新数据
|
||||
func (p *WebUpdateReq) Update() error {
|
||||
for _, pp := range p.Configs {
|
||||
_, err := sysconfmod.GetInfo(pp.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["value"] = pp.Value
|
||||
data["updatedAt"] = time.Now()
|
||||
|
||||
if _, err = sysconfdata.UpdateData(nil, pp.ID.Hex(), data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type WebDeleteReq struct {
|
||||
ID primitive.ObjectID `json:"id" binding:"required"`
|
||||
}
|
||||
|
||||
// Delete 删除数据
|
||||
func (p *WebDeleteReq) Delete() error {
|
||||
if err := sysconfdata.DeleteData(nil, p.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type WebUpdateSelectReq struct {
|
||||
ID primitive.ObjectID `json:"id" binding:"required"`
|
||||
SelectValues []sysconfmod.SelectItem `json:"selectValues" bson:"selectValues"`
|
||||
}
|
||||
|
||||
// Delete 删除数据
|
||||
func (p *WebUpdateSelectReq) Update() (err error) {
|
||||
data := make(map[string]interface{})
|
||||
data["selectValues"] = p.SelectValues
|
||||
data["updatedAt"] = time.Now()
|
||||
_, err = sysconfdata.UpdateData(nil, p.ID.Hex(), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user