package controllers import ( "github.com/linmadan/egglib-go/core/application" "github.com/linmadan/egglib-go/web/beego" "github.com/xuri/excelize/v2" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/adapter" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares" "strings" ) type UserController struct { beego.BaseController } // ListUsers 搜索用户 func (controller *UserController) ListUsers() { listUserQuery := &query.ListUserQuery{} _ = controller.Unmarshal(listUserQuery) userAuth := controller.Ctx.Input.GetData(domain.UserAuth{}).(*domain.UserAuth) listUserQuery.CompanyId = userAuth.CompanyId resp, err := (&user.UserService{}).ListUsers(listUserQuery) controller.Response(resp, err) } func (controller *UserController) ListByDepartment() { in := &query.ListByDepartmentQuery{} if err := controller.Unmarshal(in); err != nil { controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) } else { ua := middlewares.GetUser(controller.Ctx) in.CompanyId = ua.CompanyId controller.Response((&user.UserService{}).ListByDepartment(in)) } } func (controller *UserController) EditParentUser() { in := &command.EditParentCommand{} if err := controller.Unmarshal(in); err != nil { controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) } else { ua := middlewares.GetUser(controller.Ctx) in.CompanyId = int(ua.CompanyId) in.OperatorId = int(ua.UserId) controller.Response(nil, (&user.UserService{}).EditParentUser(in)) } } // ImportParentUser 导入用户上级 func (controller *UserController) ImportParentUser() { in := &command.ImportParentUserCommand{} if err := controller.Unmarshal(in); err != nil { controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) } else { if itcArray, err := controller.readExcelFormUserParent(); err != nil { controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) } else { ua := middlewares.GetUser(controller.Ctx) in.CompanyId = int(ua.CompanyId) in.OperatorId = int(ua.UserId) in.Data = itcArray if data, err := (&user.UserService{}).ImportParentUser(in); nil != err { controller.Response(nil, err) } else { controller.Response(data, nil) } } } } func (controller *UserController) readExcelFormUserParent() ([]adapter.ImportParentUser, error) { // 读取文件 _, header, err := controller.GetFile("file") if err != nil { controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error())) return nil, err } file, err := header.Open() if err != nil { controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error())) return nil, err } defer func() { if err := file.Close(); err != nil { return } }() reader, err := excelize.OpenReader(file) if err != nil { controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error())) return nil, err } index := reader.GetActiveSheetIndex() rows, err := reader.GetRows(reader.GetSheetName(index)) if err != nil { controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "读取excel错误:"+err.Error())) return nil, err } if len(rows) <= 0 { return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "文件内数据不能为空:"+err.Error()) } adapters := make([]adapter.ImportParentUser, 0) for rowIndex, row := range rows { if rowIndex < 2 { // 头2行不读取 continue } ipu := adapter.ImportParentUser{} for colIndex, colCell := range row { switch colIndex { case 0: ipu.Name = strings.TrimSpace(colCell) // 员工名称 case 1: ipu.Phone = strings.TrimSpace(colCell) // 员工手机号码 case 2: ipu.ParentName = strings.TrimSpace(colCell) // 直接上级名称 case 3: ipu.ParentPhone = strings.TrimSpace(colCell) // 直接上级手机号码 } } adapters = append(adapters, ipu) } if len(adapters) <= 0 { return adapters, application.ThrowError(application.INTERNAL_SERVER_ERROR, "文件内数据不能为空:"+err.Error()) } return adapters, err }