123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package model
- import (
- "github.com/pkg/errors"
- "kpt.xdmy/apiserver/dao"
- "kpt.xdmy/pkg/log"
- )
- var d *dao.Dao
- var m *Model
- type Model struct {
- d *dao.Dao
- }
- func ModelInit(dao *dao.Dao) {
- d = dao
- }
- // type Model struct {
- // ID int `json:"id" gorm:"column:id" `
- // }
- type Request interface {
- Validate() bool
- }
- func First(pi interface{}) error {
- e := d.DB.Where(pi).First(pi).Error
- return errors.Wrapf(e, "model First ")
- }
- func Insert(pi interface{}) error {
- e := d.DB.Create(pi).Error
- return errors.Wrapf(e, "model Insert")
- }
- func Find(c map[string]interface{}, pi interface{}) error {
- e := d.DB.Where(c).Find(pi).Error
- log.ErrorJ(e, "model Find ", c)
- return e
- }
- func Update(c interface{}, pi interface{}) error {
- e := d.DB.Where(c).Updates(pi).Error
- log.Errorf(e, "model Update %v, %v", c, pi)
- return errors.Wrapf(e, "model Update ")
- }
- func UpdateAll(c map[string]interface{}, pi interface{}) error {
- e := d.DB.Where(c).Select("*").Updates(pi).Error
- log.ErrorJ(e, "model UpdateAll", c, pi)
- return e
- }
- func Raw(sql string, args map[string]interface{}, r interface{}) error {
- e := d.DB.Raw(sql, args).Scan(r).Error
- return errors.Wrapf(e, "model Raw %s, %v", sql, args)
- }
|