Browse Source

update

tags/v0.0.1
dengbiao 2 months ago
parent
commit
88cbb855e3
100 changed files with 2904 additions and 0 deletions
  1. +46
    -0
      .gitignore
  2. +45
    -0
      README.md
  3. +44
    -0
      src/dao/admin_dao.go
  4. +25
    -0
      src/dao/admin_role_dao.go
  5. +13
    -0
      src/dao/agent_dao.go
  6. +5
    -0
      src/dao/agent_domain_dao.go
  7. +5
    -0
      src/dao/agent_permission_dao.go
  8. +8
    -0
      src/dao/agent_permission_group_dao.go
  9. +5
    -0
      src/dao/agent_permission_group_permission_dao.go
  10. +25
    -0
      src/dao/agent_role_dao.go
  11. +11
    -0
      src/dao/agent_role_permission_group_dao.go
  12. +16
    -0
      src/dao/agent_with_role_dao.go
  13. +13
    -0
      src/dao/medium_dao.go
  14. +10
    -0
      src/dao/medium_domain_dao.go
  15. +5
    -0
      src/dao/medium_permission_dao.go
  16. +8
    -0
      src/dao/medium_permission_group_dao.go
  17. +5
    -0
      src/dao/medium_permission_group_permission_dao.go
  18. +25
    -0
      src/dao/medium_role_dao.go
  19. +11
    -0
      src/dao/medium_role_permission_group_dao.go
  20. +16
    -0
      src/dao/medium_with_role_dao.go
  21. +5
    -0
      src/dao/permission_dao.go
  22. +8
    -0
      src/dao/permission_group_dao.go
  23. +5
    -0
      src/dao/permission_group_permission_dao.go
  24. +25
    -0
      src/dao/role_dao.go
  25. +11
    -0
      src/dao/role_permission_group_dao.go
  26. +14
    -0
      src/dao/sys_cfg_dao.go
  27. +112
    -0
      src/db.go
  28. +82
    -0
      src/db_sys_cfg.go
  29. +104
    -0
      src/dbs.go
  30. +194
    -0
      src/dbs_map.go
  31. +115
    -0
      src/implement/admin_implement.go
  32. +77
    -0
      src/implement/admin_role_implement.go
  33. +14
    -0
      src/implement/agent_domain_implement.go
  34. +53
    -0
      src/implement/agent_implement.go
  35. +33
    -0
      src/implement/agent_permission_group_implement.go
  36. +14
    -0
      src/implement/agent_permission_group_permission_implement.go
  37. +14
    -0
      src/implement/agent_permission_implement.go
  38. +69
    -0
      src/implement/agent_role_implement.go
  39. +32
    -0
      src/implement/agent_role_permission_group_implement.go
  40. +77
    -0
      src/implement/agent_with_role_implement.go
  41. +36
    -0
      src/implement/medium_domain_implement.go
  42. +53
    -0
      src/implement/medium_implement.go
  43. +33
    -0
      src/implement/medium_permission_group_implement.go
  44. +14
    -0
      src/implement/medium_permission_group_permission_implement.go
  45. +14
    -0
      src/implement/medium_permission_implement.go
  46. +69
    -0
      src/implement/medium_role_implement.go
  47. +32
    -0
      src/implement/medium_role_permission_group_implement.go
  48. +77
    -0
      src/implement/medium_with_role_implement.go
  49. +33
    -0
      src/implement/permission_group_implement.go
  50. +14
    -0
      src/implement/permission_group_permission_implement.go
  51. +14
    -0
      src/implement/permission_implement.go
  52. +73
    -0
      src/implement/role_implement.go
  53. +32
    -0
      src/implement/role_permission_group_implement.go
  54. +124
    -0
      src/implement/sys_cfg_implement.go
  55. +13
    -0
      src/model/admin.go
  56. +10
    -0
      src/model/admin_role.go
  57. +13
    -0
      src/model/agent.go
  58. +8
    -0
      src/model/agent_domain.go
  59. +10
    -0
      src/model/agent_permission.go
  60. +11
    -0
      src/model/agent_permission_group.go
  61. +9
    -0
      src/model/agent_permission_group_permission.go
  62. +10
    -0
      src/model/agent_role.go
  63. +9
    -0
      src/model/agent_role_permission_group.go
  64. +10
    -0
      src/model/agent_with_role.go
  65. +14
    -0
      src/model/db_mapping.go
  66. +13
    -0
      src/model/medium.go
  67. +8
    -0
      src/model/medium_domain.go
  68. +10
    -0
      src/model/medium_permission.go
  69. +11
    -0
      src/model/medium_permission_group.go
  70. +9
    -0
      src/model/medium_permission_group_permission.go
  71. +10
    -0
      src/model/medium_role.go
  72. +9
    -0
      src/model/medium_role_permission_group.go
  73. +10
    -0
      src/model/medium_with_role.go
  74. +10
    -0
      src/model/permission.go
  75. +11
    -0
      src/model/permission_group.go
  76. +9
    -0
      src/model/permission_group_permission.go
  77. +10
    -0
      src/model/role.go
  78. +9
    -0
      src/model/role_permission_group.go
  79. +7
    -0
      src/model/sys_cfg.go
  80. +44
    -0
      src/super/dao/admin_dao.go
  81. +25
    -0
      src/super/dao/admin_role_dao.go
  82. +9
    -0
      src/super/dao/agent_list_dao.go
  83. +9
    -0
      src/super/dao/medium_list_dao.go
  84. +5
    -0
      src/super/dao/permission_dao.go
  85. +8
    -0
      src/super/dao/permission_group_dao.go
  86. +5
    -0
      src/super/dao/permission_group_permission_dao.go
  87. +25
    -0
      src/super/dao/role_dao.go
  88. +11
    -0
      src/super/dao/role_permission_group_dao.go
  89. +115
    -0
      src/super/implement/admin_implement.go
  90. +77
    -0
      src/super/implement/admin_role_implement.go
  91. +44
    -0
      src/super/implement/agent_list_implement.go
  92. +44
    -0
      src/super/implement/medium_list_implement.go
  93. +33
    -0
      src/super/implement/permission_group_implement.go
  94. +14
    -0
      src/super/implement/permission_group_permission_implement.go
  95. +14
    -0
      src/super/implement/permission_implement.go
  96. +73
    -0
      src/super/implement/role_implement.go
  97. +32
    -0
      src/super/implement/role_permission_group_implement.go
  98. +12
    -0
      src/super/model/admin.go
  99. +10
    -0
      src/super/model/admin_role.go
  100. +25
    -0
      src/super/model/agent_list.go

+ 46
- 0
.gitignore View File

