A

allied-creation-manufacture

天联制造项目

Administrator提交了 · 6224fc6a 更新 Dockerfile

生产制造项目说明

1.数据导入导出模块

1.1.Excel 导入流程

  • 原型说明

选择模板下载、文件上传

导入数据检验有错,显示具体错误

POST/v1/web/file-import

Content-Type    multipart/form-data

params:

file   文件
code   对应导入模块的编码
  • 对接步骤以导入公司用户模块为例

1.定义接口导入的codeADMIN_SYSTEM-MANAGE_BASE_USER 2.根据接口解析导入文件的数据 3.调用基础库解析数据并传到后台


// 1.解析列
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 3 //第二行开始读取
excelImport.DataFields = []excel.DataField{
    {EnName: "userCode", CnName: "*用户编码"},
    {EnName: "userName", CnName: "*用户姓名"},
    {EnName: "org", CnName: "*组织机构"},
    {EnName: "department", CnName: "*所属部门"},
    {EnName: "enableStatus", CnName: "*用户状态"},
    {EnName: "phone", CnName: "*手机号"},
    {EnName: "employeeType", CnName: "*员工类型"},
    {EnName: "icCardNumber", CnName: "IC卡号"},
    {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)
for _, v := range excelData {
    if srv.fieldValueAllEmpty(v) {
        continue
    }
    item := allied_creation_user.BatchAddUserItem{
        CompanyId:    importDataCommand.Operator.CompanyId,
        UserType:     domain.UserTypeEmployee,
        UserCode:     strings.TrimSpace(v["userCode"]),
        Org:          strings.TrimSpace(v["org"]),
        Department:   strings.TrimSpace(v["department"]),
        UserName:     strings.TrimSpace(v["userName"]),
        Phone:        strings.TrimSpace(v["phone"]),
        Email:        strings.TrimSpace(v["email"]),
        EnableStatus: strings.TrimSpace(v["enableStatus"]),
        EmployeeType: strings.TrimSpace(v["employeeType"]),
        IcCardNumber: strings.TrimSpace(v["icCardNumber"]),
    }
    users = append(users, item)
}
// 2.向后台服务发起批量添加请求
userGateway := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator)
result, err := userGateway.UserBatchAdd(allied_creation_user.ReqBatchAddUser{
    Users:    users,
    Password: initPassword,
})

2.2 Excel 导出流程

  • 功能原型说明

实现可以自定义选择列进行导出,以及选择导出格式,导出格式有 xlsx\csv,功能如下图所示:

  • 前后端交互说明

接口定义

导出接口

POST /v1/web/file-export

// request 请求参数
{
    // 业务编码
    "code":"string"
    // 选择的导出的列
    "fields":[]
    // 导出文件格式
    "format":"xlsx"
    // 业务查询条件
    "where":{}
}

导出列查询接口

GET/v1/web/file-export/fields/:code

// response 应答数据
{
    "fields":[
        {
            "enName":"字段键名",
            "cnName":"字段展示名称",
            "selected":true // 默认勾选
        }
    ]
}
  • 交互流程

1.前台根据 导出列查询接口 获取可以导出的列; 2.勾选需要导出的列、文件格式,调用 导出接口 向后台发起导出数据请求;

2.2.1 后端导出实现

  1. 导出数据实现接口
type ExcelMaker interface {
    DataFieldList() []DataField                             //字段元数据列表
    CellValue(index int, enName string) (value interface{}) //获取单元格字段值
    DataListLen() int                                       //数据列表大小
    TableTitle() []string                                   //列表顶部自定义内容
}

如下所示

//ExportCooperationUserData 导出共创用户数据
type ExportCooperationUserData struct {
    SourceData    []allied_creation_user.UserDetail //具体数据
    SelectedField []string
}

var _ excel.ExcelMaker = (*ExportCooperationUserData)(nil)

func (data ExportCooperationUserData) AllFields() []DataFieldOptions {
    return []DataFieldOptions{
        {EnName: "UserCode", CnName: "用户编码"},
        {EnName: "UserName", CnName: "用户姓名"},
        {EnName: "Phone", CnName: "手机号"},
        {EnName: "CooperationCompany", CnName: "共创公司"},
        {EnName: "CooperationDeadline", CnName: "共创到期"},
        {EnName: "EnableStatus", CnName: "状态"},
    }
}

func (data ExportCooperationUserData) DataFieldList() []excel.DataField {
    fields := []excel.DataField{}
    allFields := data.AllFields()
    for _, value2 := range allFields {
        if len(data.SelectedField) == 0 || value2.IsDefault {
            fields = append(fields, excel.DataField{EnName: value2.EnName, CnName: value2.CnName})
            continue
        }
        for _, value3 := range data.SelectedField {
            if value2.EnName == value3 {
                fields = append(fields, excel.DataField{EnName: value2.EnName, CnName: value2.CnName})
            }
        }
    }
    return fields
}

func (data ExportCooperationUserData) CellValue(index int, enName string) (value interface{}) {
    if index > data.DataListLen() {
        return ""
    }
    switch enName {
    case "UserCode":
        return data.SourceData[index].UserCode
    case "UserName":
        return data.SourceData[index].UserInfo.UserName
    case "CooperationCompany":
        return data.SourceData[index].CooperationInfo.CooperationCompany
    case "CooperationDeadline":
        if data.SourceData[index].CooperationInfo.CooperationDeadline.IsZero() || data.SourceData[index].CooperationInfo.CooperationDeadline.Unix() == 0 {
            return ""
        }
        return data.SourceData[index].CooperationInfo.CooperationDeadline.Format("2006-01-02")
    case "Phone":
        return data.SourceData[index].UserInfo.Phone
    case "Email":
        return data.SourceData[index].UserInfo.Email
    case "EnableStatus":
        status := data.SourceData[index].EnableStatus
        statusName := ""
        // 状态(1:启用  2:禁用  3:注销)
        switch status {
        case 1:
            statusName = "启用"
        case 2:
            statusName = "禁用"
        case 3:
            statusName = "注销"
        }
        return statusName
    }
    return nil
}

func (data ExportCooperationUserData) DataListLen() int {
    return len(data.SourceData)
}

func (data ExportCooperationUserData) TableTitle() []string {
    return nil
}
  1. 返回文件
//返回文件
excelTool := excel.NewExcelExport()
err = excelTool.ExportData(data, "")
controller.responseExcelByFile(controller.Ctx, excelTool, filename)


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
}