作者 yangfu

模型初始化

... ... @@ -13,3 +13,43 @@ func CreateMenuRepository(options map[string]interface{}) (domain.MenuRepository
}
return repository.NewMenuRepository(transactionContext)
}
func CreateOrgRepository(options map[string]interface{}) (domain.OrgRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewOrgRepository(transactionContext)
}
func CreateRoleRepository(options map[string]interface{}) (domain.RoleRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewRoleRepository(transactionContext)
}
func CreateUsersBaseRepository(options map[string]interface{}) (domain.UsersBaseRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewUsersBaseRepository(transactionContext)
}
func CreateUsersRepository(options map[string]interface{}) (domain.UsersRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewUsersRepository(transactionContext)
}
func CreateCompanyRepository(options map[string]interface{}) (domain.CompanyRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewCompanyRepository(transactionContext)
}
... ...
package domain
import "time"
// 公司状态状态 1:已注册 2:待认证 3:已认证
const (
CompanyRegistered = iota + 1
CompanyUnauthenticated
CompanyAuthenticated
)
// 企业信息 (base)
type Company struct {
// 企业id
CompanyId int64 `json:"companyId"`
// 企业配置信息
CompanyConfig *CompanyConfig `json:"companyConfig"`
// 企业基本信息
CompanyInfo *CompanyInfo `json:"companyInfo"`
// 公司状态
Status int `json:"status"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt"`
}
type CompanyRepository interface {
Save(company *Company) (*Company, error)
Remove(company *Company) (*Company, error)
FindOne(queryOptions map[string]interface{}) (*Company, error)
Find(queryOptions map[string]interface{}) (int64, []*Company, error)
}
func (company *Company) Identify() interface{} {
if company.CompanyId == 0 {
return nil
}
return company.CompanyId
}
func (company *Company) Update(data map[string]interface{}) error {
if systemName, ok := data["systemName"]; ok {
company.CompanyConfig.SystemName = systemName.(string)
}
if theme, ok := data["theme"]; ok {
company.CompanyConfig.Theme = theme.(string)
}
if companyName, ok := data["companyName"]; ok {
company.CompanyInfo.CompanyName = companyName.(string)
}
if scale, ok := data["scale"]; ok {
company.CompanyInfo.Scale = scale.(string)
}
if logo, ok := data["logo"]; ok {
company.CompanyInfo.Logo = logo.(string)
}
if address, ok := data["address"]; ok {
company.CompanyInfo.Address = address.(string)
}
if industryCategory, ok := data["industryCategory"]; ok {
company.CompanyInfo.IndustryCategory = industryCategory.(string)
}
if contacts, ok := data["contacts"]; ok {
company.CompanyInfo.Contacts = contacts.(string)
}
if registTime, ok := data["registTime"]; ok {
company.CompanyInfo.RegisteredTime = registTime.(time.Time)
}
if registStatus, ok := data["registStatus"]; ok {
company.CompanyInfo.status = registStatus.(int)
}
if status, ok := data["status"]; ok {
company.Status = status.(int)
}
if createdAt, ok := data["createdAt"]; ok {
company.CreatedAt = createdAt.(time.Time)
}
if updatedAt, ok := data["updatedAt"]; ok {
company.UpdatedAt = updatedAt.(time.Time)
}
return nil
}
... ...
package domain
// 公司配置信息
type CompanyConfig struct {
// 系统名称
SystemName string `json:"systemName"`
// 主题
Theme string `json:"theme"`
}
... ...
package domain
import "time"
// 公司信息
type CompanyInfo struct {
// 企业名称
CompanyName string `json:"companyName"`
// 规模
Scale string `json:"scale"`
// 公司Logo地址
Logo string `json:"logo"`
// 公司地址
Address string `json:"address"`
// 所属行业
IndustryCategory string `json:"industryCategory"`
// 联系人
Contacts string `json:"contacts"`
// 注册时间
RegisteredTime time.Time `json:"registeredTime"`
// 状态 1:已注册 2:待认证 3:已认证
status int `json:"status"`
}
... ...
package domain
import "time"
// 共创信息
type CooperationInfo struct {
// 共创公司
CooperationCompany string `json:"cooperationCompany"`
// 共创到期时间 (yyyy-MM-dd)
CooperationDeadline time.Time `json:"cooperationDeadline"`
}
... ...
package domain
// 冗余附加数据
type Ext struct {
// 用户姓名
UsersName string `json:"usersName,omitempty"`
// 组织名称
OrgName string `json:"orgName,omitempty"`
// 手机号码
Phone string `json:"phone,omitempty"`
// 部门名称
DepName string `json:"depName,omitempty"`
// 父级部门名称
ParentDepName string `json:"parentDepName,omitempty"`
}
... ...
... ... @@ -37,31 +37,31 @@ type MenuType string
// 系统菜单
type Menu struct {
// 菜单编号
MenuId int64 `json:"menuId"`
MenuId int64 `json:"menuId,omitempty"`
// 父级id
ParentId int64 `json:"parentId"`
ParentId int64 `json:"parentId,omitempty"`
// 菜单名称
MenuName string `json:"menuName"`
MenuName string `json:"menuName,omitempty"`
// 菜单编码 SYSTEM_USER_EDIT / 100101 (字符编码)
Code string `json:"code"`
Code string `json:"code,omitempty"`
// 权限编码 users:edit
AccessCode string `json:"accessCode"`
AccessCode string `json:"accessCode,omitempty"`
// 菜单类型 (目录catalog、菜单menu、按钮button)
MenuType string `json:"menuType"`
MenuType string `json:"menuType,omitempty"`
// 菜单图标
Icon string `json:"icon"`
Icon string `json:"icon,omitempty"`
// 排序
Sort int `json:"sort"`
Sort int `json:"sort,omitempty"`
// 菜单说明
Remark string `json:"remark"`
Remark string `json:"remark,omitempty"`
// 菜单类别 (web:1、app:2)
Category string `json:"category"`
Category string `json:"category,omitempty"`
// 路径节点路径("0,11,12,")
ParentPath string `json:"parentPath"`
ParentPath string `json:"parentPath,omitempty"`
// 菜单是否公开状态,[1:显示],[2:隐藏],默认显示
IsPublish int `json:"isPublish"`
IsPublish int `json:"isPublish,omitempty"`
// 启用状态(启用:1 禁用:2),默认启用
EnableStatus int `json:"enableStatus"`
EnableStatus int `json:"enableStatus,omitempty"`
}
type MenuRepository interface {
... ...
package domain
import "time"
// 组织 organization
type Org struct {
// 组织ID
OrgId int64 `json:"orgId,omitempty"`
// 企业id
CompanyId int64 `json:"companyId,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
// 删除时间
DeletedAt time.Time `json:"deletedAt,omitempty"`
// 组织编码
OrgCode string `json:"orgCode,omitempty"`
// 组织名称
OrgName string `json:"orgName,omitempty"`
// 扩展数据
Ext *Ext `json:"ext,omitempty"`
// 是否是组织标识 1:是 2:不是
IsOrg int `json:"isOrg,omitempty"`
// 组织状态 1:启用 2:禁用 3.删除
OrgStatus int `json:"orgStatus,omitempty"`
// 父级ID
ParentId int64 `json:"parentId,omitempty"`
// 父级节点路径("0,11,12,")
ParentPath string `json:"parentPath,omitempty"`
}
type OrgRepository interface {
Save(org *Org) (*Org, error)
Remove(org *Org) (*Org, error)
FindOne(queryOptions map[string]interface{}) (*Org, error)
Find(queryOptions map[string]interface{}) (int64, []*Org, error)
}
func (org *Org) Identify() interface{} {
if org.OrgId == 0 {
return nil
}
return org.OrgId
}
func (org *Org) Update(data map[string]interface{}) error {
if createdAt, ok := data["createdAt"]; ok {
org.CreatedAt = createdAt.(time.Time)
}
if updatedAt, ok := data["updatedAt"]; ok {
org.UpdatedAt = updatedAt.(time.Time)
}
if deletedAt, ok := data["deletedAt"]; ok {
org.DeletedAt = deletedAt.(time.Time)
}
if orgCode, ok := data["orgCode"]; ok {
org.OrgCode = orgCode.(string)
}
if orgName, ok := data["orgName"]; ok {
org.OrgName = orgName.(string)
}
if usersName, ok := data["usersName"]; ok {
org.Ext.UsersName = usersName.(string)
}
if orgName, ok := data["orgName"]; ok {
org.Ext.OrgName = orgName.(string)
}
if phone, ok := data["phone"]; ok {
org.Ext.Phone = phone.(string)
}
if depName, ok := data["depName"]; ok {
org.Ext.DepName = depName.(string)
}
if parentDepName, ok := data["parentDepName"]; ok {
org.Ext.ParentDepName = parentDepName.(string)
}
if isOrg, ok := data["isOrg"]; ok {
org.IsOrg = isOrg.(int)
}
if parentId, ok := data["parentId"]; ok {
org.ParentId = parentId.(int64)
}
if parentPath, ok := data["parentPath"]; ok {
org.ParentPath = parentPath.(string)
}
return nil
}
... ...
package domain
import "time"
// 角色类型 1.普通角色 1024:超级管理员
const (
RoleTypeNormal = 1
RoleTypeAdmin = 1024
)
// 角色 (base)
type Role struct {
// 角色ID
RoleId int64 `json:"roleId,omitempty"`
// 企业id
CompanyId int64 `json:"companyId,omitempty"`
// 组织ID
OrgId int64 `json:"orgId,omitempty"`
// 角色类型 1.普通角色 1024:超级管理员
RoleType int `json:"roleType,omitempty"`
// 角色名称
RoleName string `json:"roleName,omitempty"`
// 有权限的菜单
AccessMenus []int64 `json:"accessMenus,omitempty"`
// 描述
Desc int64 `json:"desc,omitempty"`
// 扩展数据
Ext *Ext `json:"ext,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}
type RoleRepository interface {
Save(role *Role) (*Role, error)
Remove(role *Role) (*Role, error)
FindOne(queryOptions map[string]interface{}) (*Role, error)
Find(queryOptions map[string]interface{}) (int64, []*Role, error)
}
func (role *Role) Identify() interface{} {
if role.RoleId == 0 {
return nil
}
return role.RoleId
}
func (role *Role) Update(data map[string]interface{}) error {
if companyId, ok := data["companyId"]; ok {
role.CompanyId = companyId.(int64)
}
if orgId, ok := data["orgId"]; ok {
role.OrgId = orgId.(int64)
}
if roleType, ok := data["roleType"]; ok {
role.RoleType = roleType.(int)
}
if roleName, ok := data["roleName"]; ok {
role.RoleName = roleName.(string)
}
if accessMenus, ok := data["accessMenus"]; ok {
role.AccessMenus = accessMenus.([]int64)
}
if desc, ok := data["desc"]; ok {
role.Desc = desc.(int64)
}
if usersName, ok := data["usersName"]; ok {
role.Ext.UsersName = usersName.(string)
}
if orgName, ok := data["orgName"]; ok {
role.Ext.OrgName = orgName.(string)
}
if phone, ok := data["phone"]; ok {
role.Ext.Phone = phone.(string)
}
if depName, ok := data["depName"]; ok {
role.Ext.DepName = depName.(string)
}
if parentDepName, ok := data["parentDepName"]; ok {
role.Ext.ParentDepName = parentDepName.(string)
}
if createdAt, ok := data["createdAt"]; ok {
role.CreatedAt = createdAt.(time.Time)
}
if updatedAt, ok := data["updatedAt"]; ok {
role.UpdatedAt = updatedAt.(time.Time)
}
return nil
}
... ...
package domain
import "time"
// 用户类型
const (
UsersTypeEmployee = 1
UsersTypeCooperation = 2
UsersTypeCompanyAdmin = 1024
)
// 用户状态
const (
UserStatusEnable UsersStatus = 1
UserStatusDisable UsersStatus = 2
UserStatusDestroy UsersStatus = 3
)
// 用户
type Users struct {
// 用户Id 用户唯一标识
UsersId int64 `json:"usersId,omitempty"`
// 企业id
CompanyId int64 `json:"companyId,omitempty"`
// 用户基础数据id
UsersBaseId int64 `json:"usersBaseId,omitempty"`
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UsersType int `json:"usersType,omitempty"`
// 用户编号 企业内标识
UsersCode string `json:"usersCode,omitempty"`
// 组织机构
OrganizationId int64 `json:"organizationId,omitempty"`
// 所属部门
DepartmentId int64 `json:"departmentId,omitempty"`
// 用户信息 (冗余,数据存在usersBase里面)
UsersInfo *UsersInfo `json:"usersInfo,omitempty"`
// 用户关联的组织
UsersOrg []*Org `json:"usersOrg,omitempty"`
// 用户关联的角色
UsersRole []*Role `json:"usersRole,omitempty"`
// 收藏的菜单(工作台)(菜单编码列表)
FavoriteMenus []string `json:"favoriteMenus,omitempty"`
// 共创信息 (共创用户有效)
CooperationInfo *CooperationInfo `json:"cooperationInfo,omitempty"`
// 状态(1:启用 2:禁用 3:注销)
EnableStatus int `json:"enableStatus,omitempty"`
// 扩展数据
Ext *Ext `json:"ext,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}
type UsersRepository interface {
Save(users *Users) (*Users, error)
Remove(users *Users) (*Users, error)
FindOne(queryOptions map[string]interface{}) (*Users, error)
Find(queryOptions map[string]interface{}) (int64, []*Users, error)
}
func (users *Users) Identify() interface{} {
if users.UsersId == 0 {
return nil
}
return users.UsersId
}
func (users *Users) Update(data map[string]interface{}) error {
if usersId, ok := data["usersId"]; ok {
users.UsersId = usersId.(int64)
}
if companyId, ok := data["companyId"]; ok {
users.CompanyId = companyId.(int64)
}
if usersBaseId, ok := data["usersBaseId"]; ok {
users.UsersBaseId = usersBaseId.(int64)
}
if usersType, ok := data["usersType"]; ok {
users.UsersType = usersType.(int)
}
if usersCode, ok := data["usersCode"]; ok {
users.UsersCode = usersCode.(string)
}
if organizationId, ok := data["organizationId"]; ok {
users.OrganizationId = organizationId.(int64)
}
if departmentId, ok := data["departmentId"]; ok {
users.DepartmentId = departmentId.(int64)
}
if usersName, ok := data["usersName"]; ok {
users.UsersInfo.UsersName = usersName.(string)
}
if phone, ok := data["phone"]; ok {
users.UsersInfo.Phone = phone.(string)
}
if avatar, ok := data["avatar"]; ok {
users.UsersInfo.Avatar = avatar.(string)
}
if email, ok := data["email"]; ok {
users.UsersInfo.Email = email.(string)
}
if usersOrg, ok := data["usersOrg"]; ok {
users.UsersOrg = usersOrg.([]*Org)
}
if usersRole, ok := data["usersRole"]; ok {
users.UsersRole = usersRole.([]*Role)
}
if favoriteMenus, ok := data["favoriteMenus"]; ok {
users.FavoriteMenus = favoriteMenus.([]string)
}
if cooperationCompany, ok := data["cooperationCompany"]; ok {
users.CooperationInfo.CooperationCompany = cooperationCompany.(string)
}
if cooperationDeadline, ok := data["cooperationDeadline"]; ok {
users.CooperationInfo.CooperationDeadline = cooperationDeadline.(time.Time)
}
if enableStatus, ok := data["enableStatus"]; ok {
users.EnableStatus = enableStatus.(int)
}
if usersName, ok := data["usersName"]; ok {
users.Ext.UsersName = usersName.(string)
}
if orgName, ok := data["orgName"]; ok {
users.Ext.OrgName = orgName.(string)
}
if phone, ok := data["phone"]; ok {
users.Ext.Phone = phone.(string)
}
if depName, ok := data["depName"]; ok {
users.Ext.DepName = depName.(string)
}
if parentDepName, ok := data["parentDepName"]; ok {
users.Ext.ParentDepName = parentDepName.(string)
}
if createdAt, ok := data["createdAt"]; ok {
users.CreatedAt = createdAt.(time.Time)
}
if updatedAt, ok := data["updatedAt"]; ok {
users.UpdatedAt = updatedAt.(time.Time)
}
return nil
}
type UsersStatus int
... ...
package domain
import "time"
// 用户基础
type UsersBase struct {
// 用户基础数据id
UsersBaseId int64 `json:"usersBaseId,omitempty"`
// 用户信息
UsersInfo *UsersInfo `json:"usersInfo,omitempty"`
// 手机号码
Phone string `json:"phone,omitempty"`
// 密码
Password string `json:"password,omitempty"`
// IM信息
Im *Im `json:"im,omitempty"`
// 关联的用户 (冗余)
RelatedUsers []int64 `json:"relatedUsers,omitempty"`
// 账号状态 1:正常 2.禁用 3:注销
Status int `json:"status,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}
type UsersBaseRepository interface {
Save(usersBase *UsersBase) (*UsersBase, error)
Remove(usersBase *UsersBase) (*UsersBase, error)
FindOne(queryOptions map[string]interface{}) (*UsersBase, error)
Find(queryOptions map[string]interface{}) (int64, []*UsersBase, error)
}
func (usersBase *UsersBase) Identify() interface{} {
if usersBase.UsersBaseId == 0 {
return nil
}
return usersBase.UsersBaseId
}
func (usersBase *UsersBase) Update(data map[string]interface{}) error {
if usersName, ok := data["usersName"]; ok {
usersBase.UsersInfo.UsersName = usersName.(string)
}
if phone, ok := data["phone"]; ok {
usersBase.UsersInfo.Phone = phone.(string)
}
if avatar, ok := data["avatar"]; ok {
usersBase.UsersInfo.Avatar = avatar.(string)
}
if email, ok := data["email"]; ok {
usersBase.UsersInfo.Email = email.(string)
}
if phone, ok := data["phone"]; ok {
usersBase.Phone = phone.(string)
}
if password, ok := data["password"]; ok {
usersBase.Password = password.(string)
}
if accid, ok := data["accid"]; ok {
usersBase.Im.Accid = accid.(string)
}
if imToken, ok := data["imToken"]; ok {
usersBase.Im.ImToken = imToken.(string)
}
if csAccountId, ok := data["csAccountId"]; ok {
usersBase.Im.CsAccountId = csAccountId.(string)
}
if relatedUsers, ok := data["relatedUsers"]; ok {
usersBase.RelatedUsers = relatedUsers.([]int64)
}
if status, ok := data["status"]; ok {
usersBase.Status = status.(int)
}
if createdAt, ok := data["createdAt"]; ok {
usersBase.CreatedAt = createdAt.(time.Time)
}
if updatedAt, ok := data["updatedAt"]; ok {
usersBase.UpdatedAt = updatedAt.(time.Time)
}
return nil
}
... ...
package domain
// 用户信息 (base)
type UsersInfo struct {
// 用户姓名
UsersName string `json:"usersName,omitempty"`
// 手机号码
Phone string `json:"phone,omitempty"`
// 头像
Avatar string `json:"avatar,omitempty"`
// 邮箱
Email string `json:"email,omitempty"`
}
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"time"
)
type Company struct {
tableName string `pg:"users.company,alias:company"`
// 企业id
CompanyId int64
// 企业配置信息
CompanyConfig *domain.CompanyConfig
// 企业基本信息
CompanyInfo *domain.CompanyInfo
// 公司状态
Status int
// 创建时间
CreatedAt time.Time
// 更新时间
UpdatedAt time.Time
}
... ...
... ... @@ -24,8 +24,8 @@ type Menu struct {
Category string
// 父级节点路径("0,11,12,")
ParentPath string
// 菜单是否公开状态,[0:隐藏],[1:显示],默认显示
// 菜单是否公开状态,[2:隐藏],[1:显示],默认显示
IsPublish int
// 启用状态(启用:1 禁用:0),默认启用
// 启用状态(启用:1 禁用:2),默认启用
EnableStatus int
}
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"time"
)
type Org struct {
tableName string `pg:"users.org,alias:org"`
// 组织ID
OrgId int64
// 企业id
CompanyId int64
// 创建时间
CreatedAt time.Time
// 更新时间
UpdatedAt time.Time
// 删除时间
DeletedAt time.Time
// 组织编码
OrgCode string
// 组织名称
OrgName string
// 扩展数据
Ext *domain.Ext
// 是否是组织标识 1:是 2:不是
IsOrg int
// 组织状态 1:启用 2:禁用 3.删除
OrgStatus int `json:"orgStatus"`
// 父级ID
ParentId int64
// 父级节点路径("0,11,12,")
ParentPath string
}
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"time"
)
type Role struct {
tableName string `pg:"users.role,alias:role"`
// 角色ID
RoleId int64
// 企业id
CompanyId int64
// 组织ID
OrgId int64
// 角色类型 1.普通角色 1024:超级管理员
RoleType int
// 角色名称
RoleName string
// 有权限的菜单
AccessMenus []int64 `pg:",array"`
// 描述
Desc int64
// 扩展数据
Ext *domain.Ext
// 创建时间
CreatedAt time.Time
// 更新时间
UpdatedAt time.Time
}
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"time"
)
type Users struct {
tableName string `pg:"users.users,alias:users"`
// 用户Id 用户唯一标识
UsersId int64
// 企业id
CompanyId int64
// 用户基础数据id
UsersBaseId int64
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UsersType int
// 用户编号 企业内标识
UsersCode string
// 组织机构
OrganizationId int64
// 所属部门
DepartmentId int64
// 用户信息 (冗余,数据存在usersBase里面)
//UsersInfo *domain.UsersInfo
// 用户关联的组织
UsersOrg []*domain.Org `pg:",array"`
// 用户关联的角色
UsersRole []*domain.Role `pg:",array"`
// 收藏的菜单(工作台)(菜单编码列表)
FavoriteMenus []string `pg:",array"`
// 共创信息 (共创用户有效)
CooperationInfo *domain.CooperationInfo
// 状态(1:启用 2:禁用 3:注销)
EnableStatus int
// 扩展数据
Ext *domain.Ext
// 创建时间
CreatedAt time.Time
// 更新时间
UpdatedAt time.Time
}
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"time"
)
type UsersBase struct {
tableName string `pg:"users.users_base,alias:users_base"`
// 用户基础数据id
UsersBaseId int64
// 用户信息
UsersInfo *domain.UsersInfo
// 手机号码
Phone string
// 密码
Password string
// IM信息
Im *domain.Im
// 关联的用户 (冗余)
RelatedUsers []int64 `pg:",array"`
// 账号状态 1:正常 2.禁用 3:注销
Status int
// 创建时间
CreatedAt time.Time
// 更新时间
UpdatedAt time.Time
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
)
func TransformToCompanyDomainModelFromPgModels(companyModel *models.Company) (*domain.Company, error) {
return &domain.Company{
CompanyId: companyModel.CompanyId,
CompanyConfig: companyModel.CompanyConfig,
CompanyInfo: companyModel.CompanyInfo,
Status: companyModel.Status,
CreatedAt: companyModel.CreatedAt,
UpdatedAt: companyModel.UpdatedAt,
}, nil
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
)
func TransformToOrgDomainModelFromPgModels(orgModel *models.Org) (*domain.Org, error) {
return &domain.Org{
OrgId: orgModel.OrgId,
CompanyId: orgModel.CompanyId,
CreatedAt: orgModel.CreatedAt,
UpdatedAt: orgModel.UpdatedAt,
DeletedAt: orgModel.DeletedAt,
OrgCode: orgModel.OrgCode,
OrgName: orgModel.OrgName,
Ext: orgModel.Ext,
IsOrg: orgModel.IsOrg,
ParentId: orgModel.ParentId,
ParentPath: orgModel.ParentPath,
}, nil
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
)
func TransformToRoleDomainModelFromPgModels(roleModel *models.Role) (*domain.Role, error) {
return &domain.Role{
RoleId: roleModel.RoleId,
CompanyId: roleModel.CompanyId,
OrgId: roleModel.OrgId,
RoleType: roleModel.RoleType,
RoleName: roleModel.RoleName,
AccessMenus: roleModel.AccessMenus,
Desc: roleModel.Desc,
Ext: roleModel.Ext,
CreatedAt: roleModel.CreatedAt,
UpdatedAt: roleModel.UpdatedAt,
}, nil
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
)
func TransformToUsersDomainModelFromPgModels(usersModel *models.Users) (*domain.Users, error) {
return &domain.Users{
UsersId: usersModel.UsersId,
CompanyId: usersModel.CompanyId,
UsersBaseId: usersModel.UsersBaseId,
UsersType: usersModel.UsersType,
UsersCode: usersModel.UsersCode,
OrganizationId: usersModel.OrganizationId,
DepartmentId: usersModel.DepartmentId,
//UsersInfo: usersModel.UsersInfo,
UsersOrg: usersModel.UsersOrg,
UsersRole: usersModel.UsersRole,
FavoriteMenus: usersModel.FavoriteMenus,
CooperationInfo: usersModel.CooperationInfo,
EnableStatus: usersModel.EnableStatus,
Ext: usersModel.Ext,
CreatedAt: usersModel.CreatedAt,
UpdatedAt: usersModel.UpdatedAt,
}, nil
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
)
func TransformToUsersBaseDomainModelFromPgModels(usersBaseModel *models.UsersBase) (*domain.UsersBase, error) {
return &domain.UsersBase{
UsersBaseId: usersBaseModel.UsersBaseId,
UsersInfo: usersBaseModel.UsersInfo,
Phone: usersBaseModel.Phone,
Password: usersBaseModel.Password,
Im: usersBaseModel.Im,
RelatedUsers: usersBaseModel.RelatedUsers,
Status: usersBaseModel.Status,
CreatedAt: usersBaseModel.CreatedAt,
UpdatedAt: usersBaseModel.UpdatedAt,
}, nil
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform"
)
type CompanyRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *CompanyRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Company, error) {
sqlBuildFields := []string{
"company_id",
"company_config",
"company_info",
"status",
"created_at",
"updated_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "company_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if company.Identify() == nil {
companyId, err := repository.nextIdentify()
if err != nil {
return company, err
} else {
company.CompanyId = companyId
}
if _, err := tx.QueryOne(
pg.Scan(
&company.CompanyId,
&company.CompanyConfig,
&company.CompanyInfo,
&company.Status,
&company.CreatedAt,
&company.UpdatedAt,
),
fmt.Sprintf("INSERT INTO companys (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
company.CompanyId,
company.CompanyConfig,
company.CompanyInfo,
company.Status,
company.CreatedAt,
company.UpdatedAt,
); err != nil {
return company, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&company.CompanyConfig,
&company.CompanyInfo,
&company.Status,
&company.CreatedAt,
&company.UpdatedAt,
),
fmt.Sprintf("UPDATE companys SET %s WHERE company_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
company.CompanyConfig,
company.CompanyInfo,
company.Status,
company.CreatedAt,
company.UpdatedAt,
company.Identify(),
); err != nil {
return company, err
}
}
return company, nil
}
func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Company, error) {
tx := repository.transactionContext.PgTx
companyModel := new(models.Company)
companyModel.CompanyId = company.Identify().(int64)
if _, err := tx.Model(companyModel).WherePK().Delete(); err != nil {
return company, err
}
return company, nil
}
func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) {
tx := repository.transactionContext.PgTx
companyModel := new(models.Company)
query := sqlbuilder.BuildQuery(tx.Model(companyModel), queryOptions)
query.SetWhereByQueryOption("company.company_id = ?", "companyId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if companyModel.CompanyId == 0 {
return nil, nil
} else {
return transform.TransformToCompanyDomainModelFromPgModels(companyModel)
}
}
func (repository *CompanyRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Company, error) {
tx := repository.transactionContext.PgTx
var companyModels []*models.Company
companys := make([]*domain.Company, 0)
query := sqlbuilder.BuildQuery(tx.Model(&companyModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("company_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, companys, err
} else {
for _, companyModel := range companyModels {
if company, err := transform.TransformToCompanyDomainModelFromPgModels(companyModel); err != nil {
return 0, companys, err
} else {
companys = append(companys, company)
}
}
return int64(count), companys, nil
}
}
func NewCompanyRepository(transactionContext *pgTransaction.TransactionContext) (*CompanyRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CompanyRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform"
)
type OrgRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *OrgRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *OrgRepository) Save(org *domain.Org) (*domain.Org, error) {
sqlBuildFields := []string{
"org_id",
"company_id",
"created_at",
"updated_at",
"deleted_at",
"org_code",
"org_name",
"ext",
"org_status",
"is_org",
"parent_id",
"parent_path",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "org_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if org.Identify() == nil {
orgId, err := repository.nextIdentify()
if err != nil {
return org, err
} else {
org.OrgId = orgId
}
if _, err := tx.QueryOne(
pg.Scan(
&org.OrgId,
&org.CompanyId,
&org.CreatedAt,
&org.UpdatedAt,
&org.DeletedAt,
&org.OrgCode,
&org.OrgName,
&org.Ext,
&org.OrgStatus,
&org.IsOrg,
&org.ParentId,
&org.ParentPath,
),
fmt.Sprintf("INSERT INTO orgs (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
org.OrgId,
org.CompanyId,
org.CreatedAt,
org.UpdatedAt,
org.DeletedAt,
org.OrgCode,
org.OrgName,
org.Ext,
&org.OrgStatus,
org.IsOrg,
org.ParentId,
org.ParentPath,
); err != nil {
return org, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&org.OrgId,
&org.CompanyId,
&org.CreatedAt,
&org.UpdatedAt,
&org.DeletedAt,
&org.OrgCode,
&org.OrgName,
&org.Ext,
&org.IsOrg,
&org.ParentId,
&org.ParentPath,
),
fmt.Sprintf("UPDATE orgs SET %s WHERE org_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
org.CompanyId,
org.CreatedAt,
org.UpdatedAt,
org.DeletedAt,
org.OrgCode,
org.OrgName,
org.Ext,
org.IsOrg,
org.ParentId,
org.ParentPath,
org.Identify(),
); err != nil {
return org, err
}
}
return org, nil
}
func (repository *OrgRepository) Remove(org *domain.Org) (*domain.Org, error) {
tx := repository.transactionContext.PgTx
orgModel := new(models.Org)
orgModel.OrgId = org.Identify().(int64)
if _, err := tx.Model(orgModel).WherePK().Delete(); err != nil {
return org, err
}
return org, nil
}
func (repository *OrgRepository) FindOne(queryOptions map[string]interface{}) (*domain.Org, error) {
tx := repository.transactionContext.PgTx
orgModel := new(models.Org)
query := sqlbuilder.BuildQuery(tx.Model(orgModel), queryOptions)
query.SetWhereByQueryOption("org.org_id = ?", "orgId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if orgModel.OrgId == 0 {
return nil, nil
} else {
return transform.TransformToOrgDomainModelFromPgModels(orgModel)
}
}
func (repository *OrgRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Org, error) {
tx := repository.transactionContext.PgTx
var orgModels []*models.Org
orgs := make([]*domain.Org, 0)
query := sqlbuilder.BuildQuery(tx.Model(&orgModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("org_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, orgs, err
} else {
for _, orgModel := range orgModels {
if org, err := transform.TransformToOrgDomainModelFromPgModels(orgModel); err != nil {
return 0, orgs, err
} else {
orgs = append(orgs, org)
}
}
return int64(count), orgs, nil
}
}
func NewOrgRepository(transactionContext *pgTransaction.TransactionContext) (*OrgRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &OrgRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform"
)
type RoleRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *RoleRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error) {
sqlBuildFields := []string{
"role_id",
"company_id",
"org_id",
"role_type",
"role_name",
"access_menus",
"desc",
"ext",
"created_at",
"updated_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "role_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if role.Identify() == nil {
roleId, err := repository.nextIdentify()
if err != nil {
return role, err
} else {
role.RoleId = roleId
}
if _, err := tx.QueryOne(
pg.Scan(
&role.RoleId,
&role.CompanyId,
&role.OrgId,
&role.RoleType,
&role.RoleName,
pg.Array(&role.AccessMenus),
&role.Desc,
&role.Ext,
&role.CreatedAt,
&role.UpdatedAt,
),
fmt.Sprintf("INSERT INTO roles (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
role.RoleId,
role.CompanyId,
role.OrgId,
role.RoleType,
role.RoleName,
pg.Array(role.AccessMenus),
role.Desc,
role.Ext,
role.CreatedAt,
role.UpdatedAt,
); err != nil {
return role, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&role.RoleId,
&role.CompanyId,
&role.OrgId,
&role.RoleType,
&role.RoleName,
pg.Array(&role.AccessMenus),
&role.Desc,
&role.Ext,
&role.CreatedAt,
&role.UpdatedAt,
),
fmt.Sprintf("UPDATE roles SET %s WHERE role_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
role.CompanyId,
role.OrgId,
role.RoleType,
role.RoleName,
pg.Array(role.AccessMenus),
role.Desc,
role.Ext,
role.CreatedAt,
role.UpdatedAt,
role.Identify(),
); err != nil {
return role, err
}
}
return role, nil
}
func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error) {
tx := repository.transactionContext.PgTx
roleModel := new(models.Role)
roleModel.RoleId = role.Identify().(int64)
if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil {
return role, err
}
return role, nil
}
func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
tx := repository.transactionContext.PgTx
roleModel := new(models.Role)
query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions)
query.SetWhereByQueryOption("role.role_id = ?", "roleId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if roleModel.RoleId == 0 {
return nil, nil
} else {
return transform.TransformToRoleDomainModelFromPgModels(roleModel)
}
}
func (repository *RoleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Role, error) {
tx := repository.transactionContext.PgTx
var roleModels []*models.Role
roles := make([]*domain.Role, 0)
query := sqlbuilder.BuildQuery(tx.Model(&roleModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("role_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, roles, err
} else {
for _, roleModel := range roleModels {
if role, err := transform.TransformToRoleDomainModelFromPgModels(roleModel); err != nil {
return 0, roles, err
} else {
roles = append(roles, role)
}
}
return int64(count), roles, nil
}
}
func NewRoleRepository(transactionContext *pgTransaction.TransactionContext) (*RoleRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &RoleRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform"
)
type UsersBaseRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *UsersBaseRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *UsersBaseRepository) Save(usersBase *domain.UsersBase) (*domain.UsersBase, error) {
sqlBuildFields := []string{
"users_base_id",
"users_info",
"phone",
"password",
"im",
"related_users",
"status",
"created_at",
"updated_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "usersBase_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if usersBase.Identify() == nil {
usersBaseId, err := repository.nextIdentify()
if err != nil {
return usersBase, err
} else {
usersBase.UsersBaseId = usersBaseId
}
if _, err := tx.QueryOne(
pg.Scan(
&usersBase.UsersBaseId,
&usersBase.UsersInfo,
&usersBase.Phone,
&usersBase.Password,
&usersBase.Im,
pg.Array(&usersBase.RelatedUsers),
&usersBase.Status,
&usersBase.CreatedAt,
&usersBase.UpdatedAt,
),
fmt.Sprintf("INSERT INTO users_bases (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
usersBase.UsersBaseId,
usersBase.UsersInfo,
usersBase.Phone,
usersBase.Password,
usersBase.Im,
pg.Array(usersBase.RelatedUsers),
usersBase.Status,
usersBase.CreatedAt,
usersBase.UpdatedAt,
); err != nil {
return usersBase, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&usersBase.UsersBaseId,
&usersBase.UsersInfo,
&usersBase.Phone,
&usersBase.Password,
&usersBase.Im,
pg.Array(&usersBase.RelatedUsers),
&usersBase.Status,
&usersBase.CreatedAt,
&usersBase.UpdatedAt,
),
fmt.Sprintf("UPDATE users_bases SET %s WHERE users_base_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
usersBase.UsersInfo,
usersBase.Phone,
usersBase.Password,
usersBase.Im,
pg.Array(usersBase.RelatedUsers),
usersBase.Status,
usersBase.CreatedAt,
usersBase.UpdatedAt,
usersBase.Identify(),
); err != nil {
return usersBase, err
}
}
return usersBase, nil
}
func (repository *UsersBaseRepository) Remove(usersBase *domain.UsersBase) (*domain.UsersBase, error) {
tx := repository.transactionContext.PgTx
usersBaseModel := new(models.UsersBase)
usersBaseModel.UsersBaseId = usersBase.Identify().(int64)
if _, err := tx.Model(usersBaseModel).WherePK().Delete(); err != nil {
return usersBase, err
}
return usersBase, nil
}
func (repository *UsersBaseRepository) FindOne(queryOptions map[string]interface{}) (*domain.UsersBase, error) {
tx := repository.transactionContext.PgTx
usersBaseModel := new(models.UsersBase)
query := sqlbuilder.BuildQuery(tx.Model(usersBaseModel), queryOptions)
query.SetWhereByQueryOption("users_base.users_base_id = ?", "usersBaseId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if usersBaseModel.UsersBaseId == 0 {
return nil, nil
} else {
return transform.TransformToUsersBaseDomainModelFromPgModels(usersBaseModel)
}
}
func (repository *UsersBaseRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.UsersBase, error) {
tx := repository.transactionContext.PgTx
var usersBaseModels []*models.UsersBase
usersBases := make([]*domain.UsersBase, 0)
query := sqlbuilder.BuildQuery(tx.Model(&usersBaseModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("users_base_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, usersBases, err
} else {
for _, usersBaseModel := range usersBaseModels {
if usersBase, err := transform.TransformToUsersBaseDomainModelFromPgModels(usersBaseModel); err != nil {
return 0, usersBases, err
} else {
usersBases = append(usersBases, usersBase)
}
}
return int64(count), usersBases, nil
}
}
func NewUsersBaseRepository(transactionContext *pgTransaction.TransactionContext) (*UsersBaseRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &UsersBaseRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform"
)
type UsersRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *UsersRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *UsersRepository) Save(users *domain.Users) (*domain.Users, error) {
sqlBuildFields := []string{
"users_id",
"company_id",
"users_base_id",
"users_type",
"users_code",
"organization_id",
"department_id",
"users_info",
"users_org",
"users_role",
"favorite_menus",
"cooperation_info",
"enable_status",
"ext",
"created_at",
"updated_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "users_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if users.Identify() == nil {
usersId, err := repository.nextIdentify()
if err != nil {
return users, err
} else {
users.UsersId = usersId
}
if _, err := tx.QueryOne(
pg.Scan(
&users.UsersId,
&users.CompanyId,
&users.UsersBaseId,
&users.UsersType,
&users.UsersCode,
&users.OrganizationId,
&users.DepartmentId,
pg.Array(&users.UsersInfo),
pg.Array(&users.UsersOrg),
pg.Array(&users.UsersRole),
pg.Array(&users.FavoriteMenus),
&users.CooperationInfo,
&users.EnableStatus,
&users.Ext,
&users.CreatedAt,
&users.UpdatedAt,
),
fmt.Sprintf("INSERT INTO userss (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
users.UsersId,
users.CompanyId,
users.UsersBaseId,
users.UsersType,
users.UsersCode,
users.OrganizationId,
users.DepartmentId,
pg.Array(users.UsersInfo),
pg.Array(users.UsersOrg),
pg.Array(users.UsersRole),
pg.Array(users.FavoriteMenus),
users.CooperationInfo,
users.EnableStatus,
users.Ext,
users.CreatedAt,
users.UpdatedAt,
); err != nil {
return users, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&users.UsersId,
&users.CompanyId,
&users.UsersBaseId,
&users.UsersType,
&users.UsersCode,
&users.OrganizationId,
&users.DepartmentId,
pg.Array(&users.UsersInfo),
pg.Array(&users.UsersOrg),
pg.Array(&users.UsersRole),
pg.Array(&users.FavoriteMenus),
&users.CooperationInfo,
&users.EnableStatus,
&users.Ext,
&users.CreatedAt,
&users.UpdatedAt,
),
fmt.Sprintf("UPDATE userss SET %s WHERE users_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
users.CompanyId,
users.UsersBaseId,
users.UsersType,
users.UsersCode,
users.OrganizationId,
users.DepartmentId,
pg.Array(users.UsersInfo),
pg.Array(users.UsersOrg),
pg.Array(users.UsersRole),
pg.Array(users.FavoriteMenus),
users.CooperationInfo,
users.EnableStatus,
users.Ext,
users.CreatedAt,
users.UpdatedAt,
users.Identify(),
); err != nil {
return users, err
}
}
return users, nil
}
func (repository *UsersRepository) Remove(users *domain.Users) (*domain.Users, error) {
tx := repository.transactionContext.PgTx
usersModel := new(models.Users)
usersModel.UsersId = users.Identify().(int64)
if _, err := tx.Model(usersModel).WherePK().Delete(); err != nil {
return users, err
}
return users, nil
}
func (repository *UsersRepository) FindOne(queryOptions map[string]interface{}) (*domain.Users, error) {
tx := repository.transactionContext.PgTx
usersModel := new(models.Users)
query := sqlbuilder.BuildQuery(tx.Model(usersModel), queryOptions)
query.SetWhereByQueryOption("users.users_id = ?", "usersId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if usersModel.UsersId == 0 {
return nil, nil
} else {
return transform.TransformToUsersDomainModelFromPgModels(usersModel)
}
}
func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Users, error) {
tx := repository.transactionContext.PgTx
var usersModels []*models.Users
userss := make([]*domain.Users, 0)
query := sqlbuilder.BuildQuery(tx.Model(&usersModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("users_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, userss, err
} else {
for _, usersModel := range usersModels {
if users, err := transform.TransformToUsersDomainModelFromPgModels(usersModel); err != nil {
return 0, userss, err
} else {
userss = append(userss, users)
}
}
return int64(count), userss, nil
}
}
func NewUsersRepository(transactionContext *pgTransaction.TransactionContext) (*UsersRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &UsersRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...