@@ -0,0 +1,46 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
.idea
.vscode
*.log
.DS_Store
Thumbs.db
*.swp
*.swn
*.swo
*.swm
*.7z
*.zip
*.rar
*.tar
*.tar.gz
go.sum
/etc/cfg.yaml
images
test/test.json
etc/cfg.yml
t.json
t1.json
t2.json
t3.json
t.go
wait-for-it.sh
test.go
xorm
test.csv
nginx.conf
.devcontainer
.devcontainer/Dockerfile
.devcontainer/sources.list
/t1.go
/tmp/*
.idea/*
/.idea/modules.xml

+ 45
- 0
README.md View File

@@ -0,0 +1,45 @@
# 智莺生活 mysql 模型库

## 项目概述
集成智莺生活Saas项目所用到的所有数据库、表,并提供类ORM操作类。

-------------

## 项目结构
如下:

1. **文档**
- `README.md` - 项目的入口点和概述。
- `LICENSE` - 项目的许可证。

2. **源代码**
- `src` - 主代码目录。
- `dao` - dao包(接口类)。
- `models` - mysql表模型。
- `implement` - 实现包(实现所有接口)。

3. **测试用例**
- `****_test.go` - 测试对应orm类功能。
- `md` - 测试用例所需结构体
- `tmp` - 运行临时日志

4. **工具类**
- `logx` - 日志输出用到。
- `*****.go` - 其他常用工具函数。

5. **配置文件**
- `etc`
- `db_tpl` - 数据库模板文件
- `cfg.yml` - 配置文件。

6. **脚手架脚本**
- `cmd_db.bat` - 作用于 windows 系统自动生成models目录下的模型
- `cmd_db.sh` - 作用于 linux 系统自动生成models目录下的模型

## 安装和运行
确保安装了所需的依赖,然后运行以下命令:

```bash
.\cmd_db.bat hello_world ---> 自动生成模型类
.\cmd_dao.bat hello_world ---> 自动生成接口类
.\cmd_implement.bat hello_world ---> 自动生成实现类

+ 44
- 0
src/dao/admin_dao.go View File

@@ -0,0 +1,44 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type AdminDao interface {
CreateAdminId() (admId int, err error)
AdminDeleteBySession(session *xorm.Session, admId interface{}) (int64, error)
GetAdmin(id int) (m *model.Admin, err error)
GetAdminByUserName(userName string) (m *model.Admin, err error)
GetAdminRolePermission(admId int) (list []*AdminRolePermission, total int64, err error)
FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error)
UpdateAdmin(m *model.Admin, columns ...string) (int64, error)
AdminInsert(m *model.Admin) (int64, error)
FindAdminRolePermissionGroup(admId int) (list []*AdminRolePermissionGroup, total int64, err error)
}

type AdminRolePermissionGroup struct {
model.Admin `xorm:"extends"`
model.AdminRole `xorm:"extends"`
model.Role `xorm:"extends"`
model.RolePermissionGroup `xorm:"extends"`
model.PermissionGroup `xorm:"extends"`
}

func (AdminRolePermissionGroup) TableName() string {
return "admin"
}

type AdminRolePermission struct {
model.Admin `xorm:"extends"`
model.AdminRole `xorm:"extends"`
model.Role `xorm:"extends"`
model.RolePermissionGroup `xorm:"extends"`
model.PermissionGroup `xorm:"extends"`
model.PermissionGroupPermission `xorm:"extends"`
model.Permission `xorm:"extends"`
}

func (AdminRolePermission) TableName() string {
return "admin"
}

+ 25
- 0
src/dao/admin_role_dao.go View File

@@ -0,0 +1,25 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type AdminRoleDao interface {
FindAdminRole(id int) (*[]model.AdminRole, error)
FindAdminRoleByRoleId(id int) (*[]model.AdminRole, error)
AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error)
GetAdminRoleByRole(id int) (m *model.AdminRole, err error)
AdminRoleDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error)
AdminRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
BatchAddAdminRoleBySession(session *xorm.Session, mm []*model.AdminRole) (int64, error)
}

type AdminRole struct {
model.Admin `xorm:"extends"`
model.AdminRole `xorm:"extends"`
}

func (AdminRole) TableName() string {
return "admin_role"
}

+ 13
- 0
src/dao/agent_dao.go View File

@@ -0,0 +1,13 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type AgentDao interface {
GetAgentByUsername(username string) (medium *model.Agent, err error)
AgentInsert(m *model.Agent) (int64, error)
AgentDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
UpdateAgent(m *model.Agent, columns ...string) (int64, error)
}

+ 5
- 0
src/dao/agent_domain_dao.go View File

@@ -0,0 +1,5 @@
package dao

type AgentDomainDao interface {
//TODO:: You can add specific method definitions here
}

+ 5
- 0
src/dao/agent_permission_dao.go View File

@@ -0,0 +1,5 @@
package dao

type AgentPermissionDao interface {
//TODO:: You can add specific method definitions here
}

+ 8
- 0
src/dao/agent_permission_group_dao.go View File

@@ -0,0 +1,8 @@
package dao

import "applet/app/db/model"

type AgentPermissionGroupDao interface {
FindAgentPermissionGroup() (*[]model.AgentPermissionGroup, error)
FindAgentPermissionGroupV2() (*[]model.AgentPermissionGroup, error)
}

+ 5
- 0
src/dao/agent_permission_group_permission_dao.go View File

@@ -0,0 +1,5 @@
package dao

type AgentPermissionGroupPermissionDao interface {
//TODO:: You can add specific method definitions here
}

+ 25
- 0
src/dao/agent_role_dao.go View File

@@ -0,0 +1,25 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type AgentRoleDao interface {
FindAgentRole() (*[]model.AgentRole, error)
GetAgentRole() (m *model.AgentRole, err error)
UpdateAgentRole(m *model.AgentRole, columns ...string) (int64, error)
AgentRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
AgentRoleInsert(m *model.AgentRole) (int, error)
FindPermissionGroupByAgentRole(roleId int) (list []*AgentRolePermissionGroup, total int64, err error)
}

type AgentRolePermissionGroup struct {
model.AgentRole `xorm:"extends"`
model.AgentRolePermissionGroup `xorm:"extends"`
model.AgentPermissionGroup `xorm:"extends"`
}

func (AgentRolePermissionGroup) TableName() string {
return "agent_role"
}

+ 11
- 0
src/dao/agent_role_permission_group_dao.go View File

@@ -0,0 +1,11 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type AgentRolePermissionGroupDao interface {
AgentRolePermissionGroupDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error)
BatchAddAgentRolePermissionGroupBySession(session *xorm.Session, mm []*model.AgentRolePermissionGroup) (int64, error)
}

+ 16
- 0
src/dao/agent_with_role_dao.go View File

@@ -0,0 +1,16 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type AgentWithRoleDao interface {
FindAgentWithRole(id int) (*[]model.AgentWithRole, error)
FindAgentWithRoleByRoleId(id int) (*[]model.AgentWithRole, error)
AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error)
GetAgentWithRoleByRole(id int) (m *model.AgentWithRole, err error)
AgentWithRoleDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error)
AgentWithRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
BatchAddAgentWithRoleBySession(session *xorm.Session, mm []*model.AgentWithRole) (int64, error)
}

+ 13
- 0
src/dao/medium_dao.go View File

@@ -0,0 +1,13 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type MediumDao interface {
GetMediumByUsername(username string) (medium *model.Medium, err error)
MediumInsert(m *model.Medium) (int64, error)
MediumDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
UpdateMedium(m *model.Medium, columns ...string) (int64, error)
}

+ 10
- 0
src/dao/medium_domain_dao.go View File

@@ -0,0 +1,10 @@
package dao

import (
"applet/app/db/model"
)

type MediumDomainDao interface {
GetMediumDomain(mediumId int) (medium *model.MediumDomain, err error)
MediumDomainInsert(m *model.MediumDomain) (int64, error)
}

+ 5
- 0
src/dao/medium_permission_dao.go View File

@@ -0,0 +1,5 @@
package dao

type MediumPermissionDao interface {
//TODO:: You can add specific method definitions here
}

+ 8
- 0
src/dao/medium_permission_group_dao.go View File

@@ -0,0 +1,8 @@
package dao

import "applet/app/db/model"

type MediumPermissionGroupDao interface {
FindMediumPermissionGroup() (*[]model.MediumPermissionGroup, error)
FindMediumPermissionGroupV2() (*[]model.MediumPermissionGroup, error)
}

+ 5
- 0
src/dao/medium_permission_group_permission_dao.go View File

@@ -0,0 +1,5 @@
package dao

type MediumPermissionGroupPermissionDao interface {
//TODO:: You can add specific method definitions here
}

+ 25
- 0
src/dao/medium_role_dao.go View File

@@ -0,0 +1,25 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type MediumRoleDao interface {
FindMediumRole() (*[]model.MediumRole, error)
GetMediumRole() (m *model.MediumRole, err error)
UpdateMediumRole(m *model.MediumRole, columns ...string) (int64, error)
MediumRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
MediumRoleInsert(m *model.MediumRole) (int, error)
FindPermissionGroupByMediumRole(roleId int) (list []*MediumRolePermissionGroup, total int64, err error)
}

type MediumRolePermissionGroup struct {
model.MediumRole `xorm:"extends"`
model.MediumRolePermissionGroup `xorm:"extends"`
model.MediumPermissionGroup `xorm:"extends"`
}

func (MediumRolePermissionGroup) TableName() string {
return "medium_role"
}

+ 11
- 0
src/dao/medium_role_permission_group_dao.go View File

@@ -0,0 +1,11 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type MediumRolePermissionGroupDao interface {
MediumRolePermissionGroupDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error)
BatchAddMediumRolePermissionGroupBySession(session *xorm.Session, mm []*model.MediumRolePermissionGroup) (int64, error)
}

+ 16
- 0
src/dao/medium_with_role_dao.go View File

@@ -0,0 +1,16 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type MediumWithRoleDao interface {
FindMediumWithRole(id int) (*[]model.MediumWithRole, error)
FindMediumWithRoleByRoleId(id int) (*[]model.MediumWithRole, error)
AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error)
GetMediumWithRoleByRole(id int) (m *model.MediumWithRole, err error)
MediumWithRoleDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error)
MediumWithRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
BatchAddMediumWithRoleBySession(session *xorm.Session, mm []*model.MediumWithRole) (int64, error)
}

+ 5
- 0
src/dao/permission_dao.go View File

@@ -0,0 +1,5 @@
package dao

type PermissionDao interface {
//TODO:: You can add specific method definitions here
}

+ 8
- 0
src/dao/permission_group_dao.go View File

@@ -0,0 +1,8 @@
package dao

import "applet/app/db/model"

type PermissionGroupDao interface {
FindPermissionGroup() (*[]model.PermissionGroup, error)
FindPermissionGroupV2() (*[]model.PermissionGroup, error)
}

+ 5
- 0
src/dao/permission_group_permission_dao.go View File

@@ -0,0 +1,5 @@
package dao

type PermissionGroupPermissionDao interface {
//TODO:: You can add specific method definitions here
}

+ 25
- 0
src/dao/role_dao.go View File

@@ -0,0 +1,25 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type RoleDao interface {
FindRole() (*[]model.Role, error)
GetRole() (m *model.Role, err error)
UpdateRole(m *model.Role, columns ...string) (int64, error)
RoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
RoleInsert(m *model.Role) (int, error)
FindPermissionGroupByRole(roleId int) (list []*RolePermissionGroup, total int64, err error)
}

type RolePermissionGroup struct {
model.Role `xorm:"extends"`
model.RolePermissionGroup `xorm:"extends"`
model.PermissionGroup `xorm:"extends"`
}

func (RolePermissionGroup) TableName() string {
return "role"
}

+ 11
- 0
src/dao/role_permission_group_dao.go View File

@@ -0,0 +1,11 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type RolePermissionGroupDao interface {
RolePermissionGroupDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error)
BatchAddRolePermissionGroupBySession(session *xorm.Session, mm []*model.RolePermissionGroup) (int64, error)
}

+ 14
- 0
src/dao/sys_cfg_dao.go View File

@@ -0,0 +1,14 @@
package dao

import "applet/app/db/model"

type SysCfgDao interface {
SysCfgGetAll() (*[]model.SysCfg, error)
SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error)
SysCfgGetOne(key string) (*model.SysCfg, error)
SysCfgInsert(key, val, memo string) bool
SysCfgUpdate(key, val string) bool
SysCfgGetWithDb(HKey string) string
SysCfgDel(HKey string) error
SysCfgFindWithDb(keys ...string) map[string]string
}

+ 112
- 0
src/db.go View File

@@ -0,0 +1,112 @@
package db

import (
"database/sql"
"fmt"
"os"
"time"

_ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic
"xorm.io/xorm"
"xorm.io/xorm/log"

"applet/app/cfg"
"applet/app/utils/logx"
)

var Db *xorm.Engine

//根据DB配置文件初始化数据库
func InitDB(c *cfg.DBCfg) error {
var (
err error
f *os.File
)
//创建Orm引擎
if Db, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", c.User, c.Psw, c.Host, c.Name)); err != nil {
return err
}
Db.SetConnMaxLifetime(c.MaxLifetime * time.Second) //设置最长连接时间
Db.SetMaxOpenConns(c.MaxOpenConns) //设置最大打开连接数
Db.SetMaxIdleConns(c.MaxIdleConns) //设置连接池的空闲数大小
if err = Db.Ping(); err != nil { //尝试ping数据库
return err
}
if c.ShowLog { //根据配置文件设置日志
Db.ShowSQL(true) //设置是否打印sql
Db.Logger().SetLevel(0) //设置日志等级
//修改日志文件存放路径文件名是%s.log
path := fmt.Sprintf(c.Path, c.Name)
f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
os.RemoveAll(c.Path)
if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil {
return err
}
}
logger := log.NewSimpleLogger(f)
logger.ShowSQL(true)
Db.SetLogger(logger)
}
return nil
}

/********************************************* 公用方法 *********************************************/

// 数据批量插入
func DbInsertBatch(Db *xorm.Engine, m ...interface{}) error {
if len(m) == 0 {
return nil
}
id, err := Db.Insert(m...)
if id == 0 || err != nil {
return logx.Warn("cannot insert data :", err)
}
return nil
}

// QueryNativeString 查询原生sql
func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) {
results, err := Db.SQL(sql, args...).QueryString()
return results, err
}

// UpdateComm common update
func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
row, err := Db.ID(id).Update(model)
return row, err
}

// InsertComm common insert
func InsertComm(Db *xorm.Engine, model interface{}) (int64, error) {
row, err := Db.InsertOne(model)
return row, err
}

// ExecuteOriginalSql 执行原生sql
func ExecuteOriginalSql(session *xorm.Session, sql string) (sql.Result, error) {
result, err := session.Exec(sql)
if err != nil {
_ = logx.Warn(err)
return nil, err
}
return result, nil
}

// GetComm
// payload *model
// return *model,has,err
func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
has, err := Db.Get(model)
if err != nil {
_ = logx.Warn(err)
return nil, false, err
}
return model, has, nil
}

// InsertCommWithSession common insert
func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
row, err := session.InsertOne(model)
return row, err
}

+ 82
- 0
src/db_sys_cfg.go View File

@@ -0,0 +1,82 @@
package db

