作者 Your Name

更新 批量添加产能

  1 +package command
  2 +
  3 +import "errors"
  4 +
  5 +type BatchAddProductCapacitiesCmd struct {
  6 + WorkshopName string `json:"workshopName"` //车间
  7 + LineName string `json:"lineName"` //生产线
  8 + SectionName string `json:"sectionName"` //工段
  9 + WorkerName string `json:"workerName"` //员工
  10 + Weigh string `json:"weigh"` //产量重量
  11 + BatchNumber string `json:"batchNumber"` //计划 批次号
  12 + RecordDate string `json:"recordDate"` //日期 2006-01-02
  13 + WorkOn string `json:"workOn"` //上班班次 1:全天 2:白班 4:中班 8:夜班
  14 + FailReason string `json:"failReason"`
  15 +}
  16 +
  17 +func (data *BatchAddProductCapacitiesCmd) ValidField() error {
  18 + if len(data.RecordDate) == 0 {
  19 + return errors.New("日期未填写")
  20 + }
  21 + if len(data.WorkshopName) == 0 {
  22 + return errors.New("车间未填写")
  23 + }
  24 + if len(data.LineName) == 0 {
  25 + return errors.New("生产线未填写")
  26 + }
  27 + if len(data.SectionName) == 0 {
  28 + return errors.New("工段未填写")
  29 + }
  30 + if len(data.WorkerName) == 0 {
  31 + return errors.New("姓名未填写")
  32 + }
  33 + if len(data.BatchNumber) == 0 {
  34 + return errors.New("批次未填写")
  35 + }
  36 + if len(data.Weigh) == 0 {
  37 + return errors.New("重量未填写")
  38 + }
  39 + if len(data.WorkOn) == 0 {
  40 + return errors.New("上班班次未填写")
  41 + }
  42 + return nil
  43 +}
