作者 yangfu

用户模型

  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: department/v1
  14 + group: department
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @handler getDepartment
  19 + post /department/:id (DepartmentGetRequest) returns (DepartmentGetResponse)
  20 + @handler saveDepartment
  21 + post /department (DepartmentSaveRequest) returns (DepartmentSaveResponse)
  22 + @handler deleteDepartment
  23 + delete /department/:id (DepartmentDeleteRequest) returns (DepartmentDeleteResponse)
  24 + @handler updateDepartment
  25 + put /department/:id (DepartmentUpdateRequest) returns (DepartmentUpdateResponse)
  26 + @handler searchDepartment
  27 + post /department/search (DepartmentSearchRequest) returns (DepartmentSearchResponse)
  28 +}
  29 +
  30 +type (
  31 + DepartmentGetRequest {
  32 + Id int64 `path:"id"`
  33 + }
  34 + DepartmentGetResponse struct{
  35 + Department DepartmentItem `json:"department"`
  36 + }
  37 +
  38 + DepartmentSaveRequest struct{
  39 + Department DepartmentItem `json:"department"`
  40 + }
  41 + DepartmentSaveResponse struct{}
  42 +
  43 + DepartmentDeleteRequest struct{
  44 + Id int64 `path:"id"`
  45 + }
  46 + DepartmentDeleteResponse struct{}
  47 +
  48 + DepartmentUpdateRequest struct{
  49 + Id int64 `path:"id"`
  50 + Department DepartmentItem `json:"department"`
  51 + }
  52 + DepartmentUpdateResponse struct{}
  53 +
  54 + DepartmentSearchRequest struct{
  55 + Page int `json:"page"`
  56 + Size int `json:"size"`
  57 + }
  58 + DepartmentSearchResponse{
  59 + List []DepartmentItem `json:"list"`
  60 + Total int64 `json:"total"`
  61 + }
  62 + DepartmentItem struct{
  63 +
  64 + }
  65 +)
  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: role/v1
  14 + group: role
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @handler getRole
  19 + post /role/:id (RoleGetRequest) returns (RoleGetResponse)
  20 + @handler saveRole
  21 + post /role (RoleSaveRequest) returns (RoleSaveResponse)
  22 + @handler deleteRole
  23 + delete /role/:id (RoleDeleteRequest) returns (RoleDeleteResponse)
  24 + @handler updateRole
  25 + put /role/:id (RoleUpdateRequest) returns (RoleUpdateResponse)
  26 + @handler searchRole
  27 + post /role/search (RoleSearchRequest) returns (RoleSearchResponse)
  28 +}
  29 +
  30 +type (
  31 + RoleGetRequest {
  32 + Id int64 `path:"id"`
  33 + }
  34 + RoleGetResponse struct{
  35 + Role RoleItem `json:"role"`
  36 + }
  37 +
  38 + RoleSaveRequest struct{
  39 + Role RoleItem `json:"role"`
  40 + }
  41 + RoleSaveResponse struct{}
  42 +
  43 + RoleDeleteRequest struct{
  44 + Id int64 `path:"id"`
  45 + }
  46 + RoleDeleteResponse struct{}
  47 +
  48 + RoleUpdateRequest struct{
  49 + Id int64 `path:"id"`
  50 + Role RoleItem `json:"role"`
  51 + }
  52 + RoleUpdateResponse struct{}
  53 +
  54 + RoleSearchRequest struct{
  55 + Page int `json:"page"`
  56 + Size int `json:"size"`
  57 + }
  58 + RoleSearchResponse{
  59 + List []RoleItem `json:"list"`
  60 + Total int64 `json:"total"`
  61 + }
  62 + RoleItem struct{
  63 +
  64 + }
  65 +)
  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: user/v1
  14 + group: user
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @handler getUser
  19 + post /user/:id (UserGetRequest) returns (UserGetResponse)
  20 + @handler saveUser
  21 + post /user (UserSaveRequest) returns (UserSaveResponse)
  22 + @handler deleteUser
  23 + delete /user/:id (UserDeleteRequest) returns (UserDeleteResponse)
  24 + @handler updateUser
  25 + put /user/:id (UserUpdateRequest) returns (UserUpdateResponse)
  26 + @handler searchUser
  27 + post /user/search (UserSearchRequest) returns (UserSearchResponse)
  28 +}
  29 +
  30 +type (
  31 + UserGetRequest {
  32 + Id int64 `path:"id"`
  33 + }
  34 + UserGetResponse struct{
  35 + User UserItem `json:"user"`
  36 + }
  37 +
  38 + UserSaveRequest struct{
  39 + User UserItem `json:"user"`
  40 + }
  41 + UserSaveResponse struct{}
  42 +
  43 + UserDeleteRequest struct{
  44 + Id int64 `path:"id"`
  45 + }
  46 + UserDeleteResponse struct{}
  47 +
  48 + UserUpdateRequest struct{
  49 + Id int64 `path:"id"`
  50 + User UserItem `json:"user"`
  51 + }
  52 + UserUpdateResponse struct{}
  53 +
  54 + UserSearchRequest struct{
  55 + Page int `json:"page"`
  56 + Size int `json:"size"`
  57 + }
  58 + UserSearchResponse{
  59 + List []UserItem `json:"list"`
  60 + Total int64 `json:"total"`
  61 + }
  62 + UserItem struct{
  63 +
  64 + }
  65 +)
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message DepartmentGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message DepartmentGetResp{
  12 + DepartmentItem User = 1;
  13 +}
  14 +
  15 +message DepartmentSaveReq {
  16 +
  17 +}
  18 +message DepartmentSaveResp{
  19 +
  20 +}
  21 +
  22 +message DepartmentDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message DepartmentDeleteResp{
  26 +
  27 +}
  28 +
  29 +message DepartmentUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message DepartmentUpdateResp{
  33 +
  34 +}
  35 +
  36 +message DepartmentSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message DepartmentSearchResp{
  41 + repeated DepartmentItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message DepartmentItem {
  45 +
  46 +}
  47 +
  48 +service DepartmentService {
  49 + rpc DepartmentGet(DepartmentGetReq) returns(DepartmentGetResp);
  50 + rpc DepartmentSave(DepartmentSaveReq) returns(DepartmentSaveResp);
  51 + rpc DepartmentDelete(DepartmentDeleteReq) returns(DepartmentDeleteResp);
  52 + rpc DepartmentUpdate(DepartmentUpdateReq) returns(DepartmentUpdateResp);
  53 + rpc DepartmentSearch(DepartmentSearchReq) returns(DepartmentSearchResp);
  54 +}
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message RoleGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message RoleGetResp{
  12 + RoleItem User = 1;
  13 +}
  14 +
  15 +message RoleSaveReq {
  16 +
  17 +}
  18 +message RoleSaveResp{
  19 +
  20 +}
  21 +
  22 +message RoleDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message RoleDeleteResp{
  26 +
  27 +}
  28 +
  29 +message RoleUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message RoleUpdateResp{
  33 +
  34 +}
  35 +
  36 +message RoleSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message RoleSearchResp{
  41 + repeated RoleItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message RoleItem {
  45 +
  46 +}
  47 +
  48 +service RoleService {
  49 + rpc RoleGet(RoleGetReq) returns(RoleGetResp);
  50 + rpc RoleSave(RoleSaveReq) returns(RoleSaveResp);
  51 + rpc RoleDelete(RoleDeleteReq) returns(RoleDeleteResp);
  52 + rpc RoleUpdate(RoleUpdateReq) returns(RoleUpdateResp);
  53 + rpc RoleSearch(RoleSearchReq) returns(RoleSearchResp);
  54 +}
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message UserGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message UserGetResp{
  12 + UserItem User = 1;
  13 +}
  14 +
  15 +message UserSaveReq {
  16 +
  17 +}
  18 +message UserSaveResp{
  19 +
  20 +}
  21 +
  22 +message UserDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message UserDeleteResp{
  26 +
  27 +}
  28 +
  29 +message UserUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message UserUpdateResp{
  33 +
  34 +}
  35 +
  36 +message UserSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message UserSearchResp{
  41 + repeated UserItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message UserItem {
  45 +
  46 +}
  47 +
  48 +service UserService {
  49 + rpc UserGet(UserGetReq) returns(UserGetResp);
  50 + rpc UserSave(UserSaveReq) returns(UserSaveResp);
  51 + rpc UserDelete(UserDeleteReq) returns(UserDeleteResp);
  52 + rpc UserUpdate(UserUpdateReq) returns(UserUpdateResp);
  53 + rpc UserSearch(UserSearchReq) returns(UserSearchResp);
  54 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  6 + "gorm.io/gorm"
  7 +)
  8 +
  9 +type Department struct {
  10 + Id int64 // 唯一标识
  11 +
  12 + CreatedAt int64 `json:"createdAt,omitempty"`
  13 + UpdatedAt int64 `json:"updatedAt,omitempty"`
  14 + DeletedAt int64 `json:"deletedAt,omitempty"`
  15 + Version int `json:"version,omitempty"`
  16 +}
  17 +
  18 +func (m *Department) TableName() string {
  19 + return "department"
  20 +}
  21 +
  22 +func (m *Department) BeforeCreate(tx *gorm.DB) (err error) {
  23 + // m.CreatedAt = time.Now().Unix()
  24 + // m.UpdatedAt = time.Now().Unix()
  25 + return
  26 +}
  27 +
  28 +func (m *Department) BeforeUpdate(tx *gorm.DB) (err error) {
  29 + // m.UpdatedAt = time.Now().Unix()
  30 + return
  31 +}
  32 +
  33 +func (m *Department) CacheKeyFunc() string {
  34 + if m.Id == 0 {
  35 + return ""
  36 + }
  37 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  38 +}
  39 +
  40 +func (m *Department) CacheKeyFuncByObject(obj interface{}) string {
  41 + if v, ok := obj.(*Department); ok {
  42 + return v.CacheKeyFunc()
  43 + }
  44 + return ""
  45 +}
  46 +
  47 +func (m *Department) CachePrimaryKeyFunc() string {
  48 + if len("") == 0 {
  49 + return ""
  50 + }
  51 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  52 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  6 + "gorm.io/gorm"
  7 + "time"
  8 +)
  9 +
  10 +type Role struct {
  11 + Id int64 // 唯一标识
  12 +
  13 + CreatedAt int64 `json:"createdAt,omitempty"`
  14 + UpdatedAt int64 `json:"updatedAt,omitempty"`
  15 + DeletedAt int64 `json:"deletedAt,omitempty"`
  16 + Version int `json:"version,omitempty"`
  17 +}
  18 +
  19 +func (m *Role) TableName() string {
  20 + return "role"
  21 +}
  22 +
  23 +func (m *Role) BeforeCreate(tx *gorm.DB) (err error) {
  24 + m.CreatedAt = time.Now().Unix()
  25 + m.UpdatedAt = time.Now().Unix()
  26 + return
  27 +}
  28 +
  29 +func (m *Role) BeforeUpdate(tx *gorm.DB) (err error) {
  30 + m.UpdatedAt = time.Now().Unix()
  31 + return
  32 +}
  33 +
  34 +func (m *Role) CacheKeyFunc() string {
  35 + if m.Id == 0 {
  36 + return ""
  37 + }
  38 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  39 +}
  40 +
  41 +func (m *Role) CacheKeyFuncByObject(obj interface{}) string {
  42 + if v, ok := obj.(*Role); ok {
  43 + return v.CacheKeyFunc()
  44 + }
  45 + return ""
  46 +}
  47 +
  48 +func (m *Role) CachePrimaryKeyFunc() string {
  49 + if len("") == 0 {
  50 + return ""
  51 + }
  52 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  53 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  6 + "gorm.io/gorm"
  7 + "time"
  8 +)
  9 +
  10 +type User struct {
  11 + Id int64 // 唯一标识
  12 +
  13 + CreatedAt int64 `json:"createdAt,omitempty"`
  14 + UpdatedAt int64 `json:"updatedAt,omitempty"`
  15 + DeletedAt int64 `json:"deletedAt,omitempty"`
  16 + Version int `json:"version,omitempty"`
  17 +}
  18 +
  19 +func (m *User) TableName() string {
  20 + return "user"
  21 +}
  22 +
  23 +func (m *User) BeforeCreate(tx *gorm.DB) (err error) {
  24 + m.CreatedAt = time.Now().Unix()
  25 + m.UpdatedAt = time.Now().Unix()
  26 + return
  27 +}
  28 +
  29 +func (m *User) BeforeUpdate(tx *gorm.DB) (err error) {
  30 + m.UpdatedAt = time.Now().Unix()
  31 + return
  32 +}
  33 +
  34 +func (m *User) CacheKeyFunc() string {
  35 + if m.Id == 0 {
  36 + return ""
  37 + }
  38 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  39 +}
  40 +
  41 +func (m *User) CacheKeyFuncByObject(obj interface{}) string {
  42 + if v, ok := obj.(*User); ok {
  43 + return v.CacheKeyFunc()
  44 + }
  45 + return ""
  46 +}
  47 +
  48 +func (m *User) CachePrimaryKeyFunc() string {
  49 + if len("") == 0 {
  50 + return ""
  51 + }
  52 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  53 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type DepartmentRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *DepartmentRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Department) (*domain.Department, error) {
  19 + var (
  20 + err error
  21 + m = &models.Department{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *DepartmentRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Department) (*domain.Department, error) {
  36 + var (
  37 + err error
  38 + m *models.Department
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *DepartmentRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Department) (*domain.Department, error) {
  55 + var (
  56 + err error
  57 + m *models.Department
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *DepartmentRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Department) (*domain.Department, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.Department{Id: dm.Identify().(int64)}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *DepartmentRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Department, error) {
  94 + var (
  95 + err error
  96 + tx = conn.DB()
  97 + m = new(models.Department)
  98 + )
  99 + queryFunc := func() (interface{}, error) {
  100 + tx = tx.Model(m).Where("id = ?", id).First(m)
  101 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  102 + return nil, domain.ErrNotFound
  103 + }
  104 + return m, tx.Error
  105 + }
  106 + cacheModel := new(models.Department)
  107 + cacheModel.Id = id
  108 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  109 + return nil, err
  110 + }
  111 + return repository.ModelToDomainModel(m)
  112 +}
  113 +
  114 +func (repository *DepartmentRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Department, error) {
  115 + var (
  116 + tx = conn.DB()
  117 + ms []*models.Department
  118 + dms = make([]*domain.Department, 0)
  119 + total int64
  120 + )
  121 + queryFunc := func() (interface{}, error) {
  122 + tx = tx.Model(&ms).Order("id desc")
  123 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  124 + return dms, tx.Error
  125 + }
  126 + return dms, nil
  127 + }
  128 +
  129 + if _, err := repository.Query(queryFunc); err != nil {
  130 + return 0, nil, err
  131 + }
  132 +
  133 + for _, item := range ms {
  134 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  135 + return 0, dms, err
  136 + } else {
  137 + dms = append(dms, dm)
  138 + }
  139 + }
  140 + return total, dms, nil
  141 +}
  142 +
  143 +func (repository *DepartmentRepository) ModelToDomainModel(from *models.Department) (*domain.Department, error) {
  144 + to := &domain.Department{}
  145 + err := copier.Copy(to, from)
  146 + return to, err
  147 +}
  148 +
  149 +func (repository *DepartmentRepository) DomainModelToModel(from *domain.Department) (*models.Department, error) {
  150 + to := &models.Department{}
  151 + err := copier.Copy(to, from)
  152 + return to, err
  153 +}
  154 +
  155 +func NewDepartmentRepository(cache *cache.CachedRepository) domain.DepartmentRepository {
  156 + return &DepartmentRepository{CachedRepository: cache}
  157 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type RoleRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *RoleRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Role) (*domain.Role, error) {
  19 + var (
  20 + err error
  21 + m = &models.Role{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *RoleRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Role) (*domain.Role, error) {
  36 + var (
  37 + err error
  38 + m *models.Role
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *RoleRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Role) (*domain.Role, error) {
  55 + var (
  56 + err error
  57 + m *models.Role
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *RoleRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Role) (*domain.Role, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.Role{Id: dm.Identify().(int64)}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *RoleRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Role, error) {
  94 + var (
  95 + err error
  96 + tx = conn.DB()
  97 + m = new(models.Role)
  98 + )
  99 + queryFunc := func() (interface{}, error) {
  100 + tx = tx.Model(m).Where("id = ?", id).First(m)
  101 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  102 + return nil, domain.ErrNotFound
  103 + }
  104 + return m, tx.Error
  105 + }
  106 + cacheModel := new(models.Role)
  107 + cacheModel.Id = id
  108 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  109 + return nil, err
  110 + }
  111 + return repository.ModelToDomainModel(m)
  112 +}
  113 +
  114 +func (repository *RoleRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Role, error) {
  115 + var (
  116 + tx = conn.DB()
  117 + ms []*models.Role
  118 + dms = make([]*domain.Role, 0)
  119 + total int64
  120 + )
  121 + queryFunc := func() (interface{}, error) {
  122 + tx = tx.Model(&ms).Order("id desc")
  123 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  124 + return dms, tx.Error
  125 + }
  126 + return dms, nil
  127 + }
  128 +
  129 + if _, err := repository.Query(queryFunc); err != nil {
  130 + return 0, nil, err
  131 + }
  132 +
  133 + for _, item := range ms {
  134 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  135 + return 0, dms, err
  136 + } else {
  137 + dms = append(dms, dm)
  138 + }
  139 + }
  140 + return total, dms, nil
  141 +}
  142 +
  143 +func (repository *RoleRepository) ModelToDomainModel(from *models.Role) (*domain.Role, error) {
  144 + to := &domain.Role{}
  145 + err := copier.Copy(to, from)
  146 + return to, err
  147 +}
  148 +
  149 +func (repository *RoleRepository) DomainModelToModel(from *domain.Role) (*models.Role, error) {
  150 + to := &models.Role{}
  151 + err := copier.Copy(to, from)
  152 + return to, err
  153 +}
  154 +
  155 +func NewRoleRepository(cache *cache.CachedRepository) domain.RoleRepository {
  156 + return &RoleRepository{CachedRepository: cache}
  157 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type UserRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *UserRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.User) (*domain.User, error) {
  19 + var (
  20 + err error
  21 + m = &models.User{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *UserRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.User) (*domain.User, error) {
  36 + var (
  37 + err error
  38 + m *models.User
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *UserRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.User) (*domain.User, error) {
  55 + var (
  56 + err error
  57 + m *models.User
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *UserRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.User) (*domain.User, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.User{Id: dm.Identify().(int64)}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *UserRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.User, error) {
  94 + var (
  95 + err error
  96 + tx = conn.DB()
  97 + m = new(models.User)
  98 + )
  99 + queryFunc := func() (interface{}, error) {
  100 + tx = tx.Model(m).Where("id = ?", id).First(m)
  101 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  102 + return nil, domain.ErrNotFound
  103 + }
  104 + return m, tx.Error
  105 + }
  106 + cacheModel := new(models.User)
  107 + cacheModel.Id = id
  108 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  109 + return nil, err
  110 + }
  111 + return repository.ModelToDomainModel(m)
  112 +}
  113 +
  114 +func (repository *UserRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.User, error) {
  115 + var (
  116 + tx = conn.DB()
  117 + ms []*models.User
  118 + dms = make([]*domain.User, 0)
  119 + total int64
  120 + )
  121 + queryFunc := func() (interface{}, error) {
  122 + tx = tx.Model(&ms).Order("id desc")
  123 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  124 + return dms, tx.Error
  125 + }
  126 + return dms, nil
  127 + }
  128 +
  129 + if _, err := repository.Query(queryFunc); err != nil {
  130 + return 0, nil, err
  131 + }
  132 +
  133 + for _, item := range ms {
  134 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  135 + return 0, dms, err
  136 + } else {
  137 + dms = append(dms, dm)
  138 + }
  139 + }
  140 + return total, dms, nil
  141 +}
  142 +
  143 +func (repository *UserRepository) ModelToDomainModel(from *models.User) (*domain.User, error) {
  144 + to := &domain.User{}
  145 + err := copier.Copy(to, from)
  146 + return to, err
  147 +}
  148 +
  149 +func (repository *UserRepository) DomainModelToModel(from *domain.User) (*models.User, error) {
  150 + to := &models.User{}
  151 + err := copier.Copy(to, from)
  152 + return to, err
  153 +}
  154 +
  155 +func NewUserRepository(cache *cache.CachedRepository) domain.UserRepository {
  156 + return &UserRepository{CachedRepository: cache}
  157 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  6 +)
  7 +
  8 +type Department struct {
  9 + Id int64 `json:"id,omitempty"` // 部门ID
  10 + CompanyId int64 `json:"companyId,omitempty"` // 公司ID
  11 + ParentId int64 `json:"parentId,omitempty"` // 父级ID
  12 + Name string `json:"name,omitempty"` // 部门名称
  13 +
  14 + CreatedAt int64 `json:"createdAt,omitempty"`
  15 + UpdatedAt int64 `json:"updatedAt,omitempty"`
  16 + DeletedAt int64 `json:"deletedAt,omitempty"`
  17 + Version int `json:"version,omitempty"`
  18 +}
  19 +
  20 +type DepartmentRepository interface {
  21 + Insert(ctx context.Context, conn transaction.Conn, dm *Department) (*Department, error)
  22 + Update(ctx context.Context, conn transaction.Conn, dm *Department) (*Department, error)
  23 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Department) (*Department, error)
  24 + Delete(ctx context.Context, conn transaction.Conn, dm *Department) (*Department, error)
  25 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Department, error)
  26 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Department, error)
  27 +}
  28 +
  29 +func (m *Department) Identify() interface{} {
  30 + if m.Id == 0 {
  31 + return nil
  32 + }
  33 + return m.Id
  34 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  6 +)
  7 +
  8 +type Role struct {
  9 + Id int64 `json:"id"` // 角色ID
  10 + Name string `json:"name"` // 角色名称
  11 + Auths []int64 `json:"auths"` // 角色权限列表
  12 + Remark string `json:"remark"` // 备注
  13 + Users []int64 `json:"users"` // 绑定的用户
  14 +
  15 + CreatedAt int64 `json:"createdAt,omitempty"`
  16 + UpdatedAt int64 `json:"updatedAt,omitempty"`
  17 + DeletedAt int64 `json:"deletedAt,omitempty"`
  18 + Version int `json:"version,omitempty"`
  19 +}
  20 +
  21 +type RoleRepository interface {
  22 + Insert(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
  23 + Update(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
  24 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
  25 + Delete(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
  26 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Role, error)
  27 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Role, error)
  28 +}
  29 +
  30 +func (m *Role) Identify() interface{} {
  31 + if m.Id == 0 {
  32 + return nil
  33 + }
  34 + return m.Id
  35 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  6 +)
  7 +
  8 +type User struct {
  9 + Id int64 `json:"id,omitempty"` // 用户ID
  10 + CompanyId int64 `json:"companyId,omitempty"` // 公司ID
  11 + DepartmentId int64 `json:"departmentId,omitempty"` // 部门ID
  12 + Roles []int64 `json:"roleId,omitempty"` // 角色
  13 + Flag int `json:"flag"` // 标识 1:管理员 2:普通用户 (有绑定角色是管理员)
  14 + Name string `json:"name,omitempty"` // 名称
  15 + Avatar string `json:"avatar,omitempty"` // 头像
  16 + Phone string `json:"phone,omitempty"` // 手机号 唯一
  17 + Position string `json:"position,omitempty"` // 职位
  18 + Enable int `json:"enable,omitempty"` // 启用状态 1:启用 2:禁用
  19 + AuditStatus int `json:"auditStatus,omitempty"` // 审核状态 0:待审核 1:审核通过 2:拒绝
  20 +
  21 + CreatedAt int64 `json:"createdAt,omitempty"`
  22 + UpdatedAt int64 `json:"updatedAt,omitempty"`
  23 + DeletedAt int64 `json:"deletedAt,omitempty"`
  24 + Version int `json:"version,omitempty"`
  25 +}
  26 +
  27 +type UserRepository interface {
  28 + Insert(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
  29 + Update(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
  30 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
  31 + Delete(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
  32 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*User, error)
  33 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*User, error)
  34 +}
  35 +
  36 +func (m *User) Identify() interface{} {
  37 + if m.Id == 0 {
  38 + return nil
  39 + }
  40 + return m.Id
  41 +}
1 CREATE TABLE `comment` ( 1 CREATE TABLE `comment` (
2 `id` int(0) NOT NULL COMMENT '唯一标识', 2 `id` int(0) NOT NULL COMMENT '唯一标识',
3 PRIMARY KEY (`id`) USING BTREE 3 PRIMARY KEY (`id`) USING BTREE
  4 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  5 +
  6 +CREATE TABLE `department` (
  7 + `id` int(0) NOT NULL COMMENT '唯一标识',
  8 + PRIMARY KEY (`id`) USING BTREE
  9 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  10 +
  11 +CREATE TABLE `role` (
  12 + `id` int(0) NOT NULL COMMENT '唯一标识',
  13 + PRIMARY KEY (`id`) USING BTREE
  14 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  15 +
  16 +CREATE TABLE `user` (
  17 + `id` int(0) NOT NULL COMMENT '唯一标识',
  18 + PRIMARY KEY (`id`) USING BTREE
4 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; 19 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;