service/vendor/codecreate/template/service.txt

74 lines
2.0 KiB
Plaintext

package logic
import (
"service/api/consts"
"github.com/gin-gonic/gin"
#{moduleName}proto "service/api/proto/#{moduleName}/proto"
goproto "google.golang.org/protobuf/proto"
"service/app/mix/dao"
"service/dbstruct"
"service/library/idgenerator"
"service/library/logger"
"time"
)
type #{EntityName} struct {
store *dao.Store
}
func New#{EntityName}(store *dao.Store) (a *#{EntityName}) {
a = &#{EntityName}{
store: store,
}
return
}
func (p *#{EntityName}) OpCreate(ctx *gin.Context, req *#{moduleName}proto.OpCreateReq) error {
req.#{EntityName}.Id = goproto.Int64(idgenerator.Gen#{EntityName}Id())
req.#{EntityName}.Ct = goproto.Int64(time.Now().Unix())
req.#{EntityName}.Ut = goproto.Int64(time.Now().Unix())
req.#{EntityName}.DelFlag = goproto.Int64(consts.Exist)
err := p.store.Create#{EntityName}(ctx, req.#{EntityName})
if err != nil {
logger.Error("Create#{EntityName} fail, err: %v", err)
return err
}
return nil
}
func (p *#{EntityName}) OpUpdate(ctx *gin.Context, req *#{moduleName}proto.OpUpdateReq) error {
err := p.store.Update#{EntityName}(ctx, req.#{EntityName})
if err != nil {
logger.Error("Update#{EntityName} fail, err: %v", err)
return err
}
return nil
}
func (p *#{EntityName}) OpDelete(ctx *gin.Context, id int64) error {
err := p.store.Delete#{EntityName}(ctx, id)
if err != nil {
logger.Error("Delete#{EntityName} fail, err: %v", err)
return err
}
return nil
}
func (p *#{EntityName}) OpList(ctx *gin.Context, req *#{moduleName}proto.OpListReq) ([]*dbstruct.#{EntityName}, error) {
list, err := p.store.Get#{EntityName}List(ctx, req)
if err != nil {
logger.Error("Get#{EntityName}List fail, err: %v", err)
return make([]*dbstruct.#{EntityName}, 0), err
}
return list, nil
}
func (p *#{EntityName}) GetListByIds(ctx *gin.Context, ids []int64) ([]*dbstruct.#{EntityName}, error) {
list, err := p.store.Get#{EntityName}ListByIds(ctx, ids)
if err != nil {
logger.Error("Get#{EntityName}ListByIds fail, ids: %v, err: %v", ids, err)
return make([]*dbstruct.#{EntityName}, 0), err
}
return list, nil
}