@@ -2,7 +2,7 @@ package command @@ -2,7 +2,7 @@ package command
2 2
3 import "errors" 3 import "errors"
4 4
5 -//批量添加生产记录 5 +// 批量添加生产记录,二级品
6 type BatchAddProductRecordCommand struct { 6 type BatchAddProductRecordCommand struct {
7 CreatedDate string `json:"createdDate"` // 日期 7 CreatedDate string `json:"createdDate"` // 日期
8 WorkshopName string `json:"workshopName"` // 车间 8 WorkshopName string `json:"workshopName"` // 车间
1 package command 1 package command
2 2
3 -type SaveProductRecordCmd struct { 3 +type SaveProductCapacitiesCmd struct {
4 //id 4 //id
5 ProductRecordId int `json:"productRecordId"` 5 ProductRecordId int `json:"productRecordId"`
6 // 车间ID 6 // 车间ID
@@ -15,8 +15,6 @@ type SaveProductRecordCmd struct { @@ -15,8 +15,6 @@ type SaveProductRecordCmd struct {
15 Weigh float64 `cname:"重量" json:"weigh" valid:"Required" ` 15 Weigh float64 `cname:"重量" json:"weigh" valid:"Required" `
16 //计划id 16 //计划id
17 ProductPlanId int `json:"productPlanId"` 17 ProductPlanId int `json:"productPlanId"`
18 - // 参与类型 1:正常 2:支援  
19 - ParticipateType int `json:"participateType"`  
20 //日期 18 //日期
21 RecordDate string `json:"recordDate"` 19 RecordDate string `json:"recordDate"`
22 //上班班次 1:全天 2:白班 4:中班 8:夜班 20 //上班班次 1:全天 2:白班 4:中班 8:夜班
1 package service 1 package service
2 2
3 import ( 3 import (
  4 + "strconv"
  5 + "strings"
4 "time" 6 "time"
5 7
6 "github.com/linmadan/egglib-go/core/application" 8 "github.com/linmadan/egglib-go/core/application"
@@ -15,7 +17,7 @@ import ( @@ -15,7 +17,7 @@ import (
15 //产能管理 17 //产能管理
16 18
17 // 产能管理 页面上手动创建员工生产记录 19 // 产能管理 页面上手动创建员工生产记录
18 -func (productRecordService *ProductRecordService) SaveProductCapacities(operateInfo *domain.OperateInfo, param *command.SaveProductRecordCmd) (map[string]interface{}, error) { 20 +func (productRecordService *ProductRecordService) SaveProductCapacities(operateInfo *domain.OperateInfo, param *command.SaveProductCapacitiesCmd) (map[string]interface{}, error) {
19 transactionContext, err := factory.CreateTransactionContext(nil) 21 transactionContext, err := factory.CreateTransactionContext(nil)
20 if err != nil { 22 if err != nil {
21 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 23 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -334,3 +336,207 @@ func (productRecordService *ProductRecordService) ApproveProductCapacities(opera @@ -334,3 +336,207 @@ func (productRecordService *ProductRecordService) ApproveProductCapacities(opera
334 "productRecordId": recordData.ProductRecordId, 336 "productRecordId": recordData.ProductRecordId,
335 }, nil 337 }, nil
336 } 338 }
  339 +
  340 +// 从excel导入 批量添加
  341 +func (srv *ProductRecordService) BatchAddProductCapacities(operate *domain.OperateInfo, dataList []command.BatchAddProductCapacitiesCmd) (
  342 + failRows []interface{}, err error) {
  343 + transactionContext, err := factory.CreateTransactionContext(nil)
  344 + if err != nil {
  345 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  346 + }
  347 + if err := transactionContext.StartTransaction(); err != nil {
  348 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  349 + }
  350 + defer func() {
  351 + transactionContext.RollbackTransaction()
  352 + }()
  353 +
  354 + //获取当前操作人
  355 + userSrv := domainService.NewUserService()
  356 + operateUser, err := userSrv.User(operate.UserId)
  357 + if err != nil {
  358 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "查询操作人数据失败。"+err.Error())
  359 + }
  360 + //获取当前操作人的组织
  361 + var org *domain.Org
  362 + org, err = userSrv.Organization(operate.OrgId)
  363 + if err != nil {
  364 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  365 + }
  366 +
  367 + //生产班组 数据
  368 + productGroupRepo, _ := factory.CreateProductGroupRepository(map[string]interface{}{
  369 + "transactionContext": transactionContext,
  370 + })
  371 +
  372 + //生产计划数据
  373 + productPlanRepo, _ := factory.CreateProductPlanRepository(map[string]interface{}{
  374 + "transactionContext": transactionContext,
  375 + })
  376 +
  377 + _, productGroupList, err := productGroupRepo.Find(map[string]interface{}{
  378 + "companyId": operate.CompanyId,
  379 + "orgId": operate.OrgId,
  380 + })
  381 + if err != nil {
  382 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  383 + }
  384 + //车间名称+/+线别名称+/+工段名称 作为键名
  385 + workStationMap := map[string]*domain.WorkStation{}
  386 + //车间名称+/+工人名 作为键名
  387 + workerMap := map[string][]*domain.User{}
  388 + //班组名称 作为键名
  389 + productGroupMap := map[string]*domain.ProductGroup{}
  390 + for _, v := range productGroupList {
  391 + workStationName := strings.Join([]string{
  392 + v.WorkStation.WorkshopName, v.WorkStation.LineName, v.WorkStation.SectionName,
  393 + }, "/")
  394 + workStationMap[workStationName] = v.WorkStation
  395 + productGroupMap[v.GroupName] = v
  396 + for _, vv := range v.GroupMembers {
  397 + k := v.WorkStation.WorkshopName + "/" + vv.UserName
  398 + isIn := false
  399 + for _, vvv := range workerMap[k] {
  400 + if vvv.UserId == vv.UserId {
  401 + isIn = true
  402 + break
  403 + }
  404 + }
  405 + if !isIn {
  406 + workerMap[k] = append(workerMap[k], vv)
  407 + }
  408 + }
  409 + }
  410 +
  411 + productRecordList := []*domain.ProductRecord{}
  412 +
  413 + nowTime := time.Now()
  414 + for i := range dataList {
  415 + //检查字段
  416 + err = dataList[i].ValidField()
  417 + if err != nil {
  418 + dataList[i].FailReason = err.Error()
  419 + failRows = append(failRows, dataList[i])
  420 + continue
  421 + }
  422 + //检查日期格式
  423 + productDate, err := time.ParseInLocation("2006-01-02", dataList[i].RecordDate, time.Local)
  424 + if err != nil {
  425 + dataList[i].FailReason = "日期格式错误,例 2006-01-02。"
  426 + failRows = append(failRows, dataList[i])
  427 + continue
  428 + }
  429 + //检查工位
  430 + var workStation *domain.WorkStation
  431 + workStationName := dataList[i].WorkerName + "/" + dataList[i].LineName + "/" + dataList[i].SectionName
  432 + if v, ok := workStationMap[workStationName]; ok {
  433 + workStation = v
  434 + } else {
  435 + dataList[i].FailReason = "车间、线别、工段不存在"
  436 + failRows = append(failRows, dataList[i])
  437 + continue
  438 + }
  439 +
  440 + //检查员工姓名
  441 + var worker *domain.User
  442 + workKey := dataList[i].WorkshopName + "/" + dataList[i].WorkerName
  443 + if u, ok := workerMap[workKey]; ok {
  444 + if len(u) > 1 {
  445 + dataList[i].FailReason = "当前车间存在重复的用户名"
  446 + failRows = append(failRows, dataList[i])
  447 + continue
  448 + }
  449 + worker = u[0]
  450 + } else {
  451 + dataList[i].FailReason = "当前车间不存在用户" + dataList[i].WorkerName
  452 + failRows = append(failRows, dataList[i])
  453 + continue
  454 + }
  455 + //二级品重量
  456 + weigh, err := strconv.ParseFloat(dataList[i].Weigh, 64)
  457 + if err != nil {
  458 + dataList[i].FailReason = "重量填写错误"
  459 + failRows = append(failRows, dataList[i])
  460 + continue
  461 + }
  462 + //按批次获取生产计划
  463 + productPlanData, err := productPlanRepo.FindOne(map[string]interface{}{
  464 + "batchNumber": dataList[i].BatchNumber,
  465 + "companyId": operate.CompanyId,
  466 + })
  467 + if err != nil {
  468 + dataList[i].FailReason = "批次号不存在"
  469 + failRows = append(failRows, dataList[i])
  470 + continue
  471 + }
  472 + //检查上班班次 1:全天 2:白班 4:中班 8:夜班
  473 + workerOn := 0
  474 + switch dataList[i].WorkOn {
  475 + case "全天":
  476 + workerOn = 1
  477 + case "白班":
  478 + workerOn = 2
  479 + case "中班":
  480 + workerOn = 4
  481 + case "夜班":
  482 + workerOn = 8
  483 + default:
  484 + dataList[i].FailReason = "上班班次 填写错误"
  485 + failRows = append(failRows, dataList[i])
  486 + continue
  487 + }
  488 +
  489 + tempItem := &domain.ProductRecord{
  490 + ProductRecordId: 0,
  491 + CompanyId: operate.CompanyId,
  492 + OrgId: operate.OrgId,
  493 + ProductRecordType: domain.RecordTypeReceiveMaterial,
  494 + ProductWorker: worker,
  495 + WorkStation: workStation,
  496 + CreatedAt: productDate,
  497 + UpdatedAt: nowTime,
  498 + DeletedAt: time.Time{},
  499 + ProductRecordInfo: &domain.ProductRecordInfo{
  500 + ProductDate: productDate.Local().Format("2006-01-02"),
  501 + Original: weigh,
  502 + Weigh: weigh,
  503 + WeighBefore: weigh,
  504 + WeighAfter: weigh,
  505 + ApproveStatus: domain.ProductRecordAutoApproved,
  506 + ApproveAt: nowTime.Unix(),
  507 + ApproveUser: operateUser,
  508 + UnitConversionId: productPlanData.Ext.ProductPlanExt.ProductId,
  509 + ProductPlanId: productPlanData.ProductPlanId,
  510 + PlanProductName: productPlanData.PlanProductName,
  511 + BatchNumber: productPlanData.BatchNumber,
  512 + ProductGroupId: 0,
  513 + WorkOn: workerOn,
  514 + },
  515 + Ext: &domain.Ext{
  516 + Operator: operateUser,
  517 + OrgName: org.OrgName,
  518 + },
  519 + PreRecord: &domain.ProductRecord{},
  520 + }
  521 + productRecordList = append(productRecordList, tempItem)
  522 + }
  523 + if len(failRows) > 0 {
  524 + return failRows, nil
  525 + }
  526 + productRecordRepo, _ := factory.CreateProductRecordRepository(map[string]interface{}{
  527 + "transactionContext": transactionContext,
  528 + })
  529 + for i := range productRecordList {
  530 + _, err := productRecordRepo.Save(productRecordList[i])
  531 + if err != nil {
  532 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  533 + }
  534 + }
  535 + if err := transactionContext.CommitTransaction(); err != nil {
  536 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  537 + }
  538 + if err := transactionContext.CommitTransaction(); err != nil {
  539 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  540 + }
  541 + return failRows, nil
  542 +}