作者 yangfu

导入用户

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/beego/beego/v2/core/validation"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  7 + "io"
  8 +)
  9 +
  10 +type ImportDataCommand struct {
  11 + //操作人
  12 + Operator domain.Operator `json:"-"`
  13 + Reader io.Reader `json:"-"`
  14 +}
  15 +
  16 +func (importDataCommand *ImportDataCommand) Valid(validation *validation.Validation) {
  17 +
  18 +}
  19 +
  20 +func (importDataCommand *ImportDataCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(importDataCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + for _, validErr := range valid.Errors {
  28 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  29 + }
  30 + }
  31 + return nil
  32 +}
@@ -2,10 +2,14 @@ package service @@ -2,10 +2,14 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 - 5 + "github.com/linmadan/egglib-go/core/application"
  6 + "github.com/linmadan/egglib-go/utils/excel"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
  11 + "strconv"
  12 + "time"
9 ) 13 )
10 14
11 type ExcelDataService struct { 15 type ExcelDataService struct {
@@ -15,7 +19,7 @@ func NewExcelDataService(param map[string]interface{}) *ExcelDataService { @@ -15,7 +19,7 @@ func NewExcelDataService(param map[string]interface{}) *ExcelDataService {
15 return &ExcelDataService{} 19 return &ExcelDataService{}
16 } 20 }
17 21
18 -// 导出公司用户信息列表 22 +// ExportCompanyUser 导出公司用户信息列表
19 func (srv ExcelDataService) ExportCompanyUser(companyUserListQuery *query.CompanyUserListQuery) (ExportCompanyUserData, error) { 23 func (srv ExcelDataService) ExportCompanyUser(companyUserListQuery *query.CompanyUserListQuery) (ExportCompanyUserData, error) {
20 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(companyUserListQuery.Operator) 24 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(companyUserListQuery.Operator)
21 result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ 25 result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
@@ -35,7 +39,7 @@ func (srv ExcelDataService) ExportCompanyUser(companyUserListQuery *query.Compan @@ -35,7 +39,7 @@ func (srv ExcelDataService) ExportCompanyUser(companyUserListQuery *query.Compan
35 return ExportCompanyUserData(result.Users), nil 39 return ExportCompanyUserData(result.Users), nil
36 } 40 }
37 41
38 -// 导出共创用户信息列表 42 +// ExportCooperationUser 导出共创用户信息列表
39 func (srv ExcelDataService) ExportCooperationUser(companyUserListQuery *query.CompanyUserListQuery) (ExportCooperationUserData, error) { 43 func (srv ExcelDataService) ExportCooperationUser(companyUserListQuery *query.CompanyUserListQuery) (ExportCooperationUserData, error) {
40 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(companyUserListQuery.Operator) 44 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(companyUserListQuery.Operator)
41 result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ 45 result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
@@ -54,3 +58,107 @@ func (srv ExcelDataService) ExportCooperationUser(companyUserListQuery *query.Co @@ -54,3 +58,107 @@ func (srv ExcelDataService) ExportCooperationUser(companyUserListQuery *query.Co
54 } 58 }
55 return ExportCooperationUserData(result.Users), nil 59 return ExportCooperationUserData(result.Users), nil
56 } 60 }
  61 +
  62 +// ImportCompanyUser 导入公司用户信息
  63 +func (srv ExcelDataService) ImportCompanyUser(importDataCommand *command.ImportDataCommand) (interface{}, error) {
  64 + excelImport := excel.NewExcelImport()
  65 + excelImport.RowBegin = 2 //第二行开始读取
  66 + excelImport.DataFields = []excel.DataField{
  67 + {EnName: "UserCode", CnName: "*用户编号"},
  68 + {EnName: "UserName", CnName: "*用户姓名"},
  69 + {EnName: "OrganizationID", CnName: "*组织机构"},
  70 + {EnName: "DepartmentID", CnName: "*所属部门"},
  71 + {EnName: "Phone", CnName: "*手机号"},
  72 + {EnName: "Email", CnName: "邮箱"},
  73 + }
  74 + excelData, err := excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
  75 + if err != nil {
  76 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  77 + }
  78 + users := make([]allied_creation_user.BatchAddUserItem, 0)
  79 + var fail int
  80 + for _, v := range excelData {
  81 + item := allied_creation_user.BatchAddUserItem{
  82 + CompanyID: importDataCommand.Operator.CompanyId,
  83 + UserType: domain.UserTypeEmployee,
  84 + UserCode: v["UserCode"],
  85 + OrganizationID: 0,
  86 + DepartmentID: 0,
  87 + UserInfo: allied_creation_user.BatchAddUserItemUserInfo{
  88 + UserName: v["UserName"],
  89 + Phone: v["Phone"],
  90 + Email: v["Email"],
  91 + },
  92 + CooperationInfo: allied_creation_user.BatchAddUserItemCooperationInfo{},
  93 + }
  94 + item.OrganizationID, err = strconv.Atoi(v["OrganizationID"])
  95 + if err != nil {
  96 + fail++
  97 + continue
  98 + }
  99 + item.DepartmentID, err = strconv.Atoi(v["DepartmentID"])
  100 + if err != nil {
  101 + fail++
  102 + continue
  103 + }
  104 + users = append(users, item)
  105 + }
  106 + userGateway := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator)
  107 + result, err := userGateway.UserBatchAdd(allied_creation_user.ReqBatchAddUser{
  108 + Users: users,
  109 + })
  110 + if err != nil {
  111 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  112 + }
  113 + return result, nil
  114 +}
  115 +
  116 +// ImportCooperationUser 导入共创用户信息
  117 +func (srv ExcelDataService) ImportCooperationUser(importDataCommand *command.ImportDataCommand) (interface{}, error) {
  118 + excelImport := excel.NewExcelImport()
  119 + excelImport.RowBegin = 2 //第二行开始读取
  120 + excelImport.DataFields = []excel.DataField{
  121 + {EnName: "UserCode", CnName: "*用户编号"},
  122 + {EnName: "UserName", CnName: "*用户姓名"},
  123 + {EnName: "CooperationCompany", CnName: "*合伙公司"},
  124 + {EnName: "CooperationDeadline", CnName: "*合伙到期"},
  125 + {EnName: "Phone", CnName: "*手机号"},
  126 + {EnName: "Email", CnName: "邮箱"},
  127 + }
  128 + excelData, err := excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
  129 + if err != nil {
  130 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  131 + }
  132 + users := make([]allied_creation_user.BatchAddUserItem, 0)
  133 + var fail int
  134 + for _, v := range excelData {
  135 + item := allied_creation_user.BatchAddUserItem{
  136 + CompanyID: importDataCommand.Operator.CompanyId,
  137 + UserType: domain.UserTypeCooperation,
  138 + UserCode: v["UserCode"],
  139 + OrganizationID: int(importDataCommand.Operator.OrgId),
  140 + UserInfo: allied_creation_user.BatchAddUserItemUserInfo{
  141 + UserName: v["UserName"],
  142 + Phone: v["Phone"],
  143 + Email: v["Email"],
  144 + },
  145 + CooperationInfo: allied_creation_user.BatchAddUserItemCooperationInfo{
  146 + CooperationCompany: v["CooperationCompany"],
  147 + },
  148 + }
  149 + item.CooperationInfo.CooperationDeadline, err = time.Parse("2006-01-02", v["CooperationDeadline"])
  150 + if err != nil {
  151 + fail++
  152 + continue
  153 + }
  154 + users = append(users, item)
  155 + }
  156 + userGateway := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator)
  157 + result, err := userGateway.UserBatchAdd(allied_creation_user.ReqBatchAddUser{
  158 + Users: users,
  159 + })
  160 + if err != nil {
  161 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  162 + }
  163 + return result, nil
  164 +}
@@ -349,20 +349,22 @@ type ( @@ -349,20 +349,22 @@ type (
349 } 349 }
350 350
351 BatchAddUserItem struct { 351 BatchAddUserItem struct {
352 - CompanyID int `json:"companyId"` 352 + CompanyID int64 `json:"companyId"`
353 UserType int `json:"userType"` 353 UserType int `json:"userType"`
354 UserCode string `json:"userCode"` 354 UserCode string `json:"userCode"`
355 OrganizationID int `json:"organizationId"` 355 OrganizationID int `json:"organizationId"`
356 DepartmentID int `json:"departmentId"` 356 DepartmentID int `json:"departmentId"`
357 - UserInfo struct { 357 + UserInfo BatchAddUserItemUserInfo `json:"userInfo"`
  358 + CooperationInfo BatchAddUserItemCooperationInfo `json:"cooperationInfo"`
  359 + }
  360 + BatchAddUserItemUserInfo struct {
358 Phone string `json:"phone"` 361 Phone string `json:"phone"`
359 UserName string `json:"userName"` 362 UserName string `json:"userName"`
360 Email string `json:"email"` 363 Email string `json:"email"`
361 Avatar string `json:"avatar"` 364 Avatar string `json:"avatar"`
362 - } `json:"userInfo"`  
363 - CooperationInfo struct { 365 + }
  366 + BatchAddUserItemCooperationInfo struct {
364 CooperationCompany string `json:"cooperationCompany"` 367 CooperationCompany string `json:"cooperationCompany"`
365 CooperationDeadline time.Time `json:"cooperationDeadline"` 368 CooperationDeadline time.Time `json:"cooperationDeadline"`
366 - } `json:"cooperationInfo"`  
367 } 369 }
368 ) 370 )
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
  4 + "fmt"
