gowebfactroy-v4生成泛型接口的通用代码

配置

factroy:
  author: leijianming@163.com
  Time2Int: "true"
  JsonCamel: "true"   
  pkgNow: gitee.com/ichub/gowebfactroy-v4/mysql/
  pkgNew: gitee.com/ichub/gowebfactroy-v4/code/mysql/db/

下载工具

go get -u gitee.com/ichub/gowebfactroy-v4
go installl gitee.com/ichub/gowebfactroy-v4/cmd/gowebfactroy-v4

命令

gowebfactroy-v4  mysql -l

Usage:
  gomenu mysql [flags]
  gomenu mysql [command]

Available Commands:
  list        mysql list
  table       mysql tablename
  tables      mysql tables

Flags:
  -h, --help   help for mysql

执行

gowebfactroy-v4  mysql table employee

测试用例

func (this *TestEmployeeDaoSuite) Test006_Query() {
    logrus.Info("start  Query ...")
    pageRequest := page.NewPageRequest(3,1)

    pageResult := this.Dao.Query(pageRequest)
    logrus.Info(pageRequest,pageResult)
    this.Equal(200, pageResult.Code)


}

测试结果

{
     "code": 200,
     "msg": "成功",
     "page_size": 3,
     "current": 1,
     "total": 643,
     "data": [
          {
               "id": 1,
               "department_id": 1,
               "name": "中山之郎",
               ...
               "work_id": "19902238",
               "created_at": "0001-01-01T00:00:00Z",
               "updated_at": "0001-01-01T00:00:00Z",
               "deleted_at": "0001-01-01T00:00:00Z"
          },
          {
               "id": 2,
               "department_id": 91,
              ...
               "work_id": "10000002",
               "created_at": "0001-01-01T00:00:00Z",
               "updated_at": "0001-01-01T00:00:00Z",
               "deleted_at": "0001-01-01T00:00:00Z"
          },
          {
               "id": 3,
               "depa 
               "email": "zhao@qq.com",
               "phone": "15698887795",
               "address": "陕西 
               "specialty": "电子工程",
               ...
               "created_at": "0001-01-01T00:00:00Z",
               "updated_at": "0001-01-01T00:00:00Z",
               "deleted_at": "0001-01-01T00:00:00Z"
          }
     ]

生成代码

var InstEmployeeDao = NewEmployeeDao()

type EmployeeDao struct {
    basedto.BaseEntitySingle `gorm:"-"`

    *generaldao.BaseDao[int32, model.Employee]
}
func NewEmployeeDao() *EmployeeDao {
    var d = &EmployeeDao{
       BaseDao: generaldao.NewBaseDao[int32, model.Employee](),
    }
    d.InitProxy(d)
    return d
}
func (this *EmployeeDao) GetDB() *gorm.DB {

    return ichubcontext.FindBeanIchubClientFactroy().GetDBMysql()

}

 

接口

type GeneralDaoIface[P int32 | int64 | string, E IBaseModel[P]] interface {
    Insert(entity *E) (P, error)
    DeleteById(pkey P) error
    Save(entity *E) (P, error)
    Update(entity *E) (P, error)
    UpdateNotNull(pkey P, maps map[string]any) (P, error)
    UpdateMap(pkey P, maps map[string]interface{}) (P, error)
    FindById(pkey P) (entity *E, found bool, err error)
    FindByIds(pks string) (*[]E, error)
    Query() *page.PageResult
    QueryModel() *pagemodel.PageResult[E]
    Count() (int, error)

    GetDB() *gorm.DB
}

接口实现

type BaseDao[P int32 | int64 | string, E generaliface.IBaseModel[P]] struct {
    *page.PageRequest
}

func NewBaseDao[P int32 | int64 | string, E generaliface.IBaseModel[P]]() *BaseDao[P, E] {
    var dao = &BaseDao[P, E]{
       PageRequest: page.FindBeanPageRequest(),
    }
    dao.FuncGetDb = dao.GetDB
    return dao
}
func (this *BaseDao[P, E]) GetDB() *gorm.DB {

    return ichubcontext.FindBeanIchubClientFactroy().GetDB()

}

func (this *BaseDao[P, E]) Insert(entity *E) (P, error) {
    err := this.GetDB().Model(entity).Create(entity).Error
    if err != nil {
       logrus.Error(err.Error())
       return (*entity).PkeyValue(), err
    }
    return (*entity).PkeyValue(), err

}

func (this *BaseDao[P, E]) DeleteById(pkey P) error {
    var entity E

    err := this.GetDB().Where(entity.PkeyName()+"=?", pkey).Delete(&entity).Error
    return err
}

func (this *BaseDao[P, E]) Save(entity *E) (P, error) {
    err := this.GetDB().Model(entity).Where((*entity).PkeyName()+"=?", (*entity).PkeyValue()).
       Save(entity).Error
    if err != nil {
       logrus.Error(err.Error())
       return (*entity).PkeyValue(), err
    }
    return (*entity).PkeyValue(), err
}

func (this *BaseDao[P, E]) Update(entity *E) (P, error) {

    err := this.GetDB().Model(entity).Where((*entity).PkeyName()+"=?", (*entity).PkeyValue()).Updates(entity).Error
    if err != nil {
       logrus.Error(err.Error())
       return (*entity).PkeyValue(), err
    }
    return (*entity).PkeyValue(), err
}

func (this *BaseDao[P, E]) UpdateNotNull(pkeyValue P, maps map[string]interface{}) (P, error) {
    var entity = new(E)
    err := this.GetDB().Model(entity).Where((*entity).PkeyName()+"=?", pkeyValue).Updates(maps).Error
    if err != nil {
       logrus.Error(err.Error())
       return (*entity).PkeyValue(), err
    }
    return pkeyValue, err
}

func (this *BaseDao[P, E]) UpdateMap(pkey P, maps map[string]interface{}) (P, error) {
    return this.UpdateNotNull(pkey, maps)
}

func (this *BaseDao[P, E]) FindById(pkey P) (*E, bool, error) {
    var entity = new(E)

    db := this.GetDB().First(entity, pkey)
    baseutils.IfProxy(entity)
    return entity, db.RecordNotFound(), db.Error

}

func (this *BaseDao[P, E]) FindByIds(pks string) (*[]E, error) {
    var entity = new(E)

    this.PageRequest.In((*entity).PkeyName(), []any{pks})
    var ret = this.QueryModel()
    var err error = nil
    if !ret.IsSuccess() {
       err = errors.New(ret.Msg)
    }
    return &ret.Data, err
}

func (this *BaseDao[P, E]) QueryModel() *pagemodel.PageResult[E] {
    var pageresult = this.Query()
    var result = pagemodel.DefaultResult[E]()

    return result.ValueOfPageResult(pageresult)
}
func (this *BaseDao[P, E]) Query() *page.PageResult {
    return this.PageRequest.Query(new(E), &[]E{})
}

func (this *BaseDao[P, E]) Count() (int, error) {

    return this.PageRequest.Count(new(E))

}

func (this *BaseDao[E, P]) formatTime(sec int64) string {
    timeLayout := "2006-01-02 15:04:05"
    datetime := time.Unix(sec, 0).Format(timeLayout)
    return datetime
}

func (this *BaseDao[P, E]) formatDate(sec int64) string {
    timeLayout := "2006-01-02"
    datetime := time.Unix(sec, 0).Format(timeLayout)
    return datetime
}

func (this *BaseDao[E, P]) localTimeFormat(ptime time.Time) string {
    timeLayout := "2006-01-02 15:04:05"
    return ptime.Format(timeLayout)

}

func (this *BaseDao[E, P]) localTimeUTCFormat(ptime time.Time) string {
    timeLayout := "2006-01-02 15:04:05"
    return ptime.UTC().Format(timeLayout)

}

func (this *BaseDao[E, P]) localDateFormat(localDateInt basemodel.LocalDateInt) string {
    timeLayout := "2006-01-02"
    datetime := localDateInt.Time.Format(timeLayout)
    return datetime
}

func (this *BaseDao[E, P]) localDatetimeFormat(localTimeInt basemodel.LocalTimeInt) string {
    timeLayout := "2006-01-02 15:04:05"
    datetime := localTimeInt.Time.Format(timeLayout)
    return datetime
}
func (this *BaseDao[E, P]) GetDBMysql() *gorm.DB {

    return ichubcontext.FindBeanIchubClientFactroy().GetDBMysql()

}
func (this *BaseDao[E, P]) GetDBMeidusa() *gorm.DB {

    return ichubcontext.FindBeanIchubClientFactroy().GetDBMeidusa()

}
func (this *BaseDao[E, P]) GetDBCockdb() *gorm.DB {

    return ichubcontext.FindBeanIchubClientFactroy().GetDB()
}

  • 18
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Inception-v4 是一种用于图像分类的卷积神经网络模型,它是 Google 在 2016 年提出的一种改进版本,相较于 Inception-v3,它在网络架构上做了一些改进,可以获得更好的性能。下面是使用 TensorFlow 搭建 Inception-v4 模型的代码: ```python import tensorflow as tf def conv2d_bn(x, filters, kernel_size, strides=1, padding='same', activation='relu', use_bias=False): x = tf.keras.layers.Conv2D(filters, kernel_size, strides=strides, padding=padding, use_bias=use_bias)(x) x = tf.keras.layers.BatchNormalization()(x) if activation is not None: x = tf.keras.layers.Activation(activation)(x) return x def block_inception_a(x): branch_1x1 = conv2d_bn(x, 96, 1) branch_5x5 = conv2d_bn(x, 64, 1) branch_5x5 = conv2d_bn(branch_5x5, 96, 5) branch_3x3dbl = conv2d_bn(x, 64, 1) branch_3x3dbl = conv2d_bn(branch_3x3dbl, 96, 3) branch_3x3dbl = conv2d_bn(branch_3x3dbl, 96, 3) branch_pool = tf.keras.layers.AveragePooling2D(3, strides=1, padding='same')(x) branch_pool = conv2d_bn(branch_pool, 96, 1) x = tf.keras.layers.concatenate([branch_1x1, branch_5x5, branch_3x3dbl, branch_pool], axis=3) return x def block_reduction_a(x): branch_3x3 = conv2d_bn(x, 384, 3, strides=2, padding='valid') branch_3x3dbl = conv2d_bn(x, 192, 1) branch_3x3dbl = conv2d_bn(branch_3x3dbl, 224, 3) branch_3x3dbl = conv2d_bn(branch_3x3dbl, 256, 3, strides=2, padding='valid') branch_pool = tf.keras.layers.MaxPooling2D(3, strides=2, padding='valid')(x) x = tf.keras.layers.concatenate([branch_3x3, branch_3x3dbl, branch_pool], axis=3) return x def block_inception_b(x): branch_1x1 = conv2d_bn(x, 384, 1) branch_7x7 = conv2d_bn(x, 192, 1) branch_7x7 = conv2d_bn(branch_7x7, 224, [1, 7]) branch_7x7 = conv2d_bn(branch_7x7, 256, [7, 1]) branch_7x7dbl = conv2d_bn(x, 192, 1) branch_7x7dbl = conv2d_bn(branch_7x7dbl, 192, [7, 1]) branch_7x7dbl = conv2d_bn(branch_7x7dbl, 224, [1, 7]) branch_7x7dbl = conv2d_bn(branch_7x7dbl, 224, [7, 1]) branch_7x7dbl = conv2d_bn(branch_7x7dbl, 256, [1, 7]) branch_pool = tf.keras.layers.AveragePooling2D(3, strides=1, padding='same')(x) branch_pool = conv2d_bn(branch_pool, 128, 1) x = tf.keras.layers.concatenate([branch_1x1, branch_7x7, branch_7x7dbl, branch_pool], axis=3) return x def block_reduction_b(x): branch_3x3 = conv2d_bn(x, 192, 1) branch_3x3 = conv2d_bn(branch_3x3, 192, 3, strides=2, padding='valid') branch_7x7x3 = conv2d_bn(x, 256, 1) branch_7x7x3 = conv2d_bn(branch_7x7x3, 256, [1, 7]) branch_7x7x3 = conv2d_bn(branch_7x7x3, 320, [7, 1]) branch_7x7x3 = conv2d_bn(branch_7x7x3, 320, 3, strides=2, padding='valid') branch_pool = tf.keras.layers.MaxPooling2D(3, strides=2, padding='valid')(x) x = tf.keras.layers.concatenate([branch_3x3, branch_7x7x3, branch_pool], axis=3) return x def inception_v4(input_shape=(299, 299, 3), num_classes=1001): inputs = tf.keras.layers.Input(shape=input_shape) x = conv2d_bn(inputs, 32, 3, strides=2, padding='valid') x = conv2d_bn(x, 32, 3, padding='valid') x = conv2d_bn(x, 64, 3) x = tf.keras.layers.MaxPooling2D(3, strides=2)(x) x = conv2d_bn(x, 80, 1) x = conv2d_bn(x, 192, 3, padding='valid') x = tf.keras.layers.MaxPooling2D(3, strides=2)(x) x = block_inception_a(x) x = block_inception_a(x) x = block_inception_a(x) x = block_reduction_a(x) x = block_inception_b(x) x = block_inception_b(x) x = block_inception_b(x) x = block_inception_b(x) x = block_reduction_b(x) x = block_inception_a(x) x = block_inception_a(x) x = tf.keras.layers.GlobalAveragePooling2D()(x) x = tf.keras.layers.Dropout(0.2)(x) outputs = tf.keras.layers.Dense(num_classes, activation='softmax')(x) model = tf.keras.models.Model(inputs, outputs) return model ``` 这个代码定义了 `conv2d_bn` 函数来创建一个卷积层和一个批归一化层,然后使用 `block_inception_a`、`block_reduction_a`、`block_inception_b` 和 `block_reduction_b` 函数来搭建 Inception-v4 的模型结构。最后,使用 `inception_v4` 函数将这些层组合起来,并返回一个 Keras 模型对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值