- Store and StoreMap add DeletedAt.
This commit is contained in:
@@ -3,11 +3,8 @@ package dao
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"github.com/astaxie/beego/orm"
|
||||
)
|
||||
|
||||
@@ -54,7 +51,6 @@ type DaoDB struct {
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
|
||||
// beego orm的对于传代表字段的字串,数据库字段名(完全匹配,区分大小写),结构体字段名(不区分大小写)都可以
|
||||
|
||||
func GetDB() *DaoDB {
|
||||
@@ -115,31 +111,6 @@ func GetEntity(db *DaoDB, item interface{}, cols ...string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func GetEntities(db *DaoDB, item interface{}, conditions map[string]interface{}, isIncludeDeleted bool) (err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
}
|
||||
err = utils.CallFuncLogError(func() error {
|
||||
typeInfo := reflect.TypeOf(item).Elem().Elem()
|
||||
if typeInfo.Kind() == reflect.Ptr {
|
||||
typeInfo = typeInfo.Elem()
|
||||
}
|
||||
qs := db.db.QueryTable(reflect.New(typeInfo).Interface())
|
||||
for k, v := range conditions {
|
||||
qs = qs.Filter(k, v)
|
||||
}
|
||||
qs = qs.Limit(-1)
|
||||
if !isIncludeDeleted {
|
||||
if _, ok := typeInfo.FieldByName(model.FieldDeletedAt); ok {
|
||||
qs = qs.Filter(model.FieldDeletedAt, utils.DefaultTimeValue)
|
||||
}
|
||||
}
|
||||
_, err = qs.All(item)
|
||||
return err
|
||||
}, reflect.TypeOf(item).Name())
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateEntity(db *DaoDB, item interface{}, cols ...string) (num int64, err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
@@ -151,25 +122,6 @@ func UpdateEntity(db *DaoDB, item interface{}, cols ...string) (num int64, err e
|
||||
return num, err
|
||||
}
|
||||
|
||||
func UpdateEntityByKV(db *DaoDB, item interface{}, kvs map[string]interface{}, conditions map[string]interface{}) (num int64, err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
}
|
||||
err = utils.CallFuncLogError(func() error {
|
||||
qs := db.db.QueryTable(item)
|
||||
if conditions == nil {
|
||||
qs = qs.Filter(model.FieldID, jxutils.GetObjFieldByName(item, model.FieldID))
|
||||
} else {
|
||||
for k, v := range conditions {
|
||||
qs = qs.Filter(k, v)
|
||||
}
|
||||
}
|
||||
num, err = qs.Update(kvs)
|
||||
return err
|
||||
}, reflect.TypeOf(item).Name())
|
||||
return num, err
|
||||
}
|
||||
|
||||
func CreateEntity(db *DaoDB, item interface{}) (err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
@@ -192,31 +144,6 @@ func DeleteEntity(db *DaoDB, item interface{}, cols ...string) (num int64, err e
|
||||
return num, err
|
||||
}
|
||||
|
||||
func DeleteEntityLogically(db *DaoDB, item interface{}, conditions map[string]interface{}, logicDeletedBy string, kvs map[string]interface{}) (num int64, err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
}
|
||||
err = utils.CallFuncLogError(func() error {
|
||||
qs := db.db.QueryTable(item)
|
||||
if len(conditions) == 0 {
|
||||
qs = qs.Filter(model.FieldID, jxutils.GetObjFieldByName(item, model.FieldID))
|
||||
} else {
|
||||
for k, v := range conditions {
|
||||
qs = qs.Filter(k, v)
|
||||
}
|
||||
}
|
||||
qs = qs.Filter(model.FieldDeletedAt, utils.DefaultTimeValue)
|
||||
updateValues := map[string]interface{}{
|
||||
model.FieldDeletedAt: time.Now(),
|
||||
model.FieldUpdatedAt: time.Now(),
|
||||
model.FieldLastOperator: logicDeletedBy,
|
||||
}
|
||||
num, err = qs.Update(utils.MergeMaps(updateValues, kvs))
|
||||
return err
|
||||
}, reflect.TypeOf(item).Name())
|
||||
return num, err
|
||||
}
|
||||
|
||||
func ExecuteSQL(db *DaoDB, sql string, params ...interface{}) (num int64, err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
|
||||
71
business/model/dao/dao_bz.go
Normal file
71
business/model/dao/dao_bz.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
)
|
||||
|
||||
// 这里面的函数要求实体是IDCUDL的,即含有ID, UpdatedAt, LastOperator, DeletedAt字段
|
||||
|
||||
func GetEntitiesByKV(db *DaoDB, item interface{}, conditions map[string]interface{}, isIncludeDeleted bool) (err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
}
|
||||
err = utils.CallFuncLogError(func() error {
|
||||
typeInfo := reflect.TypeOf(item).Elem().Elem()
|
||||
if typeInfo.Kind() == reflect.Ptr {
|
||||
typeInfo = typeInfo.Elem()
|
||||
}
|
||||
qs := db.db.QueryTable(reflect.New(typeInfo).Interface())
|
||||
for k, v := range conditions {
|
||||
qs = qs.Filter(k, v)
|
||||
}
|
||||
qs = qs.Limit(-1)
|
||||
if !isIncludeDeleted {
|
||||
if _, ok := typeInfo.FieldByName(model.FieldDeletedAt); ok {
|
||||
qs = qs.Filter(model.FieldDeletedAt, utils.DefaultTimeValue)
|
||||
}
|
||||
}
|
||||
_, err = qs.All(item)
|
||||
return err
|
||||
}, reflect.TypeOf(item).Name())
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateEntityByKV(db *DaoDB, item interface{}, kvs map[string]interface{}, conditions map[string]interface{}) (num int64, err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
}
|
||||
err = utils.CallFuncLogError(func() error {
|
||||
qs := db.db.QueryTable(item)
|
||||
if conditions == nil {
|
||||
qs = qs.Filter(model.FieldID, jxutils.GetObjFieldByName(item, model.FieldID))
|
||||
} else {
|
||||
for k, v := range conditions {
|
||||
qs = qs.Filter(k, v)
|
||||
}
|
||||
}
|
||||
num, err = qs.Update(kvs)
|
||||
return err
|
||||
}, reflect.TypeOf(item).Name())
|
||||
return num, err
|
||||
}
|
||||
|
||||
func UpdateEntityLogically(db *DaoDB, item interface{}, kvs map[string]interface{}, userName string, conditions map[string]interface{}) (num int64, err error) {
|
||||
return UpdateEntityByKV(db, item, utils.MergeMaps(kvs, map[string]interface{}{
|
||||
model.FieldUpdatedAt: time.Now(),
|
||||
model.FieldLastOperator: userName,
|
||||
}), utils.MergeMaps(conditions, map[string]interface{}{
|
||||
model.FieldDeletedAt: utils.DefaultTimeValue,
|
||||
}))
|
||||
}
|
||||
|
||||
func DeleteEntityLogically(db *DaoDB, item interface{}, kvs map[string]interface{}, userName string, conditions map[string]interface{}) (num int64, err error) {
|
||||
return UpdateEntityLogically(db, item, utils.MergeMaps(kvs, map[string]interface{}{
|
||||
model.FieldDeletedAt: time.Now(),
|
||||
}), userName, conditions)
|
||||
}
|
||||
@@ -44,17 +44,26 @@ func TestGetPlaceByName(t *testing.T) {
|
||||
func TestUpdateKV(t *testing.T) {
|
||||
dummy := &model.Store{}
|
||||
kvs := map[string]interface{}{
|
||||
"status": 100,
|
||||
"Tel1": "tel1",
|
||||
"tEl2": "tel2",
|
||||
"deliverY_Range_type": 15,
|
||||
"status": 100,
|
||||
"Tel1": "tel1",
|
||||
"tEl2": "tel2",
|
||||
"deliveryRangeType": 15,
|
||||
}
|
||||
cond := map[string]interface{}{
|
||||
"id": 100002,
|
||||
}
|
||||
num, err := UpdateEntityByKV(nil, dummy, kvs, cond)
|
||||
num, err := UpdateEntityLogically(nil, dummy, kvs, "autotest", cond)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(num)
|
||||
}
|
||||
|
||||
func TestWrapAddIDCULEntity(t *testing.T) {
|
||||
dummy := &model.Store{}
|
||||
WrapAddIDCULEntity(dummy, "autotest")
|
||||
if dummy.LastOperator != "autotest" {
|
||||
t.Fatal("last operator is not same")
|
||||
}
|
||||
t.Log(dummy)
|
||||
}
|
||||
|
||||
@@ -16,17 +16,17 @@ func NormalFilterMapByStructObject(mapData map[string]interface{}, obj interface
|
||||
|
||||
func NormalMakeMapByStructObject(mapData map[string]interface{}, obj interface{}, userName string) (retVal map[string]interface{}) {
|
||||
retVal, _ = NormalFilterMapByStructObject(mapData, obj)
|
||||
if len(retVal) > 0 {
|
||||
WrapUpdateULEntity(retVal, userName)
|
||||
}
|
||||
// if len(retVal) > 0 {
|
||||
// WrapUpdateULEntity(retVal, userName)
|
||||
// }
|
||||
return retVal
|
||||
}
|
||||
|
||||
func NormalMakeMapByFieldList(mapData map[string]interface{}, fields []string, userName string) (retVal map[string]interface{}) {
|
||||
retVal, _ = jxutils.FilterMapByFieldList(mapData, fields)
|
||||
if len(retVal) > 0 {
|
||||
WrapUpdateULEntity(retVal, userName)
|
||||
}
|
||||
// if len(retVal) > 0 {
|
||||
// WrapUpdateULEntity(retVal, userName)
|
||||
// }
|
||||
return retVal
|
||||
}
|
||||
|
||||
|
||||
@@ -112,9 +112,9 @@ func init() {
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
ModelIDCUL
|
||||
ModelIDCULD
|
||||
|
||||
Name string `orm:"size(255);unique" json:"name"`
|
||||
Name string `orm:"size(255)" json:"name"`
|
||||
CityCode int `orm:"default(0);null" json:"cityCode"` // todo ?
|
||||
DistrictCode int `orm:"default(0);null" json:"districtCode"` // todo ?
|
||||
Address string `orm:"size(255)" json:"address"`
|
||||
@@ -133,8 +133,14 @@ type Store struct {
|
||||
DeliveryType int8 `orm:"-" json:"-"`
|
||||
}
|
||||
|
||||
func (*Store) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
[]string{"Name", "DeletedAt"},
|
||||
}
|
||||
}
|
||||
|
||||
type StoreSub struct {
|
||||
ModelIDCUL
|
||||
ModelIDCULD
|
||||
|
||||
StoreID int `orm:"column(store_id)"`
|
||||
Index int // 子店序号,为0表示主店
|
||||
@@ -148,12 +154,12 @@ type StoreSub struct {
|
||||
|
||||
func (*StoreSub) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
[]string{"StoreID", "Index"},
|
||||
[]string{"StoreID", "Index", "DeletedAt"},
|
||||
}
|
||||
}
|
||||
|
||||
type StoreMap struct {
|
||||
ModelIDCUL
|
||||
ModelIDCULD
|
||||
|
||||
StoreID int `orm:"column(store_id)" json:"storeID"`
|
||||
VendorID int `orm:"column(vendor_id)" json:"vendorID"`
|
||||
@@ -170,13 +176,13 @@ type StoreMap struct {
|
||||
|
||||
func (*StoreMap) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
[]string{"StoreID", "VendorID"},
|
||||
[]string{"VendorStoreID", "VendorID"},
|
||||
[]string{"StoreID", "VendorID", "DeletedAt"},
|
||||
[]string{"VendorStoreID", "VendorID", "DeletedAt"},
|
||||
}
|
||||
}
|
||||
|
||||
type StoreCourierMap struct {
|
||||
ModelIDCUL
|
||||
ModelIDCULD
|
||||
|
||||
StoreID int `orm:"column(store_id)"`
|
||||
VendorID int `orm:"column(vendor_id)"`
|
||||
@@ -186,7 +192,7 @@ type StoreCourierMap struct {
|
||||
|
||||
func (*StoreCourierMap) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
[]string{"StoreID", "VendorID"},
|
||||
[]string{"VendorStoreID", "VendorID"},
|
||||
[]string{"StoreID", "VendorID", "DeletedAt"},
|
||||
[]string{"VendorStoreID", "VendorID", "DeletedAt"},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user