import (
"applet/app/db/model"
"applet/app/md"
"applet/app/utils/cache"
"applet/app/utils/logx"
"fmt"
"xorm.io/xorm"
)

// 系统配置get
func SysCfgGetAll(Db *xorm.Engine) (*[]model.SysCfg, error) {
var cfgList []model.SysCfg
if err := Db.Cols("key,val,memo").Find(&cfgList); err != nil {
return nil, logx.Error(err)
}
return &cfgList, nil
}

// 获取一条记录
func SysCfgGetOne(Db *xorm.Engine, key string) (*model.SysCfg, error) {
var cfgList model.SysCfg
if has, err := Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
return nil, logx.Error(err)
}
return &cfgList, nil
}

// 返回最后插入id
func SysCfgInsert(Db *xorm.Engine, key, val, memo string) bool {
cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
_, err := Db.InsertOne(&cfg)
if err != nil {
logx.Error(err)
return false
}
return true
}

func SysCfgUpdate(Db *xorm.Engine, key, val, memo string) bool {
cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
_, err := Db.Where("`key`=?", key).Cols("val,memo").Update(&cfg)
if err != nil {
logx.Error(err)
return false
}
return true
}
func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string {
cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId) + HKey
get, err := cache.GetString(cacheKey)
if err != nil || get == "" {
cfg, err := SysCfgGetOne(eg, HKey)
if err != nil || cfg == nil {
_ = logx.Error(err)
return ""
}

// key是否存在
cacheKeyExist := false
if cache.Exists(cacheKey) {
cacheKeyExist = true
}

// 设置缓存
_, err = cache.SetEx(cacheKey, cfg.Val, 30)
if err != nil {
_ = logx.Error(err)
return ""
}
if !cacheKeyExist { // 如果是首次设置 设置过期时间
_, err := cache.Expire(cacheKey, md.CfgCacheTime)
if err != nil {
_ = logx.Error(err)
return ""
}
}
return cfg.Val
}
return get
}

+ 104
- 0
src/dbs.go View File

@@ -0,0 +1,104 @@
package db

import (
"fmt"
"os"
"time"

"xorm.io/xorm"
"xorm.io/xorm/log"

"applet/app/cfg"
"applet/app/db/model"
"applet/app/utils/logx"
)

var DBs map[string]*xorm.Engine

// 每个站长都要有自己的syscfg 缓存, 键是站长id,值是缓存名
// var SysCfgMapKey map[string]string

func NewDB(c *cfg.DBCfg) (*xorm.Engine, error) {
db, err := xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", c.User, c.Psw, c.Host, c.Name))
if err != nil {
return nil, err
}
db.SetConnMaxLifetime(c.MaxLifetime * time.Second)
db.SetMaxOpenConns(c.MaxOpenConns)
db.SetMaxIdleConns(c.MaxIdleConns)
err = db.Ping()
if err != nil {
return nil, err
}
if c.ShowLog {
db.ShowSQL(true)
db.Logger().SetLevel(log.LOG_DEBUG)
f, err := os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
os.RemoveAll(c.Path)
if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil {
return nil, err
}
}
logger := log.NewSimpleLogger(f)
logger.ShowSQL(true)
db.SetLogger(logger)
}
return db, nil
}

// InitDBs is 初始化多数据库
func InitDBs(ch chan int) {
// 初始化多数据库
var tables *[]model.DbMapping
InitMapDbs(cfg.DB, cfg.Prd)
ch <- 1
// 每10s 查询一次模板数据库的db mapping 表,如果有新增数据库记录,则添加到 DBs中
ticker := time.NewTicker(time.Duration(time.Second * 120))
for range ticker.C {
if cfg.Prd {
tables = GetAllDatabasePrd() //默认获取全部
} else {
tables = GetAllDatabaseDev() //默认获取全部
}
if tables == nil {
logx.Warn("no database tables data")
continue
}
for _, item := range *tables {
_, ok := DBs[item.DbMasterId]
if !ok {
// 不在db.DBs 则添加进去
dbCfg := cfg.DBCfg{
Name: item.DbName,
ShowLog: cfg.DB.ShowLog,
MaxLifetime: cfg.DB.MaxLifetime,
MaxOpenConns: cfg.DB.MaxOpenConns,
MaxIdleConns: cfg.DB.MaxIdleConns,
Path: fmt.Sprintf(cfg.DB.Path, item.DbName),
}
if item.DbHost != "" && item.DbUsername != "" && item.DbPassword != "" {
dbCfg.Host = item.DbHost
dbCfg.User = item.DbUsername
dbCfg.Psw = item.DbPassword
} else {
dbCfg.Host = cfg.DB.Host
dbCfg.User = cfg.DB.User
dbCfg.Psw = cfg.DB.Psw
}
e, err := NewDB(&dbCfg)
if err != nil || e == nil {
logx.Warnf("db engine can't create, please check config, params[host:%s, user:%s, psw: %s, name: %s], err: %v", dbCfg.Host, dbCfg.User, dbCfg.Psw, dbCfg.Name, err)
} else {
DBs[item.DbMasterId] = e
}
}
// 如果 被禁用则删除
if item.DeletedAt == 1 {
logx.Infof("%s have been removed", item.DbMasterId)
delete(DBs, item.DbMasterId)
}
}
}

}

+ 194
- 0
src/dbs_map.go View File

@@ -0,0 +1,194 @@
package db

import (
"errors"
"fmt"

"xorm.io/xorm"

"applet/app/cfg"
"applet/app/db/model"
"applet/app/utils/logx"
)

func MapBaseExists() (bool, error) {
return Db.IsTableExist("db_mapping")
}

func InitMapDbs(c *cfg.DBCfg, prd bool) {
var tables *[]model.DbMapping
exists, err := MapBaseExists()
if !exists || err != nil {
logx.Fatalf("db_mapping not exists : %v", err)
}
// tables := MapAllDatabases(debug)
if prd {
tables = GetAllDatabasePrd() //debug 获取生产
} else {
tables = GetAllDatabaseDev() //debug 获取开发
}

if tables == nil {
logx.Fatal("no database tables data")
}
var e *xorm.Engine
DBs = map[string]*xorm.Engine{}
for _, v := range *tables {
if v.DbName != "" && v.DeletedAt == 0 && v.DbName != c.Name {
dbCfg := cfg.DBCfg{
Name: v.DbName,
ShowLog: c.ShowLog,
MaxLifetime: c.MaxLifetime,
MaxOpenConns: c.MaxOpenConns,
MaxIdleConns: c.MaxIdleConns,
Path: fmt.Sprintf(c.Path, v.DbName),
}
if v.DbHost != "" && v.DbUsername != "" && v.DbPassword != "" {
dbCfg.Host = v.DbHost
dbCfg.User = v.DbUsername
dbCfg.Psw = v.DbPassword
} else {
dbCfg.Host = c.Host
dbCfg.User = c.User
dbCfg.Psw = c.Psw
}
e, err = NewDB(&dbCfg)
if err != nil || e == nil {
logx.Warnf("db engine can't create, please check config, params[host:%s, user:%s, psw: %s, name: %s], err: %v", dbCfg.Host, dbCfg.User, dbCfg.Psw, dbCfg.Name, err)
} else {
DBs[v.DbMasterId] = e
}
}
}
}

func MapAllDatabases(debug bool) *[]model.DbMapping {
sql := "`db_name` != ?"
if debug {
sql = "`db_name` = ?"
}
var m []model.DbMapping
if err := Db.Where(sql, cfg.DB.Name).Find(&m); err != nil || len(m) == 0 {
logx.Warn(err)
return nil
}
return &m
}

// GetAllDatabasePrd is 获取生成库 所有db 除了 deleted_at = 1 的
func GetAllDatabasePrd() *[]model.DbMapping {
var m []model.DbMapping
if err := Db.Where("deleted_at != ? AND is_dev = '0' ", 1).Find(&m); err != nil || len(m) == 0 {
logx.Warn(err)
return nil
}
return &m
}

// GetAllDatabaseDev is 获取开发库 所有db 除了 deleted_at = 1 的
func GetAllDatabaseDev() *[]model.DbMapping {
var m []model.DbMapping
var err error
fmt.Println("cfg.Local is: ", cfg.Local)
if cfg.Local { // 本地调试 加快速度
fmt.Println("notice:LOCAL TEST, only masterId:** 123456 ** available!")
err = Db.Where("deleted_at != ? AND is_dev = '1' AND db_master_id=?", 1, 123456).Find(&m)
} else {
err = Db.Where("deleted_at != ? AND is_dev = '1' ", 1).Find(&m)
}

//err := Db.Where("deleted_at != ? AND is_dev = '1' and db_master_id='123456'", 1).Find(&m)
if err != nil || len(m) == 0 {
logx.Warn(err)
return nil
}
return &m
}

//GetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
func GetDatabaseByMasterID(Db *xorm.Engine, id string) (*model.DbMapping, error) {
var m model.DbMapping
has, err := Db.Where("db_master_id=?", id).Get(&m)
if !has {
return nil, errors.New("Not Found DB data by " + id)
}
if err != nil {
return nil, err
}
if m.DbHost == "" {
m.DbHost = cfg.DB.Host
m.DbUsername = cfg.DB.User
m.DbPassword = cfg.DB.Psw
}
return &m, nil
}

//SessionGetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
func SessionGetDatabaseByMasterID(Db *xorm.Session, id string) (*model.DbMapping, error) {
var m model.DbMapping
has, err := Db.Where("db_master_id=?", id).Get(&m)
if !has {
return nil, errors.New("Not Found DB data by " + id)
}
if err != nil {
return nil, err
}
if m.DbHost == "" {
m.DbHost = cfg.DB.Host
m.DbName = cfg.DB.Name
m.DbUsername = cfg.DB.User
m.DbPassword = cfg.DB.Psw
}
return &m, nil
}

// 获取自动任务队列
func MapCrontabCfg(eg *xorm.Engine) *[]model.SysCfg {
var c []model.SysCfg
// 数据库查询如果有下划线会认为是一个任意字符
if err := eg.Where("`key` LIKE 'mall_cron\\_%' AND val != ''").Cols("`key`,`val`").Find(&c); err != nil || len(c) == 0 {
logx.Warn(err)
return nil
}
return &c
}

// 获取官方域名
func GetOfficialDomainInfoByType(Db *xorm.Engine, masterId, key string) (string, error) {
type SysCfg struct {
K string
V string
Memo string
}
var domainBase SysCfg

has, err := Db.Where("k=?", "domain_base").Get(&domainBase)
if err != nil {
return "", err
}
if has == false {
return "", errors.New("can not find key by : domain_base")
}

if key == "wap" {
return "h5." + masterId + "." + domainBase.V, nil
}

if key == "api" {
var apiDomain SysCfg
has, err = Db.Where("k=?", "domain_api_base").Get(&apiDomain)
if err != nil {
return "", err
}
if has == false {
return "", errors.New("can not find key by : domain_api_base")
}
return apiDomain.V, nil
}

if key == "admin" {
return "admin." + masterId + "." + domainBase.V, nil
}
// 默认返回H5的
return "h5." + masterId + "." + domainBase.V, nil
}

