func (c Admin) DoAddDepartment(name, info string, parent_id int) revel.Result { c.Validation.Required(name).Message("名称不能为空") if c.Validation.HasErrors() { c.Flash.Error(utils.ValidationErrorToString(c.Validation.Errors)) c.Validation.Keep() c.FlashParams() return c.Redirect("/admin/addDepartment") } //获取上一级的depth,本级加1 depth := 0 if parent_id > 0 { department := &models.Department{} app.Engine.Id(parent_id).Get(department) if nil != department { depth = department.Depth } } l := new(models.Department) l.Name = name l.Info = info l.Depth = (depth + 1) l.ParentId = parent_id l.UpdatedAt = time.Now() l.CreatedAt = time.Now() app.Engine.Insert(l) return c.Redirect("/admin/listDepartment") }
func (c Admin) Import() revel.Result { if !utils.DATA_IMPORTED { dataFilePath := "/Users/baiyuxiong/Desktop/users.csv" file, err := os.Open(dataFilePath) if err != nil { log.Fatal(err) } defer file.Close() line := 1 scanner := bufio.NewScanner(file) for scanner.Scan() { if line != 1 { fmt.Println(scanner.Text()) cells := strings.Split(scanner.Text(), ",") if len(cells) == 11 { employment_type := 0 for k, v := range utils.EmploymentType { if cells[2] == v { employment_type = k break } } gender := 0 for k, v := range utils.UserGender { if cells[6] == v { gender = k break } } level := 1 if cells[5] != "一般员工" { level = 2 } department_id := 0 depts := make([]models.Department, 0) app.Engine.Find(&depts) for _, v := range depts { if cells[4] == v.Name { department_id = v.Id break } } if department_id == 0 { dept := new(models.Department) dept.ParentId = 0 dept.Depth = 2 if cells[4] == "领导" { if cells[5] == "经理" { dept.Depth = 0 } else { dept.Depth = 1 } } dept.LeaderId = 0 dept.Name = cells[4] dept.Info = cells[4] dept.UpdatedAt = time.Now() dept.CreatedAt = time.Now() app.Engine.Insert(dept) department_id = dept.Id } join_date, _ := time.Parse("2006.01.02", cells[7]) birth_date, _ := time.Parse("2006.01.02", cells[8]) service.AddUser(cells[1], cells[1], department_id, employment_type, gender, level, cells[3], "", "", cells[5], "", join_date, birth_date, "") } } line++ } if err := scanner.Err(); err != nil { return c.RenderText(err.Error()) } return c.RenderText("ok") } else { return c.RenderText("Imported") } }