123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package services
- import (
- "eta/eta_data_init/models"
- "eta/eta_data_init/utils"
- "fmt"
- "github.com/xuri/excelize/v2"
- "os"
- "path/filepath"
- "strings"
- "time"
- )
- // InitAdmin 初始化系统用户数据
- func InitAdmin(dataPath string) {
- var err error
- defer func() {
- if err != nil {
- fmt.Println("InitAdmin Err:" + err.Error())
- utils.FileLog.Info("InitAdmin Err: " + err.Error())
- }
- }()
- //读取excel
- path, err := filepath.Abs(os.Args[0])
- if err != nil {
- fmt.Println(err)
- }
- dir := filepath.Dir(path)
- fmt.Println("dir:" + dir)
- dataPath = dir + dataPath
- fmt.Println("dataPath:" + dataPath)
- f, err := excelize.OpenFile(dataPath)
- if err != nil {
- fmt.Println(err)
- return
- }
- defer func() {
- if err := f.Close(); err != nil {
- fmt.Println(err)
- }
- }()
- rows, err := f.GetRows("Sheet1")
- if err != nil {
- fmt.Println(err)
- return
- }
- fmt.Println("rows len:", len(rows))
- mobileMap := make(map[string]*models.Admin)
- emailMap := make(map[string]*models.Admin)
- userMap := make(map[string]*models.Admin)
- deptMap := make(map[string]*models.SysDepartment)
- groupMap := make(map[string]*models.SysGroup)
- admins, err := models.GetSysAdminList(``, make([]interface{}, 0))
- if err != nil {
- err = fmt.Errorf("GetSysAdminList err: %s", err.Error())
- return
- }
- for _, v := range admins {
- if v.Mobile != "" {
- mobileMap[v.Mobile] = v
- }
- if v.Email != "" {
- emailMap[v.Email] = v
- }
- userMap[v.AdminName] = v
- }
- //获取部门
- deptList, err := models.GetSysDepartmentAll()
- if err != nil {
- fmt.Println("GetSysDepartmentAll err:" + err.Error())
- return
- }
- for _, v := range deptList {
- deptMap[v.DepartmentName] = v
- }
- //获取分组
- groupList, err := models.GetSysGroupAll()
- if err != nil {
- fmt.Println("GetSysGroupAll err:" + err.Error())
- return
- }
- for _, v := range groupList {
- groupMap[v.GroupName] = v
- }
- //获取角色
- roleMap := make(map[string]*models.SysRoleItem)
- //获取分组
- roleList, err := models.GetSysRoleAll()
- if err != nil {
- fmt.Println("GetSysRoleAll err:" + err.Error())
- return
- }
- for _, v := range roleList {
- roleMap[v.RoleName] = v
- }
- for rk, row := range rows {
- //if rk > 3 {
- // return
- //}
- if rk > 0 {
- var adminName, password, realName, telAreaCode, mobile, departmentName, email, roleName string
- for ck, colCell := range row {
- colCell = strings.TrimSpace(colCell)
- switch ck {
- case 0:
- adminName = colCell
- case 1:
- password = colCell
- case 2:
- realName = colCell
- case 3:
- telAreaCode = colCell
- case 4:
- mobile = colCell
- case 5:
- departmentName = colCell
- case 6:
- email = colCell
- case 8:
- roleName = colCell
- }
- }
- if adminName != "" &&
- realName != "" {
- fmt.Println(adminName, password, realName, telAreaCode, mobile, departmentName, email, roleName)
- count, err := models.GetSysAdminCount(adminName)
- if err != nil {
- fmt.Println("判断用户是否存在失败,Err:" + err.Error())
- return
- }
- if count > 0 { //用户已存在
- continue
- } else { //新增
- pass := utils.MD5(password)
- admin := new(models.Admin)
- admin.AdminName = adminName
- admin.RealName = realName
- admin.Password = pass
- admin.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
- admin.Enabled = 1
- admin.LastLoginTime = time.Now().Format(utils.FormatDateTime)
- admin.CreatedTime = time.Now()
- admin.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
- admin.Mobile = mobile
- admin.RoleType = 0
- if role, ok := roleMap[roleName]; ok {
- admin.RoleId = role.RoleId
- admin.RoleTypeCode = role.RoleTypeCode
- } else {
- fmt.Println("角色不存在:" + roleName)
- return
- }
- admin.RoleName = roleName
- var groupName, deptName string
- deptArr := strings.Split(departmentName, "/")
- fmt.Println(deptArr)
- if len(deptArr) < 2 {
- fmt.Println("部门信息错误,至少两级")
- return
- }
- deptName = deptArr[0]
- groupName = deptArr[1]
- if dept, ok := deptMap[deptName]; ok {
- admin.DepartmentId = dept.DepartmentId
- }
- admin.DepartmentName = deptName
- if group, ok := groupMap[groupName]; ok {
- admin.GroupId = group.GroupId
- admin.GroupName = groupName
- }
- admin.Email = email
- admin.TelAreaCode = telAreaCode
- admin.IsLdap = 0
- err = models.AddAdmin(admin)
- if err != nil {
- fmt.Println("新增失败,Err:" + err.Error())
- return
- }
- }
- }
- }
- }
- }
|