+ 115
- 0
src/implement/admin_implement.go View File

@@ -0,0 +1,115 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewAdminDb(engine *xorm.Engine) dao.AdminDao {
return &AdminDb{Db: engine}
}

type AdminDb struct {
Db *xorm.Engine
}

func (a AdminDb) CreateAdminId() (admId int, err error) {
m := new(model.Admin)
has, err := a.Db.Desc("adm_id").Get(m)
if err != nil {
return 0, logx.Error(err)
}
if has == false {
return 0, nil
}
admId = m.AdmId + 1
return admId, nil
}

func (a AdminDb) AdminDeleteBySession(session *xorm.Session, admId interface{}) (int64, error) {
if reflect.TypeOf(admId).Kind() == reflect.Slice {
return session.In("adm_id", admId).Delete(model.Admin{})
} else {
return session.Where("adm_id = ?", admId).Delete(model.Admin{})
}
}

func (a AdminDb) GetAdmin(id int) (m *model.Admin, err error) {
m = new(model.Admin)
has, err := a.Db.Where("adm_id =?", id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (a AdminDb) GetAdminByUserName(userName string) (m *model.Admin, err error) {
m = new(model.Admin)
has, err := a.Db.Where("username =?", userName).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (a AdminDb) GetAdminRolePermission(admId int) (list []*dao.AdminRolePermission, total int64, err error) {
total, err = a.Db.Where("admin.adm_id =?", admId).
Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
Join("LEFT", "role", "admin_role.role_id = role.id").
Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
Join("LEFT", "permission_group_permission", "permission_group.id = permission_group_permission.group_id").
Join("LEFT", "permission", "permission_group_permission.permission_id = permission.id").
FindAndCount(&list)
return
}

func (a AdminDb) FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error) {
sess := a.Db.Desc("adm_id").Limit(limit, (page-1)*limit)
if username != "" {
sess.And("username like ?", "%"+username+"%")
}
if state != 0 {
sess.And("state = ?", state)
}
total, err = sess.FindAndCount(&list)
if err != nil {
return nil, 0, err
}
return
}

func (a AdminDb) UpdateAdmin(m *model.Admin, columns ...string) (int64, error) {
affected, err := a.Db.Where("adm_id =?", m.AdmId).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

func (a AdminDb) AdminInsert(m *model.Admin) (int64, error) {
insertAffected, err := a.Db.InsertOne(m)
if err != nil {
return 0, err
}
return insertAffected, nil
}

func (a AdminDb) FindAdminRolePermissionGroup(admId int) (list []*dao.AdminRolePermissionGroup, total int64, err error) {
total, err = a.Db.Where("admin.adm_id =?", admId).
Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
Join("LEFT", "role", "admin_role.role_id = role.id").
//Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
//Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
FindAndCount(&list)
return
}

+ 77
- 0
src/implement/admin_role_implement.go View File

@@ -0,0 +1,77 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewAdminRoleDb(engine *xorm.Engine) dao.AdminRoleDao {
return &AdminRoleDb{Db: engine}
}

type AdminRoleDb struct {
Db *xorm.Engine
}

func (a AdminRoleDb) FindAdminRole(id int) (*[]model.AdminRole, error) {
var m []model.AdminRole
if err := a.Db.Where("adm_id =?", id).Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (a AdminRoleDb) FindAdminRoleByRoleId(id int) (*[]model.AdminRole, error) {
var m []model.AdminRole
if err := a.Db.Where("role_id =?", id).Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (a AdminRoleDb) AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error) {
if reflect.TypeOf(admId).Kind() == reflect.Slice {
return session.In("adm_id", admId).Delete(model.AdminRole{})
} else {
return session.Where("adm_id = ?", admId).Delete(model.AdminRole{})
}
}

func (a AdminRoleDb) GetAdminRoleByRole(id int) (m *model.AdminRole, err error) {
m = new(model.AdminRole)
has, err := a.Db.Where("role_id =?", id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (a AdminRoleDb) AdminRoleDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error) {
if reflect.TypeOf(roleId).Kind() == reflect.Slice {
return session.In("role_id", roleId).Delete(model.AdminRole{})
} else {
return session.Where("role_id = ?", roleId).Delete(model.AdminRole{})
}
}

func (a AdminRoleDb) AdminRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("adm_id", id).Delete(model.AdminRole{})
} else {
return session.Where("adm_id = ?", id).Delete(model.AdminRole{})
}
}

func (a AdminRoleDb) BatchAddAdminRoleBySession(session *xorm.Session, mm []*model.AdminRole) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 14
- 0
src/implement/agent_domain_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

func NewAgentDomainDb(engine *xorm.Engine) dao.AgentDomainDao {
return &AgentDomainDb{Db: engine}
}

type AgentDomainDb struct {
Db *xorm.Engine
}

+ 53
- 0
src/implement/agent_implement.go View File

@@ -0,0 +1,53 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewAgentDb(engine *xorm.Engine) dao.AgentDao {
return &AgentDb{Db: engine}
}

type AgentDb struct {
Db *xorm.Engine
}

func (a AgentDb) GetAgentByUsername(username string) (agent *model.Agent, err error) {
agent = new(model.Agent)
has, err := a.Db.Where("username =?", username).Get(agent)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return agent, nil
}

func (a AgentDb) AgentInsert(m *model.Agent) (int64, error) {
insertAffected, err := a.Db.InsertOne(m)
if err != nil {
return 0, err
}
return insertAffected, nil
}

func (a AgentDb) AgentDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.Admin{})
} else {
return session.Where("id = ?", id).Delete(model.Admin{})
}
}

func (a AgentDb) UpdateAgent(m *model.Agent, columns ...string) (int64, error) {
affected, err := a.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

+ 33
- 0
src/implement/agent_permission_group_implement.go View File

@@ -0,0 +1,33 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/enum"
"applet/app/utils/logx"
"xorm.io/xorm"
)

func NewAgentPermissionGroupDb(engine *xorm.Engine) dao.AgentPermissionGroupDao {
return &AgentPermissionGroupDb{Db: engine}
}

type AgentPermissionGroupDb struct {
Db *xorm.Engine
}

func (a AgentPermissionGroupDb) FindAgentPermissionGroup() (*[]model.AgentPermissionGroup, error) {
var mm []model.AgentPermissionGroup
if err := a.Db.Where("state = ?", enum.PermissionGroupStateForNormal).Asc("id").Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

func (a AgentPermissionGroupDb) FindAgentPermissionGroupV2() (*[]model.AgentPermissionGroup, error) {
var mm []model.AgentPermissionGroup
if err := a.Db.Asc("id").Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

+ 14
- 0
src/implement/agent_permission_group_permission_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

func NewAgentPermissionGroupPermissionDb(engine *xorm.Engine) dao.AgentPermissionGroupPermissionDao {
return &AgentPermissionGroupPermissionDb{Db: engine}
}

type AgentPermissionGroupPermissionDb struct {
Db *xorm.Engine
}

+ 14
- 0
src/implement/agent_permission_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

func NewAgentPermissionDb(engine *xorm.Engine) dao.AgentPermissionDao {
return &AgentPermissionDb{Db: engine}
}

type AgentPermissionDb struct {
Db *xorm.Engine
}

+ 69
- 0
src/implement/agent_role_implement.go View File

@@ -0,0 +1,69 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewAgentRoleDb(engine *xorm.Engine) dao.AgentRoleDao {
return &AgentRoleDb{Db: engine}
}

type AgentRoleDb struct {
Db *xorm.Engine
}

func (a AgentRoleDb) FindAgentRole() (*[]model.AgentRole, error) {
var mm []model.AgentRole
if err := a.Db.Desc("id").Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

func (a AgentRoleDb) GetAgentRole() (m *model.AgentRole, err error) {
m = new(model.AgentRole)
has, err := a.Db.Where("id = ?", m.Id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (a AgentRoleDb) UpdateAgentRole(m *model.AgentRole, columns ...string) (int64, error) {
affected, err := a.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

func (a AgentRoleDb) AgentRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.Role{})
} else {
return session.Where("id = ?", id).Delete(model.Role{})
}
}

func (a AgentRoleDb) AgentRoleInsert(m *model.AgentRole) (int, error) {
_, err := a.Db.InsertOne(m)
if err != nil {
return 0, err
}
return m.Id, nil
}

func (a AgentRoleDb) FindPermissionGroupByAgentRole(roleId int) (list []*dao.AgentRolePermissionGroup, total int64, err error) {
total, err = a.Db.Where("agent_role.id =?", roleId).
Join("LEFT", "agent_role_permission_group", "agent_role_permission_group.role_id = agent_role.id").
Join("LEFT", "agent_permission_group", "agent_permission_group.id = agent_role_permission_group.group_id").
FindAndCount(&list)
return
}

+ 32
- 0
src/implement/agent_role_permission_group_implement.go View File

@@ -0,0 +1,32 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"reflect"
"xorm.io/xorm"
)

func NewAgentRolePermissionGroupDb(engine *xorm.Engine) dao.AgentRolePermissionGroupDao {
return &AgentRolePermissionGroupDb{Db: engine}
}

type AgentRolePermissionGroupDb struct {
Db *xorm.Engine
}

func (a AgentRolePermissionGroupDb) AgentRolePermissionGroupDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error) {
if reflect.TypeOf(roleId).Kind() == reflect.Slice {
return session.In("role_id", roleId).Delete(model.AgentRolePermissionGroup{})
} else {
return session.Where("role_id = ?", roleId).Delete(model.AgentRolePermissionGroup{})
}
}

func (a AgentRolePermissionGroupDb) BatchAddAgentRolePermissionGroupBySession(session *xorm.Session, mm []*model.AgentRolePermissionGroup) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 77
- 0
src/implement/agent_with_role_implement.go View File

@@ -0,0 +1,77 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewAgentWithRoleDb(engine *xorm.Engine) dao.AgentWithRoleDao {
return &AgentWithRoleDb{Db: engine}
}

type AgentWithRoleDb struct {
Db *xorm.Engine
}

func (a AgentWithRoleDb) FindAgentWithRole(id int) (*[]model.AgentWithRole, error) {
var mm []model.AgentWithRole
if err := a.Db.Where("adm_id =?", id).Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

func (a AgentWithRoleDb) FindAgentWithRoleByRoleId(id int) (*[]model.AgentWithRole, error) {
var mm []model.AgentWithRole
if err := a.Db.Where("role_id =?", id).Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

func (a AgentWithRoleDb) AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error) {
if reflect.TypeOf(admId).Kind() == reflect.Slice {
return session.In("adm_id", admId).Delete(model.AgentWithRole{})
} else {
return session.Where("adm_id = ?", admId).Delete(model.AgentWithRole{})
}
}

func (a AgentWithRoleDb) GetAgentWithRoleByRole(id int) (mm *model.AgentWithRole, err error) {
mm = new(model.AgentWithRole)
has, err := a.Db.Where("role_id =?", id).Get(mm)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return mm, nil
}

func (a AgentWithRoleDb) AgentWithRoleDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error) {
if reflect.TypeOf(roleId).Kind() == reflect.Slice {
return session.In("role_id", roleId).Delete(model.AgentWithRole{})
} else {
return session.Where("role_id = ?", roleId).Delete(model.AgentWithRole{})
}
}

func (a AgentWithRoleDb) AgentWithRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("agent_id", id).Delete(model.AgentWithRole{})
} else {
return session.Where("agent_id = ?", id).Delete(model.AgentWithRole{})
}
}

