接口
type IpagedbRequestModel[T any] interface { Insert(entity *T) error Update(entity *T) error Insert2Result(entity *T) *pagemodel.IchubResult[T] Update2Result(entity *T) *pagemodel.IchubResult[T] DeleteById(id int64, keys ...string) *pagemodel.IchubResult[T] DeleteByIdOf(id int64, model *T, keys ...string) *pagemodel.IchubResult[T] FindById(id int64, keys ...string) *pagemodel.IchubResult[T] FindByIds(ids string, key ...string) *pagemodel.PageResult[T] GeneralQuery() *pagemodel.PageResult[T] } 实现
package pagedb import ( "git.ichub.com/general/webcli120/goconfig/base/basedto" "git.ichub.com/general/webcli120/goconfig/base/goutils" "git.ichub.com/general/webcli120/goconfig/base/jsonutils" "git.ichub.com/general/webcli120/goweb/pagemodel" "github.com/gogf/gf/v2/util/gconv" "github.com/sirupsen/logrus" "strings" ) type PagedbRequestModel[T any] struct { basedto.BaseEntity *PageDbRequest } func DefaultModel[T any]() *PagedbRequestModel[T] { return NewPagedbRequestModel[T]() } func NewPagedbRequestModel[T any]() *PagedbRequestModel[T] { var model = &PagedbRequestModel[T]{ PageDbRequest: Default(), } model.InitProxy(model) return model } func (self *PagedbRequestModel[T]) GeneralQuery() *pagemodel.PageResult[T] { var resultModel = pagemodel.DefaultResult[T]() var result = self.PageDbRequest.GeneralQuery() if result.IsSuccess() && result.Data != nil { var models = make([]T, 0) var bytes, err = jsonutils.ToJsonBytes(result.Data) if err != nil { goutils.Error(err) return resultModel } jsonutils.FromJsonByte(bytes, &models) result.Data = models resultModel.Data = models } return resultModel } func (self *PagedbRequestModel[T]) FindById(id int64, keys ...string) *pagemodel.IchubResult[T] { self.PageDbRequest.Eq("id", id) var result = self.GeneralQuery() var res = pagemodel.NewIchubResult[T]() res.Code = result.Code res.Msg = result.Msg if res.IsSuccess() && result.Data != nil && len(result.Data) > 0 { res.Data = result.Data[0] res.Total = 1 } return res } func (self *PagedbRequestModel[T]) DeleteById(id int64, keys ...string) *pagemodel.IchubResult[T] { var db = self.GetDB() if self.TableName != "" { db = db.Table(self.TableName) } //db.Delete("id",id) var entity map[string]any = make(map[string]any, 0) err := db.Where("id=?", id).Delete(&entity).Error var res = pagemodel.NewIchubResult[T]() if err != nil { logrus.Error(err.Error()) res.FailMsg(err.Error()) } return res } func (self *PagedbRequestModel[T]) DeleteByIdOf(id int64, model *T, keys ...string) *pagemodel.IchubResult[T] { var db = self.GetDB() if self.TableName != "" { db = db.Table(self.TableName) } //db.Delete("id",id) var entity map[string]any = make(map[string]any, 0) err := db.Where("id=?", id).Delete(&entity).Error var res = pagemodel.NewIchubResult[T]() if err != nil { logrus.Error(err.Error()) res.FailMsg(err.Error()) } return res } func (self *PagedbRequestModel[T]) FindByIds(ids string, key ...string) *pagemodel.PageResult[T] { var idsint = gconv.SliceInt64(strings.Split(ids, ",")) var params = gconv.SliceAny(idsint) self.PageDbRequest.In("id", params) var resultModel = pagemodel.DefaultResult[T]() var result = self.PageDbRequest.GeneralQuery() if result.IsSuccess() && result.Data != nil { var models = make([]T, 0) var bytes, err = jsonutils.ToJsonBytes(result.Data) if err != nil { goutils.Error(err) return resultModel } jsonutils.FromJsonByte(bytes, &models) result.Data = models resultModel.Data = models } return resultModel } func (self *PagedbRequestModel[T]) Insert(entity *T) error { var db = self.GetDB() if self.TableName != "" { db = db.Table(self.TableName) } err := db.Create(entity).Error if err != nil { logrus.Println(err.Error()) return err } logrus.Info(entity) return nil } func (self *PagedbRequestModel[T]) Update(entity *T) error { var db = self.GetDB() if self.TableName != "" { db = db.Table(self.TableName) } err := db.Save(entity).Error if err != nil { logrus.Println(err.Error()) return err } return nil } func (self *PagedbRequestModel[T]) Insert2Result(entity *T) *pagemodel.IchubResult[T] { var err = self.Insert(entity) var result = pagemodel.DefaultIchubResult[T]() result.Data = *entity if err != nil { result.FailMsg(err.Error()) } return result } func (self *PagedbRequestModel[T]) Update2Result(entity *T) *pagemodel.IchubResult[T] { var err = self.Update(entity) var result = pagemodel.DefaultIchubResult[T]() result.Data = *entity if err != nil { result.FailMsg(err.Error()) } return result } 测试用例
package dao /* @Title 文件名称: TestShopDaoSuite.go @Description 描述: 测试服务TestEmployeeDaoSuite @Author 作者: [email protected] 时间: 2024-06-29 08:24:24 @Update 作者: [email protected] 时间: 2024-06-29 08:24:24 */ import ( "git.ichub.com/general/webcli120/goconfig/base/basedao" "git.ichub.com/general/webcli120/goconfig/base/jsonutils" "git.ichub.com/general/webcli120/goweb/pagedb" "git.ichub.com/general/webcli120/test/db/dao" "git.ichub.com/general/webcli120/test/db/model" "github.com/sirupsen/logrus" "github.com/stretchr/testify/suite" "testing" //"time" ) type TestShopDaoSuite struct { suite.Suite basedao.BaseDao Dao dao.EmployeeDAO } func TestShopDaoSuites(t *testing.T) { suite.Run(t, new(TestShopDaoSuite)) } /* @title 函数名称: Setup @description : 测试套前置脚本 @auth 作者: [email protected] @date 时间: 2024-06-29 08:24:24 @param 输入参数名: 无 @return 返回参数名: 无 */ func (this *TestShopDaoSuite) SuiteSetupTest() { logrus.Info("setUp all tests") // mysql postgres cockroach } /* @title : 测试套后置脚本 @auth 作者: [email protected] @description 函数名称: Teardown @date 时间: 2024-06-29 08:24:24 @param 输入参数名: 无 @return 返回参数名: 无 */ func (this *TestShopDaoSuite) SuiteTearDownTest() { logrus.Info("tearDown all tests") } /* @title 函数名称: Save @description : 保存接口 @auth 作者 : [email protected] @date 时间 : 2024-06-29 08:24:24 @param 输入参数名: t *testing.T @return 返回参数名: 无 */ func (this *TestShopDaoSuite) Save() int32 { var entity model.Employee this.Dao.Save(&entity) return entity.Id } /* @title 函数名称: SaveEntity @description : 保存接口 @auth 作者 : [email protected] @date 时间 : 2024-06-29 08:24:24 @param 输入参数名: 无 @return 返回参数名: 无 */ func (this *TestShopDaoSuite) Test010_QueryShop() { logrus.Info("start Query ...") var req = pagedb.Default() req.TableName = "contact_shop" req.TimeToInt = true var result = req.GeneralQuery() //&TableModel{}, []TableModel{}) logrus.Info(req, result) this.Equal(200, result.Code) } type GeneralEntity struct { Id int `json:"id" gorm:"primary_key"` Name string `json:"name"` } func (this *TestShopDaoSuite) Test011_QueryShopUser() { logrus.Info("start Query ...") var req = pagedb.Default() req.TableName = "contact_user" req.TimeToInt = true var result = req.GeneralQuery() //&TableModel{}, []TableModel{}) logrus.Info(req, result) this.Equal(200, result.Code) } func (this *TestShopDaoSuite) Test012_QueryAsShopUser() { logrus.Info("start Query User as...") var req = pagedb.Default() req.PageSize = 2 req.TableName = "contact_user" req.TimeToInt = true var result = req.GeneralQuery() logrus.Info(req, result) this.Equal(200, result.Code) var res = pagedb.GeneralQueryAs[GeneralEntity](result) logrus.Info(res) } func (this *TestShopDaoSuite) Test013_QueryAsModel() { logrus.Info("start Query User as...") var req = pagedb.DefaultModel[GeneralEntity]() req.PageSize = 2 req.TableName = "contact_user" req.TimeToInt = true var result = req.GeneralQuery() logrus.Info(req, result) this.Equal(200, result.Code) } func (this *TestShopDaoSuite) Test014_QueryModelShopMember() { logrus.Info("start Query User as...") var defaultModel = pagedb.DefaultModel[GeneralEntity]() defaultModel.PageSize = 2 defaultModel.TableName = "contact_shop_member" defaultModel.TimeToInt = true var result = defaultModel.GeneralQuery() logrus.Info(defaultModel) this.Equal(200, result.Code) logrus.Info(result) } func (this *TestShopDaoSuite) Test015_FindByIdShopMember() { logrus.Info("start Query User as...") var defaultModel = pagedb.DefaultModel[GeneralEntity]() defaultModel.PageSize = 2 defaultModel.TableName = "contact_shop_member" defaultModel.TimeToInt = true //722622553218875393 var result = defaultModel.FindById(722622553218875393) logrus.Info(defaultModel) this.Equal(200, result.Code) logrus.Info(result) } func (this *TestShopDaoSuite) Test016_FindByIdsShopMember() { logrus.Info("start Query User as...") var defaultModel = pagedb.DefaultModel[GeneralEntity]() defaultModel.PageSize = 2 defaultModel.TableName = "contact_shop_member" defaultModel.TimeToInt = true //722622553218875393 var result = defaultModel.FindByIds("722612810728800257,722622553218875393") logrus.Info(defaultModel) this.Equal(200, result.Code) logrus.Info(result) } func (this *TestShopDaoSuite) Test017_InsertSave() { logrus.Info("start Query User as...") var defaultModel = pagedb.DefaultModel[GeneralEntity]() defaultModel.TableName = "contact_shop_member" var err = defaultModel.Insert(&GeneralEntity{Name: "leijmdas"}) logrus.Info(err) } func (this *TestShopDaoSuite) Test017_InsertSave2Result() { logrus.Info("start Query User as...") var defaultModel = pagedb.DefaultModel[GeneralEntity]() defaultModel.TableName = "contact_shop_member" var result = defaultModel.Insert2Result(&GeneralEntity{Name: "leijmdas"}) logrus.Info(jsonutils.ToJsonPretty(result)) } func (this *TestShopDaoSuite) Test016_DeleteByIdShopMember() { logrus.Info("start Query User as...") var defaultModel = pagedb.DefaultModel[GeneralEntity]() defaultModel.PageSize = 2 defaultModel.TableName = "contact_shop_member" defaultModel.TimeToInt = true var result = defaultModel.DeleteById(992959707715141635) logrus.Info(defaultModel) this.Equal(200, result.Code) logrus.Info(jsonutils.ToJsonPretty(result)) } 测试结果
func (this *TestShopDaoSuite) Test013_QueryAsModel() { logrus.Info("start Query User as...") var req = pagedb.DefaultModel[GeneralEntity]() req.PageSize = 2 req.TableName = "contact_user" req.TimeToInt = true var result = req.GeneralQuery() logrus.Info(req, result) this.Equal(200, result.Code) }
INFO[2024-08-88 14:23:12]D:/go-ichub/git.ichub.com/webcli120/test/db/test/dao/shop_dao_test.go:140 git.ichub.com/general/webcli120/test/db/test/dao.(*TestShopDaoSuite).Test013_QueryAsModel() {
"page_size": 2,
"current": 1,
"order_by": null,
"fields": null,
"es_bool_type": 0,
"table_name": "contact_user",
"fields_name": "id,created_at,updated_at,deleted_at,contract_id,identities_id,name,avatar,registered_user_name,registered_cellphone,registered_email,language_id,time_zone_id,location_id,about,description,is_inactive,natural_person_id,legal_person_id,invitation_id,is_partner,status,created_by,updated_by,deleted_by,latest_shop_id,slug,short_name,partnered_user_id,last_login_at,rowid,id_10,partner_id_10,version_10,latest_account_id,shop_count,station,sound_switch,wechat_nickname,wx_nickname",
"time_to_int": true
} {
"code": 200,
"msg": "成功",
"data": [
{
"id": 722612810136944641,
"name": "万强"
},
{
"id": 722622552618205185,
"name": "杨旭"
}
]
}