import_product_trouble.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"
	productTroubleCommand "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productTrouble/command"
	productTroubleService "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productTrouble/service"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils/converter"
)

// ImportProductTrouble 导入事故记录
func (srv ExcelDataService) ImportProductTrouble(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: "workerName", CnName: "姓名"},
		{EnName: "typesName", CnName: "事故类型"},
		{EnName: "amountLoss", CnName: "损失金额"},
	}
	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([]productTroubleCommand.BatchAddProductTroubleCommand, 0, len(excelData))
	item := productTroubleCommand.BatchAddProductTroubleCommand{}
	for _, v := range excelData {
		item = productTroubleCommand.BatchAddProductTroubleCommand{
			RecordDate:   v["recordDate"],
			WorkshopName: v["workshopName"],
			LineName:     v["lineName"],
			SectionName:  v["sectionName"],
			WorkerName:   v["workerName"],
			AmountLoss:   v["amountLoss"],
			TypesName:    v["typesName"],
			FailReason:   "",
		}
		items = append(items, item)
	}
	svr := productTroubleService.NewProductTroubleService(nil)
	failRows, err := svr.BatchAddProductTrouble(importDataCommand.Operator, items)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return srv.importResultWithHeader(excelImport.DataFields, failRows, len(items)), nil
}