- basic cms structure.

This commit is contained in:
gazebo
2018-09-02 19:11:54 +08:00
parent d46bc8981a
commit 945431d566
19 changed files with 576 additions and 211 deletions

View File

@@ -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)
}