func (a AgentWithRoleDb) BatchAddAgentWithRoleBySession(session *xorm.Session, mm []*model.AgentWithRole) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 36
- 0
src/implement/medium_domain_implement.go View File

@@ -0,0 +1,36 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"xorm.io/xorm"
)

func NewMediumDomainDb(engine *xorm.Engine) dao.MediumDomainDao {
return &MediumDomainDb{Db: engine}
}

type MediumDomainDb struct {
Db *xorm.Engine
}

func (m MediumDomainDb) GetMediumDomain(mediumId int) (medium *model.MediumDomain, err error) {
medium = new(model.MediumDomain)
has, err := m.Db.Where("medium_id =?", mediumId).Get(medium)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return medium, nil
}

func (m MediumDomainDb) MediumDomainInsert(medium *model.MediumDomain) (int64, error) {
insertAffected, err := m.Db.InsertOne(medium)
if err != nil {
return 0, err
}
return insertAffected, nil
}

+ 53
- 0
src/implement/medium_implement.go View File

@@ -0,0 +1,53 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewMediumDb(engine *xorm.Engine) dao.MediumDao {
return &MediumDb{Db: engine}
}

type MediumDb struct {
Db *xorm.Engine
}

func (m MediumDb) UpdateMedium(medium *model.Medium, columns ...string) (int64, error) {
affected, err := m.Db.Where("id =?", medium.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

func (m MediumDb) GetMediumByUsername(username string) (medium *model.Medium, err error) {
medium = new(model.Medium)
has, err := m.Db.Where("username =?", username).Get(medium)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return medium, nil
}

func (m MediumDb) MediumInsert(medium *model.Medium) (int64, error) {
insertAffected, err := m.Db.InsertOne(medium)
if err != nil {
return 0, err
}
return insertAffected, nil
}

func (m MediumDb) MediumDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.Admin{})
} else {
return session.Where("id = ?", id).Delete(model.Admin{})
}
}

+ 33
- 0
src/implement/medium_permission_group_implement.go View File

@@ -0,0 +1,33 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/enum"
"applet/app/utils/logx"
"xorm.io/xorm"
)

func NewMediumPermissionGroupDb(engine *xorm.Engine) dao.MediumPermissionGroupDao {
return &MediumPermissionGroupDb{Db: engine}
}

type MediumPermissionGroupDb struct {
Db *xorm.Engine
}

func (m MediumPermissionGroupDb) FindMediumPermissionGroup() (*[]model.MediumPermissionGroup, error) {
var mm []model.MediumPermissionGroup
if err := m.Db.Where("state = ?", enum.PermissionGroupStateForNormal).Asc("id").Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

func (m MediumPermissionGroupDb) FindMediumPermissionGroupV2() (*[]model.MediumPermissionGroup, error) {
var mm []model.MediumPermissionGroup
if err := m.Db.Asc("id").Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

+ 14
- 0
src/implement/medium_permission_group_permission_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

func NewMediumPermissionGroupPermissionDb(engine *xorm.Engine) dao.MediumPermissionGroupPermissionDao {
return &MediumPermissionGroupPermissionDb{Db: engine}
}

type MediumPermissionGroupPermissionDb struct {
Db *xorm.Engine
}

+ 14
- 0
src/implement/medium_permission_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

func NewMediumPermissionDb(engine *xorm.Engine) dao.MediumPermissionDao {
return &MediumPermissionDb{Db: engine}
}

type MediumPermissionDb struct {
Db *xorm.Engine
}

+ 69
- 0
src/implement/medium_role_implement.go View File

@@ -0,0 +1,69 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewMediumRoleDb(engine *xorm.Engine) dao.MediumRoleDao {
return &MediumRoleDb{Db: engine}
}

type MediumRoleDb struct {
Db *xorm.Engine
}

func (m MediumRoleDb) FindMediumRole() (*[]model.MediumRole, error) {
var mm []model.MediumRole
if err := m.Db.Desc("id").Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

func (m MediumRoleDb) GetMediumRole() (mm *model.MediumRole, err error) {
mm = new(model.MediumRole)
has, err := m.Db.Where("id = ?", mm.Id).Get(mm)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return mm, nil
}

func (m MediumRoleDb) UpdateMediumRole(mm *model.MediumRole, columns ...string) (int64, error) {
affected, err := m.Db.Where("id =?", mm.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

func (m MediumRoleDb) MediumRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.MediumRole{})
} else {
return session.Where("id = ?", id).Delete(model.MediumRole{})
}
}

func (m MediumRoleDb) MediumRoleInsert(mm *model.MediumRole) (int, error) {
_, err := m.Db.InsertOne(mm)
if err != nil {
return 0, err
}
return mm.Id, nil
}

func (m MediumRoleDb) FindPermissionGroupByMediumRole(roleId int) (list []*dao.MediumRolePermissionGroup, total int64, err error) {
total, err = m.Db.Where("medium_role.id =?", roleId).
Join("LEFT", "medium_role_permission_group", "medium_role_permission_group.role_id = medium_role.id").
Join("LEFT", "medium_permission_group", "medium_permission_group.id = medium_role_permission_group.group_id").
FindAndCount(&list)
return
}

+ 32
- 0
src/implement/medium_role_permission_group_implement.go View File

@@ -0,0 +1,32 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"reflect"
"xorm.io/xorm"
)

func NewMediumRolePermissionGroupDb(engine *xorm.Engine) dao.MediumRolePermissionGroupDao {
return &MediumRolePermissionGroupDb{Db: engine}
}

type MediumRolePermissionGroupDb struct {
Db *xorm.Engine
}

func (m MediumRolePermissionGroupDb) MediumRolePermissionGroupDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error) {
if reflect.TypeOf(roleId).Kind() == reflect.Slice {
return session.In("role_id", roleId).Delete(model.MediumRolePermissionGroup{})
} else {
return session.Where("role_id = ?", roleId).Delete(model.MediumRolePermissionGroup{})
}
}

func (m MediumRolePermissionGroupDb) BatchAddMediumRolePermissionGroupBySession(session *xorm.Session, mm []*model.MediumRolePermissionGroup) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 77
- 0
src/implement/medium_with_role_implement.go View File

@@ -0,0 +1,77 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewMediumWithRoleDb(engine *xorm.Engine) dao.MediumWithRoleDao {
return &MediumWithRoleDb{Db: engine}
}

type MediumWithRoleDb struct {
Db *xorm.Engine
}

func (m MediumWithRoleDb) FindMediumWithRole(id int) (*[]model.MediumWithRole, error) {
var mm []model.MediumWithRole
if err := m.Db.Where("adm_id =?", id).Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

func (m MediumWithRoleDb) FindMediumWithRoleByRoleId(id int) (*[]model.MediumWithRole, error) {
var mm []model.MediumWithRole
if err := m.Db.Where("role_id =?", id).Find(&mm); err != nil {
return nil, logx.Error(err)
}
return &mm, nil
}

func (m MediumWithRoleDb) AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error) {
if reflect.TypeOf(admId).Kind() == reflect.Slice {
return session.In("adm_id", admId).Delete(model.MediumWithRole{})
} else {
return session.Where("adm_id = ?", admId).Delete(model.MediumWithRole{})
}
}

func (m MediumWithRoleDb) GetMediumWithRoleByRole(id int) (mm *model.MediumWithRole, err error) {
mm = new(model.MediumWithRole)
has, err := m.Db.Where("role_id =?", id).Get(mm)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return mm, nil
}

func (m MediumWithRoleDb) MediumWithRoleDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error) {
if reflect.TypeOf(roleId).Kind() == reflect.Slice {
return session.In("role_id", roleId).Delete(model.MediumWithRole{})
} else {
return session.Where("role_id = ?", roleId).Delete(model.MediumWithRole{})
}
}

func (m MediumWithRoleDb) MediumWithRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("medium_id", id).Delete(model.MediumWithRole{})
} else {
return session.Where("medium_id = ?", id).Delete(model.MediumWithRole{})
}
}

func (m MediumWithRoleDb) BatchAddMediumWithRoleBySession(session *xorm.Session, mm []*model.MediumWithRole) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 33
- 0
src/implement/permission_group_implement.go View File

@@ -0,0 +1,33 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/enum"
"applet/app/utils/logx"
"xorm.io/xorm"
)

func NewPermissionGroupDb(engine *xorm.Engine) dao.PermissionGroupDao {
return &PermissionGroupDb{Db: engine}
}

type PermissionGroupDb struct {
Db *xorm.Engine
}

func (p PermissionGroupDb) FindPermissionGroup() (*[]model.PermissionGroup, error) {
var m []model.PermissionGroup
if err := p.Db.Where("state = ?", enum.PermissionGroupStateForNormal).Asc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (p PermissionGroupDb) FindPermissionGroupV2() (*[]model.PermissionGroup, error) {
var m []model.PermissionGroup
if err := p.Db.Asc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

+ 14
- 0
src/implement/permission_group_permission_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

type PermissionGroupPermissionDb struct {
Db *xorm.Engine
}

func NewPermissionGroupPermissionDb(engine *xorm.Engine) dao.PermissionGroupPermissionDao {
return &PermissionGroupPermissionDb{Db: engine}
}

+ 14
- 0
src/implement/permission_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

type PermissionDb struct {
Db *xorm.Engine
}

func NewPermissionDb(engine *xorm.Engine) dao.PermissionDao {
return &PermissionDb{Db: engine}
}

+ 73
- 0
src/implement/role_implement.go View File

@@ -0,0 +1,73 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewRoleDb(engine *xorm.Engine, id int) dao.RoleDao {
return &RoleDb{
Db: engine,
Id: id,
}
}

type RoleDb struct {
Db *xorm.Engine
Id int `json:"id"`
}

func (r RoleDb) FindRole() (*[]model.Role, error) {
var m []model.Role
if err := r.Db.Desc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (r RoleDb) GetRole() (m *model.Role, err error) {
m = new(model.Role)
has, err := r.Db.Where("id = ?", r.Id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (r RoleDb) UpdateRole(m *model.Role, columns ...string) (int64, error) {
affected, err := r.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

func (r RoleDb) RoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.Role{})
} else {
return session.Where("id = ?", id).Delete(model.Role{})
}
}

func (r RoleDb) RoleInsert(m *model.Role) (int, error) {
_, err := r.Db.InsertOne(m)
if err != nil {
return 0, err
}
return m.Id, nil
}

func (r RoleDb) FindPermissionGroupByRole(roleId int) (list []*dao.RolePermissionGroup, total int64, err error) {
total, err = r.Db.Where("role.id =?", roleId).
Join("LEFT", "role_permission_group", "role_permission_group.role_id = role.id").
Join("LEFT", "permission_group", "permission_group.id = role_permission_group.group_id").
FindAndCount(&list)
return
}

+ 32
- 0
src/implement/role_permission_group_implement.go View File

@@ -0,0 +1,32 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"reflect"
"xorm.io/xorm"
)

func NewRolePermissionGroupDb(engine *xorm.Engine) dao.RolePermissionGroupDao {
return &RolePermissionGroupDb{Db: engine}
}

type RolePermissionGroupDb struct {
Db *xorm.Engine
}

func (r RolePermissionGroupDb) RolePermissionGroupDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error) {
if reflect.TypeOf(roleId).Kind() == reflect.Slice {
return session.In("role_id", roleId).Delete(model.RolePermissionGroup{})
} else {
return session.Where("role_id = ?", roleId).Delete(model.RolePermissionGroup{})
}
}

func (r RolePermissionGroupDb) BatchAddRolePermissionGroupBySession(session *xorm.Session, mm []*model.RolePermissionGroup) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 124
- 0
src/implement/sys_cfg_implement.go View File

@@ -0,0 +1,124 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/md"
"applet/app/utils/cache"
"applet/app/utils/logx"
"fmt"
"xorm.io/xorm"
)