4 "github.com/linmadan/egglib-go/core/application" 5 "github.com/linmadan/egglib-go/core/application"
5 "github.com/linmadan/egglib-go/utils/json" 6 "github.com/linmadan/egglib-go/utils/json"
6 "github.com/linmadan/egglib-go/web/beego" 7 "github.com/linmadan/egglib-go/web/beego"
@@ -8,6 +9,8 @@ import ( @@ -8,6 +9,8 @@ import (
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/middleware" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/middleware"
  12 + "io"
  13 + "path/filepath"
11 ) 14 )
12 15
13 type BaseController struct { 16 type BaseController struct {
@@ -108,6 +111,19 @@ func (controller *BaseController) BodyKeys() map[string]interface{} { @@ -108,6 +111,19 @@ func (controller *BaseController) BodyKeys() map[string]interface{} {
108 return bodyKV 111 return bodyKV
109 } 112 }
110 113
  114 +func (controller *BaseController) GetExcelFile() (io.Reader, error) {
  115 + excelFile, fileHeader, err := controller.GetFile("file")
  116 + if err != nil {
  117 + log.Logger.Error(err.Error())
  118 + return nil, fmt.Errorf("上传文件不存在")
  119 + }
  120 + ext := filepath.Ext(fileHeader.Filename)
  121 + if !(ext == "xlsx" || ext == "xls") {
  122 + return nil, fmt.Errorf("仅支持上传文件格式 xls/xlsx")
  123 + }
  124 + return excelFile, nil
  125 +}
  126 +
111 func Must(err error) { 127 func Must(err error) {
112 if err != nil { 128 if err != nil {
113 log.Logger.Error(err.Error()) 129 log.Logger.Error(err.Error())
1 package web_client 1 package web_client
2 2
3 import ( 3 import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command"
4 "path/filepath" 5 "path/filepath"
5 6
6 "github.com/beego/beego/v2/server/web/context" 7 "github.com/beego/beego/v2/server/web/context"
@@ -139,3 +140,31 @@ func (controller ExcelDataController) ImportDividendsReturnedOrder() { @@ -139,3 +140,31 @@ func (controller ExcelDataController) ImportDividendsReturnedOrder() {
139 } 140 }
140 controller.Response(result, nil) 141 controller.Response(result, nil)
141 } 142 }
  143 +
  144 +func (controller ExcelDataController) ImportCompanyUser() {
  145 + excelService := service.NewExcelDataService(nil)
  146 + r, err := controller.GetExcelFile()
  147 + if err != nil {
  148 + controller.Response(nil, err)
  149 + return
  150 + }
  151 + cmd := &command.ImportDataCommand{}
  152 + cmd.Operator = controller.GetOperator()
  153 + cmd.Reader = r
  154 + data, err := excelService.ImportCompanyUser(cmd)
  155 + controller.Response(data, err)
  156 +}
  157 +
  158 +func (controller ExcelDataController) ImportCooperationUser() {
  159 + excelService := service.NewExcelDataService(nil)
  160 + r, err := controller.GetExcelFile()
  161 + if err != nil {
  162 + controller.Response(nil, err)
  163 + return
  164 + }
  165 + cmd := &command.ImportDataCommand{}
  166 + cmd.Operator = controller.GetOperator()
  167 + cmd.Reader = r
  168 + data, err := excelService.ImportCooperationUser(cmd)
  169 + controller.Response(data, err)
  170 +}
@@ -8,4 +8,7 @@ import ( @@ -8,4 +8,7 @@ import (
8 func init() { 8 func init() {
9 web.Router("/v1/web/excel/export/company-user", &web_client.ExcelDataController{}, "Post:ExportCompanyUser") 9 web.Router("/v1/web/excel/export/company-user", &web_client.ExcelDataController{}, "Post:ExportCompanyUser")
10 web.Router("/v1/web/excel/export/cooperation-user", &web_client.ExcelDataController{}, "Post:ExportCooperationUser") 10 web.Router("/v1/web/excel/export/cooperation-user", &web_client.ExcelDataController{}, "Post:ExportCooperationUser")
  11 +
  12 + web.Router("/v1/web/excel/import/company-user", &web_client.ExcelDataController{}, "Post:ImportCompanyUser")
  13 + web.Router("/v1/web/excel/import/cooperation-user", &web_client.ExcelDataController{}, "Post:ImportCooperationUser")
11 } 14 }