feat: 合并Gin_Vue
This commit is contained in:
30
src/modules/system/repository/sys_config.go
Normal file
30
src/modules/system/repository/sys_config.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysConfig 参数配置表 数据层接口
|
||||
type ISysConfig interface {
|
||||
// SelectDictDataPage 分页查询参数配置列表数据
|
||||
SelectConfigPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectConfigList 查询参数配置列表
|
||||
SelectConfigList(sysConfig model.SysConfig) []model.SysConfig
|
||||
|
||||
// SelectConfigValueByKey 通过参数键名查询参数键值
|
||||
SelectConfigValueByKey(configKey string) string
|
||||
|
||||
// SelectConfigByIds 通过配置ID查询参数配置信息
|
||||
SelectConfigByIds(configIds []string) []model.SysConfig
|
||||
|
||||
// CheckUniqueConfig 校验配置参数是否唯一
|
||||
CheckUniqueConfig(sysConfig model.SysConfig) string
|
||||
|
||||
// InsertConfig 新增参数配置
|
||||
InsertConfig(sysConfig model.SysConfig) string
|
||||
|
||||
// UpdateConfig 修改参数配置
|
||||
UpdateConfig(sysConfig model.SysConfig) int64
|
||||
|
||||
// DeleteConfigByIds 批量删除参数配置信息
|
||||
DeleteConfigByIds(configIds []string) int64
|
||||
}
|
||||
339
src/modules/system/repository/sys_config.impl.go
Normal file
339
src/modules/system/repository/sys_config.impl.go
Normal file
@@ -0,0 +1,339 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysConfigImpl 结构体
|
||||
var NewSysConfigImpl = &SysConfigImpl{
|
||||
selectSql: `select
|
||||
config_id, config_name, config_key, config_value, config_type, create_by, create_time, update_by, update_time, remark
|
||||
from sys_config`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"config_id": "ConfigID",
|
||||
"config_name": "ConfigName",
|
||||
"config_key": "ConfigKey",
|
||||
"config_value": "ConfigValue",
|
||||
"config_type": "ConfigType",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// SysConfigImpl 参数配置表 数据层处理
|
||||
type SysConfigImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysConfigImpl) convertResultRows(rows []map[string]any) []model.SysConfig {
|
||||
arr := make([]model.SysConfig, 0)
|
||||
for _, row := range rows {
|
||||
sysConfig := model.SysConfig{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysConfig, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysConfig)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectDictDataPage 分页查询参数配置列表数据
|
||||
func (r *SysConfigImpl) SelectConfigPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["configName"]; ok && v != "" {
|
||||
conditions = append(conditions, "config_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["configType"]; ok && v != "" {
|
||||
conditions = append(conditions, "config_type = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["configKey"]; ok && v != "" {
|
||||
conditions = append(conditions, "config_key like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "create_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysConfig{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_config"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectConfigList 查询参数配置列表
|
||||
func (r *SysConfigImpl) SelectConfigList(sysConfig model.SysConfig) []model.SysConfig {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysConfig.ConfigName != "" {
|
||||
conditions = append(conditions, "config_name like concat(?, '%')")
|
||||
params = append(params, sysConfig.ConfigName)
|
||||
}
|
||||
if sysConfig.ConfigType != "" {
|
||||
conditions = append(conditions, "config_type = ?")
|
||||
params = append(params, sysConfig.ConfigType)
|
||||
}
|
||||
if sysConfig.ConfigKey != "" {
|
||||
conditions = append(conditions, "config_key like concat(?, '%')")
|
||||
params = append(params, sysConfig.ConfigKey)
|
||||
}
|
||||
if sysConfig.CreateTime > 0 {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
params = append(params, sysConfig.CreateTime)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysConfig{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectConfigValueByKey 通过参数键名查询参数键值
|
||||
func (r *SysConfigImpl) SelectConfigValueByKey(configKey string) string {
|
||||
querySql := "select config_value as 'str' from sys_config where config_key = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{configKey})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// SelectConfigByIds 通过配置ID查询参数配置信息
|
||||
func (r *SysConfigImpl) SelectConfigByIds(configIds []string) []model.SysConfig {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(configIds))
|
||||
querySql := r.selectSql + " where config_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(configIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysConfig{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueConfig 校验配置参数是否唯一
|
||||
func (r *SysConfigImpl) CheckUniqueConfig(sysConfig model.SysConfig) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysConfig.ConfigKey != "" {
|
||||
conditions = append(conditions, "config_key = ?")
|
||||
params = append(params, sysConfig.ConfigKey)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select config_id as 'str' from sys_config " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// InsertConfig 新增参数配置
|
||||
func (r *SysConfigImpl) InsertConfig(sysConfig model.SysConfig) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysConfig.ConfigName != "" {
|
||||
params["config_name"] = sysConfig.ConfigName
|
||||
}
|
||||
if sysConfig.ConfigKey != "" {
|
||||
params["config_key"] = sysConfig.ConfigKey
|
||||
}
|
||||
if sysConfig.ConfigValue != "" {
|
||||
params["config_value"] = sysConfig.ConfigValue
|
||||
}
|
||||
if sysConfig.ConfigType != "" {
|
||||
params["config_type"] = sysConfig.ConfigType
|
||||
}
|
||||
if sysConfig.Remark != "" {
|
||||
params["remark"] = sysConfig.Remark
|
||||
}
|
||||
if sysConfig.CreateBy != "" {
|
||||
params["create_by"] = sysConfig.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_config (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateConfig 修改参数配置
|
||||
func (r *SysConfigImpl) UpdateConfig(sysConfig model.SysConfig) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysConfig.ConfigName != "" {
|
||||
params["config_name"] = sysConfig.ConfigName
|
||||
}
|
||||
if sysConfig.ConfigKey != "" {
|
||||
params["config_key"] = sysConfig.ConfigKey
|
||||
}
|
||||
if sysConfig.ConfigValue != "" {
|
||||
params["config_value"] = sysConfig.ConfigValue
|
||||
}
|
||||
if sysConfig.ConfigType != "" {
|
||||
params["config_type"] = sysConfig.ConfigType
|
||||
}
|
||||
if sysConfig.Remark != "" {
|
||||
params["remark"] = sysConfig.Remark
|
||||
}
|
||||
if sysConfig.UpdateBy != "" {
|
||||
params["update_by"] = sysConfig.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_config set " + strings.Join(keys, ",") + " where config_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysConfig.ConfigID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteConfigByIds 批量删除参数配置信息
|
||||
func (r *SysConfigImpl) DeleteConfigByIds(configIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(configIds))
|
||||
sql := "delete from sys_config where config_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(configIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
42
src/modules/system/repository/sys_dept.go
Normal file
42
src/modules/system/repository/sys_dept.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysDept 部门表 数据层接口
|
||||
type ISysDept interface {
|
||||
// SelectDeptList 查询部门管理数据
|
||||
SelectDeptList(sysDept model.SysDept, dataScopeSQL string) []model.SysDept
|
||||
|
||||
// SelectDeptListByRoleId 根据角色ID查询部门树信息
|
||||
SelectDeptListByRoleId(roleId string, deptCheckStrictly bool) []string
|
||||
|
||||
// SelectDeptById 根据部门ID查询信息
|
||||
SelectDeptById(deptId string) model.SysDept
|
||||
|
||||
// SelectChildrenDeptById 根据ID查询所有子部门
|
||||
SelectChildrenDeptById(deptId string) []model.SysDept
|
||||
|
||||
// HasChildByDeptId 是否存在子节点
|
||||
HasChildByDeptId(deptId string) int64
|
||||
|
||||
// CheckDeptExistUser 查询部门是否存在用户
|
||||
CheckDeptExistUser(deptId string) int64
|
||||
|
||||
// CheckUniqueDept 校验部门是否唯一
|
||||
CheckUniqueDept(sysDept model.SysDept) string
|
||||
|
||||
// InsertDept 新增部门信息
|
||||
InsertDept(sysDept model.SysDept) string
|
||||
|
||||
// UpdateDept 修改部门信息
|
||||
UpdateDept(sysDept model.SysDept) int64
|
||||
|
||||
// UpdateDeptStatusNormal 修改所在部门正常状态
|
||||
UpdateDeptStatusNormal(deptIds []string) int64
|
||||
|
||||
// UpdateDeptChildren 修改子元素关系
|
||||
UpdateDeptChildren(sysDepts []model.SysDept) int64
|
||||
|
||||
// DeleteDeptById 删除部门管理信息
|
||||
DeleteDeptById(deptId string) int64
|
||||
}
|
||||
393
src/modules/system/repository/sys_dept.impl.go
Normal file
393
src/modules/system/repository/sys_dept.impl.go
Normal file
@@ -0,0 +1,393 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysDeptImpl 结构体
|
||||
var NewSysDeptImpl = &SysDeptImpl{
|
||||
selectSql: `select
|
||||
d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time
|
||||
from sys_dept d`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"dept_id": "DeptID",
|
||||
"parent_id": "ParentID",
|
||||
"ancestors": "Ancestors",
|
||||
"dept_name": "DeptName",
|
||||
"order_num": "OrderNum",
|
||||
"leader": "Leader",
|
||||
"phone": "Phone",
|
||||
"email": "Email",
|
||||
"status": "Status",
|
||||
"del_flag": "DelFlag",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"parent_name": "ParentName",
|
||||
},
|
||||
}
|
||||
|
||||
// SysDeptImpl 部门表 数据层处理
|
||||
type SysDeptImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysDeptImpl) convertResultRows(rows []map[string]any) []model.SysDept {
|
||||
arr := make([]model.SysDept, 0)
|
||||
for _, row := range rows {
|
||||
sysDept := model.SysDept{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysDept, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysDept)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectDeptList 查询部门管理数据
|
||||
func (r *SysDeptImpl) SelectDeptList(sysDept model.SysDept, dataScopeSQL string) []model.SysDept {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysDept.DeptID != "" {
|
||||
conditions = append(conditions, "dept_id = ?")
|
||||
params = append(params, sysDept.DeptID)
|
||||
}
|
||||
if sysDept.ParentID != "" {
|
||||
conditions = append(conditions, "parent_id = ?")
|
||||
params = append(params, sysDept.ParentID)
|
||||
}
|
||||
if sysDept.DeptName != "" {
|
||||
conditions = append(conditions, "dept_name like concat(?, '%')")
|
||||
params = append(params, sysDept.DeptName)
|
||||
}
|
||||
if sysDept.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysDept.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := " where d.del_flag = '0' "
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " and " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
orderSql := " order by d.parent_id, d.order_num asc "
|
||||
querySql := r.selectSql + whereSql + dataScopeSQL + orderSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysDept{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectDeptListByRoleId 根据角色ID查询部门树信息
|
||||
func (r *SysDeptImpl) SelectDeptListByRoleId(roleId string, deptCheckStrictly bool) []string {
|
||||
querySql := `select d.dept_id as 'str' from sys_dept d
|
||||
left join sys_role_dept rd on d.dept_id = rd.dept_id
|
||||
where rd.role_id = ? `
|
||||
var params []any
|
||||
params = append(params, roleId)
|
||||
// 展开
|
||||
if deptCheckStrictly {
|
||||
querySql += ` and d.dept_id not in
|
||||
(select d.parent_id from sys_dept d
|
||||
inner join sys_role_dept rd on d.dept_id = rd.dept_id
|
||||
and rd.role_id = ?) `
|
||||
params = append(params, roleId)
|
||||
}
|
||||
querySql += "order by d.parent_id, d.order_num"
|
||||
|
||||
// 查询结果
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []string{}
|
||||
}
|
||||
|
||||
if len(results) > 0 {
|
||||
ids := make([]string, 0)
|
||||
for _, v := range results {
|
||||
ids = append(ids, fmt.Sprintf("%v", v["str"]))
|
||||
}
|
||||
return ids
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
// SelectDeptById 根据部门ID查询信息
|
||||
func (r *SysDeptImpl) SelectDeptById(deptId string) model.SysDept {
|
||||
querySql := `select d.dept_id, d.parent_id, d.ancestors,
|
||||
d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status,
|
||||
(select dept_name from sys_dept where dept_id = d.parent_id) parent_name
|
||||
from sys_dept d where d.dept_id = ?`
|
||||
results, err := datasource.RawDB("", querySql, []any{deptId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.SysDept{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.SysDept{}
|
||||
}
|
||||
|
||||
// SelectChildrenDeptById 根据ID查询所有子部门
|
||||
func (r *SysDeptImpl) SelectChildrenDeptById(deptId string) []model.SysDept {
|
||||
querySql := r.selectSql + " where find_in_set(?, d.ancestors)"
|
||||
results, err := datasource.RawDB("", querySql, []any{deptId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysDept{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// HasChildByDeptId 是否存在子节点
|
||||
func (r *SysDeptImpl) HasChildByDeptId(deptId string) int64 {
|
||||
querySql := "select count(1) as 'total' from sys_dept where status = '1' and parent_id = ? limit 1"
|
||||
results, err := datasource.RawDB("", querySql, []any{deptId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return 0
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return parse.Number(results[0]["total"])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// CheckDeptExistUser 查询部门是否存在用户
|
||||
func (r *SysDeptImpl) CheckDeptExistUser(deptId string) int64 {
|
||||
querySql := "select count(1) as 'total' from sys_user where dept_id = ? and del_flag = '0'"
|
||||
results, err := datasource.RawDB("", querySql, []any{deptId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return 0
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return parse.Number(results[0]["total"])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// CheckUniqueDept 校验部门是否唯一
|
||||
func (r *SysDeptImpl) CheckUniqueDept(sysDept model.SysDept) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysDept.DeptName != "" {
|
||||
conditions = append(conditions, "dept_name = ?")
|
||||
params = append(params, sysDept.DeptName)
|
||||
}
|
||||
if sysDept.ParentID != "" {
|
||||
conditions = append(conditions, "parent_id = ?")
|
||||
params = append(params, sysDept.ParentID)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
if whereSql == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select dept_id as 'str' from sys_dept " + whereSql + " and del_flag = '0' limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// InsertDept 新增部门信息
|
||||
func (r *SysDeptImpl) InsertDept(sysDept model.SysDept) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysDept.DeptID != "" {
|
||||
params["dept_id"] = sysDept.DeptID
|
||||
}
|
||||
if sysDept.ParentID != "" {
|
||||
params["parent_id"] = sysDept.ParentID
|
||||
}
|
||||
if sysDept.DeptName != "" {
|
||||
params["dept_name"] = sysDept.DeptName
|
||||
}
|
||||
if sysDept.Ancestors != "" {
|
||||
params["ancestors"] = sysDept.Ancestors
|
||||
}
|
||||
if sysDept.OrderNum > 0 {
|
||||
params["order_num"] = sysDept.OrderNum
|
||||
}
|
||||
if sysDept.Leader != "" {
|
||||
params["leader"] = sysDept.Leader
|
||||
}
|
||||
if sysDept.Phone != "" {
|
||||
params["phone"] = sysDept.Phone
|
||||
}
|
||||
if sysDept.Email != "" {
|
||||
params["email"] = sysDept.Email
|
||||
}
|
||||
if sysDept.Status != "" {
|
||||
params["status"] = sysDept.Status
|
||||
}
|
||||
if sysDept.CreateBy != "" {
|
||||
params["create_by"] = sysDept.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_dept (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateDept 修改部门信息
|
||||
func (r *SysDeptImpl) UpdateDept(sysDept model.SysDept) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysDept.ParentID != "" {
|
||||
params["parent_id"] = sysDept.ParentID
|
||||
}
|
||||
if sysDept.DeptName != "" {
|
||||
params["dept_name"] = sysDept.DeptName
|
||||
}
|
||||
if sysDept.Ancestors != "" {
|
||||
params["ancestors"] = sysDept.Ancestors
|
||||
}
|
||||
if sysDept.OrderNum > 0 {
|
||||
params["order_num"] = sysDept.OrderNum
|
||||
}
|
||||
if sysDept.Leader != "" {
|
||||
params["leader"] = sysDept.Leader
|
||||
}
|
||||
if sysDept.Phone != "" {
|
||||
params["phone"] = sysDept.Phone
|
||||
}
|
||||
if sysDept.Email != "" {
|
||||
params["email"] = sysDept.Email
|
||||
}
|
||||
if sysDept.Status != "" {
|
||||
params["status"] = sysDept.Status
|
||||
}
|
||||
if sysDept.UpdateBy != "" {
|
||||
params["update_by"] = sysDept.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_dept set " + strings.Join(keys, ",") + " where dept_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysDept.DeptID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// UpdateDeptStatusNormal 修改所在部门正常状态
|
||||
func (r *SysDeptImpl) UpdateDeptStatusNormal(deptIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(deptIds))
|
||||
sql := "update sys_dept set status = '1' where dept_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(deptIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("update err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// UpdateDeptChildren 修改子元素关系
|
||||
func (r *SysDeptImpl) UpdateDeptChildren(sysDepts []model.SysDept) int64 {
|
||||
// 无参数
|
||||
if len(sysDepts) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// 更新条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
for _, dept := range sysDepts {
|
||||
caseSql := fmt.Sprintf("WHEN dept_id = '%s' THEN '%s'", dept.DeptID, dept.Ancestors)
|
||||
conditions = append(conditions, caseSql)
|
||||
params = append(params, dept.DeptID)
|
||||
}
|
||||
|
||||
cases := strings.Join(conditions, " ")
|
||||
placeholders := repo.KeyPlaceholderByQuery(len(params))
|
||||
sql := "update sys_dept set ancestors = CASE " + cases + " END where dept_id in (" + placeholders + ")"
|
||||
results, err := datasource.ExecDB("", sql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// DeleteDeptById 删除部门管理信息
|
||||
func (r *SysDeptImpl) DeleteDeptById(deptId string) int64 {
|
||||
sql := "update sys_dept set status = '0', del_flag = '1' where dept_id = ?"
|
||||
results, err := datasource.ExecDB("", sql, []any{deptId})
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
33
src/modules/system/repository/sys_dict_data.go
Normal file
33
src/modules/system/repository/sys_dict_data.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysDictData 字典类型数据表 数据层接口
|
||||
type ISysDictData interface {
|
||||
// SelectDictDataPage 根据条件分页查询字典数据
|
||||
SelectDictDataPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectDictDataList 根据条件查询字典数据
|
||||
SelectDictDataList(sysDictData model.SysDictData) []model.SysDictData
|
||||
|
||||
// SelectDictDataByCodes 根据字典数据编码查询信息
|
||||
SelectDictDataByCodes(dictCodes []string) []model.SysDictData
|
||||
|
||||
// CountDictDataByType 查询字典数据
|
||||
CountDictDataByType(dictType string) int64
|
||||
|
||||
// CheckUniqueDictData 校验字典数据是否唯一
|
||||
CheckUniqueDictData(sysDictData model.SysDictData) string
|
||||
|
||||
// DeleteDictDataByCodes 批量删除字典数据信息
|
||||
DeleteDictDataByCodes(dictCodes []string) int64
|
||||
|
||||
// InsertDictData 新增字典数据信息
|
||||
InsertDictData(sysDictData model.SysDictData) string
|
||||
|
||||
// UpdateDictData 修改字典数据信息
|
||||
UpdateDictData(sysDictData model.SysDictData) int64
|
||||
|
||||
// UpdateDictDataType 同步修改字典类型
|
||||
UpdateDictDataType(oldDictType string, newDictType string) int64
|
||||
}
|
||||
368
src/modules/system/repository/sys_dict_data.impl.go
Normal file
368
src/modules/system/repository/sys_dict_data.impl.go
Normal file
@@ -0,0 +1,368 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysDictDataImpl 结构体
|
||||
var NewSysDictDataImpl = &SysDictDataImpl{
|
||||
selectSql: `select
|
||||
dict_code, dict_sort, dict_label, dict_value, dict_type, tag_class, tag_type, status, create_by, create_time, remark
|
||||
from sys_dict_data`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"dict_code": "DictCode",
|
||||
"dict_sort": "DictSort",
|
||||
"dict_label": "DictLabel",
|
||||
"dict_value": "DictValue",
|
||||
"dict_type": "DictType",
|
||||
"tag_class": "TagClass",
|
||||
"tag_type": "TagType",
|
||||
"status": "Status",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// SysDictDataImpl 字典类型数据表 数据层处理
|
||||
type SysDictDataImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysDictDataImpl) convertResultRows(rows []map[string]any) []model.SysDictData {
|
||||
arr := make([]model.SysDictData, 0)
|
||||
for _, row := range rows {
|
||||
sysDictData := model.SysDictData{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysDictData, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysDictData)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectDictDataPage 根据条件分页查询字典数据
|
||||
func (r *SysDictDataImpl) SelectDictDataPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["dictType"]; ok && v != "" {
|
||||
conditions = append(conditions, "dict_type = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["dictLabel"]; ok && v != "" {
|
||||
conditions = append(conditions, "dict_label like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysDictData{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_dict_data"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " order by dict_sort asc limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectDictDataList 根据条件查询字典数据
|
||||
func (r *SysDictDataImpl) SelectDictDataList(sysDictData model.SysDictData) []model.SysDictData {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysDictData.DictLabel != "" {
|
||||
conditions = append(conditions, "dict_label like concat(?, '%')")
|
||||
params = append(params, sysDictData.DictLabel)
|
||||
}
|
||||
if sysDictData.DictType != "" {
|
||||
conditions = append(conditions, "dict_type = ?")
|
||||
params = append(params, sysDictData.DictType)
|
||||
}
|
||||
if sysDictData.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysDictData.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
orderSql := " order by dict_sort asc "
|
||||
querySql := r.selectSql + whereSql + orderSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysDictData{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectDictDataByCodes 根据字典数据编码查询信息
|
||||
func (r *SysDictDataImpl) SelectDictDataByCodes(dictCodes []string) []model.SysDictData {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(dictCodes))
|
||||
querySql := r.selectSql + " where dict_code in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(dictCodes)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysDictData{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CountDictDataByType 查询字典数据
|
||||
func (r *SysDictDataImpl) CountDictDataByType(dictType string) int64 {
|
||||
querySql := "select count(1) as 'total' from sys_dict_data where dict_type = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{dictType})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return 0
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return parse.Number(results[0]["total"])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// CheckUniqueDictData 校验字典数据是否唯一
|
||||
func (r *SysDictDataImpl) CheckUniqueDictData(sysDictData model.SysDictData) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysDictData.DictType != "" {
|
||||
conditions = append(conditions, "dict_type = ?")
|
||||
params = append(params, sysDictData.DictType)
|
||||
}
|
||||
if sysDictData.DictLabel != "" {
|
||||
conditions = append(conditions, "dict_label = ?")
|
||||
params = append(params, sysDictData.DictLabel)
|
||||
}
|
||||
if sysDictData.DictValue != "" {
|
||||
conditions = append(conditions, "dict_value = ?")
|
||||
params = append(params, sysDictData.DictValue)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select dict_code as 'str' from sys_dict_data " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// DeleteDictDataByCodes 批量删除字典数据信息
|
||||
func (r *SysDictDataImpl) DeleteDictDataByCodes(dictCodes []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(dictCodes))
|
||||
sql := "delete from sys_dict_data where dict_code in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(dictCodes)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// InsertDictData 新增字典数据信息
|
||||
func (r *SysDictDataImpl) InsertDictData(sysDictData model.SysDictData) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysDictData.DictSort > 0 {
|
||||
params["dict_sort"] = sysDictData.DictSort
|
||||
}
|
||||
if sysDictData.DictLabel != "" {
|
||||
params["dict_label"] = sysDictData.DictLabel
|
||||
}
|
||||
if sysDictData.DictValue != "" {
|
||||
params["dict_value"] = sysDictData.DictValue
|
||||
}
|
||||
if sysDictData.DictType != "" {
|
||||
params["dict_type"] = sysDictData.DictType
|
||||
}
|
||||
if sysDictData.TagClass != "" {
|
||||
params["tag_class"] = sysDictData.TagClass
|
||||
}
|
||||
if sysDictData.TagType != "" {
|
||||
params["tag_type"] = sysDictData.TagType
|
||||
}
|
||||
if sysDictData.Status != "" {
|
||||
params["status"] = sysDictData.Status
|
||||
}
|
||||
if sysDictData.Remark != "" {
|
||||
params["remark"] = sysDictData.Remark
|
||||
}
|
||||
if sysDictData.CreateBy != "" {
|
||||
params["create_by"] = sysDictData.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_dict_data (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateDictData 修改字典数据信息
|
||||
func (r *SysDictDataImpl) UpdateDictData(sysDictData model.SysDictData) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysDictData.DictSort > 0 {
|
||||
params["dict_sort"] = sysDictData.DictSort
|
||||
}
|
||||
if sysDictData.DictLabel != "" {
|
||||
params["dict_label"] = sysDictData.DictLabel
|
||||
}
|
||||
if sysDictData.DictValue != "" {
|
||||
params["dict_value"] = sysDictData.DictValue
|
||||
}
|
||||
if sysDictData.DictType != "" {
|
||||
params["dict_type"] = sysDictData.DictType
|
||||
}
|
||||
if sysDictData.TagClass != "" {
|
||||
params["tag_class"] = sysDictData.TagClass
|
||||
}
|
||||
if sysDictData.TagType != "" {
|
||||
params["tag_type"] = sysDictData.TagType
|
||||
}
|
||||
if sysDictData.Status != "" {
|
||||
params["status"] = sysDictData.Status
|
||||
}
|
||||
if sysDictData.Remark != "" {
|
||||
params["remark"] = sysDictData.Remark
|
||||
}
|
||||
if sysDictData.UpdateBy != "" {
|
||||
params["update_by"] = sysDictData.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_dict_data set " + strings.Join(keys, ",") + " where dict_code = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysDictData.DictCode)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// UpdateDictDataType 同步修改字典类型
|
||||
func (r *SysDictDataImpl) UpdateDictDataType(oldDictType string, newDictType string) int64 {
|
||||
// 参数拼接
|
||||
params := make([]any, 0)
|
||||
if oldDictType == "" || newDictType == "" {
|
||||
return 0
|
||||
}
|
||||
params = append(params, newDictType)
|
||||
params = append(params, oldDictType)
|
||||
|
||||
// 构建执行语句
|
||||
sql := "update sys_dict_data set dict_type = ? where dict_type = ?"
|
||||
|
||||
// 执行更新
|
||||
rows, err := datasource.ExecDB("", sql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
30
src/modules/system/repository/sys_dict_type.go
Normal file
30
src/modules/system/repository/sys_dict_type.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysDictType 字典类型表 数据层接口
|
||||
type ISysDictType interface {
|
||||
// SelectDictTypePage 根据条件分页查询字典类型
|
||||
SelectDictTypePage(query map[string]any) map[string]any
|
||||
|
||||
// SelectDictTypeList 根据条件查询字典类型
|
||||
SelectDictTypeList(sysDictType model.SysDictType) []model.SysDictType
|
||||
|
||||
// SelectDictTypeByIDs 根据字典类型ID查询信息
|
||||
SelectDictTypeByIDs(dictIDs []string) []model.SysDictType
|
||||
|
||||
// SelectDictTypeByType 根据字典类型查询信息
|
||||
SelectDictTypeByType(dictType string) model.SysDictType
|
||||
|
||||
// CheckUniqueDictType 校验字典类型是否唯一
|
||||
CheckUniqueDictType(sysDictType model.SysDictType) string
|
||||
|
||||
// InsertDictType 新增字典类型信息
|
||||
InsertDictType(sysDictType model.SysDictType) string
|
||||
|
||||
// UpdateDictType 修改字典类型信息
|
||||
UpdateDictType(sysDictType model.SysDictType) int64
|
||||
|
||||
// DeleteDictTypeByIDs 批量删除字典类型信息
|
||||
DeleteDictTypeByIDs(dictIDs []string) int64
|
||||
}
|
||||
334
src/modules/system/repository/sys_dict_type.impl.go
Normal file
334
src/modules/system/repository/sys_dict_type.impl.go
Normal file
@@ -0,0 +1,334 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysDictTypeImpl 结构体
|
||||
var NewSysDictTypeImpl = &SysDictTypeImpl{
|
||||
selectSql: `select
|
||||
dict_id, dict_name, dict_type, status, create_by, create_time, remark
|
||||
from sys_dict_type`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"dict_id": "DictID",
|
||||
"dict_name": "DictName",
|
||||
"dict_type": "DictType",
|
||||
"remark": "Remark",
|
||||
"status": "Status",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// SysDictTypeImpl 字典类型表 数据层处理
|
||||
type SysDictTypeImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysDictTypeImpl) convertResultRows(rows []map[string]any) []model.SysDictType {
|
||||
arr := make([]model.SysDictType, 0)
|
||||
for _, row := range rows {
|
||||
sysDictType := model.SysDictType{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysDictType, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysDictType)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectDictTypePage 根据条件分页查询字典类型
|
||||
func (r *SysDictTypeImpl) SelectDictTypePage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["dictName"]; ok && v != "" {
|
||||
conditions = append(conditions, "dict_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["dictType"]; ok && v != "" {
|
||||
conditions = append(conditions, "dict_type like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "create_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysDictType{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_dict_type"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectDictTypeList 根据条件查询字典类型
|
||||
func (r *SysDictTypeImpl) SelectDictTypeList(sysDictType model.SysDictType) []model.SysDictType {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysDictType.DictName != "" {
|
||||
conditions = append(conditions, "dict_name like concat(?, '%')")
|
||||
params = append(params, sysDictType.DictName)
|
||||
}
|
||||
if sysDictType.DictType != "" {
|
||||
conditions = append(conditions, "dict_type like concat(?, '%')")
|
||||
params = append(params, sysDictType.DictType)
|
||||
}
|
||||
if sysDictType.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysDictType.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysDictType{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectDictTypeByIDs 根据字典类型ID查询信息
|
||||
func (r *SysDictTypeImpl) SelectDictTypeByIDs(dictIDs []string) []model.SysDictType {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(dictIDs))
|
||||
querySql := r.selectSql + " where dict_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(dictIDs)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysDictType{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectDictTypeByType 根据字典类型查询信息
|
||||
func (r *SysDictTypeImpl) SelectDictTypeByType(dictType string) model.SysDictType {
|
||||
querySql := r.selectSql + " where dict_type = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{dictType})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.SysDictType{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.SysDictType{}
|
||||
}
|
||||
|
||||
// CheckUniqueDictType 校验字典是否唯一
|
||||
func (r *SysDictTypeImpl) CheckUniqueDictType(sysDictType model.SysDictType) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysDictType.DictName != "" {
|
||||
conditions = append(conditions, "dict_name = ?")
|
||||
params = append(params, sysDictType.DictName)
|
||||
}
|
||||
if sysDictType.DictType != "" {
|
||||
conditions = append(conditions, "dict_type = ?")
|
||||
params = append(params, sysDictType.DictType)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select dict_id as 'str' from sys_dict_type " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// InsertDictType 新增字典类型信息
|
||||
func (r *SysDictTypeImpl) InsertDictType(sysDictType model.SysDictType) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysDictType.DictName != "" {
|
||||
params["dict_name"] = sysDictType.DictName
|
||||
}
|
||||
if sysDictType.DictType != "" {
|
||||
params["dict_type"] = sysDictType.DictType
|
||||
}
|
||||
if sysDictType.Status != "" {
|
||||
params["status"] = sysDictType.Status
|
||||
}
|
||||
if sysDictType.Remark != "" {
|
||||
params["remark"] = sysDictType.Remark
|
||||
}
|
||||
if sysDictType.CreateBy != "" {
|
||||
params["create_by"] = sysDictType.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_dict_type (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateDictType 修改字典类型信息
|
||||
func (r *SysDictTypeImpl) UpdateDictType(sysDictType model.SysDictType) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysDictType.DictName != "" {
|
||||
params["dict_name"] = sysDictType.DictName
|
||||
}
|
||||
if sysDictType.DictType != "" {
|
||||
params["dict_type"] = sysDictType.DictType
|
||||
}
|
||||
if sysDictType.Status != "" {
|
||||
params["status"] = sysDictType.Status
|
||||
}
|
||||
if sysDictType.Remark != "" {
|
||||
params["remark"] = sysDictType.Remark
|
||||
}
|
||||
if sysDictType.UpdateBy != "" {
|
||||
params["update_by"] = sysDictType.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_dict_type set " + strings.Join(keys, ",") + " where dict_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysDictType.DictID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteDictTypeByIDs 批量删除字典类型信息
|
||||
func (r *SysDictTypeImpl) DeleteDictTypeByIDs(dictIDs []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(dictIDs))
|
||||
sql := "delete from sys_dict_type where dict_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(dictIDs)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
21
src/modules/system/repository/sys_log_login.go
Normal file
21
src/modules/system/repository/sys_log_login.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysLogLogin 系统登录日志表 数据层接口
|
||||
type ISysLogLogin interface {
|
||||
// SelectSysLogLoginPage 分页查询系统登录日志集合
|
||||
SelectSysLogLoginPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectSysLogLoginList 查询系统登录日志集合
|
||||
SelectSysLogLoginList(sysLogLogin model.SysLogLogin) []model.SysLogLogin
|
||||
|
||||
// InsertSysLogLogin 新增系统登录日志
|
||||
InsertSysLogLogin(sysLogLogin model.SysLogLogin) string
|
||||
|
||||
// DeleteSysLogLoginByIds 批量删除系统登录日志
|
||||
DeleteSysLogLoginByIds(loginIds []string) int64
|
||||
|
||||
// CleanSysLogLogin 清空系统登录日志
|
||||
CleanSysLogLogin() error
|
||||
}
|
||||
245
src/modules/system/repository/sys_log_login.impl.go
Normal file
245
src/modules/system/repository/sys_log_login.impl.go
Normal file
@@ -0,0 +1,245 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysLogLoginImpl 结构体
|
||||
var NewSysLogLoginImpl = &SysLogLoginImpl{
|
||||
selectSql: `select login_id, user_name, ipaddr, login_location,
|
||||
browser, os, status, msg, login_time from sys_log_login`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"login_id": "LoginID",
|
||||
"user_name": "UserName",
|
||||
"status": "Status",
|
||||
"ipaddr": "IPAddr",
|
||||
"login_location": "LoginLocation",
|
||||
"browser": "Browser",
|
||||
"os": "OS",
|
||||
"msg": "Msg",
|
||||
"login_time": "LoginTime",
|
||||
},
|
||||
}
|
||||
|
||||
// SysLogLoginImpl 系统登录访问表 数据层处理
|
||||
type SysLogLoginImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysLogLoginImpl) convertResultRows(rows []map[string]any) []model.SysLogLogin {
|
||||
arr := make([]model.SysLogLogin, 0)
|
||||
for _, row := range rows {
|
||||
SysLogLogin := model.SysLogLogin{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&SysLogLogin, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, SysLogLogin)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectSysLogLoginPage 分页查询系统登录日志集合
|
||||
func (r *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["ipaddr"]; ok && v != "" {
|
||||
conditions = append(conditions, "ipaddr like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["userName"]; ok && v != "" {
|
||||
conditions = append(conditions, "user_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "login_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "login_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysLogLogin{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_log_login"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " order by login_id desc limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectSysLogLoginList 查询系统登录日志集合
|
||||
func (r *SysLogLoginImpl) SelectSysLogLoginList(SysLogLogin model.SysLogLogin) []model.SysLogLogin {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if SysLogLogin.IPAddr != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, SysLogLogin.IPAddr)
|
||||
}
|
||||
if SysLogLogin.UserName != "" {
|
||||
conditions = append(conditions, "user_name like concat(?, '%')")
|
||||
params = append(params, SysLogLogin.UserName)
|
||||
}
|
||||
if SysLogLogin.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, SysLogLogin.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysLogLogin{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// InsertSysLogLogin 新增系统登录日志
|
||||
func (r *SysLogLoginImpl) InsertSysLogLogin(SysLogLogin model.SysLogLogin) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
params["login_time"] = time.Now().UnixMilli()
|
||||
if SysLogLogin.UserName != "" {
|
||||
params["user_name"] = SysLogLogin.UserName
|
||||
}
|
||||
if SysLogLogin.Status != "" {
|
||||
params["status"] = SysLogLogin.Status
|
||||
}
|
||||
if SysLogLogin.IPAddr != "" {
|
||||
params["ipaddr"] = SysLogLogin.IPAddr
|
||||
}
|
||||
if SysLogLogin.LoginLocation != "" {
|
||||
params["login_location"] = SysLogLogin.LoginLocation
|
||||
}
|
||||
if SysLogLogin.Browser != "" {
|
||||
params["browser"] = SysLogLogin.Browser
|
||||
}
|
||||
if SysLogLogin.OS != "" {
|
||||
params["os"] = SysLogLogin.OS
|
||||
}
|
||||
if SysLogLogin.Msg != "" {
|
||||
params["msg"] = SysLogLogin.Msg
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_log_login (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// DeleteSysLogLoginByIds 批量删除系统登录日志
|
||||
func (r *SysLogLoginImpl) DeleteSysLogLoginByIds(loginIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(loginIds))
|
||||
sql := "delete from sys_log_login where login_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(loginIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// CleanSysLogLogin 清空系统登录日志
|
||||
func (r *SysLogLoginImpl) CleanSysLogLogin() error {
|
||||
sql := "truncate table sys_log_login"
|
||||
_, err := datasource.ExecDB("", sql, []any{})
|
||||
return err
|
||||
}
|
||||
24
src/modules/system/repository/sys_log_operate.go
Normal file
24
src/modules/system/repository/sys_log_operate.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysLogOperate 操作日志表 数据层接口
|
||||
type ISysLogOperate interface {
|
||||
// SelectSysLogOperatePage 分页查询系统操作日志集合
|
||||
SelectSysLogOperatePage(query map[string]any) map[string]any
|
||||
|
||||
// SelectSysLogOperateList 查询系统操作日志集合
|
||||
SelectSysLogOperateList(sysLogOperate model.SysLogOperate) []model.SysLogOperate
|
||||
|
||||
// SelectSysLogOperateById 查询操作日志详细
|
||||
SelectSysLogOperateById(operId string) model.SysLogOperate
|
||||
|
||||
// InsertSysLogOperate 新增操作日志
|
||||
InsertSysLogOperate(sysLogOperate model.SysLogOperate) string
|
||||
|
||||
// DeleteSysLogOperateByIds 批量删除系统操作日志
|
||||
DeleteSysLogOperateByIds(operIds []string) int64
|
||||
|
||||
// CleanSysLogOperate 清空操作日志
|
||||
CleanSysLogOperate() error
|
||||
}
|
||||
299
src/modules/system/repository/sys_log_operate.impl.go
Normal file
299
src/modules/system/repository/sys_log_operate.impl.go
Normal file
@@ -0,0 +1,299 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysLogOperateImpl 结构体
|
||||
var NewSysLogOperateImpl = &SysLogOperateImpl{
|
||||
selectSql: `select
|
||||
oper_id, title, business_type, method, request_method, operator_type, oper_name, dept_name,
|
||||
oper_url, oper_ip, oper_location, oper_param, oper_msg, status, oper_time, cost_time
|
||||
from sys_log_operate`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"oper_id": "OperID",
|
||||
"title": "Title",
|
||||
"business_type": "BusinessType",
|
||||
"method": "Method",
|
||||
"request_method": "RequestMethod",
|
||||
"operator_type": "OperatorType",
|
||||
"oper_name": "OperName",
|
||||
"dept_name": "DeptName",
|
||||
"oper_url": "OperURL",
|
||||
"oper_ip": "OperIP",
|
||||
"oper_location": "OperLocation",
|
||||
"oper_param": "OperParam",
|
||||
"oper_msg": "OperMsg",
|
||||
"status": "Status",
|
||||
"oper_time": "OperTime",
|
||||
"cost_time": "CostTime",
|
||||
},
|
||||
}
|
||||
|
||||
// SysLogOperateImpl 操作日志表 数据层处理
|
||||
type SysLogOperateImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysLogOperateImpl) convertResultRows(rows []map[string]any) []model.SysLogOperate {
|
||||
arr := make([]model.SysLogOperate, 0)
|
||||
for _, row := range rows {
|
||||
SysLogOperate := model.SysLogOperate{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&SysLogOperate, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, SysLogOperate)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectSysLogOperatePage 分页查询系统操作日志集合
|
||||
func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["title"]; ok && v != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["businessType"]; ok && v != "" {
|
||||
conditions = append(conditions, "business_type = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["operName"]; ok && v != "" {
|
||||
conditions = append(conditions, "oper_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "oper_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "oper_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysLogOperate{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_log_operate"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " order by oper_id desc limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectSysLogOperateList 查询系统操作日志集合
|
||||
func (r *SysLogOperateImpl) SelectSysLogOperateList(SysLogOperate model.SysLogOperate) []model.SysLogOperate {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if SysLogOperate.Title != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, SysLogOperate.Title)
|
||||
}
|
||||
if SysLogOperate.BusinessType != "" {
|
||||
conditions = append(conditions, "business_type = ?")
|
||||
params = append(params, SysLogOperate.BusinessType)
|
||||
}
|
||||
if SysLogOperate.OperName != "" {
|
||||
conditions = append(conditions, "oper_name like concat(?, '%')")
|
||||
params = append(params, SysLogOperate.OperName)
|
||||
}
|
||||
if SysLogOperate.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, SysLogOperate.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysLogOperate{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectSysLogOperateById 查询操作日志详细
|
||||
func (r *SysLogOperateImpl) SelectSysLogOperateById(operId string) model.SysLogOperate {
|
||||
querySql := r.selectSql + " where oper_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{operId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.SysLogOperate{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.SysLogOperate{}
|
||||
}
|
||||
|
||||
// InsertSysLogOperate 新增操作日志
|
||||
func (r *SysLogOperateImpl) InsertSysLogOperate(SysLogOperate model.SysLogOperate) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
params["oper_time"] = time.Now().UnixMilli()
|
||||
if SysLogOperate.Title != "" {
|
||||
params["title"] = SysLogOperate.Title
|
||||
}
|
||||
if SysLogOperate.BusinessType != "" {
|
||||
params["business_type"] = SysLogOperate.BusinessType
|
||||
}
|
||||
if SysLogOperate.Method != "" {
|
||||
params["method"] = SysLogOperate.Method
|
||||
}
|
||||
if SysLogOperate.RequestMethod != "" {
|
||||
params["request_method"] = SysLogOperate.RequestMethod
|
||||
}
|
||||
if SysLogOperate.OperatorType != "" {
|
||||
params["operator_type"] = SysLogOperate.OperatorType
|
||||
}
|
||||
if SysLogOperate.DeptName != "" {
|
||||
params["dept_name"] = SysLogOperate.DeptName
|
||||
}
|
||||
if SysLogOperate.OperName != "" {
|
||||
params["oper_name"] = SysLogOperate.OperName
|
||||
}
|
||||
if SysLogOperate.OperURL != "" {
|
||||
params["oper_url"] = SysLogOperate.OperURL
|
||||
}
|
||||
if SysLogOperate.OperIP != "" {
|
||||
params["oper_ip"] = SysLogOperate.OperIP
|
||||
}
|
||||
if SysLogOperate.OperLocation != "" {
|
||||
params["oper_location"] = SysLogOperate.OperLocation
|
||||
}
|
||||
if SysLogOperate.OperParam != "" {
|
||||
params["oper_param"] = SysLogOperate.OperParam
|
||||
}
|
||||
if SysLogOperate.OperMsg != "" {
|
||||
params["oper_msg"] = SysLogOperate.OperMsg
|
||||
}
|
||||
if SysLogOperate.Status != "" {
|
||||
params["status"] = SysLogOperate.Status
|
||||
}
|
||||
if SysLogOperate.CostTime > 0 {
|
||||
params["cost_time"] = SysLogOperate.CostTime
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_log_operate (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// DeleteSysLogOperateByIds 批量删除系统操作日志
|
||||
func (r *SysLogOperateImpl) DeleteSysLogOperateByIds(operIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(operIds))
|
||||
sql := "delete from sys_log_operate where oper_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(operIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// CleanSysLogOperate 清空操作日志
|
||||
func (r *SysLogOperateImpl) CleanSysLogOperate() error {
|
||||
sql := "truncate table sys_log_operate"
|
||||
_, err := datasource.ExecDB("", sql, []any{})
|
||||
return err
|
||||
}
|
||||
36
src/modules/system/repository/sys_menu.go
Normal file
36
src/modules/system/repository/sys_menu.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysMenu 菜单表 数据层接口
|
||||
type ISysMenu interface {
|
||||
// SelectMenuList 查询系统菜单列表
|
||||
SelectMenuList(sysMenu model.SysMenu, userId string) []model.SysMenu
|
||||
|
||||
// SelectMenuPermsByUserId 根据用户ID查询权限
|
||||
SelectMenuPermsByUserId(userId string) []string
|
||||
|
||||
// SelectMenuTreeByUserId 根据用户ID查询菜单
|
||||
SelectMenuTreeByUserId(userId string) []model.SysMenu
|
||||
|
||||
// SelectMenuListByRoleId 根据角色ID查询菜单树信息
|
||||
SelectMenuListByRoleId(roleId string, menuCheckStrictly bool) []string
|
||||
|
||||
// SelectMenuByIds 根据菜单ID查询信息
|
||||
SelectMenuByIds(menuIds []string) []model.SysMenu
|
||||
|
||||
// HasChildByMenuIdAndStatus 存在菜单子节点数量与状态
|
||||
HasChildByMenuIdAndStatus(menuId, status string) int64
|
||||
|
||||
// InsertMenu 新增菜单信息
|
||||
InsertMenu(sysMenu model.SysMenu) string
|
||||
|
||||
// UpdateMenu 修改菜单信息
|
||||
UpdateMenu(sysMenu model.SysMenu) int64
|
||||
|
||||
// DeleteMenuById 删除菜单管理信息
|
||||
DeleteMenuById(menuId string) int64
|
||||
|
||||
// CheckUniqueMenu 校验菜单是否唯一
|
||||
CheckUniqueMenu(sysMenu model.SysMenu) string
|
||||
}
|
||||
476
src/modules/system/repository/sys_menu.impl.go
Normal file
476
src/modules/system/repository/sys_menu.impl.go
Normal file
@@ -0,0 +1,476 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/constants/menu"
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysMenuImpl 结构体
|
||||
var NewSysMenuImpl = &SysMenuImpl{
|
||||
selectSql: `select
|
||||
m.menu_id, m.menu_name, m.parent_id, m.menu_sort, m.path, m.component, m.is_frame, m.is_cache, m.menu_type, m.visible, m.status, ifnull(m.perms,'') as perms, m.icon, m.create_time, m.remark
|
||||
from sys_menu m`,
|
||||
|
||||
selectSqlByUser: `select distinct
|
||||
m.menu_id, m.menu_name, m.parent_id, m.menu_sort, m.path, m.component, m.is_frame, m.is_cache, m.menu_type, m.visible, m.status, ifnull(m.perms,'') as perms, m.icon, m.create_time, m.remark
|
||||
from sys_menu m
|
||||
left join sys_role_menu rm on m.menu_id = rm.menu_id
|
||||
left join sys_user_role ur on rm.role_id = ur.role_id
|
||||
left join sys_role ro on ur.role_id = ro.role_id`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"menu_id": "MenuID",
|
||||
"menu_name": "MenuName",
|
||||
"parent_name": "ParentName",
|
||||
"parent_id": "ParentID",
|
||||
"path": "Path",
|
||||
"menu_sort": "MenuSort",
|
||||
"component": "Component",
|
||||
"is_frame": "IsFrame",
|
||||
"is_cache": "IsCache",
|
||||
"menu_type": "MenuType",
|
||||
"visible": "Visible",
|
||||
"status": "Status",
|
||||
"perms": "Perms",
|
||||
"icon": "Icon",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"remark": "Remark",
|
||||
},
|
||||
}
|
||||
|
||||
// SysMenuImpl 菜单表 数据层处理
|
||||
type SysMenuImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 查询视图用户对象SQL
|
||||
selectSqlByUser string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysMenuImpl) convertResultRows(rows []map[string]any) []model.SysMenu {
|
||||
arr := make([]model.SysMenu, 0)
|
||||
for _, row := range rows {
|
||||
sysMenu := model.SysMenu{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysMenu, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysMenu)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectMenuList 查询系统菜单列表
|
||||
func (r *SysMenuImpl) SelectMenuList(sysMenu model.SysMenu, userId string) []model.SysMenu {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysMenu.MenuName != "" {
|
||||
conditions = append(conditions, "m.menu_name like concat(?, '%')")
|
||||
params = append(params, sysMenu.MenuName)
|
||||
}
|
||||
if sysMenu.Visible != "" {
|
||||
conditions = append(conditions, "m.visible = ?")
|
||||
params = append(params, sysMenu.Visible)
|
||||
}
|
||||
if sysMenu.Status != "" {
|
||||
conditions = append(conditions, "m.status = ?")
|
||||
params = append(params, sysMenu.Status)
|
||||
}
|
||||
|
||||
fromSql := r.selectSql
|
||||
|
||||
// 个人菜单
|
||||
if userId != "*" {
|
||||
fromSql = r.selectSqlByUser
|
||||
conditions = append(conditions, "ur.user_id = ?")
|
||||
params = append(params, userId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
orderSql := " order by m.parent_id, m.menu_sort"
|
||||
querySql := fromSql + whereSql + orderSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysMenu{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectMenuPermsByUserId 根据用户ID查询权限
|
||||
func (r *SysMenuImpl) SelectMenuPermsByUserId(userId string) []string {
|
||||
querySql := `select distinct m.perms as 'str' from sys_menu m
|
||||
left join sys_role_menu rm on m.menu_id = rm.menu_id
|
||||
left join sys_user_role ur on rm.role_id = ur.role_id
|
||||
left join sys_role r on r.role_id = ur.role_id
|
||||
where m.status = '1' and m.perms != '' and r.status = '1' and ur.user_id = ? `
|
||||
|
||||
// 查询结果
|
||||
results, err := datasource.RawDB("", querySql, []any{userId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []string{}
|
||||
}
|
||||
|
||||
// 读取结果
|
||||
rows := make([]string, 0)
|
||||
for _, m := range results {
|
||||
rows = append(rows, fmt.Sprintf("%v", m["str"]))
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// SelectMenuTreeByUserId 根据用户ID查询菜单
|
||||
func (r *SysMenuImpl) SelectMenuTreeByUserId(userId string) []model.SysMenu {
|
||||
var params []any
|
||||
var querySql string
|
||||
|
||||
if userId == "*" {
|
||||
// 管理员全部菜单
|
||||
querySql = r.selectSql + ` where
|
||||
m.menu_type in (?,?) and m.status = '1'
|
||||
order by m.parent_id, m.menu_sort`
|
||||
params = append(params, menu.TYPE_DIR)
|
||||
params = append(params, menu.TYPE_MENU)
|
||||
} else {
|
||||
// 用户ID权限
|
||||
querySql = r.selectSqlByUser + ` where
|
||||
m.menu_type in (?, ?) and m.status = '1'
|
||||
and ur.user_id = ? and ro.status = '1'
|
||||
order by m.parent_id, m.menu_sort`
|
||||
params = append(params, menu.TYPE_DIR)
|
||||
params = append(params, menu.TYPE_MENU)
|
||||
params = append(params, userId)
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysMenu{}
|
||||
}
|
||||
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectMenuListByRoleId 根据角色ID查询菜单树信息
|
||||
func (r *SysMenuImpl) SelectMenuListByRoleId(roleId string, menuCheckStrictly bool) []string {
|
||||
querySql := `select m.menu_id as 'str' from sys_menu m
|
||||
left join sys_role_menu rm on m.menu_id = rm.menu_id
|
||||
where rm.role_id = ? `
|
||||
var params []any
|
||||
params = append(params, roleId)
|
||||
// 展开
|
||||
if menuCheckStrictly {
|
||||
querySql += ` and m.menu_id not in
|
||||
(select m.parent_id from sys_menu m
|
||||
inner join sys_role_menu rm on m.menu_id = rm.menu_id
|
||||
and rm.role_id = ?) `
|
||||
params = append(params, roleId)
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []string{}
|
||||
}
|
||||
|
||||
if len(results) > 0 {
|
||||
ids := make([]string, 0)
|
||||
for _, v := range results {
|
||||
ids = append(ids, fmt.Sprintf("%v", v["str"]))
|
||||
}
|
||||
return ids
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
// SelectMenuByIds 根据菜单ID查询信息
|
||||
func (r *SysMenuImpl) SelectMenuByIds(menuIds []string) []model.SysMenu {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(menuIds))
|
||||
querySql := r.selectSql + " where m.menu_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(menuIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysMenu{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// HasChildByMenuIdAndStatus 存在菜单子节点数量与状态
|
||||
func (r *SysMenuImpl) HasChildByMenuIdAndStatus(menuId, status string) int64 {
|
||||
querySql := "select count(1) as 'total' from sys_menu where parent_id = ?"
|
||||
params := []any{menuId}
|
||||
|
||||
// 菜单状态
|
||||
if status != "" {
|
||||
querySql += " and status = ? and menu_type in (?, ?) "
|
||||
params = append(params, status)
|
||||
params = append(params, menu.TYPE_DIR)
|
||||
params = append(params, menu.TYPE_MENU)
|
||||
}
|
||||
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return 0
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return parse.Number(results[0]["total"])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// InsertMenu 新增菜单信息
|
||||
func (r *SysMenuImpl) InsertMenu(sysMenu model.SysMenu) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysMenu.MenuID != "" {
|
||||
params["menu_id"] = sysMenu.MenuID
|
||||
}
|
||||
if sysMenu.ParentID != "" {
|
||||
params["parent_id"] = sysMenu.ParentID
|
||||
}
|
||||
if sysMenu.MenuName != "" {
|
||||
params["menu_name"] = sysMenu.MenuName
|
||||
}
|
||||
if sysMenu.MenuSort > 0 {
|
||||
params["menu_sort"] = sysMenu.MenuSort
|
||||
}
|
||||
if sysMenu.Path != "" {
|
||||
params["path"] = sysMenu.Path
|
||||
}
|
||||
if sysMenu.Component != "" {
|
||||
params["component"] = sysMenu.Component
|
||||
}
|
||||
if sysMenu.IsFrame != "" {
|
||||
params["is_frame"] = sysMenu.IsFrame
|
||||
}
|
||||
if sysMenu.IsCache != "" {
|
||||
params["is_cache"] = sysMenu.IsCache
|
||||
}
|
||||
if sysMenu.MenuType != "" {
|
||||
params["menu_type"] = sysMenu.MenuType
|
||||
}
|
||||
if sysMenu.Visible != "" {
|
||||
params["visible"] = sysMenu.Visible
|
||||
}
|
||||
if sysMenu.Status != "" {
|
||||
params["status"] = sysMenu.Status
|
||||
}
|
||||
if sysMenu.Perms != "" {
|
||||
params["perms"] = sysMenu.Perms
|
||||
}
|
||||
if sysMenu.Icon != "" {
|
||||
params["icon"] = sysMenu.Icon
|
||||
} else {
|
||||
params["icon"] = "#"
|
||||
}
|
||||
if sysMenu.Remark != "" {
|
||||
params["remark"] = sysMenu.Remark
|
||||
}
|
||||
if sysMenu.CreateBy != "" {
|
||||
params["create_by"] = sysMenu.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 根据菜单类型重置参数
|
||||
if sysMenu.MenuType == menu.TYPE_BUTTON {
|
||||
params["component"] = ""
|
||||
params["path"] = ""
|
||||
params["icon"] = "#"
|
||||
params["is_cache"] = "1"
|
||||
params["is_frame"] = "1"
|
||||
params["visible"] = "1"
|
||||
params["status"] = "1"
|
||||
}
|
||||
if sysMenu.MenuType == menu.TYPE_DIR {
|
||||
params["component"] = ""
|
||||
params["perms"] = ""
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_menu (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateMenu 修改菜单信息
|
||||
func (r *SysMenuImpl) UpdateMenu(sysMenu model.SysMenu) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysMenu.MenuID != "" {
|
||||
params["menu_id"] = sysMenu.MenuID
|
||||
}
|
||||
if sysMenu.ParentID != "" {
|
||||
params["parent_id"] = sysMenu.ParentID
|
||||
}
|
||||
if sysMenu.MenuName != "" {
|
||||
params["menu_name"] = sysMenu.MenuName
|
||||
}
|
||||
if sysMenu.MenuSort > 0 {
|
||||
params["menu_sort"] = sysMenu.MenuSort
|
||||
}
|
||||
if sysMenu.Path != "" {
|
||||
params["path"] = sysMenu.Path
|
||||
}
|
||||
if sysMenu.Component != "" {
|
||||
params["component"] = sysMenu.Component
|
||||
}
|
||||
if sysMenu.IsFrame != "" {
|
||||
params["is_frame"] = sysMenu.IsFrame
|
||||
}
|
||||
if sysMenu.IsCache != "" {
|
||||
params["is_cache"] = sysMenu.IsCache
|
||||
}
|
||||
if sysMenu.MenuType != "" {
|
||||
params["menu_type"] = sysMenu.MenuType
|
||||
}
|
||||
if sysMenu.Visible != "" {
|
||||
params["visible"] = sysMenu.Visible
|
||||
}
|
||||
if sysMenu.Status != "" {
|
||||
params["status"] = sysMenu.Status
|
||||
}
|
||||
if sysMenu.Perms != "" {
|
||||
params["perms"] = sysMenu.Perms
|
||||
}
|
||||
if sysMenu.Icon != "" {
|
||||
params["icon"] = sysMenu.Icon
|
||||
} else {
|
||||
params["icon"] = "#"
|
||||
}
|
||||
if sysMenu.Remark != "" {
|
||||
params["remark"] = sysMenu.Remark
|
||||
}
|
||||
if sysMenu.UpdateBy != "" {
|
||||
params["update_by"] = sysMenu.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 根据菜单类型重置参数
|
||||
if sysMenu.MenuType == menu.TYPE_BUTTON {
|
||||
params["component"] = ""
|
||||
params["path"] = ""
|
||||
params["icon"] = "#"
|
||||
params["is_cache"] = "1"
|
||||
params["is_frame"] = "1"
|
||||
params["visible"] = "1"
|
||||
params["status"] = "1"
|
||||
}
|
||||
if sysMenu.MenuType == menu.TYPE_DIR {
|
||||
params["component"] = ""
|
||||
params["perms"] = ""
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_menu set " + strings.Join(keys, ",") + " where menu_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysMenu.MenuID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteMenuById 删除菜单管理信息
|
||||
func (r *SysMenuImpl) DeleteMenuById(menuId string) int64 {
|
||||
sql := "delete from sys_menu where menu_id = ?"
|
||||
results, err := datasource.ExecDB("", sql, []any{menuId})
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// CheckUniqueMenu 校验菜单是否唯一
|
||||
func (r *SysMenuImpl) CheckUniqueMenu(sysMenu model.SysMenu) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysMenu.MenuName != "" {
|
||||
conditions = append(conditions, "menu_name = ?")
|
||||
params = append(params, sysMenu.MenuName)
|
||||
}
|
||||
if sysMenu.ParentID != "" {
|
||||
conditions = append(conditions, "parent_id = ?")
|
||||
params = append(params, sysMenu.ParentID)
|
||||
}
|
||||
if sysMenu.Path != "" {
|
||||
conditions = append(conditions, "path = ?")
|
||||
params = append(params, sysMenu.Path)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
if whereSql == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select menu_id as 'str' from sys_menu " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
24
src/modules/system/repository/sys_notice.go
Normal file
24
src/modules/system/repository/sys_notice.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysNotice 通知公告表 数据层接口
|
||||
type ISysNotice interface {
|
||||
// SelectNoticePage 分页查询公告列表
|
||||
SelectNoticePage(query map[string]any) map[string]any
|
||||
|
||||
// SelectNoticeList 查询公告列表
|
||||
SelectNoticeList(sysNotice model.SysNotice) []model.SysNotice
|
||||
|
||||
// SelectNoticeById 查询公告信息
|
||||
SelectNoticeByIds(noticeIds []string) []model.SysNotice
|
||||
|
||||
// InsertNotice 新增公告
|
||||
InsertNotice(sysNotice model.SysNotice) string
|
||||
|
||||
// UpdateNotice 修改公告
|
||||
UpdateNotice(sysNotice model.SysNotice) int64
|
||||
|
||||
// DeleteNoticeByIds 批量删除公告信息
|
||||
DeleteNoticeByIds(noticeIds []string) int64
|
||||
}
|
||||
297
src/modules/system/repository/sys_notice.impl.go
Normal file
297
src/modules/system/repository/sys_notice.impl.go
Normal file
@@ -0,0 +1,297 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysNoticeImpl 结构体
|
||||
var NewSysNoticeImpl = &SysNoticeImpl{
|
||||
selectSql: `select
|
||||
notice_id, notice_title, notice_type, notice_content, status, del_flag,
|
||||
create_by, create_time, update_by, update_time, remark from sys_notice`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"notice_id": "NoticeID",
|
||||
"notice_title": "NoticeTitle",
|
||||
"notice_type": "NoticeType",
|
||||
"notice_content": "NoticeContent",
|
||||
"status": "Status",
|
||||
"del_flag": "DelFlag",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"remark": "Remark",
|
||||
},
|
||||
}
|
||||
|
||||
// SysNoticeImpl 通知公告表 数据层处理
|
||||
type SysNoticeImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysNoticeImpl) convertResultRows(rows []map[string]any) []model.SysNotice {
|
||||
arr := make([]model.SysNotice, 0)
|
||||
for _, row := range rows {
|
||||
sysNotice := model.SysNotice{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysNotice, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysNotice)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectNoticePage 分页查询公告列表
|
||||
func (r *SysNoticeImpl) SelectNoticePage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["noticeTitle"]; ok && v != "" {
|
||||
conditions = append(conditions, "notice_title like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["noticeType"]; ok && v != "" {
|
||||
conditions = append(conditions, "notice_type = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["createBy"]; ok && v != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "create_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := " where del_flag = '0' "
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " and " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysNotice{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_notice"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectNoticeList 查询公告列表
|
||||
func (r *SysNoticeImpl) SelectNoticeList(sysNotice model.SysNotice) []model.SysNotice {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysNotice.NoticeTitle != "" {
|
||||
conditions = append(conditions, "notice_title like concat(?, '%')")
|
||||
params = append(params, sysNotice.NoticeTitle)
|
||||
}
|
||||
if sysNotice.NoticeType != "" {
|
||||
conditions = append(conditions, "notice_type = ?")
|
||||
params = append(params, sysNotice.NoticeType)
|
||||
}
|
||||
if sysNotice.CreateBy != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, sysNotice.CreateBy)
|
||||
}
|
||||
if sysNotice.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysNotice.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := " where del_flag = '0' "
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " and " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysNotice{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectNoticeByIds 查询公告信息
|
||||
func (r *SysNoticeImpl) SelectNoticeByIds(noticeIds []string) []model.SysNotice {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(noticeIds))
|
||||
querySql := r.selectSql + " where notice_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(noticeIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysNotice{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// InsertNotice 新增公告
|
||||
func (r *SysNoticeImpl) InsertNotice(sysNotice model.SysNotice) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysNotice.NoticeTitle != "" {
|
||||
params["notice_title"] = sysNotice.NoticeTitle
|
||||
}
|
||||
if sysNotice.NoticeType != "" {
|
||||
params["notice_type"] = sysNotice.NoticeType
|
||||
}
|
||||
if sysNotice.NoticeContent != "" {
|
||||
params["notice_content"] = sysNotice.NoticeContent
|
||||
}
|
||||
if sysNotice.Status != "" {
|
||||
params["status"] = sysNotice.Status
|
||||
}
|
||||
if sysNotice.Remark != "" {
|
||||
params["remark"] = sysNotice.Remark
|
||||
}
|
||||
if sysNotice.CreateBy != "" {
|
||||
params["create_by"] = sysNotice.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_notice (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateNotice 修改公告
|
||||
func (r *SysNoticeImpl) UpdateNotice(sysNotice model.SysNotice) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysNotice.NoticeTitle != "" {
|
||||
params["notice_title"] = sysNotice.NoticeTitle
|
||||
}
|
||||
if sysNotice.NoticeType != "" {
|
||||
params["notice_type"] = sysNotice.NoticeType
|
||||
}
|
||||
if sysNotice.NoticeContent != "" {
|
||||
params["notice_content"] = sysNotice.NoticeContent
|
||||
}
|
||||
if sysNotice.Status != "" {
|
||||
params["status"] = sysNotice.Status
|
||||
}
|
||||
if sysNotice.Remark != "" {
|
||||
params["remark"] = sysNotice.Remark
|
||||
}
|
||||
if sysNotice.UpdateBy != "" {
|
||||
params["update_by"] = sysNotice.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_notice set " + strings.Join(keys, ",") + " where notice_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysNotice.NoticeID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteNoticeByIds 批量删除公告信息
|
||||
func (r *SysNoticeImpl) DeleteNoticeByIds(noticeIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(noticeIds))
|
||||
sql := "update sys_notice set del_flag = '1' where notice_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(noticeIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("update err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
30
src/modules/system/repository/sys_post.go
Normal file
30
src/modules/system/repository/sys_post.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysPost 岗位表 数据层接口
|
||||
type ISysPost interface {
|
||||
// SelectPostPage 查询岗位分页数据集合
|
||||
SelectPostPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectPostList 查询岗位数据集合
|
||||
SelectPostList(sysPost model.SysPost) []model.SysPost
|
||||
|
||||
// SelectPostByIds 通过岗位ID查询岗位信息
|
||||
SelectPostByIds(postIds []string) []model.SysPost
|
||||
|
||||
// SelectPostListByUserId 根据用户ID获取岗位选择框列表
|
||||
SelectPostListByUserId(userId string) []model.SysPost
|
||||
|
||||
// DeletePostByIds 批量删除岗位信息
|
||||
DeletePostByIds(postIds []string) int64
|
||||
|
||||
// UpdatePost 修改岗位信息
|
||||
UpdatePost(sysPost model.SysPost) int64
|
||||
|
||||
// InsertPost 新增岗位信息
|
||||
InsertPost(sysPost model.SysPost) string
|
||||
|
||||
// CheckUniquePost 校验岗位唯一
|
||||
CheckUniquePost(sysPost model.SysPost) string
|
||||
}
|
||||
323
src/modules/system/repository/sys_post.impl.go
Normal file
323
src/modules/system/repository/sys_post.impl.go
Normal file
@@ -0,0 +1,323 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysPostImpl 结构体
|
||||
var NewSysPostImpl = &SysPostImpl{
|
||||
selectSql: `select
|
||||
post_id, post_code, post_name, post_sort, status, create_by, create_time, remark
|
||||
from sys_post`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"post_id": "PostID",
|
||||
"post_code": "PostCode",
|
||||
"post_name": "PostName",
|
||||
"post_sort": "PostSort",
|
||||
"status": "Status",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"remark": "Remark",
|
||||
},
|
||||
}
|
||||
|
||||
// SysPostImpl 岗位表 数据层处理
|
||||
type SysPostImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysPostImpl) convertResultRows(rows []map[string]any) []model.SysPost {
|
||||
arr := make([]model.SysPost, 0)
|
||||
for _, row := range rows {
|
||||
sysPost := model.SysPost{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysPost, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysPost)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectPostPage 查询岗位分页数据集合
|
||||
func (r *SysPostImpl) SelectPostPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["postCode"]; ok && v != "" {
|
||||
conditions = append(conditions, "post_code like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["postName"]; ok && v != "" {
|
||||
conditions = append(conditions, "post_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysPost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_post"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " order by post_sort limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectPostList 查询岗位数据集合
|
||||
func (r *SysPostImpl) SelectPostList(sysPost model.SysPost) []model.SysPost {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysPost.PostCode != "" {
|
||||
conditions = append(conditions, "post_code like concat(?, '%')")
|
||||
params = append(params, sysPost.PostCode)
|
||||
}
|
||||
if sysPost.PostName != "" {
|
||||
conditions = append(conditions, "post_name like concat(?, '%')")
|
||||
params = append(params, sysPost.PostName)
|
||||
}
|
||||
if sysPost.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysPost.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
orderSql := " order by post_sort"
|
||||
querySql := r.selectSql + whereSql + orderSql
|
||||
rows, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysPost{}
|
||||
}
|
||||
return r.convertResultRows(rows)
|
||||
}
|
||||
|
||||
// SelectPostByIds 通过岗位ID查询岗位信息
|
||||
func (r *SysPostImpl) SelectPostByIds(postIds []string) []model.SysPost {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(postIds))
|
||||
querySql := r.selectSql + " where post_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(postIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysPost{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectPostListByUserId 根据用户ID获取岗位选择框列表
|
||||
func (r *SysPostImpl) SelectPostListByUserId(userId string) []model.SysPost {
|
||||
// 查询数据
|
||||
querySql := `select distinct
|
||||
p.post_id, p.post_name, p.post_code
|
||||
from sys_post p
|
||||
left join sys_user_post up on up.post_id = p.post_id
|
||||
left join sys_user u on u.user_id = up.user_id
|
||||
where u.user_id = ? order by p.post_id`
|
||||
rows, err := datasource.RawDB("", querySql, []any{userId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysPost{}
|
||||
}
|
||||
return r.convertResultRows(rows)
|
||||
}
|
||||
|
||||
// DeletePostByIds 批量删除岗位信息
|
||||
func (r *SysPostImpl) DeletePostByIds(postIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(postIds))
|
||||
sql := "delete from sys_post where post_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(postIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// UpdatePost 修改岗位信息
|
||||
func (r *SysPostImpl) UpdatePost(sysPost model.SysPost) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysPost.PostCode != "" {
|
||||
params["post_code"] = sysPost.PostCode
|
||||
}
|
||||
if sysPost.PostName != "" {
|
||||
params["post_name"] = sysPost.PostName
|
||||
}
|
||||
if sysPost.PostSort > 0 {
|
||||
params["post_sort"] = sysPost.PostSort
|
||||
}
|
||||
if sysPost.Status != "" {
|
||||
params["status"] = sysPost.Status
|
||||
}
|
||||
if sysPost.Remark != "" {
|
||||
params["remark"] = sysPost.Remark
|
||||
}
|
||||
if sysPost.UpdateBy != "" {
|
||||
params["update_by"] = sysPost.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_post set " + strings.Join(keys, ",") + " where post_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysPost.PostID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// InsertPost 新增岗位信息
|
||||
func (r *SysPostImpl) InsertPost(sysPost model.SysPost) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysPost.PostID != "" {
|
||||
params["post_id"] = sysPost.PostID
|
||||
}
|
||||
if sysPost.PostCode != "" {
|
||||
params["post_code"] = sysPost.PostCode
|
||||
}
|
||||
if sysPost.PostName != "" {
|
||||
params["post_name"] = sysPost.PostName
|
||||
}
|
||||
if sysPost.PostSort > 0 {
|
||||
params["post_sort"] = sysPost.PostSort
|
||||
}
|
||||
if sysPost.Status != "" {
|
||||
params["status"] = sysPost.Status
|
||||
}
|
||||
if sysPost.Remark != "" {
|
||||
params["remark"] = sysPost.Remark
|
||||
}
|
||||
if sysPost.CreateBy != "" {
|
||||
params["create_by"] = sysPost.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_post (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// CheckUniquePost 校验岗位唯一
|
||||
func (r *SysPostImpl) CheckUniquePost(sysPost model.SysPost) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysPost.PostName != "" {
|
||||
conditions = append(conditions, "post_name= ?")
|
||||
params = append(params, sysPost.PostName)
|
||||
}
|
||||
if sysPost.PostCode != "" {
|
||||
conditions = append(conditions, "post_code = ?")
|
||||
params = append(params, sysPost.PostCode)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select post_id as 'str' from sys_post " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
30
src/modules/system/repository/sys_role.go
Normal file
30
src/modules/system/repository/sys_role.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysRole 角色表 数据层接口
|
||||
type ISysRole interface {
|
||||
// SelectRolePage 根据条件分页查询角色数据
|
||||
SelectRolePage(query map[string]any, dataScopeSQL string) map[string]any
|
||||
|
||||
// SelectRoleList 根据条件查询角色数据
|
||||
SelectRoleList(sysRole model.SysRole, dataScopeSQL string) []model.SysRole
|
||||
|
||||
// SelectRoleListByUserId 根据用户ID获取角色选择框列表
|
||||
SelectRoleListByUserId(userId string) []model.SysRole
|
||||
|
||||
// SelectRoleByIds 通过角色ID查询角色
|
||||
SelectRoleByIds(roleIds []string) []model.SysRole
|
||||
|
||||
// UpdateRole 修改角色信息
|
||||
UpdateRole(sysRole model.SysRole) int64
|
||||
|
||||
// InsertRole 新增角色信息
|
||||
InsertRole(sysRole model.SysRole) string
|
||||
|
||||
// DeleteRoleByIds 批量删除角色信息
|
||||
DeleteRoleByIds(roleIds []string) int64
|
||||
|
||||
// CheckUniqueRole 校验角色是否唯一
|
||||
CheckUniqueRole(sysRole model.SysRole) string
|
||||
}
|
||||
382
src/modules/system/repository/sys_role.impl.go
Normal file
382
src/modules/system/repository/sys_role.impl.go
Normal file
@@ -0,0 +1,382 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysRoleImpl 结构体
|
||||
var NewSysRoleImpl = &SysRoleImpl{
|
||||
selectSql: `select distinct
|
||||
r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.menu_check_strictly,
|
||||
r.dept_check_strictly, r.status, r.del_flag, r.create_time, r.remark
|
||||
from sys_role r
|
||||
left join sys_user_role ur on ur.role_id = r.role_id
|
||||
left join sys_user u on u.user_id = ur.user_id
|
||||
left join sys_dept d on u.dept_id = d.dept_id`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"role_id": "RoleID",
|
||||
"role_name": "RoleName",
|
||||
"role_key": "RoleKey",
|
||||
"role_sort": "RoleSort",
|
||||
"data_scope": "DataScope",
|
||||
"menu_check_strictly": "MenuCheckStrictly",
|
||||
"dept_check_strictly": "DeptCheckStrictly",
|
||||
"status": "Status",
|
||||
"del_flag": "DelFlag",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"remark": "Remark",
|
||||
},
|
||||
}
|
||||
|
||||
// SysRoleImpl 角色表 数据层处理
|
||||
type SysRoleImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysRoleImpl) convertResultRows(rows []map[string]any) []model.SysRole {
|
||||
arr := make([]model.SysRole, 0)
|
||||
for _, row := range rows {
|
||||
sysRole := model.SysRole{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysRole, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysRole)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectRolePage 根据条件分页查询角色数据
|
||||
func (r *SysRoleImpl) SelectRolePage(query map[string]any, dataScopeSQL string) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["roleId"]; ok && v != "" {
|
||||
conditions = append(conditions, "r.role_id = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["roleName"]; ok && v != "" {
|
||||
conditions = append(conditions, "r.role_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["roleKey"]; ok && v != "" {
|
||||
conditions = append(conditions, "r.role_key like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "r.status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "r.create_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "r.create_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
if v, ok := query["deptId"]; ok && v != "" {
|
||||
conditions = append(conditions, `(u.dept_id = ? or u.dept_id in (
|
||||
select t.dept_id from sys_dept t where find_in_set(?, ancestors)
|
||||
))`)
|
||||
params = append(params, v)
|
||||
params = append(params, v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := " where r.del_flag = '0' "
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " and " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysRole{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := `select count(distinct r.role_id) as 'total' from sys_role r
|
||||
left join sys_user_role ur on ur.role_id = r.role_id
|
||||
left join sys_user u on u.user_id = ur.user_id
|
||||
left join sys_dept d on u.dept_id = d.dept_id`
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql+dataScopeSQL, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " order by r.role_sort asc limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + dataScopeSQL + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectRoleList 根据条件查询角色数据
|
||||
func (r *SysRoleImpl) SelectRoleList(sysRole model.SysRole, dataScopeSQL string) []model.SysRole {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysRole.RoleID != "" {
|
||||
conditions = append(conditions, "r.role_id = ?")
|
||||
params = append(params, sysRole.RoleID)
|
||||
}
|
||||
if sysRole.RoleKey != "" {
|
||||
conditions = append(conditions, "r.role_key like concat(?, '%')")
|
||||
params = append(params, sysRole.RoleKey)
|
||||
}
|
||||
if sysRole.RoleName != "" {
|
||||
conditions = append(conditions, "r.role_name like concat(?, '%')")
|
||||
params = append(params, sysRole.RoleName)
|
||||
}
|
||||
if sysRole.Status != "" {
|
||||
conditions = append(conditions, "r.status = ?")
|
||||
params = append(params, sysRole.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := " where r.del_flag = '0' "
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " and " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
orderSql := " order by r.role_sort"
|
||||
querySql := r.selectSql + whereSql + dataScopeSQL + orderSql
|
||||
rows, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysRole{}
|
||||
}
|
||||
return r.convertResultRows(rows)
|
||||
}
|
||||
|
||||
// SelectRoleListByUserId 根据用户ID获取角色选择框列表
|
||||
func (r *SysRoleImpl) SelectRoleListByUserId(userId string) []model.SysRole {
|
||||
querySql := r.selectSql + " where r.del_flag = '0' and ur.user_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{userId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysRole{}
|
||||
}
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectRoleByIds 通过角色ID查询角色
|
||||
func (r *SysRoleImpl) SelectRoleByIds(roleIds []string) []model.SysRole {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(roleIds))
|
||||
querySql := r.selectSql + " where r.role_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(roleIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysRole{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// UpdateRole 修改角色信息
|
||||
func (r *SysRoleImpl) UpdateRole(sysRole model.SysRole) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysRole.RoleName != "" {
|
||||
params["role_name"] = sysRole.RoleName
|
||||
}
|
||||
if sysRole.RoleKey != "" {
|
||||
params["role_key"] = sysRole.RoleKey
|
||||
}
|
||||
if sysRole.RoleSort > 0 {
|
||||
params["role_sort"] = sysRole.RoleSort
|
||||
}
|
||||
if sysRole.DataScope != "" {
|
||||
params["data_scope"] = sysRole.DataScope
|
||||
}
|
||||
if sysRole.MenuCheckStrictly != "" {
|
||||
params["menu_check_strictly"] = sysRole.MenuCheckStrictly
|
||||
}
|
||||
if sysRole.DeptCheckStrictly != "" {
|
||||
params["dept_check_strictly"] = sysRole.DeptCheckStrictly
|
||||
}
|
||||
if sysRole.Status != "" {
|
||||
params["status"] = sysRole.Status
|
||||
}
|
||||
if sysRole.Remark != "" {
|
||||
params["remark"] = sysRole.Remark
|
||||
}
|
||||
if sysRole.UpdateBy != "" {
|
||||
params["update_by"] = sysRole.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_role set " + strings.Join(keys, ",") + " where role_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysRole.RoleID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// InsertRole 新增角色信息
|
||||
func (r *SysRoleImpl) InsertRole(sysRole model.SysRole) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysRole.RoleID != "" {
|
||||
params["role_id"] = sysRole.RoleID
|
||||
}
|
||||
if sysRole.RoleName != "" {
|
||||
params["role_name"] = sysRole.RoleName
|
||||
}
|
||||
if sysRole.RoleKey != "" {
|
||||
params["role_key"] = sysRole.RoleKey
|
||||
}
|
||||
if sysRole.RoleSort > 0 {
|
||||
params["role_sort"] = sysRole.RoleSort
|
||||
}
|
||||
if sysRole.DataScope != "" {
|
||||
params["data_scope"] = sysRole.DataScope
|
||||
}
|
||||
if sysRole.MenuCheckStrictly != "" {
|
||||
params["menu_check_strictly"] = sysRole.MenuCheckStrictly
|
||||
}
|
||||
if sysRole.DeptCheckStrictly != "" {
|
||||
params["dept_check_strictly"] = sysRole.DeptCheckStrictly
|
||||
}
|
||||
if sysRole.Status != "" {
|
||||
params["status"] = sysRole.Status
|
||||
}
|
||||
if sysRole.Remark != "" {
|
||||
params["remark"] = sysRole.Remark
|
||||
}
|
||||
if sysRole.CreateBy != "" {
|
||||
params["create_by"] = sysRole.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_role (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// DeleteRoleByIds 批量删除角色信息
|
||||
func (r *SysRoleImpl) DeleteRoleByIds(roleIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(roleIds))
|
||||
sql := "update sys_role set del_flag = '1' where role_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(roleIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// CheckUniqueRole 校验角色是否唯一
|
||||
func (r *SysRoleImpl) CheckUniqueRole(sysRole model.SysRole) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysRole.RoleName != "" {
|
||||
conditions = append(conditions, "r.role_name = ?")
|
||||
params = append(params, sysRole.RoleName)
|
||||
}
|
||||
if sysRole.RoleKey != "" {
|
||||
conditions = append(conditions, "r.role_key = ?")
|
||||
params = append(params, sysRole.RoleKey)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select role_id as 'str' from sys_role r " + whereSql + " and r.del_flag = '0' limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
15
src/modules/system/repository/sys_role_dept.go
Normal file
15
src/modules/system/repository/sys_role_dept.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysRoleDept 角色与部门关联表 数据层接口
|
||||
type ISysRoleDept interface {
|
||||
// DeleteRoleDept 批量删除角色部门关联信息
|
||||
DeleteRoleDept(roleIds []string) int64
|
||||
|
||||
// DeleteDeptRole 批量删除部门角色关联信息
|
||||
DeleteDeptRole(deptIds []string) int64
|
||||
|
||||
// BatchRoleDept 批量新增角色部门信息
|
||||
BatchRoleDept(sysRoleDepts []model.SysRoleDept) int64
|
||||
}
|
||||
58
src/modules/system/repository/sys_role_dept.impl.go
Normal file
58
src/modules/system/repository/sys_role_dept.impl.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysRoleDeptImpl 结构体
|
||||
var NewSysRoleDeptImpl = &SysRoleDeptImpl{}
|
||||
|
||||
// SysRoleDeptImpl 角色与部门关联表 数据层处理
|
||||
type SysRoleDeptImpl struct{}
|
||||
|
||||
// DeleteRoleDept 批量删除角色部门关联信息
|
||||
func (r *SysRoleDeptImpl) DeleteRoleDept(roleIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(roleIds))
|
||||
sql := "delete from sys_role_dept where role_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(roleIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// DeleteDeptRole 批量删除部门角色关联信息
|
||||
func (r *SysRoleDeptImpl) DeleteDeptRole(deptIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(deptIds))
|
||||
sql := "delete from sys_role_dept where dept_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(deptIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// BatchRoleDept 批量新增角色部门信息
|
||||
func (r *SysRoleDeptImpl) BatchRoleDept(sysRoleDepts []model.SysRoleDept) int64 {
|
||||
keyValues := make([]string, 0)
|
||||
for _, item := range sysRoleDepts {
|
||||
keyValues = append(keyValues, fmt.Sprintf("(%s,%s)", item.RoleID, item.DeptID))
|
||||
}
|
||||
sql := "insert into sys_role_dept(role_id, dept_id) values " + strings.Join(keyValues, ",")
|
||||
results, err := datasource.ExecDB("", sql, nil)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
18
src/modules/system/repository/sys_role_menu.go
Normal file
18
src/modules/system/repository/sys_role_menu.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysRoleMenu 角色与菜单关联表 数据层接口
|
||||
type ISysRoleMenu interface {
|
||||
// CheckMenuExistRole 查询菜单分配给角色使用数量
|
||||
CheckMenuExistRole(menuId string) int64
|
||||
|
||||
// DeleteRoleMenu 批量删除角色和菜单关联
|
||||
DeleteRoleMenu(roleIds []string) int64
|
||||
|
||||
// DeleteMenuRole 批量删除菜单和角色关联
|
||||
DeleteMenuRole(menuIds []string) int64
|
||||
|
||||
// BatchRoleMenu 批量新增角色菜单信息
|
||||
BatchRoleMenu(sysRoleMenus []model.SysRoleMenu) int64
|
||||
}
|
||||
73
src/modules/system/repository/sys_role_menu.impl.go
Normal file
73
src/modules/system/repository/sys_role_menu.impl.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysRoleMenuImpl 结构体
|
||||
var NewSysRoleMenuImpl = &SysRoleMenuImpl{}
|
||||
|
||||
// SysRoleMenuImpl 角色与菜单关联表 数据层处理
|
||||
type SysRoleMenuImpl struct{}
|
||||
|
||||
// CheckMenuExistRole 查询菜单分配给角色使用数量
|
||||
func (r *SysRoleMenuImpl) CheckMenuExistRole(menuId string) int64 {
|
||||
querySql := "select count(1) as 'total' from sys_role_menu where menu_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{menuId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return 0
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return parse.Number(results[0]["total"])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// DeleteRoleMenu 批量删除角色和菜单关联
|
||||
func (r *SysRoleMenuImpl) DeleteRoleMenu(roleIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(roleIds))
|
||||
sql := "delete from sys_role_menu where role_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(roleIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// DeleteMenuRole 批量删除菜单和角色关联
|
||||
func (r *SysRoleMenuImpl) DeleteMenuRole(menuIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(menuIds))
|
||||
sql := "delete from sys_role_menu where menu_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(menuIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// BatchRoleMenu 批量新增角色菜单信息
|
||||
func (r *SysRoleMenuImpl) BatchRoleMenu(sysRoleMenus []model.SysRoleMenu) int64 {
|
||||
keyValues := make([]string, 0)
|
||||
for _, item := range sysRoleMenus {
|
||||
keyValues = append(keyValues, fmt.Sprintf("(%s,%s)", item.RoleID, item.MenuID))
|
||||
}
|
||||
sql := "insert into sys_role_menu(role_id, menu_id) values " + strings.Join(keyValues, ",")
|
||||
results, err := datasource.ExecDB("", sql, nil)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
33
src/modules/system/repository/sys_user.go
Normal file
33
src/modules/system/repository/sys_user.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysUser 用户表 数据层接口
|
||||
type ISysUser interface {
|
||||
// SelectUserPage 根据条件分页查询用户列表
|
||||
SelectUserPage(query map[string]any, dataScopeSQL string) map[string]any
|
||||
|
||||
// SelectAllocatedPage 根据条件分页查询分配用户角色列表
|
||||
SelectAllocatedPage(query map[string]any, dataScopeSQL string) map[string]any
|
||||
|
||||
// SelectUserList 根据条件查询用户列表
|
||||
SelectUserList(sysUser model.SysUser, dataScopeSQL string) []model.SysUser
|
||||
|
||||
// SelectUserByIds 通过用户ID查询用户
|
||||
SelectUserByIds(userIds []string) []model.SysUser
|
||||
|
||||
// SelectUserByUserName 通过用户登录账号查询用户
|
||||
SelectUserByUserName(userName string) model.SysUser
|
||||
|
||||
// InsertUser 新增用户信息
|
||||
InsertUser(sysUser model.SysUser) string
|
||||
|
||||
// UpdateUser 修改用户信息
|
||||
UpdateUser(sysUser model.SysUser) int64
|
||||
|
||||
// DeleteUserByIds 批量删除用户信息
|
||||
DeleteUserByIds(userIds []string) int64
|
||||
|
||||
// CheckUniqueUser 校验用户信息是否唯一
|
||||
CheckUniqueUser(sysUser model.SysUser) string
|
||||
}
|
||||
580
src/modules/system/repository/sys_user.impl.go
Normal file
580
src/modules/system/repository/sys_user.impl.go
Normal file
@@ -0,0 +1,580 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/crypto"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysUserImpl 结构体
|
||||
var NewSysUserImpl = &SysUserImpl{
|
||||
selectSql: `select
|
||||
u.user_id, u.dept_id, u.user_name, u.nick_name, u.user_type, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,
|
||||
d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.status as dept_status,
|
||||
r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
|
||||
from sys_user u
|
||||
left join sys_dept d on u.dept_id = d.dept_id
|
||||
left join sys_user_role ur on u.user_id = ur.user_id
|
||||
left join sys_role r on r.role_id = ur.role_id`,
|
||||
|
||||
sysUserMap: map[string]string{
|
||||
"user_id": "UserID",
|
||||
"dept_id": "DeptID",
|
||||
"user_name": "UserName",
|
||||
"nick_name": "NickName",
|
||||
"user_type": "UserType",
|
||||
"email": "Email",
|
||||
"phonenumber": "PhoneNumber",
|
||||
"sex": "Sex",
|
||||
"avatar": "Avatar",
|
||||
"password": "Password",
|
||||
"status": "Status",
|
||||
"del_flag": "DelFlag",
|
||||
"login_ip": "LoginIP",
|
||||
"login_date": "LoginDate",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"remark": "Remark",
|
||||
},
|
||||
|
||||
sysDeptMap: map[string]string{
|
||||
"dept_id": "DeptID",
|
||||
"parent_id": "ParentID",
|
||||
"dept_name": "DeptName",
|
||||
"ancestors": "Ancestors",
|
||||
"order_num": "OrderNum",
|
||||
"leader": "Leader",
|
||||
"dept_status": "Status",
|
||||
},
|
||||
|
||||
sysRoleMap: map[string]string{
|
||||
"role_id": "RoleID",
|
||||
"role_name": "RoleName",
|
||||
"role_key": "RoleKey",
|
||||
"role_sort": "RoleSort",
|
||||
"data_scope": "DataScope",
|
||||
"role_status": "Status",
|
||||
},
|
||||
}
|
||||
|
||||
// SysUserImpl 用户表 数据层处理
|
||||
type SysUserImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 用户信息实体映射
|
||||
sysUserMap map[string]string
|
||||
// 用户部门实体映射 一对一
|
||||
sysDeptMap map[string]string
|
||||
// 用户角色实体映射 一对多
|
||||
sysRoleMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysUserImpl) convertResultRows(rows []map[string]any) []model.SysUser {
|
||||
arr := make([]model.SysUser, 0)
|
||||
|
||||
for _, row := range rows {
|
||||
sysUser := model.SysUser{}
|
||||
sysDept := model.SysDept{}
|
||||
sysRole := model.SysRole{}
|
||||
sysUser.Roles = []model.SysRole{}
|
||||
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.sysUserMap[key]; ok {
|
||||
repo.SetFieldValue(&sysUser, keyMapper, value)
|
||||
}
|
||||
if keyMapper, ok := r.sysDeptMap[key]; ok {
|
||||
repo.SetFieldValue(&sysDept, keyMapper, value)
|
||||
}
|
||||
if keyMapper, ok := r.sysRoleMap[key]; ok {
|
||||
repo.SetFieldValue(&sysRole, keyMapper, value)
|
||||
}
|
||||
}
|
||||
|
||||
sysUser.Dept = sysDept
|
||||
if sysRole.RoleKey != "" {
|
||||
sysUser.Roles = append(sysUser.Roles, sysRole)
|
||||
}
|
||||
|
||||
one := true
|
||||
for i, a := range arr {
|
||||
if a.UserID == sysUser.UserID {
|
||||
arrUser := &arr[i]
|
||||
arrUser.Roles = append(arrUser.Roles, sysUser.Roles...)
|
||||
one = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if one {
|
||||
arr = append(arr, sysUser)
|
||||
}
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectUserPage 根据条件分页查询用户列表
|
||||
func (r *SysUserImpl) SelectUserPage(query map[string]any, dataScopeSQL string) map[string]any {
|
||||
selectUserSql := `select
|
||||
u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader
|
||||
from sys_user u
|
||||
left join sys_dept d on u.dept_id = d.dept_id`
|
||||
selectUserTotalSql := `select count(distinct u.user_id) as 'total'
|
||||
from sys_user u left join sys_dept d on u.dept_id = d.dept_id`
|
||||
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["userId"]; ok && v != "" {
|
||||
conditions = append(conditions, "u.user_id = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["userName"]; ok && v != "" {
|
||||
conditions = append(conditions, "u.user_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "u.status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["phonenumber"]; ok && v != "" {
|
||||
conditions = append(conditions, "u.phonenumber like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "u.login_date >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "u.login_date <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
if v, ok := query["deptId"]; ok && v != "" {
|
||||
conditions = append(conditions, "(u.dept_id = ? or u.dept_id in ( select t.dept_id from sys_dept t where find_in_set(?, ancestors) ))")
|
||||
params = append(params, v)
|
||||
params = append(params, v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := " where u.del_flag = '0' "
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " and " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysUser{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := selectUserTotalSql + whereSql + dataScopeSQL
|
||||
totalRows, err := datasource.RawDB("", totalSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := selectUserSql + whereSql + dataScopeSQL + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectAllocatedPage 根据条件分页查询分配用户角色列表
|
||||
func (r *SysUserImpl) SelectAllocatedPage(query map[string]any, dataScopeSQL string) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["userName"]; ok && v != "" {
|
||||
conditions = append(conditions, "u.user_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["phonenumber"]; ok && v != "" {
|
||||
conditions = append(conditions, "u.phonenumber like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "u.status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
// 分配角色用户
|
||||
if allocated, ok := query["allocated"]; ok && allocated != "" {
|
||||
if roleId, ok := query["roleId"]; ok && roleId != "" {
|
||||
if parse.Boolean(allocated) {
|
||||
conditions = append(conditions, "r.role_id = ?")
|
||||
params = append(params, roleId)
|
||||
} else {
|
||||
conditions = append(conditions, `(r.role_id != ? or r.role_id IS NULL)
|
||||
and u.user_id not in (
|
||||
select u.user_id from sys_user u
|
||||
inner join sys_user_role ur on u.user_id = ur.user_id
|
||||
and ur.role_id = ?
|
||||
)`)
|
||||
params = append(params, roleId)
|
||||
params = append(params, roleId)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := " where u.del_flag = '0' "
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " and " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysUser{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := `select count(distinct u.user_id) as 'total' from sys_user u
|
||||
left join sys_dept d on u.dept_id = d.dept_id
|
||||
left join sys_user_role ur on u.user_id = ur.user_id
|
||||
left join sys_role r on r.role_id = ur.role_id`
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql+dataScopeSQL, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := `select distinct
|
||||
u.user_id, u.dept_id, u.user_name, u.nick_name, u.email,
|
||||
u.phonenumber, u.status, u.create_time, d.dept_name
|
||||
from sys_user u
|
||||
left join sys_dept d on u.dept_id = d.dept_id
|
||||
left join sys_user_role ur on u.user_id = ur.user_id
|
||||
left join sys_role r on r.role_id = ur.role_id`
|
||||
querySql = querySql + whereSql + dataScopeSQL + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectUserList 根据条件查询用户列表
|
||||
func (r *SysUserImpl) SelectUserList(sysUser model.SysUser, dataScopeSQL string) []model.SysUser {
|
||||
selectUserSql := `select
|
||||
u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader
|
||||
from sys_user u
|
||||
left join sys_dept d on u.dept_id = d.dept_id`
|
||||
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysUser.UserID != "" {
|
||||
conditions = append(conditions, "u.user_id = ?")
|
||||
params = append(params, sysUser.UserID)
|
||||
}
|
||||
if sysUser.UserName != "" {
|
||||
conditions = append(conditions, "u.user_name like concat(?, '%')")
|
||||
params = append(params, sysUser.UserName)
|
||||
}
|
||||
if sysUser.Status != "" {
|
||||
conditions = append(conditions, "u.status = ?")
|
||||
params = append(params, sysUser.Status)
|
||||
}
|
||||
if sysUser.PhoneNumber != "" {
|
||||
conditions = append(conditions, "u.phonenumber like concat(?, '%')")
|
||||
params = append(params, sysUser.PhoneNumber)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := " where u.del_flag = '0' "
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " and " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := selectUserSql + whereSql + dataScopeSQL
|
||||
rows, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysUser{}
|
||||
}
|
||||
return r.convertResultRows(rows)
|
||||
}
|
||||
|
||||
// SelectUserByIds 通过用户ID查询用户
|
||||
func (r *SysUserImpl) SelectUserByIds(userIds []string) []model.SysUser {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(userIds))
|
||||
querySql := r.selectSql + " where u.del_flag = '0' and u.user_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(userIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysUser{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectUserByUserName 通过用户登录账号查询用户
|
||||
func (r *SysUserImpl) SelectUserByUserName(userName string) model.SysUser {
|
||||
querySql := r.selectSql + " where u.del_flag = '0' and u.user_name = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{userName})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.SysUser{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.SysUser{}
|
||||
}
|
||||
|
||||
// InsertUser 新增用户信息
|
||||
func (r *SysUserImpl) InsertUser(sysUser model.SysUser) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysUser.UserID != "" {
|
||||
params["user_id"] = sysUser.UserID
|
||||
}
|
||||
if sysUser.DeptID != "" {
|
||||
params["dept_id"] = sysUser.DeptID
|
||||
}
|
||||
if sysUser.UserName != "" {
|
||||
params["user_name"] = sysUser.UserName
|
||||
}
|
||||
if sysUser.NickName != "" {
|
||||
params["nick_name"] = sysUser.NickName
|
||||
}
|
||||
if sysUser.UserType != "" {
|
||||
params["user_type"] = sysUser.UserType
|
||||
}
|
||||
if sysUser.Avatar != "" {
|
||||
params["avatar"] = sysUser.Avatar
|
||||
}
|
||||
if sysUser.Email != "" {
|
||||
params["email"] = sysUser.Email
|
||||
}
|
||||
if sysUser.PhoneNumber != "" {
|
||||
params["phonenumber"] = sysUser.PhoneNumber
|
||||
}
|
||||
if sysUser.Sex != "" {
|
||||
params["sex"] = sysUser.Sex
|
||||
}
|
||||
if sysUser.Password != "" {
|
||||
password := crypto.BcryptHash(sysUser.Password)
|
||||
params["password"] = password
|
||||
}
|
||||
if sysUser.Status != "" {
|
||||
params["status"] = sysUser.Status
|
||||
}
|
||||
if sysUser.Remark != "" {
|
||||
params["remark"] = sysUser.Remark
|
||||
}
|
||||
if sysUser.CreateBy != "" {
|
||||
params["create_by"] = sysUser.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_user (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateUser 修改用户信息
|
||||
func (r *SysUserImpl) UpdateUser(sysUser model.SysUser) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysUser.DeptID != "" {
|
||||
params["dept_id"] = sysUser.DeptID
|
||||
}
|
||||
if sysUser.UserName != "" {
|
||||
params["user_name"] = sysUser.UserName
|
||||
}
|
||||
if sysUser.NickName != "" {
|
||||
params["nick_name"] = sysUser.NickName
|
||||
}
|
||||
if sysUser.UserType != "" {
|
||||
params["user_type"] = sysUser.UserType
|
||||
}
|
||||
if sysUser.Avatar != "" {
|
||||
params["avatar"] = sysUser.Avatar
|
||||
}
|
||||
if sysUser.Email != "" {
|
||||
if sysUser.Email == "nil" {
|
||||
params["email"] = ""
|
||||
} else {
|
||||
params["email"] = sysUser.Email
|
||||
}
|
||||
}
|
||||
if sysUser.PhoneNumber != "" {
|
||||
if sysUser.PhoneNumber == "nil" {
|
||||
params["phonenumber"] = ""
|
||||
} else {
|
||||
params["phonenumber"] = sysUser.PhoneNumber
|
||||
}
|
||||
}
|
||||
if sysUser.Sex != "" {
|
||||
params["sex"] = sysUser.Sex
|
||||
}
|
||||
if sysUser.Password != "" {
|
||||
password := crypto.BcryptHash(sysUser.Password)
|
||||
params["password"] = password
|
||||
}
|
||||
if sysUser.Status != "" {
|
||||
params["status"] = sysUser.Status
|
||||
}
|
||||
if sysUser.Remark != "" {
|
||||
params["remark"] = sysUser.Remark
|
||||
}
|
||||
if sysUser.UpdateBy != "" {
|
||||
params["update_by"] = sysUser.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
if sysUser.LoginIP != "" {
|
||||
params["login_ip"] = sysUser.LoginIP
|
||||
}
|
||||
if sysUser.LoginDate > 0 {
|
||||
params["login_date"] = sysUser.LoginDate
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_user set " + strings.Join(keys, ",") + " where user_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysUser.UserID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteUserByIds 批量删除用户信息
|
||||
func (r *SysUserImpl) DeleteUserByIds(userIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(userIds))
|
||||
sql := "update sys_user set del_flag = '1' where user_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(userIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("update err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// CheckUniqueUser 校验用户信息是否唯一
|
||||
func (r *SysUserImpl) CheckUniqueUser(sysUser model.SysUser) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysUser.UserName != "" {
|
||||
conditions = append(conditions, "user_name = ?")
|
||||
params = append(params, sysUser.UserName)
|
||||
}
|
||||
if sysUser.PhoneNumber != "" {
|
||||
conditions = append(conditions, "phonenumber = ?")
|
||||
params = append(params, sysUser.PhoneNumber)
|
||||
}
|
||||
if sysUser.Email != "" {
|
||||
conditions = append(conditions, "email = ?")
|
||||
params = append(params, sysUser.Email)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select user_id as 'str' from sys_user " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
15
src/modules/system/repository/sys_user_post.go
Normal file
15
src/modules/system/repository/sys_user_post.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysUserPost 用户与岗位关联表 数据层接口
|
||||
type ISysUserPost interface {
|
||||
// CountUserPostByPostId 通过岗位ID查询岗位使用数量
|
||||
CountUserPostByPostId(postId string) int64
|
||||
|
||||
// BatchUserPost 批量新增用户岗位信息
|
||||
BatchUserPost(sysUserPosts []model.SysUserPost) int64
|
||||
|
||||
// DeleteUserPost 批量删除用户和岗位关联
|
||||
DeleteUserPost(userIds []string) int64
|
||||
}
|
||||
60
src/modules/system/repository/sys_user_post.impl.go
Normal file
60
src/modules/system/repository/sys_user_post.impl.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysUserPostImpl 结构体
|
||||
var NewSysUserPostImpl = &SysUserPostImpl{}
|
||||
|
||||
// SysUserPostImpl 用户与岗位关联表 数据层处理
|
||||
type SysUserPostImpl struct{}
|
||||
|
||||
// CountUserPostByPostId 通过岗位ID查询岗位使用数量
|
||||
func (r *SysUserPostImpl) CountUserPostByPostId(postId string) int64 {
|
||||
querySql := "select count(1) as total from sys_user_role where role_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{postId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return 0
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return parse.Number(results[0]["total"])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// DeleteUserPost 批量删除用户和岗位关联
|
||||
func (r *SysUserPostImpl) DeleteUserPost(userIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(userIds))
|
||||
sql := "delete from sys_user_post where user_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(userIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// BatchUserPost 批量新增用户岗位信息
|
||||
func (r *SysUserPostImpl) BatchUserPost(sysUserPosts []model.SysUserPost) int64 {
|
||||
keyValues := make([]string, 0)
|
||||
for _, item := range sysUserPosts {
|
||||
keyValues = append(keyValues, fmt.Sprintf("(%s,%s)", item.UserID, item.PostID))
|
||||
}
|
||||
sql := "insert into sys_user_post(user_id, post_id) values " + strings.Join(keyValues, ",")
|
||||
results, err := datasource.ExecDB("", sql, nil)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
18
src/modules/system/repository/sys_user_role.go
Normal file
18
src/modules/system/repository/sys_user_role.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package repository
|
||||
|
||||
import "ems.agt/src/modules/system/model"
|
||||
|
||||
// ISysUserRole 用户与角色关联表 数据层接口
|
||||
type ISysUserRole interface {
|
||||
// CountUserRoleByRoleId 通过角色ID查询角色使用数量
|
||||
CountUserRoleByRoleId(roleId string) int64
|
||||
|
||||
// BatchUserRole 批量新增用户角色信息
|
||||
BatchUserRole(sysUserRoles []model.SysUserRole) int64
|
||||
|
||||
// DeleteUserRole 批量删除用户和角色关联
|
||||
DeleteUserRole(userIds []string) int64
|
||||
|
||||
// DeleteUserRoleByRoleId 批量取消授权用户角色
|
||||
DeleteUserRoleByRoleId(roleId string, userIds []string) int64
|
||||
}
|
||||
74
src/modules/system/repository/sys_user_role.impl.go
Normal file
74
src/modules/system/repository/sys_user_role.impl.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/system/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysUserRoleImpl 结构体
|
||||
var NewSysUserRoleImpl = &SysUserRoleImpl{}
|
||||
|
||||
// SysUserRoleImpl 用户与角色关联表 数据层处理
|
||||
type SysUserRoleImpl struct{}
|
||||
|
||||
// CountUserRoleByRoleId 通过角色ID查询角色使用数量
|
||||
func (r *SysUserRoleImpl) CountUserRoleByRoleId(roleId string) int64 {
|
||||
querySql := "select count(1) as total from sys_user_role where role_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{roleId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return 0
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return parse.Number(results[0]["total"])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// BatchUserRole 批量新增用户角色信息
|
||||
func (r *SysUserRoleImpl) BatchUserRole(sysUserRoles []model.SysUserRole) int64 {
|
||||
keyValues := make([]string, 0)
|
||||
for _, item := range sysUserRoles {
|
||||
keyValues = append(keyValues, fmt.Sprintf("(%s,%s)", item.UserID, item.RoleID))
|
||||
}
|
||||
sql := "insert into sys_user_role(user_id, role_id) values " + strings.Join(keyValues, ",")
|
||||
results, err := datasource.ExecDB("", sql, nil)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// DeleteUserRole 批量删除用户和角色关联
|
||||
func (r *SysUserRoleImpl) DeleteUserRole(userIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(userIds))
|
||||
sql := "delete from sys_user_role where user_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(userIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// DeleteUserRoleByRoleId 批量取消授权用户角色
|
||||
func (r *SysUserRoleImpl) DeleteUserRoleByRoleId(roleId string, userIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(userIds))
|
||||
sql := "delete from sys_user_role where role_id= ? and user_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(userIds)
|
||||
parameters = append([]any{roleId}, parameters...)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
Reference in New Issue
Block a user