import_product_capacities.go 2.4 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/excel"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/ecelData/command"
	productRecordCommand "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/command"
	productRecordService "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/service"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils/converter"
)

// ImportProductCapacities 导入产能数据
func (srv ExcelDataService) ImportProductCapacities(importDataCommand *command.ImportDataCommand) (interface{}, error) {
	excelImport := excel.NewExcelImport()
	excelImport.RowBegin = 3 //第二行开始读取
	excelImport.DataFields = []excel.DataField{
		{EnName: "recordDate", CnName: "日期"},
		{EnName: "workshopName", CnName: "车间"},
		{EnName: "lineName", CnName: "线别"},
		{EnName: "sectionName", CnName: "工段"},
		{EnName: "workOn", CnName: "班别"},
		{EnName: "workerName", CnName: "姓名"},
		{EnName: "batchNumber", CnName: "批次号"},
		{EnName: "weigh", CnName: "产量(kg)"},
	}
	excelData, err := converter.OpenImportFileFromIoReader(excelImport, importDataCommand.Reader, importDataCommand.FileExt) //excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	items := make([]productRecordCommand.BatchAddProductCapacitiesCmd, 0, len(excelData))
	item := productRecordCommand.BatchAddProductCapacitiesCmd{}
	for _, v := range excelData {
		item = productRecordCommand.BatchAddProductCapacitiesCmd{
			RecordDate:   v["recordDate"],
			WorkshopName: v["workshopName"],
			LineName:     v["lineName"],
			SectionName:  v["sectionName"],
			WorkerName:   v["workerName"],
			BatchNumber:  v["batchNumber"],
			WorkOn:       v["workOn"],
			Weigh:        v["weigh"],
			FailReason:   "",
		}
		items = append(items, item)
	}

	svr := productRecordService.NewProductRecordService(nil)
	failRows, err := svr.BatchAddProductCapacities(importDataCommand.Operator, items)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return srv.importResultWithHeader(excelImport.DataFields, failRows, len(items)), nil
}