admin.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package services
  2. import (
  3. "eta/eta_data_init/models"
  4. "eta/eta_data_init/utils"
  5. "fmt"
  6. "github.com/xuri/excelize/v2"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. )
  12. // InitAdmin 初始化系统用户数据
  13. func InitAdmin(dataPath string) {
  14. var err error
  15. defer func() {
  16. if err != nil {
  17. fmt.Println("InitAdmin Err:" + err.Error())
  18. utils.FileLog.Info("InitAdmin Err: " + err.Error())
  19. }
  20. }()
  21. //读取excel
  22. path, err := filepath.Abs(os.Args[0])
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. dir := filepath.Dir(path)
  27. fmt.Println("dir:" + dir)
  28. dataPath = dir + dataPath
  29. fmt.Println("dataPath:" + dataPath)
  30. f, err := excelize.OpenFile(dataPath)
  31. if err != nil {
  32. fmt.Println(err)
  33. return
  34. }
  35. defer func() {
  36. if err := f.Close(); err != nil {
  37. fmt.Println(err)
  38. }
  39. }()
  40. rows, err := f.GetRows("Sheet1")
  41. if err != nil {
  42. fmt.Println(err)
  43. return
  44. }
  45. fmt.Println("rows len:", len(rows))
  46. mobileMap := make(map[string]*models.Admin)
  47. emailMap := make(map[string]*models.Admin)
  48. userMap := make(map[string]*models.Admin)
  49. deptMap := make(map[string]*models.SysDepartment)
  50. groupMap := make(map[string]*models.SysGroup)
  51. admins, err := models.GetSysAdminList(``, make([]interface{}, 0))
  52. if err != nil {
  53. err = fmt.Errorf("GetSysAdminList err: %s", err.Error())
  54. return
  55. }
  56. for _, v := range admins {
  57. if v.Mobile != "" {
  58. mobileMap[v.Mobile] = v
  59. }
  60. if v.Email != "" {
  61. emailMap[v.Email] = v
  62. }
  63. userMap[v.AdminName] = v
  64. }
  65. //获取部门
  66. deptList, err := models.GetSysDepartmentAll()
  67. if err != nil {
  68. fmt.Println("GetSysDepartmentAll err:" + err.Error())
  69. return
  70. }
  71. for _, v := range deptList {
  72. deptMap[v.DepartmentName] = v
  73. }
  74. //获取分组
  75. groupList, err := models.GetSysGroupAll()
  76. if err != nil {
  77. fmt.Println("GetSysGroupAll err:" + err.Error())
  78. return
  79. }
  80. for _, v := range groupList {
  81. groupMap[v.GroupName] = v
  82. }
  83. //获取角色
  84. roleMap := make(map[string]*models.SysRoleItem)
  85. //获取分组
  86. roleList, err := models.GetSysRoleAll()
  87. if err != nil {
  88. fmt.Println("GetSysRoleAll err:" + err.Error())
  89. return
  90. }
  91. for _, v := range roleList {
  92. roleMap[v.RoleName] = v
  93. }
  94. for rk, row := range rows {
  95. //if rk > 3 {
  96. // return
  97. //}
  98. if rk > 0 {
  99. var adminName, password, realName, telAreaCode, mobile, departmentName, email, roleName string
  100. for ck, colCell := range row {
  101. colCell = strings.TrimSpace(colCell)
  102. switch ck {
  103. case 0:
  104. adminName = colCell
  105. case 1:
  106. password = colCell
  107. case 2:
  108. realName = colCell
  109. case 3:
  110. telAreaCode = colCell
  111. case 4:
  112. mobile = colCell
  113. case 5:
  114. departmentName = colCell
  115. case 6:
  116. email = colCell
  117. case 8:
  118. roleName = colCell
  119. }
  120. }
  121. if adminName != "" &&
  122. realName != "" {
  123. fmt.Println(adminName, password, realName, telAreaCode, mobile, departmentName, email, roleName)
  124. count, err := models.GetSysAdminCount(adminName)
  125. if err != nil {
  126. fmt.Println("判断用户是否存在失败,Err:" + err.Error())
  127. return
  128. }
  129. if count > 0 { //用户已存在
  130. continue
  131. } else { //新增
  132. pass := utils.MD5(password)
  133. admin := new(models.Admin)
  134. admin.AdminName = adminName
  135. admin.RealName = realName
  136. admin.Password = pass
  137. admin.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  138. admin.Enabled = 1
  139. admin.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  140. admin.CreatedTime = time.Now()
  141. admin.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  142. admin.Mobile = mobile
  143. admin.RoleType = 0
  144. if role, ok := roleMap[roleName]; ok {
  145. admin.RoleId = role.RoleId
  146. admin.RoleTypeCode = role.RoleTypeCode
  147. } else {
  148. fmt.Println("角色不存在:" + roleName)
  149. return
  150. }
  151. admin.RoleName = roleName
  152. var groupName, deptName string
  153. deptArr := strings.Split(departmentName, "/")
  154. fmt.Println(deptArr)
  155. if len(deptArr) < 2 {
  156. fmt.Println("部门信息错误,至少两级")
  157. return
  158. }
  159. deptName = deptArr[0]
  160. groupName = deptArr[1]
  161. if dept, ok := deptMap[deptName]; ok {
  162. admin.DepartmentId = dept.DepartmentId
  163. }
  164. admin.DepartmentName = deptName
  165. if group, ok := groupMap[groupName]; ok {
  166. admin.GroupId = group.GroupId
  167. admin.GroupName = groupName
  168. }
  169. admin.Email = email
  170. admin.TelAreaCode = telAreaCode
  171. admin.IsLdap = 0
  172. err = models.AddAdmin(admin)
  173. if err != nil {
  174. fmt.Println("新增失败,Err:" + err.Error())
  175. return
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }