feat: 合并Gin_Vue

This commit is contained in:
TsMask
2023-10-16 17:10:38 +08:00
parent 5289818fd4
commit 40a32cb67f
203 changed files with 19719 additions and 178 deletions

View File

@@ -0,0 +1,30 @@
package service
import "ems.agt/src/modules/system/model"
// ISysConfig 参数配置 服务层接口
type ISysConfig interface {
// SelectDictDataPage 分页查询参数配置列表数据
SelectConfigPage(query map[string]any) map[string]any
// SelectConfigValueByKey 通过参数键名查询参数键值
SelectConfigValueByKey(configKey string) string
// SelectConfigById 通过配置ID查询参数配置信息
SelectConfigById(configId string) model.SysConfig
// CheckUniqueConfigKey 校验参数键名是否唯一
CheckUniqueConfigKey(configKey, configId string) bool
// InsertConfig 新增参数配置
InsertConfig(sysConfig model.SysConfig) string
// UpdateConfig 修改参数配置
UpdateConfig(sysConfig model.SysConfig) int64
// DeleteConfigByIds 批量删除参数配置信息
DeleteConfigByIds(configIds []string) (int64, error)
// ResetConfigCache 重置参数缓存数据
ResetConfigCache()
}

View File

@@ -0,0 +1,157 @@
package service
import (
"errors"
"ems.agt/src/framework/constants/cachekey"
"ems.agt/src/framework/redis"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysConfigImpl 结构体
var NewSysConfigImpl = &SysConfigImpl{
sysConfigRepository: repository.NewSysConfigImpl,
}
// SysConfigImpl 参数配置 服务层处理
type SysConfigImpl struct {
// 参数配置表
sysConfigRepository repository.ISysConfig
}
// SelectDictDataPage 分页查询参数配置列表数据
func (r *SysConfigImpl) SelectConfigPage(query map[string]any) map[string]any {
return r.sysConfigRepository.SelectConfigPage(query)
}
// SelectConfigList 查询参数配置列表
func (r *SysConfigImpl) SelectConfigList(sysConfig model.SysConfig) []model.SysConfig {
return r.sysConfigRepository.SelectConfigList(sysConfig)
}
// SelectConfigValueByKey 通过参数键名查询参数键值
func (r *SysConfigImpl) SelectConfigValueByKey(configKey string) string {
cacheKey := r.getCacheKey(configKey)
// 从缓存中读取
cacheValue, err := redis.Get("", cacheKey)
if cacheValue != "" || err != nil {
return cacheValue
}
// 无缓存时读取数据放入缓存中
configValue := r.sysConfigRepository.SelectConfigValueByKey(configKey)
if configValue != "" {
redis.Set("", cacheKey, configValue)
return configValue
}
return ""
}
// SelectConfigById 通过配置ID查询参数配置信息
func (r *SysConfigImpl) SelectConfigById(configId string) model.SysConfig {
if configId == "" {
return model.SysConfig{}
}
configs := r.sysConfigRepository.SelectConfigByIds([]string{configId})
if len(configs) > 0 {
return configs[0]
}
return model.SysConfig{}
}
// CheckUniqueConfigKey 校验参数键名是否唯一
func (r *SysConfigImpl) CheckUniqueConfigKey(configKey, configId string) bool {
uniqueId := r.sysConfigRepository.CheckUniqueConfig(model.SysConfig{
ConfigKey: configKey,
})
if uniqueId == configId {
return true
}
return uniqueId == ""
}
// InsertConfig 新增参数配置
func (r *SysConfigImpl) InsertConfig(sysConfig model.SysConfig) string {
configId := r.sysConfigRepository.InsertConfig(sysConfig)
if configId != "" {
r.loadingConfigCache(sysConfig.ConfigKey)
}
return configId
}
// UpdateConfig 修改参数配置
func (r *SysConfigImpl) UpdateConfig(sysConfig model.SysConfig) int64 {
rows := r.sysConfigRepository.UpdateConfig(sysConfig)
if rows > 0 {
r.loadingConfigCache(sysConfig.ConfigKey)
}
return rows
}
// DeleteConfigByIds 批量删除参数配置信息
func (r *SysConfigImpl) DeleteConfigByIds(configIds []string) (int64, error) {
// 检查是否存在
configs := r.sysConfigRepository.SelectConfigByIds(configIds)
if len(configs) <= 0 {
return 0, errors.New("没有权限访问参数配置数据!")
}
for _, config := range configs {
// 检查是否为内置参数
if config.ConfigType == "Y" {
return 0, errors.New(config.ConfigID + " 配置参数属于内置参数,禁止删除!")
}
// 清除缓存
r.clearConfigCache(config.ConfigKey)
}
if len(configs) == len(configIds) {
rows := r.sysConfigRepository.DeleteConfigByIds(configIds)
return rows, nil
}
return 0, errors.New("删除参数配置信息失败!")
}
// ResetConfigCache 重置参数缓存数据
func (r *SysConfigImpl) ResetConfigCache() {
r.clearConfigCache("*")
r.loadingConfigCache("*")
}
// getCacheKey 组装缓存key
func (r *SysConfigImpl) getCacheKey(configKey string) string {
return cachekey.SYS_CONFIG_KEY + configKey
}
// loadingConfigCache 加载参数缓存数据
func (r *SysConfigImpl) loadingConfigCache(configKey string) {
// 查询全部参数
if configKey == "*" {
sysConfigs := r.SelectConfigList(model.SysConfig{})
for _, v := range sysConfigs {
key := r.getCacheKey(v.ConfigKey)
redis.Del("", key)
redis.Set("", key, v.ConfigValue)
}
return
}
// 指定参数
if configKey != "" {
cacheValue := r.sysConfigRepository.SelectConfigValueByKey(configKey)
if cacheValue != "" {
key := r.getCacheKey(configKey)
redis.Del("", key)
redis.Set("", key, cacheValue)
}
return
}
}
// clearConfigCache 清空参数缓存数据
func (r *SysConfigImpl) clearConfigCache(configKey string) bool {
key := r.getCacheKey(configKey)
keys, err := redis.GetKeys("", key)
if err != nil {
return false
}
delOk, _ := redis.DelKeys("", keys)
return delOk
}

View File

@@ -0,0 +1,39 @@
package service
import (
"ems.agt/src/framework/vo"
"ems.agt/src/modules/system/model"
)
// ISysDept 部门管理 服务层接口
type ISysDept interface {
// SelectDeptList 查询部门管理数据
SelectDeptList(sysDept model.SysDept, dataScopeSQL string) []model.SysDept
// SelectDeptListByRoleId 根据角色ID查询部门树信息
SelectDeptListByRoleId(roleId string) []string
// SelectDeptById 根据部门ID查询信息
SelectDeptById(deptId string) model.SysDept
// HasChildByDeptId 是否存在子节点
HasChildByDeptId(deptId string) int64
// CheckDeptExistUser 查询部门是否存在用户
CheckDeptExistUser(deptId string) int64
// CheckUniqueDeptName 校验同级部门名称是否唯一
CheckUniqueDeptName(deptName, parentId, deptId string) bool
// InsertDept 新增部门信息
InsertDept(sysDept model.SysDept) string
// UpdateDept 修改部门信息
UpdateDept(sysDept model.SysDept) int64
// DeleteDeptById 删除部门管理信息
DeleteDeptById(deptId string) int64
// SelectDeptTreeSelect 查询部门树结构信息
SelectDeptTreeSelect(sysDept model.SysDept, dataScopeSQL string) []vo.TreeSelect
}

View File

@@ -0,0 +1,202 @@
package service
import (
"strings"
"ems.agt/src/framework/constants/common"
"ems.agt/src/framework/vo"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysDeptImpl 结构体
var NewSysDeptImpl = &SysDeptImpl{
sysDeptRepository: repository.NewSysDeptImpl,
sysRoleRepository: repository.NewSysRoleImpl,
sysRoleDeptRepository: repository.NewSysRoleDeptImpl,
}
// SysDeptImpl 部门表 服务层处理
type SysDeptImpl struct {
// 部门服务
sysDeptRepository repository.ISysDept
// 角色服务
sysRoleRepository repository.ISysRole
// 角色与部门关联服务
sysRoleDeptRepository repository.ISysRoleDept
}
// SelectDeptList 查询部门管理数据
func (r *SysDeptImpl) SelectDeptList(sysDept model.SysDept, dataScopeSQL string) []model.SysDept {
return r.sysDeptRepository.SelectDeptList(sysDept, dataScopeSQL)
}
// SelectDeptListByRoleId 根据角色ID查询部门树信息 TODO
func (r *SysDeptImpl) SelectDeptListByRoleId(roleId string) []string {
roles := r.sysRoleRepository.SelectRoleByIds([]string{roleId})
if len(roles) > 0 {
role := roles[0]
if role.RoleID == roleId {
return r.sysDeptRepository.SelectDeptListByRoleId(
role.RoleID,
role.DeptCheckStrictly == "1",
)
}
}
return []string{}
}
// SelectDeptById 根据部门ID查询信息
func (r *SysDeptImpl) SelectDeptById(deptId string) model.SysDept {
return r.sysDeptRepository.SelectDeptById(deptId)
}
// HasChildByDeptId 是否存在子节点
func (r *SysDeptImpl) HasChildByDeptId(deptId string) int64 {
return r.sysDeptRepository.HasChildByDeptId(deptId)
}
// CheckDeptExistUser 查询部门是否存在用户
func (r *SysDeptImpl) CheckDeptExistUser(deptId string) int64 {
return r.sysDeptRepository.CheckDeptExistUser(deptId)
}
// CheckUniqueDeptName 校验同级部门名称是否唯一
func (r *SysDeptImpl) CheckUniqueDeptName(deptName, parentId, deptId string) bool {
uniqueId := r.sysDeptRepository.CheckUniqueDept(model.SysDept{
DeptName: deptName,
ParentID: parentId,
})
if uniqueId == deptId {
return true
}
return uniqueId == ""
}
// InsertDept 新增部门信息
func (r *SysDeptImpl) InsertDept(sysDept model.SysDept) string {
return r.sysDeptRepository.InsertDept(sysDept)
}
// UpdateDept 修改部门信息
func (r *SysDeptImpl) UpdateDept(sysDept model.SysDept) int64 {
parentDept := r.sysDeptRepository.SelectDeptById(sysDept.ParentID)
dept := r.sysDeptRepository.SelectDeptById(sysDept.DeptID)
// 上级与当前部门祖级列表更新
if parentDept.DeptID == sysDept.ParentID && dept.DeptID == sysDept.DeptID {
newAncestors := parentDept.Ancestors + "," + parentDept.DeptID
oldAncestors := dept.Ancestors
// 祖级列表不一致时更新
if newAncestors != oldAncestors {
sysDept.Ancestors = newAncestors
r.updateDeptChildren(sysDept.DeptID, newAncestors, oldAncestors)
}
}
// 如果该部门是启用状态,则启用该部门的所有上级部门
if sysDept.Status == common.STATUS_YES && parentDept.Status == common.STATUS_NO {
r.updateDeptStatusNormal(sysDept.Ancestors)
}
return r.sysDeptRepository.UpdateDept(sysDept)
}
// updateDeptStatusNormal 修改所在部门正常状态
func (r *SysDeptImpl) updateDeptStatusNormal(ancestors string) int64 {
if ancestors == "" || ancestors == "0" {
return 0
}
deptIds := strings.Split(ancestors, ",")
return r.sysDeptRepository.UpdateDeptStatusNormal(deptIds)
}
// updateDeptChildren 修改子元素关系
func (r *SysDeptImpl) updateDeptChildren(deptId, newAncestors, oldAncestors string) int64 {
childrens := r.sysDeptRepository.SelectChildrenDeptById(deptId)
if len(childrens) == 0 {
return 0
}
// 替换父ID
for i := range childrens {
child := &childrens[i]
ancestors := strings.Replace(child.Ancestors, oldAncestors, newAncestors, -1)
child.Ancestors = ancestors
}
return r.sysDeptRepository.UpdateDeptChildren(childrens)
}
// DeleteDeptById 删除部门管理信息
func (r *SysDeptImpl) DeleteDeptById(deptId string) int64 {
// 删除角色与部门关联
r.sysRoleDeptRepository.DeleteDeptRole([]string{deptId})
return r.sysDeptRepository.DeleteDeptById(deptId)
}
// SelectDeptTreeSelect 查询部门树结构信息
func (r *SysDeptImpl) SelectDeptTreeSelect(sysDept model.SysDept, dataScopeSQL string) []vo.TreeSelect {
sysDepts := r.sysDeptRepository.SelectDeptList(sysDept, dataScopeSQL)
depts := r.parseDataToTree(sysDepts)
tree := make([]vo.TreeSelect, 0)
for _, dept := range depts {
tree = append(tree, vo.SysDeptTreeSelect(dept))
}
return tree
}
// parseDataToTree 将数据解析为树结构,构建前端所需要下拉树结构
func (r *SysDeptImpl) parseDataToTree(sysDepts []model.SysDept) []model.SysDept {
// 节点分组
nodesMap := make(map[string][]model.SysDept)
// 节点id
treeIds := []string{}
// 树节点
tree := []model.SysDept{}
for _, item := range sysDepts {
parentID := item.ParentID
// 分组
mapItem, ok := nodesMap[parentID]
if !ok {
mapItem = []model.SysDept{}
}
mapItem = append(mapItem, item)
nodesMap[parentID] = mapItem
// 记录节点ID
treeIds = append(treeIds, item.DeptID)
}
for key, value := range nodesMap {
// 选择不是节点ID的作为树节点
found := false
for _, id := range treeIds {
if id == key {
found = true
break
}
}
if !found {
tree = append(tree, value...)
}
}
for i, node := range tree {
iN := r.parseDataToTreeComponet(node, &nodesMap)
tree[i] = iN
}
return tree
}
// parseDataToTreeComponet 递归函数处理子节点
func (r *SysDeptImpl) parseDataToTreeComponet(node model.SysDept, nodesMap *map[string][]model.SysDept) model.SysDept {
id := node.DeptID
children, ok := (*nodesMap)[id]
if ok {
node.Children = children
}
if len(node.Children) > 0 {
for i, child := range node.Children {
icN := r.parseDataToTreeComponet(child, nodesMap)
node.Children[i] = icN
}
}
return node
}

View File

@@ -0,0 +1,33 @@
package service
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
// SelectDictDataByCode 根据字典数据编码查询信息
SelectDictDataByCode(dictCode string) model.SysDictData
// SelectDictDataByType 根据字典类型查询信息
SelectDictDataByType(dictType string) []model.SysDictData
// CheckUniqueDictLabel 校验字典标签是否唯一
CheckUniqueDictLabel(dictType, dictLabel, dictCode string) bool
// CheckUniqueDictValue 校验字典键值是否唯一
CheckUniqueDictValue(dictType, dictValue, dictCode string) bool
// DeleteDictDataByCodes 批量删除字典数据信息
DeleteDictDataByCodes(dictCodes []string) (int64, error)
// InsertDictData 新增字典数据信息
InsertDictData(sysDictData model.SysDictData) string
// UpdateDictData 修改字典数据信息
UpdateDictData(sysDictData model.SysDictData) int64
}

View File

@@ -0,0 +1,110 @@
package service
import (
"errors"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysDictDataImpl 结构体
var NewSysDictDataImpl = &SysDictDataImpl{
sysDictDataRepository: repository.NewSysDictDataImpl,
sysDictTypeService: NewSysDictTypeImpl,
}
// SysDictDataImpl 字典类型数据 服务层处理
type SysDictDataImpl struct {
// 字典数据服务
sysDictDataRepository repository.ISysDictData
// 字典类型服务
sysDictTypeService ISysDictType
}
// SelectDictDataPage 根据条件分页查询字典数据
func (r *SysDictDataImpl) SelectDictDataPage(query map[string]any) map[string]any {
return r.sysDictDataRepository.SelectDictDataPage(query)
}
// SelectDictDataList 根据条件查询字典数据
func (r *SysDictDataImpl) SelectDictDataList(sysDictData model.SysDictData) []model.SysDictData {
return r.sysDictDataRepository.SelectDictDataList(sysDictData)
}
// SelectDictDataByCode 根据字典数据编码查询信息
func (r *SysDictDataImpl) SelectDictDataByCode(dictCode string) model.SysDictData {
if dictCode == "" {
return model.SysDictData{}
}
dictCodes := r.sysDictDataRepository.SelectDictDataByCodes([]string{dictCode})
if len(dictCodes) > 0 {
return dictCodes[0]
}
return model.SysDictData{}
}
// SelectDictDataByType 根据字典类型查询信息
func (r *SysDictDataImpl) SelectDictDataByType(dictType string) []model.SysDictData {
return r.sysDictTypeService.DictDataCache(dictType)
}
// CheckUniqueDictLabel 校验字典标签是否唯一
func (r *SysDictDataImpl) CheckUniqueDictLabel(dictType, dictLabel, dictCode string) bool {
uniqueId := r.sysDictDataRepository.CheckUniqueDictData(model.SysDictData{
DictType: dictType,
DictLabel: dictLabel,
})
if uniqueId == dictCode {
return true
}
return uniqueId == ""
}
// CheckUniqueDictValue 校验字典键值是否唯一
func (r *SysDictDataImpl) CheckUniqueDictValue(dictType, dictValue, dictCode string) bool {
uniqueId := r.sysDictDataRepository.CheckUniqueDictData(model.SysDictData{
DictType: dictType,
DictValue: dictValue,
})
if uniqueId == dictCode {
return true
}
return uniqueId == ""
}
// DeleteDictDataByCodes 批量删除字典数据信息
func (r *SysDictDataImpl) DeleteDictDataByCodes(dictCodes []string) (int64, error) {
// 检查是否存在
dictDatas := r.sysDictDataRepository.SelectDictDataByCodes(dictCodes)
if len(dictDatas) <= 0 {
return 0, errors.New("没有权限访问字典编码数据!")
}
if len(dictDatas) == len(dictCodes) {
for _, v := range dictDatas {
// 刷新缓存
r.sysDictTypeService.ClearDictCache(v.DictType)
r.sysDictTypeService.LoadingDictCache(v.DictType)
}
rows := r.sysDictDataRepository.DeleteDictDataByCodes(dictCodes)
return rows, nil
}
return 0, errors.New("删除字典数据信息失败!")
}
// InsertDictData 新增字典数据信息
func (r *SysDictDataImpl) InsertDictData(sysDictData model.SysDictData) string {
insertId := r.sysDictDataRepository.InsertDictData(sysDictData)
if insertId != "" {
r.sysDictTypeService.LoadingDictCache(sysDictData.DictType)
}
return insertId
}
// UpdateDictData 修改字典数据信息
func (r *SysDictDataImpl) UpdateDictData(sysDictData model.SysDictData) int64 {
rows := r.sysDictDataRepository.UpdateDictData(sysDictData)
if rows > 0 {
r.sysDictTypeService.LoadingDictCache(sysDictData.DictType)
}
return rows
}

View File

@@ -0,0 +1,45 @@
package service
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
// SelectDictTypeByID 根据字典类型ID查询信息
SelectDictTypeByID(dictID string) model.SysDictType
// SelectDictTypeByType 根据字典类型查询信息
SelectDictTypeByType(dictType string) model.SysDictType
// CheckUniqueDictName 校验字典名称是否唯一
CheckUniqueDictName(dictName, dictID string) bool
// CheckUniqueDictType 校验字典类型是否唯一
CheckUniqueDictType(dictType, dictID string) bool
// InsertDictType 新增字典类型信息
InsertDictType(sysDictType model.SysDictType) string
// UpdateDictType 修改字典类型信息
UpdateDictType(sysDictType model.SysDictType) int64
// DeleteDictTypeByIDs 批量删除字典类型信息
DeleteDictTypeByIDs(dictIDs []string) (int64, error)
// ResetDictCache 重置字典缓存数据
ResetDictCache()
// 加载字典缓存数据
LoadingDictCache(dictType string)
// 清空字典缓存数据
ClearDictCache(dictType string) bool
// DictDataCache 获取字典数据缓存数据
DictDataCache(dictType string) []model.SysDictData
}

View File

@@ -0,0 +1,211 @@
package service
import (
"encoding/json"
"errors"
"fmt"
"ems.agt/src/framework/constants/cachekey"
"ems.agt/src/framework/constants/common"
"ems.agt/src/framework/redis"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysDictTypeImpl 结构体
var NewSysDictTypeImpl = &SysDictTypeImpl{
sysDictTypeRepository: repository.NewSysDictTypeImpl,
sysDictDataRepository: repository.NewSysDictDataImpl,
}
// SysDictTypeImpl 字典类型 服务层处理
type SysDictTypeImpl struct {
// 字典类型服务
sysDictTypeRepository repository.ISysDictType
// 字典数据服务
sysDictDataRepository repository.ISysDictData
}
// SelectDictTypePage 根据条件分页查询字典类型
func (r *SysDictTypeImpl) SelectDictTypePage(query map[string]any) map[string]any {
return r.sysDictTypeRepository.SelectDictTypePage(query)
}
// SelectDictTypeList 根据条件查询字典类型
func (r *SysDictTypeImpl) SelectDictTypeList(sysDictType model.SysDictType) []model.SysDictType {
return r.sysDictTypeRepository.SelectDictTypeList(sysDictType)
}
// SelectDictTypeByID 根据字典类型ID查询信息
func (r *SysDictTypeImpl) SelectDictTypeByID(dictID string) model.SysDictType {
if dictID == "" {
return model.SysDictType{}
}
dictTypes := r.sysDictTypeRepository.SelectDictTypeByIDs([]string{dictID})
if len(dictTypes) > 0 {
return dictTypes[0]
}
return model.SysDictType{}
}
// SelectDictTypeByType 根据字典类型查询信息
func (r *SysDictTypeImpl) SelectDictTypeByType(dictType string) model.SysDictType {
return r.sysDictTypeRepository.SelectDictTypeByType(dictType)
}
// CheckUniqueDictName 校验字典名称是否唯一
func (r *SysDictTypeImpl) CheckUniqueDictName(dictName, dictID string) bool {
uniqueId := r.sysDictTypeRepository.CheckUniqueDictType(model.SysDictType{
DictName: dictName,
})
if uniqueId == dictID {
return true
}
return uniqueId == ""
}
// CheckUniqueDictType 校验字典类型是否唯一
func (r *SysDictTypeImpl) CheckUniqueDictType(dictType, dictID string) bool {
uniqueId := r.sysDictTypeRepository.CheckUniqueDictType(model.SysDictType{
DictType: dictType,
})
if uniqueId == dictID {
return true
}
return uniqueId == ""
}
// InsertDictType 新增字典类型信息
func (r *SysDictTypeImpl) InsertDictType(sysDictType model.SysDictType) string {
insertId := r.sysDictTypeRepository.InsertDictType(sysDictType)
if insertId != "" {
r.LoadingDictCache(sysDictType.DictType)
}
return insertId
}
// UpdateDictType 修改字典类型信息
func (r *SysDictTypeImpl) UpdateDictType(sysDictType model.SysDictType) int64 {
data := r.sysDictTypeRepository.SelectDictTypeByIDs([]string{sysDictType.DictID})
if len(data) == 0 {
return 0
}
// 修改字典类型key时同步更新其字典数据的类型key
oldDictType := data[0].DictType
rows := r.sysDictTypeRepository.UpdateDictType(sysDictType)
if rows > 0 && oldDictType != "" && oldDictType != sysDictType.DictType {
r.sysDictDataRepository.UpdateDictDataType(oldDictType, sysDictType.DictType)
}
// 刷新缓存
r.ClearDictCache(oldDictType)
r.LoadingDictCache(sysDictType.DictType)
return rows
}
// DeleteDictTypeByIDs 批量删除字典类型信息
func (r *SysDictTypeImpl) DeleteDictTypeByIDs(dictIDs []string) (int64, error) {
// 检查是否存在
dictTypes := r.sysDictTypeRepository.SelectDictTypeByIDs(dictIDs)
if len(dictTypes) <= 0 {
return 0, errors.New("没有权限访问字典类型数据!")
}
for _, v := range dictTypes {
// 字典类型下级含有数据
useCount := r.sysDictDataRepository.CountDictDataByType(v.DictType)
if useCount > 0 {
msg := fmt.Sprintf("【%s】存在字典数据,不能删除", v.DictName)
return 0, errors.New(msg)
}
// 清除缓存
r.ClearDictCache(v.DictType)
}
if len(dictTypes) == len(dictIDs) {
rows := r.sysDictTypeRepository.DeleteDictTypeByIDs(dictIDs)
return rows, nil
}
return 0, errors.New("删除字典数据信息失败!")
}
// ResetDictCache 重置字典缓存数据
func (r *SysDictTypeImpl) ResetDictCache() {
r.ClearDictCache("*")
r.LoadingDictCache("")
}
// getCacheKey 组装缓存key
func (r *SysDictTypeImpl) getDictCache(dictType string) string {
return cachekey.SYS_DICT_KEY + dictType
}
// LoadingDictCache 加载字典缓存数据
func (r *SysDictTypeImpl) LoadingDictCache(dictType string) {
sysDictData := model.SysDictData{
Status: common.STATUS_YES,
}
// 指定字典类型
if dictType != "" {
sysDictData.DictType = dictType
// 删除缓存
key := r.getDictCache(dictType)
redis.Del("", key)
}
sysDictDataList := r.sysDictDataRepository.SelectDictDataList(sysDictData)
if len(sysDictDataList) == 0 {
return
}
// 将字典数据按类型分组
m := make(map[string][]model.SysDictData, 0)
for _, v := range sysDictDataList {
key := v.DictType
if item, ok := m[key]; ok {
m[key] = append(item, v)
} else {
m[key] = []model.SysDictData{v}
}
}
// 放入缓存
for k, v := range m {
key := r.getDictCache(k)
values, _ := json.Marshal(v)
redis.Set("", key, string(values))
}
}
// ClearDictCache 清空字典缓存数据
func (r *SysDictTypeImpl) ClearDictCache(dictType string) bool {
key := r.getDictCache(dictType)
keys, err := redis.GetKeys("", key)
if err != nil {
return false
}
delOk, _ := redis.DelKeys("", keys)
return delOk
}
// DictDataCache 获取字典数据缓存数据
func (r *SysDictTypeImpl) DictDataCache(dictType string) []model.SysDictData {
data := []model.SysDictData{}
key := r.getDictCache(dictType)
jsonStr, _ := redis.Get("", key)
if len(jsonStr) > 7 {
err := json.Unmarshal([]byte(jsonStr), &data)
if err != nil {
data = []model.SysDictData{}
}
} else {
data = r.sysDictDataRepository.SelectDictDataList(model.SysDictData{
Status: common.STATUS_YES,
DictType: dictType,
})
if len(data) > 0 {
redis.Del("", key)
values, _ := json.Marshal(data)
redis.Set("", key, string(values))
}
}
return data
}

View File

@@ -0,0 +1,24 @@
package service
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
// NewSysLogLogin 生成系统登录日志
NewSysLogLogin(userName, status, msg string, ilobArgs ...string) string
}

View File

@@ -0,0 +1,56 @@
package service
import (
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysLogLoginImpl 结构体
var NewSysLogLoginImpl = &SysLogLoginImpl{
sysLogLoginService: repository.NewSysLogLoginImpl,
}
// SysLogLoginImpl 系统登录访问 服务层处理
type SysLogLoginImpl struct {
// 系统登录访问信息
sysLogLoginService repository.ISysLogLogin
}
// SelectSysLogLoginPage 分页查询系统登录日志集合
func (s *SysLogLoginImpl) SelectSysLogLoginPage(query map[string]any) map[string]any {
return s.sysLogLoginService.SelectSysLogLoginPage(query)
}
// SelectSysLogLoginList 查询系统登录日志集合
func (s *SysLogLoginImpl) SelectSysLogLoginList(sysSysLogLogin model.SysLogLogin) []model.SysLogLogin {
return s.sysLogLoginService.SelectSysLogLoginList(sysSysLogLogin)
}
// InsertSysLogLogin 新增系统登录日志
func (s *SysLogLoginImpl) InsertSysLogLogin(sysSysLogLogin model.SysLogLogin) string {
return s.sysLogLoginService.InsertSysLogLogin(sysSysLogLogin)
}
// DeleteSysLogLoginByIds 批量删除系统登录日志
func (s *SysLogLoginImpl) DeleteSysLogLoginByIds(loginIds []string) int64 {
return s.sysLogLoginService.DeleteSysLogLoginByIds(loginIds)
}
// CleanSysLogLogin 清空系统登录日志
func (s *SysLogLoginImpl) CleanSysLogLogin() error {
return s.sysLogLoginService.CleanSysLogLogin()
}
// NewSysLogLogin 生成系统登录日志
func (s *SysLogLoginImpl) NewSysLogLogin(userName, status, msg string, ilobArgs ...string) string {
sysSysLogLogin := model.SysLogLogin{
IPAddr: ilobArgs[0],
LoginLocation: ilobArgs[1],
OS: ilobArgs[2],
Browser: ilobArgs[3],
UserName: userName,
Status: status,
Msg: msg,
}
return s.InsertSysLogLogin(sysSysLogLogin)
}

View File

@@ -0,0 +1,24 @@
package service
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
}

View File

@@ -0,0 +1,47 @@
package service
import (
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysLogOperateImpl 结构体
var NewSysLogOperateImpl = &SysLogOperateImpl{
SysLogOperateService: repository.NewSysLogOperateImpl,
}
// SysLogOperateImpl 操作日志表 服务层处理
type SysLogOperateImpl struct {
// 操作日志信息
SysLogOperateService repository.ISysLogOperate
}
// SelectSysLogOperatePage 分页查询系统操作日志集合
func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[string]any {
return r.SysLogOperateService.SelectSysLogOperatePage(query)
}
// SelectSysLogOperateList 查询系统操作日志集合
func (r *SysLogOperateImpl) SelectSysLogOperateList(SysLogOperate model.SysLogOperate) []model.SysLogOperate {
return r.SysLogOperateService.SelectSysLogOperateList(SysLogOperate)
}
// SelectSysLogOperateById 查询操作日志详细
func (r *SysLogOperateImpl) SelectSysLogOperateById(operId string) model.SysLogOperate {
return r.SysLogOperateService.SelectSysLogOperateById(operId)
}
// InsertSysLogOperate 新增操作日志
func (r *SysLogOperateImpl) InsertSysLogOperate(SysLogOperate model.SysLogOperate) string {
return r.SysLogOperateService.InsertSysLogOperate(SysLogOperate)
}
// DeleteSysLogOperateByIds 批量删除系统操作日志
func (r *SysLogOperateImpl) DeleteSysLogOperateByIds(operIds []string) int64 {
return r.SysLogOperateService.DeleteSysLogOperateByIds(operIds)
}
// CleanSysLogOperate 清空操作日志
func (r *SysLogOperateImpl) CleanSysLogOperate() error {
return r.SysLogOperateService.CleanSysLogOperate()
}

View File

@@ -0,0 +1,51 @@
package service
import (
"ems.agt/src/framework/vo"
"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
// SelectMenuPermsByUserId 根据用户ID查询权限
SelectMenuTreeByUserId(userId string) []model.SysMenu
// SelectMenuTreeSelectByUserId 查询菜单树结构信息
SelectMenuTreeSelectByUserId(sysMenu model.SysMenu, userId string) []vo.TreeSelect
// SelectMenuListByRoleId 根据角色ID查询菜单树信息
SelectMenuListByRoleId(roleId string) []string
// SelectMenuById 根据菜单ID查询信息
SelectMenuById(menuId string) model.SysMenu
// HasChildByMenuIdAndStatus 存在菜单子节点数量与状态
HasChildByMenuIdAndStatus(menuId, status string) int64
// CheckMenuExistRole 查询菜单分配角色数量
CheckMenuExistRole(menuId string) int64
// InsertMenu 新增菜单信息
InsertMenu(sysMenu model.SysMenu) string
// UpdateMenu 修改菜单信息
UpdateMenu(sysMenu model.SysMenu) int64
// DeleteMenuById 删除菜单管理信息
DeleteMenuById(menuId string) int64
// CheckUniqueMenuName 校验菜单名称是否唯一
CheckUniqueMenuName(menuName, parentId, menuId string) bool
// CheckUniqueMenuPath 校验路由地址是否唯一(针对目录和菜单)
CheckUniqueMenuPath(path, menuId string) bool
// BuildRouteMenus 构建前端路由所需要的菜单
BuildRouteMenus(sysMenus []model.SysMenu, prefix string) []vo.Router
}

View File

@@ -0,0 +1,380 @@
package service
import (
"encoding/base64"
"strings"
"ems.agt/src/framework/constants/common"
"ems.agt/src/framework/constants/menu"
"ems.agt/src/framework/utils/parse"
"ems.agt/src/framework/utils/regular"
"ems.agt/src/framework/vo"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysMenuImpl 结构体
var NewSysMenuImpl = &SysMenuImpl{
sysMenuRepository: repository.NewSysMenuImpl,
sysRoleMenuRepository: repository.NewSysRoleMenuImpl,
sysRoleRepository: repository.NewSysRoleImpl,
}
// SysMenuImpl 菜单 服务层处理
type SysMenuImpl struct {
// 菜单服务
sysMenuRepository repository.ISysMenu
// 角色与菜单关联服务
sysRoleMenuRepository repository.ISysRoleMenu
// 角色服务
sysRoleRepository repository.ISysRole
}
// SelectMenuList 查询系统菜单列表
func (r *SysMenuImpl) SelectMenuList(sysMenu model.SysMenu, userId string) []model.SysMenu {
return r.sysMenuRepository.SelectMenuList(sysMenu, userId)
}
// SelectMenuPermsByUserId 根据用户ID查询权限
func (r *SysMenuImpl) SelectMenuPermsByUserId(userId string) []string {
return r.sysMenuRepository.SelectMenuPermsByUserId(userId)
}
// SelectMenuTreeByUserId 根据用户ID查询菜单
func (r *SysMenuImpl) SelectMenuTreeByUserId(userId string) []model.SysMenu {
sysMenus := r.sysMenuRepository.SelectMenuTreeByUserId(userId)
return r.parseDataToTree(sysMenus)
}
// SelectMenuTreeSelectByUserId 根据用户ID查询菜单树结构信息
func (r *SysMenuImpl) SelectMenuTreeSelectByUserId(sysMenu model.SysMenu, userId string) []vo.TreeSelect {
sysMenus := r.sysMenuRepository.SelectMenuList(sysMenu, userId)
menus := r.parseDataToTree(sysMenus)
tree := make([]vo.TreeSelect, 0)
for _, menu := range menus {
tree = append(tree, vo.SysMenuTreeSelect(menu))
}
return tree
}
// SelectMenuListByRoleId 根据角色ID查询菜单树信息 TODO
func (r *SysMenuImpl) SelectMenuListByRoleId(roleId string) []string {
roles := r.sysRoleRepository.SelectRoleByIds([]string{roleId})
if len(roles) > 0 {
role := roles[0]
if role.RoleID == roleId {
return r.sysMenuRepository.SelectMenuListByRoleId(
role.RoleID,
role.MenuCheckStrictly == "1",
)
}
}
return []string{}
}
// SelectMenuById 根据菜单ID查询信息
func (r *SysMenuImpl) SelectMenuById(menuId string) model.SysMenu {
if menuId == "" {
return model.SysMenu{}
}
menus := r.sysMenuRepository.SelectMenuByIds([]string{menuId})
if len(menus) > 0 {
return menus[0]
}
return model.SysMenu{}
}
// HasChildByMenuIdAndStatus 存在菜单子节点数量与状态
func (r *SysMenuImpl) HasChildByMenuIdAndStatus(menuId, status string) int64 {
return r.sysMenuRepository.HasChildByMenuIdAndStatus(menuId, status)
}
// CheckMenuExistRole 查询菜单是否存在角色
func (r *SysMenuImpl) CheckMenuExistRole(menuId string) int64 {
return r.sysRoleMenuRepository.CheckMenuExistRole(menuId)
}
// InsertMenu 新增菜单信息
func (r *SysMenuImpl) InsertMenu(sysMenu model.SysMenu) string {
return r.sysMenuRepository.InsertMenu(sysMenu)
}
// UpdateMenu 修改菜单信息
func (r *SysMenuImpl) UpdateMenu(sysMenu model.SysMenu) int64 {
return r.sysMenuRepository.UpdateMenu(sysMenu)
}
// DeleteMenuById 删除菜单管理信息
func (r *SysMenuImpl) DeleteMenuById(menuId string) int64 {
// 删除菜单与角色关联
r.sysRoleMenuRepository.DeleteMenuRole([]string{menuId})
return r.sysMenuRepository.DeleteMenuById(menuId)
}
// CheckUniqueMenuName 校验菜单名称是否唯一
func (r *SysMenuImpl) CheckUniqueMenuName(menuName, parentId, menuId string) bool {
uniqueId := r.sysMenuRepository.CheckUniqueMenu(model.SysMenu{
MenuName: menuName,
ParentID: parentId,
})
if uniqueId == menuId {
return true
}
return uniqueId == ""
}
// CheckUniqueMenuPath 校验路由地址是否唯一(针对目录和菜单)
func (r *SysMenuImpl) CheckUniqueMenuPath(path, menuId string) bool {
uniqueId := r.sysMenuRepository.CheckUniqueMenu(model.SysMenu{
Path: path,
})
if uniqueId == menuId {
return true
}
return uniqueId == ""
}
// BuildRouteMenus 构建前端路由所需要的菜单
func (r *SysMenuImpl) BuildRouteMenus(sysMenus []model.SysMenu, prefix string) []vo.Router {
routers := []vo.Router{}
for _, item := range sysMenus {
router := vo.Router{}
router.Name = r.getRouteName(item)
router.Path = r.getRouterPath(item)
router.Component = r.getComponent(item)
router.Meta = r.getRouteMeta(item)
// 子项菜单 目录类型 非路径链接
cMenus := item.Children
if len(cMenus) > 0 && item.MenuType == menu.TYPE_DIR && !regular.ValidHttp(item.Path) {
// 获取重定向地址
redirectPrefix, redirectPath := r.getRouteRedirect(
cMenus,
router.Path,
prefix,
)
router.Redirect = redirectPath
// 子菜单进入递归
router.Children = r.BuildRouteMenus(cMenus, redirectPrefix)
} else if item.ParentID == "0" && item.IsFrame == common.STATUS_YES && item.MenuType == menu.TYPE_MENU {
// 父菜单 内部跳转 菜单类型
menuPath := "/" + item.MenuID
childPath := menuPath + r.getRouterPath(item)
children := vo.Router{
Name: r.getRouteName(item),
Path: childPath,
Component: item.Component,
Meta: r.getRouteMeta(item),
}
router.Meta.HideChildInMenu = true
router.Children = append(router.Children, children)
router.Name = item.MenuID
router.Path = menuPath
router.Redirect = childPath
router.Component = menu.COMPONENT_LAYOUT_BASIC
} else if item.ParentID == "0" && item.IsFrame == common.STATUS_YES && regular.ValidHttp(item.Path) {
// 父菜单 内部跳转 路径链接
menuPath := "/" + item.MenuID
childPath := menuPath + r.getRouterPath(item)
children := vo.Router{
Name: r.getRouteName(item),
Path: childPath,
Component: menu.COMPONENT_LAYOUT_LINK,
Meta: r.getRouteMeta(item),
}
router.Meta.HideChildInMenu = true
router.Children = append(router.Children, children)
router.Name = item.MenuID
router.Path = menuPath
router.Redirect = childPath
router.Component = menu.COMPONENT_LAYOUT_BASIC
}
routers = append(routers, router)
}
return routers
}
// getRouteName 获取路由名称 路径英文首字母大写
func (r *SysMenuImpl) getRouteName(sysMenu model.SysMenu) string {
routerName := parse.ConvertToCamelCase(sysMenu.Path)
// 路径链接
if regular.ValidHttp(sysMenu.Path) {
routerName = routerName[:5] + "Link"
}
// 拼上菜单ID防止name重名
return routerName + "_" + sysMenu.MenuID
}
// getRouterPath 获取路由地址
func (r *SysMenuImpl) getRouterPath(sysMenu model.SysMenu) string {
routerPath := sysMenu.Path
// 显式路径
if routerPath == "" || strings.HasPrefix(routerPath, "/") {
return routerPath
}
// 路径链接 内部跳转
if regular.ValidHttp(routerPath) && sysMenu.IsFrame == common.STATUS_YES {
routerPath = regular.Replace(routerPath, `/^http(s)?:\/\/+/`, "")
routerPath = base64.StdEncoding.EncodeToString([]byte(routerPath))
}
// 父菜单 内部跳转
if sysMenu.ParentID == "0" && sysMenu.IsFrame == common.STATUS_YES {
routerPath = "/" + routerPath
}
return routerPath
}
// getComponent 获取组件信息
func (r *SysMenuImpl) getComponent(sysMenu model.SysMenu) string {
// 内部跳转 路径链接
if sysMenu.IsFrame == common.STATUS_YES && regular.ValidHttp(sysMenu.Path) {
return menu.COMPONENT_LAYOUT_LINK
}
// 非父菜单 目录类型
if sysMenu.ParentID != "0" && sysMenu.MenuType == menu.TYPE_DIR {
return menu.COMPONENT_LAYOUT_BLANK
}
// 组件路径 内部跳转 菜单类型
if sysMenu.Component != "" && sysMenu.IsFrame == common.STATUS_YES && sysMenu.MenuType == menu.TYPE_MENU {
// 父菜单套外层布局
if sysMenu.ParentID == "0" {
return menu.COMPONENT_LAYOUT_BASIC
}
return sysMenu.Component
}
return menu.COMPONENT_LAYOUT_BASIC
}
// getRouteMeta 获取路由元信息
func (r *SysMenuImpl) getRouteMeta(sysMenu model.SysMenu) vo.RouterMeta {
meta := vo.RouterMeta{}
if sysMenu.Icon == "#" {
meta.Icon = ""
} else {
meta.Icon = sysMenu.Icon
}
meta.Title = sysMenu.MenuName
meta.HideChildInMenu = false
meta.HideInMenu = sysMenu.Visible == common.STATUS_NO
meta.Cache = sysMenu.IsCache == common.STATUS_YES
meta.Target = ""
// 路径链接 非内部跳转
if regular.ValidHttp(sysMenu.Path) && sysMenu.IsFrame == common.STATUS_NO {
meta.Target = "_blank"
}
return meta
}
// getRouteRedirect 获取路由重定向地址(针对目录)
//
// cMenus 子菜单数组
// routerPath 当前菜单路径
// prefix 菜单重定向路径前缀
func (r *SysMenuImpl) getRouteRedirect(cMenus []model.SysMenu, routerPath string, prefix string) (string, string) {
redirectPath := ""
// 重定向为首个显示并启用的子菜单
var firstChild *model.SysMenu
for _, item := range cMenus {
if item.IsFrame == common.STATUS_YES && item.Visible == common.STATUS_YES {
firstChild = &item
break
}
}
// 检查内嵌隐藏菜单是否可做重定向
if firstChild == nil {
for _, item := range cMenus {
if item.IsFrame == common.STATUS_YES && item.Visible == common.STATUS_NO && strings.Contains(item.Path, menu.PATH_INLINE) {
firstChild = &item
break
}
}
}
if firstChild != nil {
firstChildPath := r.getRouterPath(*firstChild)
if strings.HasPrefix(firstChildPath, "/") {
redirectPath = firstChildPath
} else {
// 拼接追加路径
if !strings.HasPrefix(routerPath, "/") {
prefix += "/"
}
prefix = prefix + routerPath
redirectPath = prefix + "/" + firstChildPath
}
}
return prefix, redirectPath
}
// parseDataToTree 将数据解析为树结构,构建前端所需要下拉树结构
func (r *SysMenuImpl) parseDataToTree(sysMenus []model.SysMenu) []model.SysMenu {
// 节点分组
nodesMap := make(map[string][]model.SysMenu)
// 节点id
treeIds := []string{}
// 树节点
tree := []model.SysMenu{}
for _, item := range sysMenus {
parentID := item.ParentID
// 分组
mapItem, ok := nodesMap[parentID]
if !ok {
mapItem = []model.SysMenu{}
}
mapItem = append(mapItem, item)
nodesMap[parentID] = mapItem
// 记录节点ID
treeIds = append(treeIds, item.MenuID)
}
for key, value := range nodesMap {
// 选择不是节点ID的作为树节点
found := false
for _, id := range treeIds {
if id == key {
found = true
break
}
}
if !found {
tree = append(tree, value...)
}
}
for i, node := range tree {
iN := r.parseDataToTreeComponet(node, &nodesMap)
tree[i] = iN
}
return tree
}
// parseDataToTreeComponet 递归函数处理子节点
func (r *SysMenuImpl) parseDataToTreeComponet(node model.SysMenu, nodesMap *map[string][]model.SysMenu) model.SysMenu {
id := node.MenuID
children, ok := (*nodesMap)[id]
if ok {
node.Children = children
}
if len(node.Children) > 0 {
for i, child := range node.Children {
icN := r.parseDataToTreeComponet(child, nodesMap)
node.Children[i] = icN
}
}
return node
}

View File

@@ -0,0 +1,24 @@
package service
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 查询公告信息
SelectNoticeById(noticeId string) model.SysNotice
// InsertNotice 新增公告
InsertNotice(sysNotice model.SysNotice) string
// UpdateNotice 修改公告
UpdateNotice(sysNotice model.SysNotice) int64
// DeleteNoticeByIds 批量删除公告信息
DeleteNoticeByIds(noticeIds []string) (int64, error)
}

View File

@@ -0,0 +1,71 @@
package service
import (
"errors"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysNoticeImpl 结构体
var NewSysNoticeImpl = &SysNoticeImpl{
sysNoticeRepository: repository.NewSysNoticeImpl,
}
// SysNoticeImpl 公告 服务层处理
type SysNoticeImpl struct {
// 公告服务
sysNoticeRepository repository.ISysNotice
}
// SelectNoticePage 分页查询公告列表
func (r *SysNoticeImpl) SelectNoticePage(query map[string]any) map[string]any {
return r.sysNoticeRepository.SelectNoticePage(query)
}
// SelectNoticeList 查询公告列表
func (r *SysNoticeImpl) SelectNoticeList(sysNotice model.SysNotice) []model.SysNotice {
return r.sysNoticeRepository.SelectNoticeList(sysNotice)
}
// SelectNoticeById 查询公告信息
func (r *SysNoticeImpl) SelectNoticeById(noticeId string) model.SysNotice {
if noticeId == "" {
return model.SysNotice{}
}
configs := r.sysNoticeRepository.SelectNoticeByIds([]string{noticeId})
if len(configs) > 0 {
return configs[0]
}
return model.SysNotice{}
}
// InsertNotice 新增公告
func (r *SysNoticeImpl) InsertNotice(sysNotice model.SysNotice) string {
return r.sysNoticeRepository.InsertNotice(sysNotice)
}
// UpdateNotice 修改公告
func (r *SysNoticeImpl) UpdateNotice(sysNotice model.SysNotice) int64 {
return r.sysNoticeRepository.UpdateNotice(sysNotice)
}
// DeleteNoticeByIds 批量删除公告信息
func (r *SysNoticeImpl) DeleteNoticeByIds(noticeIds []string) (int64, error) {
// 检查是否存在
notices := r.sysNoticeRepository.SelectNoticeByIds(noticeIds)
if len(notices) <= 0 {
return 0, errors.New("没有权限访问公告信息数据!")
}
for _, notice := range notices {
// 检查是否为已删除
if notice.DelFlag == "1" {
return 0, errors.New(notice.NoticeID + " 公告信息已经删除!")
}
}
if len(notices) == len(noticeIds) {
rows := r.sysNoticeRepository.DeleteNoticeByIds(noticeIds)
return rows, nil
}
return 0, errors.New("删除公告信息失败!")
}

View File

@@ -0,0 +1,33 @@
package service
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
// SelectPostById 通过岗位ID查询岗位信息
SelectPostById(postId string) model.SysPost
// SelectPostListByUserId 根据用户ID获取岗位选择框列表
SelectPostListByUserId(userId string) []model.SysPost
// DeletePostByIds 批量删除岗位信息
DeletePostByIds(postIds []string) (int64, error)
// UpdatePost 修改岗位信息
UpdatePost(sysPost model.SysPost) int64
// InsertPost 新增岗位信息
InsertPost(sysPost model.SysPost) string
// CheckUniquePostName 校验岗位名称
CheckUniquePostName(postName, postId string) bool
// CheckUniquePostCode 校验岗位编码
CheckUniquePostCode(postCode, postId string) bool
}

View File

@@ -0,0 +1,103 @@
package service
import (
"errors"
"fmt"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysPostImpl 结构体
var NewSysPostImpl = &SysPostImpl{
sysPostRepository: repository.NewSysPostImpl,
sysUserPostRepository: repository.NewSysUserPostImpl,
}
// SysPostImpl 岗位表 服务层处理
type SysPostImpl struct {
// 岗位服务
sysPostRepository repository.ISysPost
// 用户与岗位关联服务
sysUserPostRepository repository.ISysUserPost
}
// SelectPostPage 查询岗位分页数据集合
func (r *SysPostImpl) SelectPostPage(query map[string]any) map[string]any {
return r.sysPostRepository.SelectPostPage(query)
}
// SelectPostList 查询岗位数据集合
func (r *SysPostImpl) SelectPostList(sysPost model.SysPost) []model.SysPost {
return r.sysPostRepository.SelectPostList(sysPost)
}
// SelectPostById 通过岗位ID查询岗位信息
func (r *SysPostImpl) SelectPostById(postId string) model.SysPost {
if postId == "" {
return model.SysPost{}
}
posts := r.sysPostRepository.SelectPostByIds([]string{postId})
if len(posts) > 0 {
return posts[0]
}
return model.SysPost{}
}
// SelectPostListByUserId 根据用户ID获取岗位选择框列表
func (r *SysPostImpl) SelectPostListByUserId(userId string) []model.SysPost {
return r.sysPostRepository.SelectPostListByUserId(userId)
}
// DeletePostByIds 批量删除岗位信息
func (r *SysPostImpl) DeletePostByIds(postIds []string) (int64, error) {
// 检查是否存在
posts := r.sysPostRepository.SelectPostByIds(postIds)
if len(posts) <= 0 {
return 0, errors.New("没有权限访问岗位数据!")
}
for _, post := range posts {
useCount := r.sysUserPostRepository.CountUserPostByPostId(post.PostID)
if useCount > 0 {
msg := fmt.Sprintf("【%s】已分配给用户,不能删除", post.PostName)
return 0, errors.New(msg)
}
}
if len(posts) == len(postIds) {
rows := r.sysPostRepository.DeletePostByIds(postIds)
return rows, nil
}
return 0, errors.New("删除岗位信息失败!")
}
// UpdatePost 修改岗位信息
func (r *SysPostImpl) UpdatePost(sysPost model.SysPost) int64 {
return r.sysPostRepository.UpdatePost(sysPost)
}
// InsertPost 新增岗位信息
func (r *SysPostImpl) InsertPost(sysPost model.SysPost) string {
return r.sysPostRepository.InsertPost(sysPost)
}
// CheckUniquePostName 校验岗位名称
func (r *SysPostImpl) CheckUniquePostName(postName, postId string) bool {
uniqueId := r.sysPostRepository.CheckUniquePost(model.SysPost{
PostName: postName,
})
if uniqueId == postId {
return true
}
return uniqueId == ""
}
// CheckUniquePostCode 校验岗位编码
func (r *SysPostImpl) CheckUniquePostCode(postCode, postId string) bool {
uniqueId := r.sysPostRepository.CheckUniquePost(model.SysPost{
PostCode: postCode,
})
if uniqueId == postId {
return true
}
return uniqueId == ""
}

View File

@@ -0,0 +1,42 @@
package service
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
// SelectRoleById 通过角色ID查询角色
SelectRoleById(roleId string) model.SysRole
// UpdateRole 修改角色信息
UpdateRole(sysRole model.SysRole) int64
// InsertRole 新增角色信息
InsertRole(sysRole model.SysRole) string
// DeleteRoleByIds 批量删除角色信息
DeleteRoleByIds(roleIds []string) (int64, error)
// CheckUniqueRoleName 校验角色名称是否唯一
CheckUniqueRoleName(roleName, roleId string) bool
// CheckUniqueRoleKey 校验角色权限是否唯一
CheckUniqueRoleKey(roleKey, roleId string) bool
// AuthDataScope 修改数据权限信息
AuthDataScope(sysRole model.SysRole) int64
// DeleteAuthUsers 批量取消授权用户角色
DeleteAuthUsers(roleId string, userIds []string) int64
// InsertAuthUsers 批量新增授权用户角色
InsertAuthUsers(roleId string, userIds []string) int64
}

View File

@@ -0,0 +1,189 @@
package service
import (
"errors"
"fmt"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysRoleImpl 结构体
var NewSysRoleImpl = &SysRoleImpl{
sysRoleRepository: repository.NewSysRoleImpl,
sysUserRoleRepository: repository.NewSysUserRoleImpl,
sysRoleDeptRepository: repository.NewSysRoleDeptImpl,
sysRoleMenuRepository: repository.NewSysRoleMenuImpl,
}
// SysRoleImpl 角色 服务层处理
type SysRoleImpl struct {
// 角色服务
sysRoleRepository repository.ISysRole
// 用户与角色关联服务
sysUserRoleRepository repository.ISysUserRole
// 角色与部门关联服务
sysRoleDeptRepository repository.ISysRoleDept
// 角色与菜单关联服务
sysRoleMenuRepository repository.ISysRoleMenu
}
// SelectRolePage 根据条件分页查询角色数据
func (r *SysRoleImpl) SelectRolePage(query map[string]any, dataScopeSQL string) map[string]any {
return r.sysRoleRepository.SelectRolePage(query, dataScopeSQL)
}
// SelectRoleList 根据条件查询角色数据
func (r *SysRoleImpl) SelectRoleList(sysRole model.SysRole, dataScopeSQL string) []model.SysRole {
return r.sysRoleRepository.SelectRoleList(sysRole, dataScopeSQL)
}
// SelectRoleListByUserId 根据用户ID获取角色选择框列表
func (r *SysRoleImpl) SelectRoleListByUserId(userId string) []model.SysRole {
return r.sysRoleRepository.SelectRoleListByUserId(userId)
}
// SelectRoleById 通过角色ID查询角色
func (r *SysRoleImpl) SelectRoleById(roleId string) model.SysRole {
if roleId == "" {
return model.SysRole{}
}
posts := r.sysRoleRepository.SelectRoleByIds([]string{roleId})
if len(posts) > 0 {
return posts[0]
}
return model.SysRole{}
}
// UpdateRole 修改角色信息
func (r *SysRoleImpl) UpdateRole(sysRole model.SysRole) int64 {
rows := r.sysRoleRepository.UpdateRole(sysRole)
if rows > 0 {
// 删除角色与菜单关联
r.sysRoleMenuRepository.DeleteRoleMenu([]string{sysRole.RoleID})
if len(sysRole.MenuIds) > 0 {
r.insertRoleMenu(sysRole.RoleID, sysRole.MenuIds)
}
}
return rows
}
// InsertRole 新增角色信息
func (r *SysRoleImpl) InsertRole(sysRole model.SysRole) string {
insertId := r.sysRoleRepository.InsertRole(sysRole)
if insertId != "" && len(sysRole.MenuIds) > 0 {
r.insertRoleMenu(insertId, sysRole.MenuIds)
}
return insertId
}
// insertRoleMenu 新增角色菜单信息
func (r *SysRoleImpl) insertRoleMenu(roleId string, menuIds []string) int64 {
if roleId == "" || len(menuIds) <= 0 {
return 0
}
sysRoleMenus := []model.SysRoleMenu{}
for _, menuId := range menuIds {
if menuId == "" {
continue
}
sysRoleMenus = append(sysRoleMenus, model.NewSysRoleMenu(roleId, menuId))
}
return r.sysRoleMenuRepository.BatchRoleMenu(sysRoleMenus)
}
// DeleteRoleByIds 批量删除角色信息
func (r *SysRoleImpl) DeleteRoleByIds(roleIds []string) (int64, error) {
// 检查是否存在
roles := r.sysRoleRepository.SelectRoleByIds(roleIds)
if len(roles) <= 0 {
return 0, errors.New("没有权限访问角色数据!")
}
for _, role := range roles {
// 检查是否为已删除
if role.DelFlag == "1" {
return 0, errors.New(role.RoleID + " 角色信息已经删除!")
}
// 检查分配用户
userCount := r.sysUserRoleRepository.CountUserRoleByRoleId(role.RoleID)
if userCount > 0 {
msg := fmt.Sprintf("【%s】已分配给用户,不能删除", role.RoleName)
return 0, errors.New(msg)
}
}
if len(roles) == len(roleIds) {
// 删除角色与菜单关联
r.sysRoleMenuRepository.DeleteRoleMenu(roleIds)
// 删除角色与部门关联
r.sysRoleDeptRepository.DeleteRoleDept(roleIds)
rows := r.sysRoleRepository.DeleteRoleByIds(roleIds)
return rows, nil
}
return 0, errors.New("删除角色信息失败!")
}
// CheckUniqueRoleName 校验角色名称是否唯一
func (r *SysRoleImpl) CheckUniqueRoleName(roleName, roleId string) bool {
uniqueId := r.sysRoleRepository.CheckUniqueRole(model.SysRole{
RoleName: roleName,
})
if uniqueId == roleId {
return true
}
return uniqueId == ""
}
// CheckUniqueRoleKey 校验角色权限是否唯一
func (r *SysRoleImpl) CheckUniqueRoleKey(roleKey, roleId string) bool {
uniqueId := r.sysRoleRepository.CheckUniqueRole(model.SysRole{
RoleKey: roleKey,
})
if uniqueId == roleId {
return true
}
return uniqueId == ""
}
// AuthDataScope 修改数据权限信息
func (r *SysRoleImpl) AuthDataScope(sysRole model.SysRole) int64 {
// 修改角色信息
rows := r.sysRoleRepository.UpdateRole(sysRole)
// 删除角色与部门关联
r.sysRoleDeptRepository.DeleteRoleDept([]string{sysRole.RoleID})
// 新增角色和部门信息
if sysRole.DataScope == "2" && len(sysRole.DeptIds) > 0 {
sysRoleDepts := []model.SysRoleDept{}
for _, deptId := range sysRole.DeptIds {
if deptId == "" {
continue
}
sysRoleDepts = append(sysRoleDepts, model.NewSysRoleDept(sysRole.RoleID, deptId))
}
rows += r.sysRoleDeptRepository.BatchRoleDept(sysRoleDepts)
}
return rows
}
// DeleteAuthUsers 批量取消授权用户角色
func (r *SysRoleImpl) DeleteAuthUsers(roleId string, userIds []string) int64 {
return r.sysUserRoleRepository.DeleteUserRoleByRoleId(roleId, userIds)
}
// InsertAuthUsers 批量新增授权用户角色
func (r *SysRoleImpl) InsertAuthUsers(roleId string, userIds []string) int64 {
if roleId == "" || len(userIds) <= 0 {
return 0
}
sysUserRoles := []model.SysUserRole{}
for _, userId := range userIds {
if userId == "" {
continue
}
sysUserRoles = append(sysUserRoles, model.NewSysUserRole(userId, roleId))
}
return r.sysUserRoleRepository.BatchUserRole(sysUserRoles)
}

View File

@@ -0,0 +1,45 @@
package service
import "ems.agt/src/modules/system/model"
// ISysUser 用户 服务层接口
type ISysUser interface {
// SelectUserPage 根据条件分页查询用户列表
SelectUserPage(query map[string]any, dataScopeSQL string) map[string]any
// SelectUserList 根据条件查询用户列表
SelectUserList(sysUser model.SysUser, dataScopeSQL string) []model.SysUser
// SelectAllocatedPage 根据条件分页查询分配用户角色列表
SelectAllocatedPage(query map[string]any, dataScopeSQL string) map[string]any
// SelectUserByUserName 通过用户名查询用户
SelectUserByUserName(userName string) model.SysUser
// SelectUserById 通过用户ID查询用户
SelectUserById(userId string) model.SysUser
// InsertUser 新增用户信息
InsertUser(sysUser model.SysUser) string
// UpdateUser 修改用户信息
UpdateUser(sysUser model.SysUser) int64
// UpdateUserAndRolePost 修改用户信息同时更新角色和岗位
UpdateUserAndRolePost(sysUser model.SysUser) int64
// DeleteUserByIds 批量删除用户信息
DeleteUserByIds(userIds []string) (int64, error)
// CheckUniqueUserName 校验用户名称是否唯一
CheckUniqueUserName(userName, userId string) bool
// CheckUniquePhone 校验手机号码是否唯一
CheckUniquePhone(phonenumber, userId string) bool
// CheckUniqueEmail 校验email是否唯一
CheckUniqueEmail(email, userId string) bool
// ImportUser 导入用户数据
ImportUser(rows []map[string]string, isUpdateSupport bool, operName string) string
}

View File

@@ -0,0 +1,322 @@
package service
import (
"errors"
"fmt"
"strings"
"ems.agt/src/framework/constants/admin"
"ems.agt/src/framework/constants/common"
"ems.agt/src/framework/utils/regular"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/repository"
)
// 实例化服务层 SysUserImpl 结构体
var NewSysUserImpl = &SysUserImpl{
sysUserRepository: repository.NewSysUserImpl,
sysUserRoleRepository: repository.NewSysUserRoleImpl,
sysUserPostRepository: repository.NewSysUserPostImpl,
sysDictDataService: NewSysDictDataImpl,
sysConfigService: NewSysConfigImpl,
}
// SysUserImpl 用户 服务层处理
type SysUserImpl struct {
// 用户服务
sysUserRepository repository.ISysUser
// 用户与角色服务
sysUserRoleRepository repository.ISysUserRole
// 用户与岗位服务
sysUserPostRepository repository.ISysUserPost
// 字典数据服务
sysDictDataService ISysDictData
// 参数配置服务
sysConfigService ISysConfig
}
// SelectUserPage 根据条件分页查询用户列表
func (r *SysUserImpl) SelectUserPage(query map[string]any, dataScopeSQL string) map[string]any {
return r.sysUserRepository.SelectUserPage(query, dataScopeSQL)
}
// SelectUserList 根据条件查询用户列表
func (r *SysUserImpl) SelectUserList(sysUser model.SysUser, dataScopeSQL string) []model.SysUser {
return []model.SysUser{}
}
// SelectAllocatedPage 根据条件分页查询分配用户角色列表
func (r *SysUserImpl) SelectAllocatedPage(query map[string]any, dataScopeSQL string) map[string]any {
return r.sysUserRepository.SelectAllocatedPage(query, dataScopeSQL)
}
// SelectUserByUserName 通过用户名查询用户
func (r *SysUserImpl) SelectUserByUserName(userName string) model.SysUser {
return r.sysUserRepository.SelectUserByUserName(userName)
}
// SelectUserById 通过用户ID查询用户
func (r *SysUserImpl) SelectUserById(userId string) model.SysUser {
if userId == "" {
return model.SysUser{}
}
users := r.sysUserRepository.SelectUserByIds([]string{userId})
if len(users) > 0 {
return users[0]
}
return model.SysUser{}
}
// InsertUser 新增用户信息
func (r *SysUserImpl) InsertUser(sysUser model.SysUser) string {
// 新增用户信息
insertId := r.sysUserRepository.InsertUser(sysUser)
if insertId != "" {
// 新增用户角色信息
r.insertUserRole(insertId, sysUser.RoleIDs)
// 新增用户岗位信息
r.insertUserPost(insertId, sysUser.PostIDs)
}
return insertId
}
// insertUserRole 新增用户角色信息
func (r *SysUserImpl) insertUserRole(userId string, roleIds []string) int64 {
if userId == "" || len(roleIds) <= 0 {
return 0
}
sysUserRoles := []model.SysUserRole{}
for _, roleId := range roleIds {
// 管理员角色禁止操作只能通过配置指定用户ID分配
if roleId == "" || roleId == admin.ROLE_ID {
continue
}
sysUserRoles = append(sysUserRoles, model.NewSysUserRole(userId, roleId))
}
return r.sysUserRoleRepository.BatchUserRole(sysUserRoles)
}
// insertUserPost 新增用户岗位信息
func (r *SysUserImpl) insertUserPost(userId string, postIds []string) int64 {
if userId == "" || len(postIds) <= 0 {
return 0
}
sysUserPosts := []model.SysUserPost{}
for _, postId := range postIds {
if postId == "" {
continue
}
sysUserPosts = append(sysUserPosts, model.NewSysUserPost(userId, postId))
}
return r.sysUserPostRepository.BatchUserPost(sysUserPosts)
}
// UpdateUser 修改用户信息
func (r *SysUserImpl) UpdateUser(sysUser model.SysUser) int64 {
return r.sysUserRepository.UpdateUser(sysUser)
}
// UpdateUserAndRolePost 修改用户信息同时更新角色和岗位
func (r *SysUserImpl) UpdateUserAndRolePost(sysUser model.SysUser) int64 {
// 删除用户与角色关联
r.sysUserRoleRepository.DeleteUserRole([]string{sysUser.UserID})
// 新增用户角色信息
r.insertUserRole(sysUser.UserID, sysUser.RoleIDs)
// 删除用户与岗位关联
r.sysUserPostRepository.DeleteUserPost([]string{sysUser.UserID})
// 新增用户岗位信息
r.insertUserPost(sysUser.UserID, sysUser.PostIDs)
return r.sysUserRepository.UpdateUser(sysUser)
}
// DeleteUserByIds 批量删除用户信息
func (r *SysUserImpl) DeleteUserByIds(userIds []string) (int64, error) {
// 检查是否存在
users := r.sysUserRepository.SelectUserByIds(userIds)
if len(users) <= 0 {
return 0, errors.New("没有权限访问用户数据!")
}
if len(users) == len(userIds) {
// 删除用户与角色关联
r.sysUserRoleRepository.DeleteUserRole(userIds)
// 删除用户与岗位关联
r.sysUserPostRepository.DeleteUserPost(userIds)
// ... 注意其他userId进行关联的表
// 删除用户
rows := r.sysUserRepository.DeleteUserByIds(userIds)
return rows, nil
}
return 0, errors.New("删除用户信息失败!")
}
// CheckUniqueUserName 校验用户名称是否唯一
func (r *SysUserImpl) CheckUniqueUserName(userName, userId string) bool {
uniqueId := r.sysUserRepository.CheckUniqueUser(model.SysUser{
UserName: userName,
})
if uniqueId == userId {
return true
}
return uniqueId == ""
}
// CheckUniquePhone 校验手机号码是否唯一
func (r *SysUserImpl) CheckUniquePhone(phonenumber, userId string) bool {
uniqueId := r.sysUserRepository.CheckUniqueUser(model.SysUser{
PhoneNumber: phonenumber,
})
if uniqueId == userId {
return true
}
return uniqueId == ""
}
// CheckUniqueEmail 校验email是否唯一
func (r *SysUserImpl) CheckUniqueEmail(email, userId string) bool {
uniqueId := r.sysUserRepository.CheckUniqueUser(model.SysUser{
Email: email,
})
if uniqueId == userId {
return true
}
return uniqueId == ""
}
// ImportUser 导入用户数据
func (r *SysUserImpl) ImportUser(rows []map[string]string, isUpdateSupport bool, operName string) string {
// 读取默认初始密码
initPassword := r.sysConfigService.SelectConfigValueByKey("sys.user.initPassword")
// 读取用户性别字典数据
dictSysUserSex := r.sysDictDataService.SelectDictDataByType("sys_user_sex")
// 导入记录
successNum := 0
failureNum := 0
successMsgArr := []string{}
failureMsgArr := []string{}
mustItemArr := []string{"C", "D"}
for _, row := range rows {
// 检查必填列
ownItem := true
for _, item := range mustItemArr {
if v, ok := row[item]; !ok || v == "" {
ownItem = false
break
}
}
if !ownItem {
mustItemArrStr := strings.Join(mustItemArr, "、")
failureNum++
failureMsgArr = append(failureMsgArr, fmt.Sprintf("表格中必填列表项,%s}", mustItemArrStr))
continue
}
// 用户性别转值
sysUserSex := "0"
for _, v := range dictSysUserSex {
if row["G"] == v.DictLabel {
sysUserSex = v.DictValue
break
}
}
sysUserStatus := common.STATUS_NO
if row["H"] == "正常" {
sysUserStatus = common.STATUS_YES
}
// 构建用户实体信息
newSysUser := model.SysUser{
UserType: "sys",
Password: initPassword,
DeptID: row["B"],
UserName: row["C"],
NickName: row["D"],
PhoneNumber: row["F"],
Email: row["E"],
Status: sysUserStatus,
Sex: sysUserSex,
}
// 检查手机号码格式并判断是否唯一
if newSysUser.PhoneNumber != "" {
if regular.ValidMobile(newSysUser.PhoneNumber) {
uniquePhone := r.CheckUniquePhone(newSysUser.PhoneNumber, "")
if !uniquePhone {
msg := fmt.Sprintf("序号:%s 手机号码 %s 已存在", row["A"], row["F"])
failureNum++
failureMsgArr = append(failureMsgArr, msg)
continue
}
} else {
msg := fmt.Sprintf("序号:%s 手机号码 %s 格式错误", row["A"], row["F"])
failureNum++
failureMsgArr = append(failureMsgArr, msg)
continue
}
}
// 检查邮箱格式并判断是否唯一
if newSysUser.Email != "" {
if regular.ValidEmail(newSysUser.Email) {
uniqueEmail := r.CheckUniqueEmail(newSysUser.Email, "")
if !uniqueEmail {
msg := fmt.Sprintf("序号:%s 用户邮箱 %s 已存在", row["A"], row["E"])
failureNum++
failureMsgArr = append(failureMsgArr, msg)
continue
}
} else {
msg := fmt.Sprintf("序号:%s 用户邮箱 %s 格式错误", row["A"], row["E"])
failureNum++
failureMsgArr = append(failureMsgArr, msg)
continue
}
}
// 验证是否存在这个用户
userInfo := r.sysUserRepository.SelectUserByUserName(newSysUser.UserName)
if userInfo.UserName != newSysUser.UserName {
newSysUser.CreateBy = operName
insertId := r.InsertUser(newSysUser)
if insertId != "" {
msg := fmt.Sprintf("序号:%s 登录名称 %s 导入成功", row["A"], row["C"])
successNum++
successMsgArr = append(successMsgArr, msg)
} else {
msg := fmt.Sprintf("序号:%s 登录名称 %s 导入失败", row["A"], row["E"])
failureNum++
failureMsgArr = append(failureMsgArr, msg)
}
continue
}
// 如果用户已存在 同时 是否更新支持
if userInfo.UserName == newSysUser.UserName && isUpdateSupport {
newSysUser.UserID = userInfo.UserID
newSysUser.UpdateBy = operName
rows := r.UpdateUser(newSysUser)
if rows > 0 {
msg := fmt.Sprintf("序号:%s 登录名称 %s 更新成功", row["A"], row["C"])
successNum++
successMsgArr = append(successMsgArr, msg)
} else {
msg := fmt.Sprintf("序号:%s 登录名称 %s 更新失败", row["A"], row["E"])
failureNum++
failureMsgArr = append(failureMsgArr, msg)
}
continue
}
}
if failureNum > 0 {
failureMsgArr = append([]string{fmt.Sprintf("很抱歉,导入失败!共 %d 条数据格式不正确,错误如下:", failureNum)}, failureMsgArr...)
return strings.Join(failureMsgArr, "<br/>")
}
successMsgArr = append([]string{fmt.Sprintf("恭喜您,数据已全部导入成功!共 %d 条,数据如下:", successNum)}, successMsgArr...)
return strings.Join(successMsgArr, "<br/>")
}