464 lines
12 KiB
Go
464 lines
12 KiB
Go
package controllers
|
||
|
||
import (
|
||
"encoding/json"
|
||
"server/models"
|
||
"server/services"
|
||
|
||
beego "github.com/beego/beego/v2/server/web"
|
||
)
|
||
|
||
// EmployeeController 员工控制器
|
||
type EmployeeController struct {
|
||
beego.Controller
|
||
}
|
||
|
||
// GetAllEmployees 获取所有员工(可选,用于管理员查看所有员工)
|
||
// @router /employees [get]
|
||
func (c *EmployeeController) GetAllEmployees() {
|
||
employees, err := services.GetAllEmployees()
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "获取员工列表失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "获取员工列表成功",
|
||
"data": employees,
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// GetTenantEmployees 获取租户下的所有员工
|
||
// @router /employees/tenant/:tenantId [get]
|
||
func (c *EmployeeController) GetTenantEmployees() {
|
||
tenantId, err := c.GetInt(":tenantId")
|
||
if err != nil || tenantId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "租户ID无效",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
employees, err := services.GetTenantEmployees(tenantId)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "获取员工列表失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 格式化返回数据
|
||
employeeList := make([]map[string]interface{}, 0)
|
||
for _, emp := range employees {
|
||
employeeList = append(employeeList, map[string]interface{}{
|
||
"id": emp.Id,
|
||
"tenant_id": emp.TenantId,
|
||
"employee_no": emp.EmployeeNo,
|
||
"name": emp.Name,
|
||
"phone": emp.Phone,
|
||
"email": emp.Email,
|
||
"department_id": emp.DepartmentId,
|
||
"position_id": emp.PositionId,
|
||
"role": emp.Role,
|
||
"bank_name": emp.BankName,
|
||
"bank_account": emp.BankAccount,
|
||
"status": emp.Status,
|
||
"create_time": emp.CreateTime,
|
||
"last_login_time": emp.LastLoginTime,
|
||
"last_login_ip": emp.LastLoginIp,
|
||
})
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "获取员工列表成功",
|
||
"data": employeeList,
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// GetEmployeeInfo 获取员工详情(包含关联的部门、职位、角色信息)
|
||
// @router /employees/:id [get]
|
||
func (c *EmployeeController) GetEmployeeInfo() {
|
||
id, err := c.GetInt(":id")
|
||
if err != nil || id <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "员工ID无效",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 使用联查方法获取员工详细信息
|
||
detail, err := services.GetEmployeeDetailWithRelations(id)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "员工不存在",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 构建返回数据
|
||
employeeData := map[string]interface{}{
|
||
"id": detail.Employee.Id,
|
||
"tenant_id": detail.Employee.TenantId,
|
||
"employee_no": detail.Employee.EmployeeNo,
|
||
"name": detail.Employee.Name,
|
||
"phone": detail.Employee.Phone,
|
||
"email": detail.Employee.Email,
|
||
"department_id": detail.Employee.DepartmentId,
|
||
"position_id": detail.Employee.PositionId,
|
||
"role": detail.Employee.Role,
|
||
"bank_name": detail.Employee.BankName,
|
||
"bank_account": detail.Employee.BankAccount,
|
||
"status": detail.Employee.Status,
|
||
"create_time": detail.Employee.CreateTime,
|
||
"last_login_time": detail.Employee.LastLoginTime,
|
||
"last_login_ip": detail.Employee.LastLoginIp,
|
||
}
|
||
|
||
// 添加部门详细信息(如果存在)
|
||
if detail.Department != nil {
|
||
employeeData["department"] = map[string]interface{}{
|
||
"id": detail.Department.Id,
|
||
"name": detail.Department.Name,
|
||
"code": detail.Department.Code,
|
||
"tenant_id": detail.Department.TenantId,
|
||
"parent_id": detail.Department.ParentId,
|
||
"description": detail.Department.Description,
|
||
}
|
||
}
|
||
|
||
// 添加职位详细信息(如果存在)
|
||
if detail.Position != nil {
|
||
employeeData["position"] = map[string]interface{}{
|
||
"id": detail.Position.Id,
|
||
"name": detail.Position.Name,
|
||
"code": detail.Position.Code,
|
||
"tenant_id": detail.Position.TenantId,
|
||
"department_id": detail.Position.DepartmentId,
|
||
"level": detail.Position.Level,
|
||
"description": detail.Position.Description,
|
||
}
|
||
}
|
||
|
||
// 添加角色详细信息(如果存在)
|
||
if detail.Role != nil {
|
||
employeeData["role_detail"] = map[string]interface{}{
|
||
"roleId": detail.Role.RoleId,
|
||
"roleName": detail.Role.RoleName,
|
||
"roleCode": detail.Role.RoleCode,
|
||
"tenantId": detail.Role.TenantId,
|
||
"description": detail.Role.Description,
|
||
"status": detail.Role.Status,
|
||
}
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "获取员工信息成功",
|
||
"data": employeeData,
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// AddEmployee 添加员工
|
||
// @router /employees [post]
|
||
func (c *EmployeeController) AddEmployee() {
|
||
var employeeData struct {
|
||
TenantId int `json:"tenant_id"`
|
||
EmployeeNo string `json:"employee_no"`
|
||
Name string `json:"name"`
|
||
Phone string `json:"phone"`
|
||
Email string `json:"email"`
|
||
DepartmentId int `json:"department_id"`
|
||
PositionId int `json:"position_id"`
|
||
Role int `json:"role"`
|
||
BankName string `json:"bank_name"`
|
||
BankAccount string `json:"bank_account"`
|
||
Status int `json:"status"` // 使用int,前端传递number会自动转换
|
||
}
|
||
|
||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &employeeData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
employee := &models.Employee{
|
||
TenantId: employeeData.TenantId,
|
||
EmployeeNo: employeeData.EmployeeNo,
|
||
Name: employeeData.Name,
|
||
Phone: employeeData.Phone,
|
||
Email: employeeData.Email,
|
||
DepartmentId: employeeData.DepartmentId,
|
||
PositionId: employeeData.PositionId,
|
||
Role: employeeData.Role,
|
||
BankName: employeeData.BankName,
|
||
BankAccount: employeeData.BankAccount,
|
||
Status: int8(employeeData.Status), // 转换为int8
|
||
}
|
||
|
||
// 如果没有指定租户ID,从JWT token中获取
|
||
if employee.TenantId == 0 {
|
||
if tenantId, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantId > 0 {
|
||
employee.TenantId = tenantId
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "租户ID不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
}
|
||
|
||
// 默认密码
|
||
defaultPassword := "yunzer123"
|
||
id, err := services.AddEmployee(employee, defaultPassword)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "添加员工失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
employee.Id = int(id)
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "添加员工成功",
|
||
"data": employee,
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// UpdateEmployee 更新员工信息
|
||
// @router /employees/:id [put]
|
||
func (c *EmployeeController) UpdateEmployee() {
|
||
id, err := c.GetInt(":id")
|
||
if err != nil || id <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "员工ID无效",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
var updateData struct {
|
||
EmployeeNo string `json:"employee_no"`
|
||
Name string `json:"name"`
|
||
Phone string `json:"phone"`
|
||
Email string `json:"email"`
|
||
DepartmentId int `json:"department_id"`
|
||
PositionId int `json:"position_id"`
|
||
Role int `json:"role"`
|
||
BankName string `json:"bank_name"`
|
||
BankAccount string `json:"bank_account"`
|
||
Status int `json:"status"` // 使用int,前端传递number会自动转换
|
||
}
|
||
|
||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &updateData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
employee, err := services.GetEmployeeById(id)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "员工不存在",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 更新字段
|
||
employee.EmployeeNo = updateData.EmployeeNo
|
||
employee.Name = updateData.Name
|
||
employee.Phone = updateData.Phone
|
||
employee.Email = updateData.Email
|
||
employee.DepartmentId = updateData.DepartmentId
|
||
employee.PositionId = updateData.PositionId
|
||
employee.Role = updateData.Role
|
||
employee.BankName = updateData.BankName
|
||
employee.BankAccount = updateData.BankAccount
|
||
employee.Status = int8(updateData.Status) // 转换为int8
|
||
|
||
if err := services.UpdateEmployee(employee); err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "更新员工信息失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "更新员工信息成功",
|
||
"data": employee,
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// DeleteEmployee 删除员工
|
||
// @router /employees/:id [delete]
|
||
func (c *EmployeeController) DeleteEmployee() {
|
||
id, err := c.GetInt(":id")
|
||
if err != nil || id <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "员工ID无效",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if err := services.DeleteEmployee(id); err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "删除员工失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "删除员工成功",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// ResetEmployeePassword 重置员工密码
|
||
// @router /employees/:id/reset-password [post]
|
||
func (c *EmployeeController) ResetEmployeePassword() {
|
||
id, err := c.GetInt(":id")
|
||
if err != nil || id <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "员工ID无效",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 默认密码
|
||
defaultPassword := "yunzer123"
|
||
|
||
if err := services.ResetEmployeePassword(id, defaultPassword); err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "重置密码失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "密码重置成功,新密码为: " + defaultPassword,
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// ChangeEmployeePassword 修改员工密码
|
||
// @router /employees/:id/change-password [post]
|
||
func (c *EmployeeController) ChangeEmployeePassword() {
|
||
id, err := c.GetInt(":id")
|
||
if err != nil || id <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "员工ID无效",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
var passwordData struct {
|
||
OldPassword string `json:"old_password"`
|
||
NewPassword string `json:"new_password"`
|
||
}
|
||
|
||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &passwordData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if passwordData.NewPassword == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "新密码不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if err := services.ChangeEmployeePassword(id, passwordData.OldPassword, passwordData.NewPassword); err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "修改密码失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "密码修改成功",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|