Пример #1
0
func (c Admin) DoAddUser(username, password, name, phone, phone1, position, email string, department_id, employment_type, gender, level, is_leader int, join_date, birth_date time.Time) revel.Result {

	c.Validation.Required(username).Message("用户名不能为空")
	c.Validation.Required(password).Message("密码不能为空")
	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/addUser")
	}

	//判断部门有没有领导
	if is_leader == 1 {
		dept := &models.Department{}
		app.Engine.Id(department_id).Get(dept)
		if dept.LeaderId != 0 {
			up := &models.UserProfiles{}
			app.Engine.Id(dept.LeaderId).Get(up)

			c.Flash.Error("该部门当前审批负责人为" + up.Name + ",一个部门只能指定一个审批负责人")
			c.Validation.Keep()
			c.FlashParams()
			return c.Redirect("/admin/addUser")
		}
	}

	err := service.AddUser(username, password, department_id, employment_type, gender, level, name, phone, phone1, position, email, join_date, birth_date, c.Request.RemoteAddr)
	if err != nil {
		c.Flash.Error(err.Error())
		return c.Redirect("/admin/addUser")
	}

	return c.Redirect("/admin/listUser")
}
Пример #2
0
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")
	}
}