- basic cms structure.
This commit is contained in:
@@ -1,72 +1,9 @@
|
||||
package cms
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/gormdb"
|
||||
"github.com/qor/admin"
|
||||
"github.com/qor/qor"
|
||||
"github.com/qor/qor/resource"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
curAdmin *admin.Admin
|
||||
ErrHaveNotImplementedYet = errors.New("还没有实现")
|
||||
)
|
||||
|
||||
func Init() {
|
||||
gormdb.Init()
|
||||
curAdmin = admin.New(&admin.AdminConfig{
|
||||
DB: gormdb.GetDB(),
|
||||
SiteName: "京西管理系统v0.0.1",
|
||||
})
|
||||
storeRes := curAdmin.AddResource(&model.Store{})
|
||||
lngMeta := storeRes.GetMeta("Lng")
|
||||
lngMeta.Type = "float"
|
||||
lngMeta.SetSetter(func(record interface{}, metaValue *resource.MetaValue, context *qor.Context) {
|
||||
store := record.(*model.Store)
|
||||
store.Lng = int(utils.Str2Float64((metaValue.Value.([]string))[0]) * 1000000)
|
||||
globals.SugarLogger.Debugf("metaValue:%v", reflect.TypeOf(metaValue.Value))
|
||||
})
|
||||
lngMeta.SetValuer(func(record interface{}, context *qor.Context) (result interface{}) {
|
||||
store := record.(*model.Store)
|
||||
result = float64(store.Lng) / 1000000
|
||||
return result
|
||||
})
|
||||
curAdmin.AddResource(&model.StoreSub{})
|
||||
|
||||
curAdmin.AddResource(&model.Sku{})
|
||||
curAdmin.AddResource(&model.SkuName{})
|
||||
}
|
||||
|
||||
func GetAdmin() *admin.Admin {
|
||||
return curAdmin
|
||||
}
|
||||
|
||||
// func SaveMapSlice2DB(db *gorm.DB, data []map[string]interface{}, tableName string, keyMaps map[string]string) {
|
||||
// if len(data) == 0 {
|
||||
// return
|
||||
// }
|
||||
|
||||
// sql := "INSERT INTO " + tableName + "("
|
||||
// for k := range data[0] {
|
||||
// realK, ok := keyMaps[k]
|
||||
// if !ok {
|
||||
// realK = k
|
||||
// }
|
||||
// sql += realK + ","
|
||||
// }
|
||||
// sql = sql[:len(sql)-1] + ") "
|
||||
|
||||
// for _, dataRow := range data {
|
||||
// for k, v := range dataRow {
|
||||
// realK, ok := keyMaps[k]
|
||||
// if !ok {
|
||||
// realK = k
|
||||
// }
|
||||
// sql += "("
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
19
business/jxstore/cms/sku.go
Normal file
19
business/jxstore/cms/sku.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package cms
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals/gormdb"
|
||||
)
|
||||
|
||||
func GetVendorCategories(vendorID int) ([]*model.SkuVendorCategory, error) {
|
||||
db := gormdb.GetDB()
|
||||
cats := []*model.SkuVendorCategory{}
|
||||
return cats, db.Where("vendor_id = ?", vendorID).Find(&cats).Error
|
||||
}
|
||||
|
||||
func GetSkuMetaInfo() (*model.SkuMetaInfo, error) {
|
||||
return &model.SkuMetaInfo{
|
||||
Units: model.UnitNames,
|
||||
SpecUnits: model.SpecUnitNames,
|
||||
}, nil
|
||||
}
|
||||
22
business/jxstore/cms/store.go
Normal file
22
business/jxstore/cms/store.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package cms
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals/gormdb"
|
||||
)
|
||||
|
||||
func GetCities(parentCode int, vendorID int, includeDisabled bool) ([]*model.Place, error) {
|
||||
db := gormdb.GetDB()
|
||||
places := []*model.Place{}
|
||||
sql := "enabled = 1 "
|
||||
if includeDisabled {
|
||||
sql = "1 = 1 "
|
||||
}
|
||||
if vendorID >= 0 {
|
||||
if vendorID == model.VendorIDJD {
|
||||
return places, db.Where(sql + "AND jd_code <> 0 AND level >= 2").Find(&places).Error
|
||||
}
|
||||
return nil, ErrHaveNotImplementedYet
|
||||
}
|
||||
return places, db.Where(sql+"AND parent_code = ?", parentCode).Find(&places).Error
|
||||
}
|
||||
72
business/jxstore/qorcms/qorcms.go
Normal file
72
business/jxstore/qorcms/qorcms.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package qorcms
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/gormdb"
|
||||
"github.com/qor/admin"
|
||||
"github.com/qor/qor"
|
||||
"github.com/qor/qor/resource"
|
||||
)
|
||||
|
||||
var (
|
||||
curAdmin *admin.Admin
|
||||
)
|
||||
|
||||
func Init() {
|
||||
gormdb.Init()
|
||||
curAdmin = admin.New(&admin.AdminConfig{
|
||||
DB: gormdb.GetDB(),
|
||||
SiteName: "京西管理系统v0.0.1",
|
||||
})
|
||||
storeRes := curAdmin.AddResource(&model.Store{})
|
||||
lngMeta := storeRes.GetMeta("Lng")
|
||||
lngMeta.Type = "float"
|
||||
lngMeta.SetSetter(func(record interface{}, metaValue *resource.MetaValue, context *qor.Context) {
|
||||
store := record.(*model.Store)
|
||||
store.Lng = int(utils.Str2Float64((metaValue.Value.([]string))[0]) * 1000000)
|
||||
globals.SugarLogger.Debugf("metaValue:%v", reflect.TypeOf(metaValue.Value))
|
||||
})
|
||||
lngMeta.SetValuer(func(record interface{}, context *qor.Context) (result interface{}) {
|
||||
store := record.(*model.Store)
|
||||
result = float64(store.Lng) / 1000000
|
||||
return result
|
||||
})
|
||||
curAdmin.AddResource(&model.StoreSub{})
|
||||
|
||||
curAdmin.AddResource(&model.Sku{})
|
||||
curAdmin.AddResource(&model.SkuName{})
|
||||
}
|
||||
|
||||
func GetAdmin() *admin.Admin {
|
||||
return curAdmin
|
||||
}
|
||||
|
||||
// func SaveMapSlice2DB(db *gorm.DB, data []map[string]interface{}, tableName string, keyMaps map[string]string) {
|
||||
// if len(data) == 0 {
|
||||
// return
|
||||
// }
|
||||
|
||||
// sql := "INSERT INTO " + tableName + "("
|
||||
// for k := range data[0] {
|
||||
// realK, ok := keyMaps[k]
|
||||
// if !ok {
|
||||
// realK = k
|
||||
// }
|
||||
// sql += realK + ","
|
||||
// }
|
||||
// sql = sql[:len(sql)-1] + ") "
|
||||
|
||||
// for _, dataRow := range data {
|
||||
// for k, v := range dataRow {
|
||||
// realK, ok := keyMaps[k]
|
||||
// if !ok {
|
||||
// realK = k
|
||||
// }
|
||||
// sql += "("
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
@@ -1,19 +1 @@
|
||||
package skuman
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
)
|
||||
|
||||
const (
|
||||
defJdCategoryID = 20462
|
||||
)
|
||||
|
||||
type Sku struct {
|
||||
*model.Sku
|
||||
}
|
||||
|
||||
func New(sku *model.Sku) *Sku {
|
||||
return &Sku{
|
||||
Sku: sku,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,3 +17,8 @@ type GoodsOrderCountInfo struct {
|
||||
Status int `json:"status"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type SkuMetaInfo struct {
|
||||
Units []string `json:"units"`
|
||||
SpecUnits []string `json:"specUnits"`
|
||||
}
|
||||
|
||||
@@ -1,14 +1,52 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals/gormdb"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
func GetRows(db *gorm.DB, inPtr interface{}, sql string, values ...interface{}) (err error) {
|
||||
if db == nil {
|
||||
db = gormdb.GetDB()
|
||||
}
|
||||
topTypeInfo := reflect.TypeOf(inPtr)
|
||||
if topTypeInfo.Kind() != reflect.Ptr {
|
||||
panic("SelectEntities inPtr should be slice ptr (*[]Type)")
|
||||
}
|
||||
typeInfo := topTypeInfo.Elem()
|
||||
if typeInfo.Kind() != reflect.Slice {
|
||||
fmt.Printf("type:%s", typeInfo.String())
|
||||
panic("SelectEntities inPtr should be slice ptr (*[]Type)")
|
||||
}
|
||||
elmType := typeInfo.Elem()
|
||||
|
||||
valueInfo := reflect.ValueOf(inPtr)
|
||||
rows, err := db.Raw(sql, values...).Rows()
|
||||
if err == nil {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var value reflect.Value
|
||||
if elmType.Kind() == reflect.Ptr {
|
||||
value = reflect.New(elmType.Elem())
|
||||
} else {
|
||||
value = reflect.New(elmType)
|
||||
}
|
||||
db.ScanRows(rows, value.Interface())
|
||||
if elmType.Kind() != reflect.Ptr {
|
||||
value = value.Elem()
|
||||
}
|
||||
valueInfo.Elem().Set(reflect.Append(valueInfo.Elem(), value))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func GetEntity(item interface{}, db *gorm.DB) error {
|
||||
if db == nil {
|
||||
db = gormdb.GetDB()
|
||||
@@ -20,9 +58,7 @@ func GetEntity(item interface{}, db *gorm.DB) error {
|
||||
}
|
||||
|
||||
func GetSellCities(skuNameID int, vendorID int, db *gorm.DB) (cities []*model.Place, err error) {
|
||||
if db == nil {
|
||||
db = gormdb.GetDB()
|
||||
}
|
||||
cities = []*model.Place{}
|
||||
sql := `
|
||||
SELECT DISTINCT t3.*
|
||||
FROM sku_name_place_bind t1
|
||||
@@ -33,16 +69,5 @@ func GetSellCities(skuNameID int, vendorID int, db *gorm.DB) (cities []*model.Pl
|
||||
if vendorID == model.VendorIDJD {
|
||||
sql += "AND t3.jd_code <> 0\n"
|
||||
}
|
||||
rows, err := db.Raw(sql, skuNameID).Rows()
|
||||
if err == nil {
|
||||
defer rows.Close()
|
||||
places := make([]*model.Place, 0)
|
||||
for rows.Next() {
|
||||
place := new(model.Place)
|
||||
db.ScanRows(rows, place)
|
||||
places = append(places, place)
|
||||
}
|
||||
return places, nil
|
||||
}
|
||||
return nil, err
|
||||
return cities, GetRows(nil, &cities, sql, skuNameID)
|
||||
}
|
||||
|
||||
36
business/model/dao/dao_test.go
Normal file
36
business/model/dao/dao_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"git.rosy.net.cn/jx-callback/globals/beegodb"
|
||||
"git.rosy.net.cn/jx-callback/globals/gormdb"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
func init() {
|
||||
beego.InitBeegoBeforeTest("/Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf")
|
||||
// beego.BConfig.RunMode = "dev" // InitBeegoBeforeTest会将runmode设置为test
|
||||
|
||||
globals.Init()
|
||||
beegodb.Init()
|
||||
gormdb.Init()
|
||||
api.Init()
|
||||
}
|
||||
|
||||
func TestSelectEntities(t *testing.T) {
|
||||
places := []*model.Place{}
|
||||
GetRows(nil, &places, `
|
||||
SELECT DISTINCT t3.*
|
||||
FROM sku_name_place_bind t1
|
||||
JOIN place t2 ON t1.place_code = t2.code
|
||||
JOIN place t3 ON (t2.level = 2 AND t2.code = t3.code) OR (t2.level = 1 AND t2.code = t3.parent_code)
|
||||
WHERE t1.sku_name_id = ?
|
||||
`, 40)
|
||||
|
||||
globals.SugarLogger.Debug(utils.Format4Output(places, false))
|
||||
}
|
||||
@@ -3,22 +3,22 @@ package model
|
||||
import "time"
|
||||
|
||||
type ModelO struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt *time.Time `sql:"index"`
|
||||
LastOperator string `gorm:"type:varchar(32)"` // 最后操作员
|
||||
LastOperator string `gorm:"type:varchar(32)" json:"lastOperator"` // 最后操作员
|
||||
}
|
||||
|
||||
type ModelIDCU struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type ModelIDCUO struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
LastOperator string `gorm:"type:varchar(32)"` // 最后操作员
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
LastOperator string `gorm:"type:varchar(32)" json:"lastOperator"` // 最后操作员
|
||||
}
|
||||
|
||||
@@ -10,15 +10,15 @@ const (
|
||||
)
|
||||
|
||||
type Place struct {
|
||||
ID int
|
||||
Code int `gorm:"unique_index"` // 国家标准代码
|
||||
Name string `gorm:"type:varchar(16);index"` // 如果是直辖市,省的概念不加“市”来区别
|
||||
ParentCode int // 上级代码
|
||||
PostCode string `gorm:"type:varchar(8);index"`
|
||||
Level int8 // 城市级别,参见相关常量定义
|
||||
TelCode string `gorm:"type:varchar(8);index"`
|
||||
JdCode int `gorm:"index"` // 对应的京东代码
|
||||
Enabled int8 // 是否启用
|
||||
MtpsPrice int // 分为单位
|
||||
UpdatedAt time.Time
|
||||
ID int `json:"-"`
|
||||
Code int `gorm:"unique_index" json:"code"` // 国家标准代码
|
||||
Name string `gorm:"type:varchar(16);index" json:"name"` // 如果是直辖市,省的概念不加“市”来区别
|
||||
ParentCode int `json:"parentCode"` // 上级代码
|
||||
PostCode string `gorm:"type:varchar(8);index" json:"postCode"`
|
||||
Level int8 `json:"level"` // 城市级别,参见相关常量定义
|
||||
TelCode string `gorm:"type:varchar(8);index" json:"telCode"`
|
||||
JdCode int `gorm:"index" json:"jdCode"` // 对应的京东代码
|
||||
Enabled int8 `json:"enabled"` // 是否启用
|
||||
MtpsPrice int `json:"mtpsPrice"` // 分为单位
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
@@ -47,14 +47,18 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// 这个指的是京东自已的商品分类,与商家自己的商品分类是两回事
|
||||
type SkuJdCategory struct {
|
||||
// 这个指的是厂商(比如京东到家,饿百)自已的商品分类,与商家自己的商品分类是两回事
|
||||
type SkuVendorCategory struct {
|
||||
ModelIDCUO
|
||||
Name string `gorm:"type:varchar(255);index"`
|
||||
Level int
|
||||
ParentID int
|
||||
VendorCategoryID string `gorm:"type:varchar(48);unique_index:unique_index_id_vendor_id" json:"vendorCategoryID"`
|
||||
VendorID int `gorm:"unique_index:unique_index_id_vendor_id" json:"vendorID"`
|
||||
Name string `gorm:"type:varchar(255);index" json:"name"`
|
||||
IsLeaf int8 `json:"isLeaf"`
|
||||
Level int `json:"level"`
|
||||
ParentID string `gorm:"type:varchar(255);index" json:"parentID"` // 父ID,引用的是VendorCategoryID而不是ID
|
||||
}
|
||||
|
||||
// 基础数据,除了商家商品类别外,基本都以京东到家为准
|
||||
type SkuCategory struct {
|
||||
ModelIDCUO
|
||||
Name string `gorm:"type:varchar(255);unique_index"`
|
||||
@@ -63,19 +67,17 @@ type SkuCategory struct {
|
||||
Type int8 // 类别类型
|
||||
Seq int
|
||||
|
||||
JdID int64 `gorm:"index"` // 这个是指商家自己的商品类别在京东平台上的ID
|
||||
JdCategoryID int // 这个是指对应的京东商品类别
|
||||
ElmID int64 `gorm:"index"`
|
||||
MtID string `gorm:"type:varchar(48);index"`
|
||||
DidiID string `gorm:"type:varchar(48);index"`
|
||||
}
|
||||
JdID int64 `gorm:"index"` // 这个是指商家自己的商品类别在京东平台上的ID
|
||||
JdCategoryID int // 这个是指对应的京东商品类别
|
||||
|
||||
type StoreSkuCategoryMap struct {
|
||||
ModelIDCUO
|
||||
StoreID int
|
||||
SkuCategoryID int
|
||||
// ElmID int64 `gorm:"index"` // 饿了么是单店模式,不需要
|
||||
ElmCategoryID int64 // 这个是指对应的饿了么商品类别
|
||||
|
||||
ElmID string `gorm:"type:varchar(48)"`
|
||||
// EbaiID int64 `gorm:"index"` // 饿百是单店模式,不需要
|
||||
EbaiCategoryID int64 // 这个是指对应的饿百商品类别
|
||||
|
||||
// MtID string `gorm:"type:varchar(48);index"`
|
||||
// DidiID string `gorm:"type:varchar(48);index"`
|
||||
}
|
||||
|
||||
type SkuName struct {
|
||||
@@ -84,7 +86,7 @@ type SkuName struct {
|
||||
Name string `gorm:"type:varchar(255)"`
|
||||
Comment string `gorm:"type:varchar(255)"`
|
||||
|
||||
BrandID int
|
||||
BrandID int `gorm:"default:0"` // 此属性暂时没有使用
|
||||
CategoryID int // 标准类别
|
||||
Status int
|
||||
|
||||
@@ -112,6 +114,16 @@ type SkuNamePlaceBind struct {
|
||||
PlaceCode int `gorm:"unique_index:unique_sku_name_id_sku_place_id"`
|
||||
}
|
||||
|
||||
// 以下为门店相关数据
|
||||
type StoreSkuCategoryMap struct {
|
||||
ModelIDCUO
|
||||
StoreID int
|
||||
SkuCategoryID int
|
||||
|
||||
ElmID int64 `gorm:"index"`
|
||||
EbaiID int64 `gorm:"index"`
|
||||
}
|
||||
|
||||
type StoreSkuBind struct {
|
||||
ModelIDCUO
|
||||
StoreID int `gorm:"unique_index:unique_store_id_sku_id"`
|
||||
@@ -120,5 +132,6 @@ type StoreSkuBind struct {
|
||||
Price int // 单位为分,不用int64的原因是这里不需要累加
|
||||
Status int
|
||||
|
||||
ElmID int64
|
||||
ElmID int64 `gorm:"index"`
|
||||
EbaiID int64 `gorm:"index"`
|
||||
}
|
||||
Reference in New Issue
Block a user