func NewSysCfgDb(engine *xorm.Engine, masterId string) dao.SysCfgDao {
return &SysCfgDb{
Db: engine,
MasterId: masterId,
}
}

type SysCfgDb struct {
Db *xorm.Engine
MasterId string
}

func (s SysCfgDb) SysCfgGetAll() (*[]model.SysCfg, error) {
var cfgList []model.SysCfg
if err := s.Db.Cols("key,val,memo").Find(&cfgList); err != nil {
return nil, logx.Error(err)
}
return &cfgList, nil
}

func (s SysCfgDb) SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) {
var cfgList model.SysCfg
has, err := s.Db.Where("`key`=?", key).Get(&cfgList)
if err != nil {
return nil, logx.Error(err)
}
if !has {
return nil, nil
}
return &cfgList, nil
}

func (s SysCfgDb) SysCfgGetOne(key string) (*model.SysCfg, error) {
var cfgList model.SysCfg
if has, err := s.Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
return nil, logx.Error(err)
}
return &cfgList, nil
}

func (s SysCfgDb) SysCfgInsert(key, val, memo string) bool {
cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
_, err := s.Db.InsertOne(&cfg)
if err != nil {
logx.Error(err)
return false
}
return true
}

func (s SysCfgDb) SysCfgUpdate(key, val string) bool {
cfg := model.SysCfg{Key: key, Val: val}
_, err := s.Db.Where("`key`=?", key).Cols("val").Update(&cfg)
if err != nil {
logx.Error(err)
return false
}
s.SysCfgDel(key)
return true
}

func (s SysCfgDb) SysCfgGetWithDb(HKey string) string {
cacheKey := fmt.Sprintf(md.AppCfgCacheKey, s.MasterId, HKey[0:1])
get, err := cache.HGetString(cacheKey, HKey)
if err != nil || get == "" {
cfg, err := s.SysCfgGetOne(HKey)
if err != nil || cfg == nil {
_ = logx.Error(err)
return ""
}

// key是否存在
cacheKeyExist := false
if cache.Exists(cacheKey) {
cacheKeyExist = true
}

// 设置缓存
_, err = cache.HSet(cacheKey, HKey, cfg.Val)
if err != nil {
_ = logx.Error(err)
return ""
}
if !cacheKeyExist { // 如果是首次设置 设置过期时间
_, err := cache.Expire(cacheKey, md.CfgCacheTime)
if err != nil {
_ = logx.Error(err)
return ""
}
}
return cfg.Val
}
return get
}

func (s SysCfgDb) SysCfgDel(HKey string) error {
cacheKey := fmt.Sprintf(md.AppCfgCacheKey, s.MasterId, HKey[0:1])
_, err := cache.HDel(cacheKey, HKey)
if err != nil {
return err
}
return nil
}

func (s SysCfgDb) SysCfgFindWithDb(keys ...string) map[string]string {
res := map[string]string{}
for _, v := range keys {
val := s.SysCfgGetWithDb(v)
res[v] = val
}
return res
}

+ 13
- 0
src/model/admin.go View File

@@ -0,0 +1,13 @@
package model

type Admin struct {
AdmId int `json:"adm_id" xorm:"not null comment('管理员id') INT(11)"`
Username string `json:"username" xorm:"not null default '' comment('用户名') VARCHAR(255)"`
Password string `json:"password" xorm:"not null default '' comment('密码') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
IsSuperAdministrator int `json:"is_super_administrator" xorm:"not null default 0 comment('是否为超级管理员(0:否 1:是)') TINYINT(1)"`
IsObserverAdministrator int `json:"is_observer_administrator" xorm:"not null default 0 comment('是否为观察者管理员(0:否 1:是)') TINYINT(1)"`
Memo string `json:"memo" xorm:"not null default '' comment('备注信息') VARCHAR(244)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 10
- 0
src/model/admin_role.go View File

@@ -0,0 +1,10 @@
package model

type AdminRole struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
AdmId int `json:"adm_id" xorm:"not null default 0 comment('管理员id') INT(11)"`
RoleId int `json:"role_id" xorm:"not null default 0 comment('角色id') INT(11)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 13
- 0
src/model/agent.go View File

@@ -0,0 +1,13 @@
package model

type Agent struct {
Id int `json:"id" xorm:"not null pk default 0 INT(11)"`
AgentId int `json:"agent_id" xorm:"not null default 0 comment('代理id') INT(11)"`
Username string `json:"username" xorm:"not null default '' comment('用户名') VARCHAR(255)"`
Password string `json:"password" xorm:"not null default '' comment('密码') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
IsSuperAdministrator int `json:"is_super_administrator" xorm:"not null default 0 comment('是否为超级管理员(0:否 1:是)') TINYINT(1)"`
Memo string `json:"memo" xorm:"not null default '' comment('备注信息') VARCHAR(244)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 8
- 0
src/model/agent_domain.go View File

@@ -0,0 +1,8 @@
package model

type AgentDomain struct {
Domain string `json:"domain" xorm:"not null pk comment('绑定域名') VARCHAR(100)"`
MediumId int `json:"medium_id" xorm:"not null comment('媒体id') index unique INT(11)"`
IsOfficial int `json:"is_official" xorm:"not null default 1 comment('是否官方指定域名(1:是 2:否)') TINYINT(1)"`
IsSsl int `json:"is_ssl" xorm:"not null default 0 comment('是否开启ssl:0否;1是') TINYINT(255)"`
}

+ 10
- 0
src/model/agent_permission.go View File

@@ -0,0 +1,10 @@
package model

type AgentPermission struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
Action string `json:"action" xorm:"not null default '' comment('路由') unique VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:废弃)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 11
- 0
src/model/agent_permission_group.go View File

@@ -0,0 +1,11 @@
package model

type AgentPermissionGroup struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
ParentId int `json:"parent_id" xorm:"not null default 0 comment('父级id') INT(11)"`
Key string `json:"key" xorm:"not null default '' VARCHAR(255)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:废弃)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 9
- 0
src/model/agent_permission_group_permission.go View File

@@ -0,0 +1,9 @@
package model

type AgentPermissionGroupPermission struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
GroupId int `json:"group_id" xorm:"not null default 0 comment('权限组id') INT(11)"`
PermissionId int `json:"permission_id" xorm:"not null default 0 comment('权限id') INT(11)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 10
- 0
src/model/agent_role.go View File

@@ -0,0 +1,10 @@
package model

type AgentRole struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 9
- 0
src/model/agent_role_permission_group.go View File

@@ -0,0 +1,9 @@
package model

type AgentRolePermissionGroup struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
RoleId int `json:"role_id" xorm:"not null default 0 comment('角色id') INT(11)"`
GroupId int `json:"group_id" xorm:"not null default 0 comment('权限组id') INT(11)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 10
- 0
src/model/agent_with_role.go View File

@@ -0,0 +1,10 @@
package model

type AgentWithRole struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
AgentId int `json:"agent_id" xorm:"not null default 0 comment('管理员id') INT(11)"`
RoleId int `json:"role_id" xorm:"not null default 0 comment('角色id') INT(11)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 14
- 0
src/model/db_mapping.go View File

@@ -0,0 +1,14 @@
package model

type DbMapping struct {
DbMasterId string `json:"db_master_id" xorm:"not null pk comment('站长id') VARCHAR(16)"`
DbHost string `json:"db_host" xorm:"not null default '' comment('数据库连接(带port)') VARCHAR(255)"`
DbUsername string `json:"db_username" xorm:"not null default '' comment('数据库用户名') VARCHAR(255)"`
DbPassword string `json:"db_password" xorm:"not null default '' comment('数据库用户名密码') VARCHAR(255)"`
DbName string `json:"db_name" xorm:"not null comment('数据库名') VARCHAR(255)"`
ExternalMysql string `json:"external_mysql" xorm:"not null default '0' comment('是否外部mysql(0是内部,1是外部)') VARCHAR(255)"`
IsDev int `json:"is_dev" xorm:"not null default 0 comment('开发库是1,0是生产库') TINYINT(1)"`
CreatedAt string `json:"created_at" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"`
UpdatedAt string `json:"updated_at" xorm:"not null default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"`
DeletedAt int `json:"deleted_at" xorm:"not null default 0 comment('是否已删除') TINYINT(1)"`
}

+ 13
- 0
src/model/medium.go View File

@@ -0,0 +1,13 @@
package model

type Medium struct {
Id int `json:"id" xorm:"not null pk default 0 INT(11)"`
MediumId int `json:"medium_id" xorm:"not null default 0 comment('媒体id') INT(11)"`
Username string `json:"username" xorm:"not null default '' comment('用户名') VARCHAR(255)"`
Password string `json:"password" xorm:"not null default '' comment('密码') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
IsSuperAdministrator int `json:"is_super_administrator" xorm:"not null default 0 comment('是否为超级管理员(0:否 1:是)') TINYINT(1)"`
Memo string `json:"memo" xorm:"not null default '' comment('备注信息') VARCHAR(244)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 8
- 0
src/model/medium_domain.go View File

@@ -0,0 +1,8 @@
package model

type MediumDomain struct {
Domain string `json:"domain" xorm:"not null pk comment('绑定域名') VARCHAR(100)"`
MediumId int `json:"medium_id" xorm:"not null comment('媒体id') index unique INT(11)"`
IsOfficial int `json:"is_official" xorm:"not null default 1 comment('是否官方指定域名(1:是 2:否)') TINYINT(1)"`
IsSsl int `json:"is_ssl" xorm:"not null default 0 comment('是否开启ssl:0否;1是') TINYINT(255)"`
}

+ 10
- 0
src/model/medium_permission.go View File

@@ -0,0 +1,10 @@
package model

type MediumPermission struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
Action string `json:"action" xorm:"not null default '' comment('路由') unique VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:废弃)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 11
- 0
src/model/medium_permission_group.go View File

@@ -0,0 +1,11 @@
package model

type MediumPermissionGroup struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
ParentId int `json:"parent_id" xorm:"not null default 0 comment('父级id') INT(11)"`
Key string `json:"key" xorm:"not null default '' VARCHAR(255)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:废弃)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 9
- 0
src/model/medium_permission_group_permission.go View File

@@ -0,0 +1,9 @@
package model

type MediumPermissionGroupPermission struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
GroupId int `json:"group_id" xorm:"not null default 0 comment('权限组id') INT(11)"`
PermissionId int `json:"permission_id" xorm:"not null default 0 comment('权限id') INT(11)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 10
- 0
src/model/medium_role.go View File

@@ -0,0 +1,10 @@
package model

type MediumRole struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 9
- 0
src/model/medium_role_permission_group.go View File

@@ -0,0 +1,9 @@
package model

type MediumRolePermissionGroup struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
RoleId int `json:"role_id" xorm:"not null default 0 comment('角色id') INT(11)"`
GroupId int `json:"group_id" xorm:"not null default 0 comment('权限组id') INT(11)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 10
- 0
src/model/medium_with_role.go View File

@@ -0,0 +1,10 @@
package model

type MediumWithRole struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
MediumId int `json:"medium_id" xorm:"not null default 0 comment('媒体id') INT(11)"`
RoleId int `json:"role_id" xorm:"not null default 0 comment('角色id') INT(11)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 10
- 0
src/model/permission.go View File

@@ -0,0 +1,10 @@
package model

type Permission struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
Action string `json:"action" xorm:"not null default '' comment('路由') unique VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:废弃)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 11
- 0
src/model/permission_group.go View File

@@ -0,0 +1,11 @@
package model

type PermissionGroup struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
ParentId int `json:"parent_id" xorm:"not null default 0 comment('父级id') INT(11)"`
Key string `json:"key" xorm:"not null default '' VARCHAR(255)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:废弃)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 9
- 0
src/model/permission_group_permission.go View File

@@ -0,0 +1,9 @@
package model

type PermissionGroupPermission struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
GroupId int `json:"group_id" xorm:"not null default 0 comment('权限组id') INT(11)"`
PermissionId int `json:"permission_id" xorm:"not null default 0 comment('权限id') INT(11)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 10
- 0
src/model/role.go View File

@@ -0,0 +1,10 @@
package model

type Role struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 9
- 0
src/model/role_permission_group.go View File

@@ -0,0 +1,9 @@
package model

type RolePermissionGroup struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
RoleId int `json:"role_id" xorm:"not null default 0 comment('角色id') INT(11)"`
GroupId int `json:"group_id" xorm:"not null default 0 comment('权限组id') INT(11)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 7
- 0
src/model/sys_cfg.go View File

@@ -0,0 +1,7 @@
package model

type SysCfg struct {
Key string `json:"key" xorm:"not null pk comment('键') VARCHAR(127)"`
Val string `json:"val" xorm:"comment('值') TEXT"`
Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"`
}

+ 44
- 0
src/super/dao/admin_dao.go View File

@@ -0,0 +1,44 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type AdminDao interface {
CreateAdminId() (admId int, err error)
AdminDeleteBySession(session *xorm.Session, admId interface{}) (int64, error)
GetAdmin(id int) (m *model.Admin, err error)
GetAdminByUserName(userName string) (m *model.Admin, err error)
GetAdminRolePermission(admId int) (list []*AdminRolePermission, total int64, err error)
FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error)
UpdateAdmin(m *model.Admin, columns ...string) (int64, error)
AdminInsert(m *model.Admin) (int64, error)
FindAdminRolePermissionGroup(admId int) (list []*AdminRolePermissionGroup, total int64, err error)
}

