- basic cms structure.
This commit is contained in:
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user