作者 yangfu

导入用户

package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"io"
)
type ImportDataCommand struct {
//操作人
Operator domain.Operator `json:"-"`
Reader io.Reader `json:"-"`
}
func (importDataCommand *ImportDataCommand) Valid(validation *validation.Validation) {
}
func (importDataCommand *ImportDataCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(importDataCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -2,10 +2,14 @@ package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/excel"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
"strconv"
"time"
)
type ExcelDataService struct {
... ... @@ -15,7 +19,7 @@ func NewExcelDataService(param map[string]interface{}) *ExcelDataService {
return &ExcelDataService{}
}
// 导出公司用户信息列表
// ExportCompanyUser 导出公司用户信息列表
func (srv ExcelDataService) ExportCompanyUser(companyUserListQuery *query.CompanyUserListQuery) (ExportCompanyUserData, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(companyUserListQuery.Operator)
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
... ... @@ -35,7 +39,7 @@ func (srv ExcelDataService) ExportCompanyUser(companyUserListQuery *query.Compan
return ExportCompanyUserData(result.Users), nil
}
// 导出共创用户信息列表
// ExportCooperationUser 导出共创用户信息列表
func (srv ExcelDataService) ExportCooperationUser(companyUserListQuery *query.CompanyUserListQuery) (ExportCooperationUserData, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(companyUserListQuery.Operator)
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
... ... @@ -54,3 +58,107 @@ func (srv ExcelDataService) ExportCooperationUser(companyUserListQuery *query.Co
}
return ExportCooperationUserData(result.Users), nil
}
// ImportCompanyUser 导入公司用户信息
func (srv ExcelDataService) ImportCompanyUser(importDataCommand *command.ImportDataCommand) (interface{}, error) {
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 2 //第二行开始读取
excelImport.DataFields = []excel.DataField{
{EnName: "UserCode", CnName: "*用户编号"},
{EnName: "UserName", CnName: "*用户姓名"},
{EnName: "OrganizationID", CnName: "*组织机构"},
{EnName: "DepartmentID", CnName: "*所属部门"},
{EnName: "Phone", CnName: "*手机号"},
{EnName: "Email", CnName: "邮箱"},
}
excelData, err := excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
users := make([]allied_creation_user.BatchAddUserItem, 0)
var fail int
for _, v := range excelData {
item := allied_creation_user.BatchAddUserItem{
CompanyID: importDataCommand.Operator.CompanyId,
UserType: domain.UserTypeEmployee,
UserCode: v["UserCode"],
OrganizationID: 0,
DepartmentID: 0,
UserInfo: allied_creation_user.BatchAddUserItemUserInfo{
UserName: v["UserName"],
Phone: v["Phone"],
Email: v["Email"],
},
CooperationInfo: allied_creation_user.BatchAddUserItemCooperationInfo{},
}
item.OrganizationID, err = strconv.Atoi(v["OrganizationID"])
if err != nil {
fail++
continue
}
item.DepartmentID, err = strconv.Atoi(v["DepartmentID"])
if err != nil {
fail++
continue
}
users = append(users, item)
}
userGateway := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator)
result, err := userGateway.UserBatchAdd(allied_creation_user.ReqBatchAddUser{
Users: users,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return result, nil
}
// ImportCooperationUser 导入共创用户信息
func (srv ExcelDataService) ImportCooperationUser(importDataCommand *command.ImportDataCommand) (interface{}, error) {
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 2 //第二行开始读取
excelImport.DataFields = []excel.DataField{
{EnName: "UserCode", CnName: "*用户编号"},
{EnName: "UserName", CnName: "*用户姓名"},
{EnName: "CooperationCompany", CnName: "*合伙公司"},
{EnName: "CooperationDeadline", CnName: "*合伙到期"},
{EnName: "Phone", CnName: "*手机号"},
{EnName: "Email", CnName: "邮箱"},
}
excelData, err := excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
users := make([]allied_creation_user.BatchAddUserItem, 0)
var fail int
for _, v := range excelData {
item := allied_creation_user.BatchAddUserItem{
CompanyID: importDataCommand.Operator.CompanyId,
UserType: domain.UserTypeCooperation,
UserCode: v["UserCode"],
OrganizationID: int(importDataCommand.Operator.OrgId),
UserInfo: allied_creation_user.BatchAddUserItemUserInfo{
UserName: v["UserName"],
Phone: v["Phone"],
Email: v["Email"],
},
CooperationInfo: allied_creation_user.BatchAddUserItemCooperationInfo{
CooperationCompany: v["CooperationCompany"],
},
}
item.CooperationInfo.CooperationDeadline, err = time.Parse("2006-01-02", v["CooperationDeadline"])
if err != nil {
fail++
continue
}
users = append(users, item)
}
userGateway := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator)
result, err := userGateway.UserBatchAdd(allied_creation_user.ReqBatchAddUser{
Users: users,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return result, nil
}
... ...
... ... @@ -349,20 +349,22 @@ type (
}
BatchAddUserItem struct {
CompanyID int `json:"companyId"`
CompanyID int64 `json:"companyId"`
UserType int `json:"userType"`
UserCode string `json:"userCode"`
OrganizationID int `json:"organizationId"`
DepartmentID int `json:"departmentId"`
UserInfo struct {
UserInfo BatchAddUserItemUserInfo `json:"userInfo"`
CooperationInfo BatchAddUserItemCooperationInfo `json:"cooperationInfo"`
}
BatchAddUserItemUserInfo struct {
Phone string `json:"phone"`
UserName string `json:"userName"`
Email string `json:"email"`
Avatar string `json:"avatar"`
} `json:"userInfo"`
CooperationInfo struct {
}
BatchAddUserItemCooperationInfo struct {
CooperationCompany string `json:"cooperationCompany"`
CooperationDeadline time.Time `json:"cooperationDeadline"`
} `json:"cooperationInfo"`
}
)
... ...
package controllers
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/json"
"github.com/linmadan/egglib-go/web/beego"
... ... @@ -8,6 +9,8 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/middleware"
"io"
"path/filepath"
)
type BaseController struct {
... ... @@ -108,6 +111,19 @@ func (controller *BaseController) BodyKeys() map[string]interface{} {
return bodyKV
}
func (controller *BaseController) GetExcelFile() (io.Reader, error) {
excelFile, fileHeader, err := controller.GetFile("file")
if err != nil {
log.Logger.Error(err.Error())
return nil, fmt.Errorf("上传文件不存在")
}
ext := filepath.Ext(fileHeader.Filename)
if !(ext == "xlsx" || ext == "xls") {
return nil, fmt.Errorf("仅支持上传文件格式 xls/xlsx")
}
return excelFile, nil
}
func Must(err error) {
if err != nil {
log.Logger.Error(err.Error())
... ...
package web_client
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command"
"path/filepath"
"github.com/beego/beego/v2/server/web/context"
... ... @@ -139,3 +140,31 @@ func (controller ExcelDataController) ImportDividendsReturnedOrder() {
}
controller.Response(result, nil)
}
func (controller ExcelDataController) ImportCompanyUser() {
excelService := service.NewExcelDataService(nil)
r, err := controller.GetExcelFile()
if err != nil {
controller.Response(nil, err)
return
}
cmd := &command.ImportDataCommand{}
cmd.Operator = controller.GetOperator()
cmd.Reader = r
data, err := excelService.ImportCompanyUser(cmd)
controller.Response(data, err)
}
func (controller ExcelDataController) ImportCooperationUser() {
excelService := service.NewExcelDataService(nil)
r, err := controller.GetExcelFile()
if err != nil {
controller.Response(nil, err)
return
}
cmd := &command.ImportDataCommand{}
cmd.Operator = controller.GetOperator()
cmd.Reader = r
data, err := excelService.ImportCooperationUser(cmd)
controller.Response(data, err)
}
... ...
... ... @@ -8,4 +8,7 @@ import (
func init() {
web.Router("/v1/web/excel/export/company-user", &web_client.ExcelDataController{}, "Post:ExportCompanyUser")
web.Router("/v1/web/excel/export/cooperation-user", &web_client.ExcelDataController{}, "Post:ExportCooperationUser")
web.Router("/v1/web/excel/import/company-user", &web_client.ExcelDataController{}, "Post:ImportCompanyUser")
web.Router("/v1/web/excel/import/cooperation-user", &web_client.ExcelDataController{}, "Post:ImportCooperationUser")
}
... ...