type AdminRolePermissionGroup struct {
model.Admin `xorm:"extends"`
model.AdminRole `xorm:"extends"`
model.Role `xorm:"extends"`
model.RolePermissionGroup `xorm:"extends"`
model.PermissionGroup `xorm:"extends"`
}

func (AdminRolePermissionGroup) TableName() string {
return "admin"
}

type AdminRolePermission struct {
model.Admin `xorm:"extends"`
model.AdminRole `xorm:"extends"`
model.Role `xorm:"extends"`
model.RolePermissionGroup `xorm:"extends"`
model.PermissionGroup `xorm:"extends"`
model.PermissionGroupPermission `xorm:"extends"`
model.Permission `xorm:"extends"`
}

func (AdminRolePermission) TableName() string {
return "admin"
}

+ 25
- 0
src/super/dao/admin_role_dao.go View File

@@ -0,0 +1,25 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type AdminRoleDao interface {
FindAdminRole(id int) (*[]model.AdminRole, error)
FindAdminRoleByRoleId(id int) (*[]model.AdminRole, error)
AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error)
GetAdminRoleByRole(id int) (m *model.AdminRole, err error)
AdminRoleDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error)
AdminRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
BatchAddAdminRoleBySession(session *xorm.Session, mm []*model.AdminRole) (int64, error)
}

type AdminRole struct {
model.Admin `xorm:"extends"`
model.AdminRole `xorm:"extends"`
}

func (AdminRole) TableName() string {
return "admin_role"
}

+ 9
- 0
src/super/dao/agent_list_dao.go View File

@@ -0,0 +1,9 @@
package dao

import "applet/app/db/super/model"

type AgentListDao interface {
GetAgentList(agentId int) (medium *model.AgentList, err error)
AgentListInsert(m *model.AgentList) (int64, error)
UpdateAgentList(m *model.AgentList, columns ...string) (int64, error)
}

+ 9
- 0
src/super/dao/medium_list_dao.go View File

@@ -0,0 +1,9 @@
package dao

import "applet/app/db/super/model"

type MediumListDao interface {
GetMediumList(mediumId int) (medium *model.MediumList, err error)
MediumListInsert(m *model.MediumList) (int64, error)
UpdateMediumList(m *model.MediumList, columns ...string) (int64, error)
}

+ 5
- 0
src/super/dao/permission_dao.go View File

@@ -0,0 +1,5 @@
package dao

type PermissionDao interface {
//TODO:: You can add specific method definitions here
}

+ 8
- 0
src/super/dao/permission_group_dao.go View File

@@ -0,0 +1,8 @@
package dao

import "applet/app/db/model"

type PermissionGroupDao interface {
FindPermissionGroup() (*[]model.PermissionGroup, error)
FindPermissionGroupV2() (*[]model.PermissionGroup, error)
}

+ 5
- 0
src/super/dao/permission_group_permission_dao.go View File

@@ -0,0 +1,5 @@
package dao

type PermissionGroupPermissionDao interface {
//TODO:: You can add specific method definitions here
}

+ 25
- 0
src/super/dao/role_dao.go View File

@@ -0,0 +1,25 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type RoleDao interface {
FindRole() (*[]model.Role, error)
GetRole() (m *model.Role, err error)
UpdateRole(m *model.Role, columns ...string) (int64, error)
RoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error)
RoleInsert(m *model.Role) (int, error)
FindPermissionGroupByRole(roleId int) (list []*RolePermissionGroup, total int64, err error)
}

type RolePermissionGroup struct {
model.Role `xorm:"extends"`
model.RolePermissionGroup `xorm:"extends"`
model.PermissionGroup `xorm:"extends"`
}

func (RolePermissionGroup) TableName() string {
return "role"
}

+ 11
- 0
src/super/dao/role_permission_group_dao.go View File

@@ -0,0 +1,11 @@
package dao

import (
"applet/app/db/model"
"xorm.io/xorm"
)

type RolePermissionGroupDao interface {
RolePermissionGroupDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error)
BatchAddRolePermissionGroupBySession(session *xorm.Session, mm []*model.RolePermissionGroup) (int64, error)
}

+ 115
- 0
src/super/implement/admin_implement.go View File

@@ -0,0 +1,115 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewAdminDb(engine *xorm.Engine) dao.AdminDao {
return &AdminDb{Db: engine}
}

type AdminDb struct {
Db *xorm.Engine
}

func (a AdminDb) CreateAdminId() (admId int, err error) {
m := new(model.Admin)
has, err := a.Db.Desc("adm_id").Get(m)
if err != nil {
return 0, logx.Error(err)
}
if has == false {
return 0, nil
}
admId = m.AdmId + 1
return admId, nil
}

func (a AdminDb) AdminDeleteBySession(session *xorm.Session, admId interface{}) (int64, error) {
if reflect.TypeOf(admId).Kind() == reflect.Slice {
return session.In("adm_id", admId).Delete(model.Admin{})
} else {
return session.Where("adm_id = ?", admId).Delete(model.Admin{})
}
}

