123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- package services
- import (
- "eta/eta_forum_hub/models/system"
- "eta/eta_forum_hub/services/alarm_msg"
- "eta/eta_forum_hub/utils"
- "fmt"
- "time"
- )
- // 新增或者更新角色
- func AddOrUpdateRole(req *system.AddRoleReq) (err error, isSendEmail bool) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("新增或者更新角色, AddOrUpdateRole Err: " + err.Error())
- go alarm_msg.SendAlarmMsg("新增或者更新角色, AddOrUpdateRole Err: "+err.Error(), 3)
- }
- }()
- isSendEmail = true
- isAdd := true
- roleInfo, err := system.GetSysRoleByRoleId(req.RoleId)
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取数据失败,%v", err)
- return
- }
- }
- if err == nil && roleInfo.RoleId > 0 {
- isAdd = false
- } else {
- roleInfo = new(system.SysRole)
- }
- // 角色信息
- roleInfo.RoleId = req.RoleId
- roleInfo.RoleName = req.RoleName
- roleInfo.RoleType = req.RoleType
- roleInfo.RoleTypeCode = req.RoleTypeCode
- roleInfo.RoleLevel = req.RoleLevel
- roleInfo.ModifyTime = req.ModifyTime
- roleInfo.CreateTime = req.CreateTime
- if isAdd {
- _, err = system.AddSysRole(roleInfo)
- if err != nil {
- err = fmt.Errorf("新增失败,Err:%s", err.Error())
- return
- }
- } else {
- err = roleInfo.Update([]string{})
- if err != nil {
- err = fmt.Errorf("更新失败,Err:%s", err.Error())
- return
- }
- }
- return
- }
- // 新增或者更新部门
- func AddOrUpdateDepartment(req *system.SysDepartmentAddReq) (err error, isSendEmail bool) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("新增或者更新部门, AddOrUpdateDepartment Err: " + err.Error())
- go alarm_msg.SendAlarmMsg("新增或者更新部门, AddOrUpdateDepartment Err: "+err.Error(), 3)
- }
- }()
- isSendEmail = true
- isAdd := true
- info, err := system.GetSysDepartmentById(req.DepartmentId)
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取数据失败,%v", err)
- return
- }
- }
- if err == nil && info.DepartmentId > 0 {
- isAdd = false
- } else {
- info = new(system.SysDepartment)
- }
- // 角色信息
- info.DepartmentId = req.DepartmentId
- info.DepartmentName = req.DepartmentName
- info.Sort = req.Sort
- info.CreateTime = req.CreateTime
- if isAdd {
- _, err = system.AddSysDepartment(info)
- if err != nil {
- err = fmt.Errorf("新增失败,Err:%s", err.Error())
- return
- }
- } else {
- err = info.Update([]string{})
- if err != nil {
- err = fmt.Errorf("更新失败,Err:%s", err.Error())
- return
- }
- }
- return
- }
- // 新增或者更新分组
- func AddOrUpdateGroup(req *system.SysGroupAddReq) (err error, isSendEmail bool) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("新增或者更新分组, AddOrUpdateGroup Err: " + err.Error())
- go alarm_msg.SendAlarmMsg("新增或者更新分组, AddOrUpdateGroup Err: "+err.Error(), 3)
- }
- }()
- isSendEmail = true
- if len(req.List) == 0 {
- return
- }
- for _, v := range req.List {
- if v.DepartmentId == 0 {
- continue
- }
- err = v.AddOrUpdate()
- if err != nil {
- return
- }
- }
- return
- }
- // 批量更新账号
- func BatchAddOrUpdateAdmin(reqAdmin *system.AdminBatchAddReq) (err error, isSendEmail bool) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("新增或者更新账号, BatchAddOrUpdateAdmin Err: " + err.Error())
- go alarm_msg.SendAlarmMsg("新增或者更新账号, BatchAddOrUpdateAdmin Err: "+err.Error(), 3)
- }
- }()
- isSendEmail = true
- if len(reqAdmin.List) == 0 {
- return
- }
- // 查询所有的账号,如果不在同步的列表里,则删除
- deleteAdminList := make(map[string]int)
- adminList, err := system.GetAllAdminList()
- if err != nil {
- err = fmt.Errorf("获取账号数据失败,%v", err)
- return
- }
- adminMap := make(map[string]*system.Admin)
- for _, v := range adminList {
- deleteAdminList[v.AdminName] = v.AdminId
- adminMap[v.AdminName] = v
- }
- for _, req := range reqAdmin.List {
- adminItem, ok := adminMap[req.AdminName]
- if ok {
- delete(deleteAdminList, req.AdminName)
- } else {
- adminItem = new(system.Admin)
- }
- adminItem.AdminName = req.AdminName
- adminItem.RealName = req.RealName
- adminItem.Password = req.Password
- adminItem.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
- adminItem.Enabled = req.Enabled
- adminItem.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
- adminItem.Mobile = req.Mobile
- adminItem.RoleType = req.RoleType
- adminItem.RoleId = req.RoleId
- adminItem.RoleName = req.RoleName
- adminItem.RoleTypeCode = req.RoleTypeCode
- adminItem.DepartmentId = req.DepartmentId
- adminItem.DepartmentName = req.DepartmentName
- adminItem.AdminAvatar = req.AdminAvatar
- adminItem.GroupId = req.GroupId
- adminItem.GroupName = req.GroupName
- adminItem.Role = req.Role
- adminItem.EmployeeId = req.EmployeeId
- adminItem.Email = req.Email
- adminItem.Authority = req.Authority
- adminItem.Position = req.Position
- adminItem.Province = req.Province
- adminItem.ProvinceCode = req.ProvinceCode
- adminItem.City = req.City
- adminItem.CityCode = req.CityCode
- adminItem.TelAreaCode = req.TelAreaCode
- adminItem.IsLdap = req.IsLdap
- //admin.BusinessCode = req.BusinessCode
- if !ok {
- adminItem.CreatedTime = time.Now()
- adminItem.LastLoginTime = time.Now().Format(utils.FormatDateTime)
- err = system.AddAdmin(adminItem)
- if err != nil {
- err = fmt.Errorf("新增失败,Err:%s", err.Error())
- return
- }
- } else {
- err = adminItem.Update([]string{})
- if err != nil {
- err = fmt.Errorf("更新失败,Err:%s", err.Error())
- return
- }
- }
- }
- for _, v := range deleteAdminList {
- err = system.DeleteSysuser(v)
- if err != nil {
- err = fmt.Errorf("删除失败,Err:%s", err.Error())
- return
- }
- }
- return
- }
- // 同步分组数据
- func SyncSysGroup(req *system.SysGroupAddReq) (err error, isSendEmail bool) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("新增或者更新分组, SyncSysGroup Err: " + err.Error())
- go alarm_msg.SendAlarmMsg("新增或者更新分组, SyncSysGroup Err: "+err.Error(), 3)
- }
- }()
- isSendEmail = true
- if len(req.List) == 0 {
- return
- }
- groupList, err := system.GetAllSysGroup()
- if err != nil {
- err = fmt.Errorf("获取账号数据失败,%v", err)
- return
- }
- // 查询所有的账号,如果不在同步的列表里,则删除
- deleteGroupList := make(map[int]int)
- for _, v := range groupList {
- deleteGroupList[v.GroupId] = v.GroupId
- }
- for _, v := range req.List {
- if v.GroupId > 0 {
- delete(deleteGroupList, v.GroupId)
- }
- err = v.AddOrUpdate()
- if err != nil {
- return
- }
- }
- for _, v := range deleteGroupList {
- err = system.DeleteSysGroup(v)
- if err != nil {
- err = fmt.Errorf("删除失败,Err:%s", err.Error())
- return
- }
- }
- return
- }
- // 同步部门数据
- func SyncSysDepartment(req *system.SysDepartmentSyncReq) (err error, isSendEmail bool) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("新增或者更新部门, SyncSysDepartment Err: " + err.Error())
- go alarm_msg.SendAlarmMsg("新增或者更新部门, SyncSysDepartment Err: "+err.Error(), 3)
- }
- }()
- isSendEmail = true
- if len(req.List) == 0 {
- return
- }
- list, err := system.GetAllSysDepartment()
- if err != nil {
- err = fmt.Errorf("获取账号数据失败,%v", err)
- return
- }
- // 查询所有的账号,如果不在同步的列表里,则删除
- deleteList := make(map[int]int)
- for _, v := range list {
- deleteList[v.DepartmentId] = v.DepartmentId
- }
- for _, v := range req.List {
- if v.DepartmentId > 0 {
- delete(deleteList, v.DepartmentId)
- }
- err = v.AddOrUpdate()
- if err != nil {
- return
- }
- }
- for _, v := range deleteList {
- err = system.DeleteSysDepartment(v)
- if err != nil {
- err = fmt.Errorf("删除失败,Err:%s", err.Error())
- return
- }
- }
- return
- }
- // 同步角色数据
- func SyncSysRole(req *system.SysRoleSyncReq) (err error, isSendEmail bool) {
- defer func() {
- if err != nil {
- utils.FileLog.Info("同步角色数据, SyncSysRole Err: " + err.Error())
- go alarm_msg.SendAlarmMsg("同步角色数据, SyncSysRole Err: "+err.Error(), 3)
- }
- }()
- isSendEmail = true
- if len(req.List) == 0 {
- return
- }
- list, err := system.GetSysRoleList()
- if err != nil {
- err = fmt.Errorf("获取账号数据失败,%v", err)
- return
- }
- // 查询所有的账号,如果不在同步的列表里,则删除
- deleteList := make(map[int]int)
- for _, v := range list {
- deleteList[v.RoleId] = v.RoleId
- }
- for _, v := range req.List {
- if v.RoleId > 0 {
- delete(deleteList, v.RoleId)
- }
- err = v.AddOrUpdate()
- if err != nil {
- return
- }
- }
- for _, v := range deleteList {
- err = system.DeleteSysRole(v)
- if err != nil {
- err = fmt.Errorf("删除失败,Err:%s", err.Error())
- return
- }
- }
- return
- }
|