作者 Your Name

更新 批量添加产能

package command
import "errors"
type BatchAddProductCapacitiesCmd struct {
WorkshopName string `json:"workshopName"` //车间
LineName string `json:"lineName"` //生产线
SectionName string `json:"sectionName"` //工段
WorkerName string `json:"workerName"` //员工
Weigh string `json:"weigh"` //产量重量
BatchNumber string `json:"batchNumber"` //计划 批次号
RecordDate string `json:"recordDate"` //日期 2006-01-02
WorkOn string `json:"workOn"` //上班班次 1:全天 2:白班 4:中班 8:夜班
FailReason string `json:"failReason"`
}
func (data *BatchAddProductCapacitiesCmd) ValidField() error {
if len(data.RecordDate) == 0 {
return errors.New("日期未填写")
}
if len(data.WorkshopName) == 0 {
return errors.New("车间未填写")
}
if len(data.LineName) == 0 {
return errors.New("生产线未填写")
}
if len(data.SectionName) == 0 {
return errors.New("工段未填写")
}
if len(data.WorkerName) == 0 {
return errors.New("姓名未填写")
}
if len(data.BatchNumber) == 0 {
return errors.New("批次未填写")
}
if len(data.Weigh) == 0 {
return errors.New("重量未填写")
}
if len(data.WorkOn) == 0 {
return errors.New("上班班次未填写")
}
return nil
}
... ...
... ... @@ -2,7 +2,7 @@ package command
import "errors"
//批量添加生产记录
// 批量添加生产记录,二级品
type BatchAddProductRecordCommand struct {
CreatedDate string `json:"createdDate"` // 日期
WorkshopName string `json:"workshopName"` // 车间
... ...
package command
type SaveProductRecordCmd struct {
type SaveProductCapacitiesCmd struct {
//id
ProductRecordId int `json:"productRecordId"`
// 车间ID
... ... @@ -15,8 +15,6 @@ type SaveProductRecordCmd struct {
Weigh float64 `cname:"重量" json:"weigh" valid:"Required" `
//计划id
ProductPlanId int `json:"productPlanId"`
// 参与类型 1:正常 2:支援
ParticipateType int `json:"participateType"`
//日期
RecordDate string `json:"recordDate"`
//上班班次 1:全天 2:白班 4:中班 8:夜班
... ...
package service
import (
"strconv"
"strings"
"time"
"github.com/linmadan/egglib-go/core/application"
... ... @@ -15,7 +17,7 @@ import (
//产能管理
// 产能管理 页面上手动创建员工生产记录
func (productRecordService *ProductRecordService) SaveProductCapacities(operateInfo *domain.OperateInfo, param *command.SaveProductRecordCmd) (map[string]interface{}, error) {
func (productRecordService *ProductRecordService) SaveProductCapacities(operateInfo *domain.OperateInfo, param *command.SaveProductCapacitiesCmd) (map[string]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ... @@ -334,3 +336,207 @@ func (productRecordService *ProductRecordService) ApproveProductCapacities(opera
"productRecordId": recordData.ProductRecordId,
}, nil
}
// 从excel导入 批量添加
func (srv *ProductRecordService) BatchAddProductCapacities(operate *domain.OperateInfo, dataList []command.BatchAddProductCapacitiesCmd) (
failRows []interface{}, err error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
//获取当前操作人
userSrv := domainService.NewUserService()
operateUser, err := userSrv.User(operate.UserId)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "查询操作人数据失败。"+err.Error())
}
//获取当前操作人的组织
var org *domain.Org
org, err = userSrv.Organization(operate.OrgId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
//生产班组 数据
productGroupRepo, _ := factory.CreateProductGroupRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
//生产计划数据
productPlanRepo, _ := factory.CreateProductPlanRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, productGroupList, err := productGroupRepo.Find(map[string]interface{}{
"companyId": operate.CompanyId,
"orgId": operate.OrgId,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
//车间名称+/+线别名称+/+工段名称 作为键名
workStationMap := map[string]*domain.WorkStation{}
//车间名称+/+工人名 作为键名
workerMap := map[string][]*domain.User{}
//班组名称 作为键名
productGroupMap := map[string]*domain.ProductGroup{}
for _, v := range productGroupList {
workStationName := strings.Join([]string{
v.WorkStation.WorkshopName, v.WorkStation.LineName, v.WorkStation.SectionName,
}, "/")
workStationMap[workStationName] = v.WorkStation
productGroupMap[v.GroupName] = v
for _, vv := range v.GroupMembers {
k := v.WorkStation.WorkshopName + "/" + vv.UserName
isIn := false
for _, vvv := range workerMap[k] {
if vvv.UserId == vv.UserId {
isIn = true
break
}
}
if !isIn {
workerMap[k] = append(workerMap[k], vv)
}
}
}
productRecordList := []*domain.ProductRecord{}
nowTime := time.Now()
for i := range dataList {
//检查字段
err = dataList[i].ValidField()
if err != nil {
dataList[i].FailReason = err.Error()
failRows = append(failRows, dataList[i])
continue
}
//检查日期格式
productDate, err := time.ParseInLocation("2006-01-02", dataList[i].RecordDate, time.Local)
if err != nil {
dataList[i].FailReason = "日期格式错误,例 2006-01-02。"
failRows = append(failRows, dataList[i])
continue
}
//检查工位
var workStation *domain.WorkStation
workStationName := dataList[i].WorkerName + "/" + dataList[i].LineName + "/" + dataList[i].SectionName
if v, ok := workStationMap[workStationName]; ok {
workStation = v
} else {
dataList[i].FailReason = "车间、线别、工段不存在"
failRows = append(failRows, dataList[i])
continue
}
//检查员工姓名
var worker *domain.User
workKey := dataList[i].WorkshopName + "/" + dataList[i].WorkerName
if u, ok := workerMap[workKey]; ok {
if len(u) > 1 {
dataList[i].FailReason = "当前车间存在重复的用户名"
failRows = append(failRows, dataList[i])
continue
}
worker = u[0]
} else {
dataList[i].FailReason = "当前车间不存在用户" + dataList[i].WorkerName
failRows = append(failRows, dataList[i])
continue
}
//二级品重量
weigh, err := strconv.ParseFloat(dataList[i].Weigh, 64)
if err != nil {
dataList[i].FailReason = "重量填写错误"
failRows = append(failRows, dataList[i])
continue
}
//按批次获取生产计划
productPlanData, err := productPlanRepo.FindOne(map[string]interface{}{
"batchNumber": dataList[i].BatchNumber,
"companyId": operate.CompanyId,
})
if err != nil {
dataList[i].FailReason = "批次号不存在"
failRows = append(failRows, dataList[i])
continue
}
//检查上班班次 1:全天 2:白班 4:中班 8:夜班
workerOn := 0
switch dataList[i].WorkOn {
case "全天":
workerOn = 1
case "白班":
workerOn = 2
case "中班":
workerOn = 4
case "夜班":
workerOn = 8
default:
dataList[i].FailReason = "上班班次 填写错误"
failRows = append(failRows, dataList[i])
continue
}
tempItem := &domain.ProductRecord{
ProductRecordId: 0,
CompanyId: operate.CompanyId,
OrgId: operate.OrgId,
ProductRecordType: domain.RecordTypeReceiveMaterial,
ProductWorker: worker,
WorkStation: workStation,
CreatedAt: productDate,
UpdatedAt: nowTime,
DeletedAt: time.Time{},
ProductRecordInfo: &domain.ProductRecordInfo{
ProductDate: productDate.Local().Format("2006-01-02"),
Original: weigh,
Weigh: weigh,
WeighBefore: weigh,
WeighAfter: weigh,
ApproveStatus: domain.ProductRecordAutoApproved,
ApproveAt: nowTime.Unix(),
ApproveUser: operateUser,
UnitConversionId: productPlanData.Ext.ProductPlanExt.ProductId,
ProductPlanId: productPlanData.ProductPlanId,
PlanProductName: productPlanData.PlanProductName,
BatchNumber: productPlanData.BatchNumber,
ProductGroupId: 0,
WorkOn: workerOn,
},
Ext: &domain.Ext{
Operator: operateUser,
OrgName: org.OrgName,
},
PreRecord: &domain.ProductRecord{},
}
productRecordList = append(productRecordList, tempItem)
}
if len(failRows) > 0 {
return failRows, nil
}
productRecordRepo, _ := factory.CreateProductRecordRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
for i := range productRecordList {
_, err := productRecordRepo.Save(productRecordList[i])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return failRows, nil
}
... ...