system.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package services
  2. import (
  3. "eta/eta_forum_hub/models/system"
  4. "eta/eta_forum_hub/services/alarm_msg"
  5. "eta/eta_forum_hub/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // 新增或者更新角色
  10. func AddOrUpdateRole(req *system.AddRoleReq) (err error, isSendEmail bool) {
  11. defer func() {
  12. if err != nil {
  13. utils.FileLog.Info("新增或者更新角色, AddOrUpdateRole Err: " + err.Error())
  14. go alarm_msg.SendAlarmMsg("新增或者更新角色, AddOrUpdateRole Err: "+err.Error(), 3)
  15. }
  16. }()
  17. isSendEmail = true
  18. isAdd := true
  19. roleInfo, err := system.GetSysRoleByRoleId(req.RoleId)
  20. if err != nil {
  21. if err.Error() != utils.ErrNoRow() {
  22. err = fmt.Errorf("获取数据失败,%v", err)
  23. return
  24. }
  25. }
  26. if err == nil && roleInfo.RoleId > 0 {
  27. isAdd = false
  28. } else {
  29. roleInfo = new(system.SysRole)
  30. }
  31. // 角色信息
  32. roleInfo.RoleId = req.RoleId
  33. roleInfo.RoleName = req.RoleName
  34. roleInfo.RoleType = req.RoleType
  35. roleInfo.RoleTypeCode = req.RoleTypeCode
  36. roleInfo.RoleLevel = req.RoleLevel
  37. roleInfo.ModifyTime = req.ModifyTime
  38. roleInfo.CreateTime = req.CreateTime
  39. if isAdd {
  40. _, err = system.AddSysRole(roleInfo)
  41. if err != nil {
  42. err = fmt.Errorf("新增失败,Err:%s", err.Error())
  43. return
  44. }
  45. } else {
  46. err = roleInfo.Update([]string{})
  47. if err != nil {
  48. err = fmt.Errorf("更新失败,Err:%s", err.Error())
  49. return
  50. }
  51. }
  52. return
  53. }
  54. // 新增或者更新部门
  55. func AddOrUpdateDepartment(req *system.SysDepartmentAddReq) (err error, isSendEmail bool) {
  56. defer func() {
  57. if err != nil {
  58. utils.FileLog.Info("新增或者更新部门, AddOrUpdateDepartment Err: " + err.Error())
  59. go alarm_msg.SendAlarmMsg("新增或者更新部门, AddOrUpdateDepartment Err: "+err.Error(), 3)
  60. }
  61. }()
  62. isSendEmail = true
  63. isAdd := true
  64. info, err := system.GetSysDepartmentById(req.DepartmentId)
  65. if err != nil {
  66. if err.Error() != utils.ErrNoRow() {
  67. err = fmt.Errorf("获取数据失败,%v", err)
  68. return
  69. }
  70. }
  71. if err == nil && info.DepartmentId > 0 {
  72. isAdd = false
  73. } else {
  74. info = new(system.SysDepartment)
  75. }
  76. // 角色信息
  77. info.DepartmentId = req.DepartmentId
  78. info.DepartmentName = req.DepartmentName
  79. info.Sort = req.Sort
  80. info.CreateTime = req.CreateTime
  81. if isAdd {
  82. _, err = system.AddSysDepartment(info)
  83. if err != nil {
  84. err = fmt.Errorf("新增失败,Err:%s", err.Error())
  85. return
  86. }
  87. } else {
  88. err = info.Update([]string{})
  89. if err != nil {
  90. err = fmt.Errorf("更新失败,Err:%s", err.Error())
  91. return
  92. }
  93. }
  94. return
  95. }
  96. // 新增或者更新分组
  97. func AddOrUpdateGroup(req *system.SysGroupAddReq) (err error, isSendEmail bool) {
  98. defer func() {
  99. if err != nil {
  100. utils.FileLog.Info("新增或者更新分组, AddOrUpdateGroup Err: " + err.Error())
  101. go alarm_msg.SendAlarmMsg("新增或者更新分组, AddOrUpdateGroup Err: "+err.Error(), 3)
  102. }
  103. }()
  104. isSendEmail = true
  105. if len(req.List) == 0 {
  106. return
  107. }
  108. for _, v := range req.List {
  109. if v.DepartmentId == 0 {
  110. continue
  111. }
  112. err = v.AddOrUpdate()
  113. if err != nil {
  114. return
  115. }
  116. }
  117. return
  118. }
  119. // 批量更新账号
  120. func BatchAddOrUpdateAdmin(reqAdmin *system.AdminBatchAddReq) (err error, isSendEmail bool) {
  121. defer func() {
  122. if err != nil {
  123. utils.FileLog.Info("新增或者更新账号, BatchAddOrUpdateAdmin Err: " + err.Error())
  124. go alarm_msg.SendAlarmMsg("新增或者更新账号, BatchAddOrUpdateAdmin Err: "+err.Error(), 3)
  125. }
  126. }()
  127. isSendEmail = true
  128. if len(reqAdmin.List) == 0 {
  129. return
  130. }
  131. // 查询所有的账号,如果不在同步的列表里,则删除
  132. deleteAdminList := make(map[string]int)
  133. adminList, err := system.GetAllAdminList()
  134. if err != nil {
  135. err = fmt.Errorf("获取账号数据失败,%v", err)
  136. return
  137. }
  138. adminMap := make(map[string]*system.Admin)
  139. for _, v := range adminList {
  140. deleteAdminList[v.AdminName] = v.AdminId
  141. adminMap[v.AdminName] = v
  142. }
  143. for _, req := range reqAdmin.List {
  144. adminItem, ok := adminMap[req.AdminName]
  145. if ok {
  146. delete(deleteAdminList, req.AdminName)
  147. } else {
  148. adminItem = new(system.Admin)
  149. }
  150. adminItem.AdminName = req.AdminName
  151. adminItem.RealName = req.RealName
  152. adminItem.Password = req.Password
  153. adminItem.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  154. adminItem.Enabled = req.Enabled
  155. adminItem.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  156. adminItem.Mobile = req.Mobile
  157. adminItem.RoleType = req.RoleType
  158. adminItem.RoleId = req.RoleId
  159. adminItem.RoleName = req.RoleName
  160. adminItem.RoleTypeCode = req.RoleTypeCode
  161. adminItem.DepartmentId = req.DepartmentId
  162. adminItem.DepartmentName = req.DepartmentName
  163. adminItem.AdminAvatar = req.AdminAvatar
  164. adminItem.GroupId = req.GroupId
  165. adminItem.GroupName = req.GroupName
  166. adminItem.Role = req.Role
  167. adminItem.EmployeeId = req.EmployeeId
  168. adminItem.Email = req.Email
  169. adminItem.Authority = req.Authority
  170. adminItem.Position = req.Position
  171. adminItem.Province = req.Province
  172. adminItem.ProvinceCode = req.ProvinceCode
  173. adminItem.City = req.City
  174. adminItem.CityCode = req.CityCode
  175. adminItem.TelAreaCode = req.TelAreaCode
  176. adminItem.IsLdap = req.IsLdap
  177. //admin.BusinessCode = req.BusinessCode
  178. if !ok {
  179. adminItem.CreatedTime = time.Now()
  180. adminItem.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  181. err = system.AddAdmin(adminItem)
  182. if err != nil {
  183. err = fmt.Errorf("新增失败,Err:%s", err.Error())
  184. return
  185. }
  186. } else {
  187. err = adminItem.Update([]string{})
  188. if err != nil {
  189. err = fmt.Errorf("更新失败,Err:%s", err.Error())
  190. return
  191. }
  192. }
  193. }
  194. for _, v := range deleteAdminList {
  195. err = system.DeleteSysuser(v)
  196. if err != nil {
  197. err = fmt.Errorf("删除失败,Err:%s", err.Error())
  198. return
  199. }
  200. }
  201. return
  202. }
  203. // 同步分组数据
  204. func SyncSysGroup(req *system.SysGroupAddReq) (err error, isSendEmail bool) {
  205. defer func() {
  206. if err != nil {
  207. utils.FileLog.Info("新增或者更新分组, SyncSysGroup Err: " + err.Error())
  208. go alarm_msg.SendAlarmMsg("新增或者更新分组, SyncSysGroup Err: "+err.Error(), 3)
  209. }
  210. }()
  211. isSendEmail = true
  212. if len(req.List) == 0 {
  213. return
  214. }
  215. groupList, err := system.GetAllSysGroup()
  216. if err != nil {
  217. err = fmt.Errorf("获取账号数据失败,%v", err)
  218. return
  219. }
  220. // 查询所有的账号,如果不在同步的列表里,则删除
  221. deleteGroupList := make(map[int]int)
  222. for _, v := range groupList {
  223. deleteGroupList[v.GroupId] = v.GroupId
  224. }
  225. for _, v := range req.List {
  226. if v.GroupId > 0 {
  227. delete(deleteGroupList, v.GroupId)
  228. }
  229. err = v.AddOrUpdate()
  230. if err != nil {
  231. return
  232. }
  233. }
  234. for _, v := range deleteGroupList {
  235. err = system.DeleteSysGroup(v)
  236. if err != nil {
  237. err = fmt.Errorf("删除失败,Err:%s", err.Error())
  238. return
  239. }
  240. }
  241. return
  242. }
  243. // 同步部门数据
  244. func SyncSysDepartment(req *system.SysDepartmentSyncReq) (err error, isSendEmail bool) {
  245. defer func() {
  246. if err != nil {
  247. utils.FileLog.Info("新增或者更新部门, SyncSysDepartment Err: " + err.Error())
  248. go alarm_msg.SendAlarmMsg("新增或者更新部门, SyncSysDepartment Err: "+err.Error(), 3)
  249. }
  250. }()
  251. isSendEmail = true
  252. if len(req.List) == 0 {
  253. return
  254. }
  255. list, err := system.GetAllSysDepartment()
  256. if err != nil {
  257. err = fmt.Errorf("获取账号数据失败,%v", err)
  258. return
  259. }
  260. // 查询所有的账号,如果不在同步的列表里,则删除
  261. deleteList := make(map[int]int)
  262. for _, v := range list {
  263. deleteList[v.DepartmentId] = v.DepartmentId
  264. }
  265. for _, v := range req.List {
  266. if v.DepartmentId > 0 {
  267. delete(deleteList, v.DepartmentId)
  268. }
  269. err = v.AddOrUpdate()
  270. if err != nil {
  271. return
  272. }
  273. }
  274. for _, v := range deleteList {
  275. err = system.DeleteSysDepartment(v)
  276. if err != nil {
  277. err = fmt.Errorf("删除失败,Err:%s", err.Error())
  278. return
  279. }
  280. }
  281. return
  282. }
  283. // 同步角色数据
  284. func SyncSysRole(req *system.SysRoleSyncReq) (err error, isSendEmail bool) {
  285. defer func() {
  286. if err != nil {
  287. utils.FileLog.Info("同步角色数据, SyncSysRole Err: " + err.Error())
  288. go alarm_msg.SendAlarmMsg("同步角色数据, SyncSysRole Err: "+err.Error(), 3)
  289. }
  290. }()
  291. isSendEmail = true
  292. if len(req.List) == 0 {
  293. return
  294. }
  295. list, err := system.GetSysRoleList()
  296. if err != nil {
  297. err = fmt.Errorf("获取账号数据失败,%v", err)
  298. return
  299. }
  300. // 查询所有的账号,如果不在同步的列表里,则删除
  301. deleteList := make(map[int]int)
  302. for _, v := range list {
  303. deleteList[v.RoleId] = v.RoleId
  304. }
  305. for _, v := range req.List {
  306. if v.RoleId > 0 {
  307. delete(deleteList, v.RoleId)
  308. }
  309. err = v.AddOrUpdate()
  310. if err != nil {
  311. return
  312. }
  313. }
  314. for _, v := range deleteList {
  315. err = system.DeleteSysRole(v)
  316. if err != nil {
  317. err = fmt.Errorf("删除失败,Err:%s", err.Error())
  318. return
  319. }
  320. }
  321. return
  322. }