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 } } } } } }