func (a AdminDb) GetAdmin(id int) (m *model.Admin, err error) {
m = new(model.Admin)
has, err := a.Db.Where("adm_id =?", id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (a AdminDb) GetAdminByUserName(userName string) (m *model.Admin, err error) {
m = new(model.Admin)
has, err := a.Db.Where("username =?", userName).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (a AdminDb) GetAdminRolePermission(admId int) (list []*dao.AdminRolePermission, total int64, err error) {
total, err = a.Db.Where("admin.adm_id =?", admId).
Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
Join("LEFT", "role", "admin_role.role_id = role.id").
Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
Join("LEFT", "permission_group_permission", "permission_group.id = permission_group_permission.group_id").
Join("LEFT", "permission", "permission_group_permission.permission_id = permission.id").
FindAndCount(&list)
return
}

func (a AdminDb) FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error) {
sess := a.Db.Desc("adm_id").Limit(limit, (page-1)*limit)
if username != "" {
sess.And("username like ?", "%"+username+"%")
}
if state != 0 {
sess.And("state = ?", state)
}
total, err = sess.FindAndCount(&list)
if err != nil {
return nil, 0, err
}
return
}

func (a AdminDb) UpdateAdmin(m *model.Admin, columns ...string) (int64, error) {
affected, err := a.Db.Where("adm_id =?", m.AdmId).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

func (a AdminDb) AdminInsert(m *model.Admin) (int64, error) {
insertAffected, err := a.Db.InsertOne(m)
if err != nil {
return 0, err
}
return insertAffected, nil
}

func (a AdminDb) FindAdminRolePermissionGroup(admId int) (list []*dao.AdminRolePermissionGroup, total int64, err error) {
total, err = a.Db.Where("admin.adm_id =?", admId).
Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id").
Join("LEFT", "role", "admin_role.role_id = role.id").
//Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id").
//Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id").
FindAndCount(&list)
return
}

+ 77
- 0
src/super/implement/admin_role_implement.go View File

@@ -0,0 +1,77 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewAdminRoleDb(engine *xorm.Engine) dao.AdminRoleDao {
return &AdminRoleDb{Db: engine}
}

type AdminRoleDb struct {
Db *xorm.Engine
}

func (a AdminRoleDb) FindAdminRole(id int) (*[]model.AdminRole, error) {
var m []model.AdminRole
if err := a.Db.Where("adm_id =?", id).Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (a AdminRoleDb) FindAdminRoleByRoleId(id int) (*[]model.AdminRole, error) {
var m []model.AdminRole
if err := a.Db.Where("role_id =?", id).Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (a AdminRoleDb) AdminDeleteBySessionForAdmId(session *xorm.Session, admId interface{}) (int64, error) {
if reflect.TypeOf(admId).Kind() == reflect.Slice {
return session.In("adm_id", admId).Delete(model.AdminRole{})
} else {
return session.Where("adm_id = ?", admId).Delete(model.AdminRole{})
}
}

func (a AdminRoleDb) GetAdminRoleByRole(id int) (m *model.AdminRole, err error) {
m = new(model.AdminRole)
has, err := a.Db.Where("role_id =?", id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (a AdminRoleDb) AdminRoleDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error) {
if reflect.TypeOf(roleId).Kind() == reflect.Slice {
return session.In("role_id", roleId).Delete(model.AdminRole{})
} else {
return session.Where("role_id = ?", roleId).Delete(model.AdminRole{})
}
}

func (a AdminRoleDb) AdminRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("adm_id", id).Delete(model.AdminRole{})
} else {
return session.Where("adm_id = ?", id).Delete(model.AdminRole{})
}
}

func (a AdminRoleDb) BatchAddAdminRoleBySession(session *xorm.Session, mm []*model.AdminRole) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 44
- 0
src/super/implement/agent_list_implement.go View File

@@ -0,0 +1,44 @@
package implement

import (
"applet/app/db/super/dao"
"applet/app/db/super/model"
"applet/app/utils/logx"
"xorm.io/xorm"
)

func NewAgentListDb(engine *xorm.Engine) dao.AgentListDao {
return &AgentListDb{Db: engine}
}

type AgentListDb struct {
Db *xorm.Engine
}

func (a AgentListDb) GetAgentList(agentId int) (medium *model.AgentList, err error) {
medium = new(model.AgentList)
has, err := a.Db.Where("agent_id =?", agentId).Get(medium)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return medium, nil
}

func (a AgentListDb) AgentListInsert(m *model.AgentList) (int64, error) {
insertAffected, err := a.Db.InsertOne(m)
if err != nil {
return 0, err
}
return insertAffected, nil
}

func (a AgentListDb) UpdateAgentList(m *model.AgentList, columns ...string) (int64, error) {
affected, err := a.Db.Where("agent_id =?", m.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

+ 44
- 0
src/super/implement/medium_list_implement.go View File

@@ -0,0 +1,44 @@
package implement

import (
"applet/app/db/super/dao"
"applet/app/db/super/model"
"applet/app/utils/logx"
"xorm.io/xorm"
)

func NewMediumListDb(engine *xorm.Engine) dao.MediumListDao {
return &MediumListDb{Db: engine}
}

type MediumListDb struct {
Db *xorm.Engine
}

func (m MediumListDb) GetMediumList(mediumId int) (medium *model.MediumList, err error) {
medium = new(model.MediumList)
has, err := m.Db.Where("medium_id =?", mediumId).Get(medium)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return medium, nil
}

func (m MediumListDb) MediumListInsert(medium *model.MediumList) (int64, error) {
insertAffected, err := m.Db.InsertOne(medium)
if err != nil {
return 0, err
}
return insertAffected, nil
}

func (m MediumListDb) UpdateMediumList(medium *model.MediumList, columns ...string) (int64, error) {
affected, err := m.Db.Where("medium_id =?", medium.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

+ 33
- 0
src/super/implement/permission_group_implement.go View File

@@ -0,0 +1,33 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/enum"
"applet/app/utils/logx"
"xorm.io/xorm"
)

func NewPermissionGroupDb(engine *xorm.Engine) dao.PermissionGroupDao {
return &PermissionGroupDb{Db: engine}
}

type PermissionGroupDb struct {
Db *xorm.Engine
}

func (p PermissionGroupDb) FindPermissionGroup() (*[]model.PermissionGroup, error) {
var m []model.PermissionGroup
if err := p.Db.Where("state = ?", enum.PermissionGroupStateForNormal).Asc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (p PermissionGroupDb) FindPermissionGroupV2() (*[]model.PermissionGroup, error) {
var m []model.PermissionGroup
if err := p.Db.Asc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

+ 14
- 0
src/super/implement/permission_group_permission_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

type PermissionGroupPermissionDb struct {
Db *xorm.Engine
}

func NewPermissionGroupPermissionDb(engine *xorm.Engine) dao.PermissionGroupPermissionDao {
return &PermissionGroupPermissionDb{Db: engine}
}

+ 14
- 0
src/super/implement/permission_implement.go View File

@@ -0,0 +1,14 @@
package implement

import (
"applet/app/db/dao"
"xorm.io/xorm"
)

type PermissionDb struct {
Db *xorm.Engine
}

func NewPermissionDb(engine *xorm.Engine) dao.PermissionDao {
return &PermissionDb{Db: engine}
}

+ 73
- 0
src/super/implement/role_implement.go View File

@@ -0,0 +1,73 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

func NewRoleDb(engine *xorm.Engine, id int) dao.RoleDao {
return &RoleDb{
Db: engine,
Id: id,
}
}

type RoleDb struct {
Db *xorm.Engine
Id int `json:"id"`
}

func (r RoleDb) FindRole() (*[]model.Role, error) {
var m []model.Role
if err := r.Db.Desc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (r RoleDb) GetRole() (m *model.Role, err error) {
m = new(model.Role)
has, err := r.Db.Where("id = ?", r.Id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (r RoleDb) UpdateRole(m *model.Role, columns ...string) (int64, error) {
affected, err := r.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

func (r RoleDb) RoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.Role{})
} else {
return session.Where("id = ?", id).Delete(model.Role{})
}
}

func (r RoleDb) RoleInsert(m *model.Role) (int, error) {
_, err := r.Db.InsertOne(m)
if err != nil {
return 0, err
}
return m.Id, nil
}

func (r RoleDb) FindPermissionGroupByRole(roleId int) (list []*dao.RolePermissionGroup, total int64, err error) {
total, err = r.Db.Where("role.id =?", roleId).
Join("LEFT", "role_permission_group", "role_permission_group.role_id = role.id").
Join("LEFT", "permission_group", "permission_group.id = role_permission_group.group_id").
FindAndCount(&list)
return
}

+ 32
- 0
src/super/implement/role_permission_group_implement.go View File

@@ -0,0 +1,32 @@
package implement

import (
"applet/app/db/dao"
"applet/app/db/model"
"reflect"
"xorm.io/xorm"
)

func NewRolePermissionGroupDb(engine *xorm.Engine) dao.RolePermissionGroupDao {
return &RolePermissionGroupDb{Db: engine}
}

type RolePermissionGroupDb struct {
Db *xorm.Engine
}

func (r RolePermissionGroupDb) RolePermissionGroupDeleteForRoleBySession(session *xorm.Session, roleId interface{}) (int64, error) {
if reflect.TypeOf(roleId).Kind() == reflect.Slice {
return session.In("role_id", roleId).Delete(model.RolePermissionGroup{})
} else {
return session.Where("role_id = ?", roleId).Delete(model.RolePermissionGroup{})
}
}

func (r RolePermissionGroupDb) BatchAddRolePermissionGroupBySession(session *xorm.Session, mm []*model.RolePermissionGroup) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 12
- 0
src/super/model/admin.go View File

@@ -0,0 +1,12 @@
package model

type Admin struct {
AdmId int `json:"adm_id" xorm:"not null comment('管理员id') INT(11)"`
Username string `json:"username" xorm:"not null default '' comment('用户名') VARCHAR(255)"`
Password string `json:"password" xorm:"not null default '' comment('密码') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
IsSuperAdministrator int `json:"is_super_administrator" xorm:"not null default 0 comment('是否为超级管理员(0:否 1:是)') TINYINT(1)"`
Memo string `json:"memo" xorm:"not null default '' comment('备注信息') VARCHAR(244)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 10
- 0
src/super/model/admin_role.go View File

@@ -0,0 +1,10 @@
package model

type AdminRole struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
AdmId int `json:"adm_id" xorm:"not null default 0 comment('管理员id') INT(11)"`
RoleId int `json:"role_id" xorm:"not null default 0 comment('角色id') INT(11)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 25
- 0
src/super/model/agent_list.go View File

@@ -0,0 +1,25 @@
package model

type AgentList struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Uuid int `json:"uuid" xorm:"not null comment('站长id') index unique(IDX_UUID_TYPE) INT(11)"`
AgentId int `json:"agent_id" xorm:"not null comment('代理id') unique(IDX_UUID_TYPE) INT(11)"`
Kind int `json:"kind" xorm:"not null default 1 comment('类型(1:企业 2:个人)') TINYINT(1)"`
CompanyName string `json:"company_name" xorm:"not null default '' comment('公司名称') VARCHAR(255)"`
CompanyAbbreviation string `json:"company_abbreviation" xorm:"not null default '' comment('公司简称') VARCHAR(255)"`
UnifiedSocialCreditCode string `json:"unified_social_credit_code" xorm:"not null default '' comment('统一社会信用代码') VARCHAR(255)"`
CertificateType int `json:"certificate_type" xorm:"not null default 1 comment('证件类型') TINYINT(1)"`
BusinessLicenseImgUrl string `json:"business_license_img_url" xorm:"not null default '' comment('营业执照照片') VARCHAR(255)"`
LegalRepresentative string `json:"legal_representative" xorm:"not null default '' comment('法定代表人') CHAR(50)"`
CountryRegionId int `json:"country_region_id" xorm:"not null default 1 comment('国家地区id') TINYINT(3)"`
CountryRegion string `json:"country_region" xorm:"not null default '' comment('国家地区') CHAR(50)"`
RegisteredAddressProvinceId int `json:"registered_address_province_id" xorm:"not null default 0 comment('注册地址-省份id') INT(11)"`
RegisteredAddressCityId int `json:"registered_address_city_id" xorm:"not null default 0 comment('注册地址-市id') INT(11)"`
RegisteredAddressCountyId int `json:"registered_address_county_id" xorm:"not null default 0 comment('注册地址-县/区id') INT(11)"`
RegisteredAddress string `json:"registered_address" xorm:"not null default '' comment('注册地址') VARCHAR(255)"`
BusinessLicenseAddress string `json:"business_license_address" xorm:"not null default '' comment('营业执照地址') VARCHAR(255)"`
CertificateValidity string `json:"certificate_validity" xorm:"not null default '0000-00-00' comment('证件有效期') CHAR(50)"`
State int `json:"state" xorm:"not null default 0 comment('状态(0:待审核 1:审核通过 2:审核拒绝)') TINYINT(1)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save