作者 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"`
UserType int `json:"userType"`
UserCode string `json:"userCode"`
OrganizationID int `json:"organizationId"`
DepartmentID int `json:"departmentId"`
UserInfo struct {
Phone string `json:"phone"`
UserName string `json:"userName"`
Email string `json:"email"`
Avatar string `json:"avatar"`
} `json:"userInfo"`
CooperationInfo struct {
CooperationCompany string `json:"cooperationCompany"`
CooperationDeadline time.Time `json:"cooperationDeadline"`
} `json:"cooperationInfo"`
CompanyID int64 `json:"companyId"`
UserType int `json:"userType"`
UserCode string `json:"userCode"`
OrganizationID int `json:"organizationId"`
DepartmentID int `json:"departmentId"`
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"`
}
BatchAddUserItemCooperationInfo struct {
CooperationCompany string `json:"cooperationCompany"`
CooperationDeadline time.Time `json:"cooperationDeadline"`
}
)
... ...
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 (
"path/filepath"
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/utils/excel"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
)
type ExcelDataController struct {
baseController
}
func (controller *ExcelDataController) responseExcelByFile(ctx *context.Context, excelExport *excel.ExcelExport, fileName string) error {
ctx.Output.Header("Content-Disposition", "attachment; filename="+fileName)
ctx.Output.Header("Content-Description", "File Transfer")
ctx.Output.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
ctx.Output.Header("Content-Transfer-Encoding", "binary")
ctx.Output.Header("Expires", "0")
ctx.Output.Header("Cache-Control", "must-revalidate")
ctx.Output.Header("Pragma", "public")
//跳过保存文件,直接写入ctx.ResponseWriter
excelExport.ExcelFile.Write(ctx.ResponseWriter)
return nil
}
//导出公司用户
func (controller ExcelDataController) ExportCompanyUser() {
excelService := service.NewExcelDataService(nil)
companyUserListQuery := &query.CompanyUserListQuery{}
err := controller.Unmarshal(companyUserListQuery)
if err != nil {
log.Logger.Debug("json err:" + err.Error())
controller.Response(nil, err)
return
}
companyUserListQuery.Operator = controller.GetOperator()
data, err := excelService.ExportCompanyUser(companyUserListQuery)
if err != nil {
log.Logger.Debug("excelService.ExportCompanyUser err:" + err.Error())
controller.Response(nil, err)
return
}
excelTool := excel.NewExcelExport()
err = excelTool.ExportData(data, "")
if err != nil {
log.Logger.Debug("excelTool.ExportData err:" + err.Error())
controller.Response(nil, err)
return
}
controller.responseExcelByFile(controller.Ctx, excelTool, "导出公司用户")
}
//导出共创用户
func (controller ExcelDataController) ExportCooperationUser() {
excelService := service.NewExcelDataService(nil)
companyUserListQuery := &query.CompanyUserListQuery{}
err := controller.Unmarshal(companyUserListQuery)
if err != nil {
log.Logger.Debug("json err:" + err.Error())
controller.Response(nil, err)
return
}
companyUserListQuery.Operator = controller.GetOperator()
data, err := excelService.ExportCooperationUser(companyUserListQuery)
if err != nil {
log.Logger.Debug("excelService.ExportCompanyUser err:" + err.Error())
controller.Response(nil, err)
return
}
excelTool := excel.NewExcelExport()
err = excelTool.ExportData(data, "")
if err != nil {
log.Logger.Debug("excelTool.ExportData err:" + err.Error())
controller.Response(nil, err)
return
}
controller.responseExcelByFile(controller.Ctx, excelTool, "导出共创用户")
}
func (controller ExcelDataController) ImportDividendsReturnedOrder() {
excelFile, fileHeader, err := controller.GetFile("file")
if err != nil {
log.Logger.Error("接收文件失败," + err.Error())
controller.Response(nil, err)
return
}
if filepath.Ext(fileHeader.Filename) == "" {
//TODO 判断文件类型
log.Logger.Error("获取到的文件:" + fileHeader.Filename)
}
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 2 //第二行开始读取
excelImport.DataFields = []excel.DataField{
{EnName: "OriginalOrderNum", CnName: "*来源源单号"},
{EnName: "DividendsReturnedCustomerName", CnName: "*客户名称"},
{EnName: "OrderGoodName", CnName: "*产品名称"},
{EnName: "DividendsReturnedDate", CnName: "*退货日期"},
{EnName: "OrderTime", CnName: "*订单日期"},
{EnName: "RegionName", CnName: "*退货区域"},
{EnName: "OrderGoodQuantity", CnName: "*退货数量"},
{EnName: "OrderGoodPrice", CnName: "*退货价格"},
{EnName: "CooperationContractNumber", CnName: "项目合约编号"},
}
excelData, err := excelImport.OpenExcelFromIoReader(excelFile)
if err != nil {
log.Logger.Error("解析excel文件失败," + err.Error())
controller.Response(nil, err)
return
}
//退货单数据
returnedOrderData := []allied_creation_cooperation.ImportDividendsReturnedOrderData{}
for _, v := range excelData {
//TODO 按需转化
item := allied_creation_cooperation.ImportDividendsReturnedOrderData{
OriginalOrderNum: v["OriginalOrderNum"],
DividendsReturnedCustomerName: v["DividendsReturnedCustomerName"],
OrderGoodName: v["OrderGoodName"],
DividendsReturnedDate: v["DividendsReturnedDate"],
OrderTime: v["OrderTime"],
RegionName: v["RegionName"],
OrderGoodQuantity: v["OrderGoodQuantity"],
OrderGoodPrice: v["OrderGoodPrice"],
CooperationContractNumber: v["CooperationContractNumber"],
}
returnedOrderData = append(returnedOrderData, item)
}
creationCooperationGate := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(controller.GetOperator())
result, err := creationCooperationGate.ImportDividendsReturnedOrder(allied_creation_cooperation.ReqImportDividendsReturnedOrder{
DividendsReturnedOrderData: returnedOrderData,
})
if err != nil {
controller.Response(nil, err)
return
}
controller.Response(result, nil)
}
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"
"github.com/linmadan/egglib-go/utils/excel"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
)
type ExcelDataController struct {
baseController
}
func (controller *ExcelDataController) responseExcelByFile(ctx *context.Context, excelExport *excel.ExcelExport, fileName string) error {
ctx.Output.Header("Content-Disposition", "attachment; filename="+fileName)
ctx.Output.Header("Content-Description", "File Transfer")
ctx.Output.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
ctx.Output.Header("Content-Transfer-Encoding", "binary")
ctx.Output.Header("Expires", "0")
ctx.Output.Header("Cache-Control", "must-revalidate")
ctx.Output.Header("Pragma", "public")
//跳过保存文件,直接写入ctx.ResponseWriter
excelExport.ExcelFile.Write(ctx.ResponseWriter)
return nil
}
//导出公司用户
func (controller ExcelDataController) ExportCompanyUser() {
excelService := service.NewExcelDataService(nil)
companyUserListQuery := &query.CompanyUserListQuery{}
err := controller.Unmarshal(companyUserListQuery)
if err != nil {
log.Logger.Debug("json err:" + err.Error())
controller.Response(nil, err)
return
}
companyUserListQuery.Operator = controller.GetOperator()
data, err := excelService.ExportCompanyUser(companyUserListQuery)
if err != nil {
log.Logger.Debug("excelService.ExportCompanyUser err:" + err.Error())
controller.Response(nil, err)
return
}
excelTool := excel.NewExcelExport()
err = excelTool.ExportData(data, "")
if err != nil {
log.Logger.Debug("excelTool.ExportData err:" + err.Error())
controller.Response(nil, err)
return
}
controller.responseExcelByFile(controller.Ctx, excelTool, "导出公司用户")
}
//导出共创用户
func (controller ExcelDataController) ExportCooperationUser() {
excelService := service.NewExcelDataService(nil)
companyUserListQuery := &query.CompanyUserListQuery{}
err := controller.Unmarshal(companyUserListQuery)
if err != nil {
log.Logger.Debug("json err:" + err.Error())
controller.Response(nil, err)
return
}
companyUserListQuery.Operator = controller.GetOperator()
data, err := excelService.ExportCooperationUser(companyUserListQuery)
if err != nil {
log.Logger.Debug("excelService.ExportCompanyUser err:" + err.Error())
controller.Response(nil, err)
return
}
excelTool := excel.NewExcelExport()
err = excelTool.ExportData(data, "")
if err != nil {
log.Logger.Debug("excelTool.ExportData err:" + err.Error())
controller.Response(nil, err)
return
}
controller.responseExcelByFile(controller.Ctx, excelTool, "导出共创用户")
}
func (controller ExcelDataController) ImportDividendsReturnedOrder() {
excelFile, fileHeader, err := controller.GetFile("file")
if err != nil {
log.Logger.Error("接收文件失败," + err.Error())
controller.Response(nil, err)
return
}
if filepath.Ext(fileHeader.Filename) == "" {
//TODO 判断文件类型
log.Logger.Error("获取到的文件:" + fileHeader.Filename)
}
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 2 //第二行开始读取
excelImport.DataFields = []excel.DataField{
{EnName: "OriginalOrderNum", CnName: "*来源源单号"},
{EnName: "DividendsReturnedCustomerName", CnName: "*客户名称"},
{EnName: "OrderGoodName", CnName: "*产品名称"},
{EnName: "DividendsReturnedDate", CnName: "*退货日期"},
{EnName: "OrderTime", CnName: "*订单日期"},
{EnName: "RegionName", CnName: "*退货区域"},
{EnName: "OrderGoodQuantity", CnName: "*退货数量"},
{EnName: "OrderGoodPrice", CnName: "*退货价格"},
{EnName: "CooperationContractNumber", CnName: "项目合约编号"},
}
excelData, err := excelImport.OpenExcelFromIoReader(excelFile)
if err != nil {
log.Logger.Error("解析excel文件失败," + err.Error())
controller.Response(nil, err)
return
}
//退货单数据
returnedOrderData := []allied_creation_cooperation.ImportDividendsReturnedOrderData{}
for _, v := range excelData {
//TODO 按需转化
item := allied_creation_cooperation.ImportDividendsReturnedOrderData{
OriginalOrderNum: v["OriginalOrderNum"],
DividendsReturnedCustomerName: v["DividendsReturnedCustomerName"],
OrderGoodName: v["OrderGoodName"],
DividendsReturnedDate: v["DividendsReturnedDate"],
OrderTime: v["OrderTime"],
RegionName: v["RegionName"],
OrderGoodQuantity: v["OrderGoodQuantity"],
OrderGoodPrice: v["OrderGoodPrice"],
CooperationContractNumber: v["CooperationContractNumber"],
}
returnedOrderData = append(returnedOrderData, item)
}
creationCooperationGate := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(controller.GetOperator())
result, err := creationCooperationGate.ImportDividendsReturnedOrder(allied_creation_cooperation.ReqImportDividendsReturnedOrder{
DividendsReturnedOrderData: returnedOrderData,
})
if err != nil {
controller.Response(nil, err)
return
}
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")
}
... ...