|
@@ -2,6 +2,8 @@ package service |
|
@@ -2,6 +2,8 @@ package service |
2
|
|
2
|
|
3
|
import (
|
3
|
import (
|
4
|
"fmt"
|
4
|
"fmt"
|
|
|
5
|
+ "strconv"
|
|
|
6
|
+ "strings"
|
5
|
"time"
|
7
|
"time"
|
6
|
|
8
|
|
7
|
"github.com/linmadan/egglib-go/core/application"
|
9
|
"github.com/linmadan/egglib-go/core/application"
|
|
@@ -54,7 +56,7 @@ func (productRecordService *ProductRecordService) ApproveProductRecord(cmd *comm |
|
@@ -54,7 +56,7 @@ func (productRecordService *ProductRecordService) ApproveProductRecord(cmd *comm |
54
|
}
|
56
|
}
|
55
|
|
57
|
|
56
|
// 创建生产记录服务
|
58
|
// 创建生产记录服务
|
57
|
-func (productRecordService *ProductRecordService) CreateProductRecord(param *command.CreateProductRecordCommand) (interface{}, error) {
|
59
|
+func (productRecordService *ProductRecordService) CreateProductRecord(operateInfo *domain.OperateInfo, param *command.CreateProductRecordCommand) (interface{}, error) {
|
58
|
if err := param.ValidateCommand(); err != nil {
|
60
|
if err := param.ValidateCommand(); err != nil {
|
59
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
61
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
60
|
}
|
62
|
}
|
|
@@ -68,10 +70,28 @@ func (productRecordService *ProductRecordService) CreateProductRecord(param *com |
|
@@ -68,10 +70,28 @@ func (productRecordService *ProductRecordService) CreateProductRecord(param *com |
68
|
defer func() {
|
70
|
defer func() {
|
69
|
transactionContext.RollbackTransaction()
|
71
|
transactionContext.RollbackTransaction()
|
70
|
}()
|
72
|
}()
|
|
|
73
|
+
|
|
|
74
|
+ dataTime, err := time.ParseInLocation("2006-01-02", param.CreatedDate, time.Local)
|
|
|
75
|
+ if err != nil {
|
|
|
76
|
+ return nil, application.ThrowError(application.ARG_ERROR, "日期格式错误")
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ productPlanRepo, _ := factory.CreateProductPlanRepository(map[string]interface{}{
|
|
|
80
|
+ "transactionContext": transactionContext,
|
|
|
81
|
+ })
|
|
|
82
|
+
|
|
|
83
|
+ _, err = productPlanRepo.FindOne(map[string]interface{}{
|
|
|
84
|
+ "product_plan_id": param.ProductPlanId,
|
|
|
85
|
+ "company_id": operateInfo.CompanyId,
|
|
|
86
|
+ })
|
|
|
87
|
+ if err != nil {
|
|
|
88
|
+ return nil, application.ThrowError(application.ARG_ERROR, "生产计划id错误,"+err.Error())
|
|
|
89
|
+ }
|
|
|
90
|
+
|
71
|
productRecordDomainService, _ := domainService.NewPGProductRecordService(transactionContext.(*pg.TransactionContext))
|
91
|
productRecordDomainService, _ := domainService.NewPGProductRecordService(transactionContext.(*pg.TransactionContext))
|
72
|
submitProductRecordCommand := domainService.SubmitOptions{
|
92
|
submitProductRecordCommand := domainService.SubmitOptions{
|
73
|
- CompanyId: param.CompanyId,
|
|
|
74
|
- OrgId: param.OrgId,
|
93
|
+ CompanyId: operateInfo.CompanyId,
|
|
|
94
|
+ OrgId: operateInfo.OrgId,
|
75
|
ProductPlanId: param.ProductPlanId,
|
95
|
ProductPlanId: param.ProductPlanId,
|
76
|
WorkshopId: param.WorkshopId,
|
96
|
WorkshopId: param.WorkshopId,
|
77
|
LineId: param.LineId,
|
97
|
LineId: param.LineId,
|
|
@@ -79,18 +99,31 @@ func (productRecordService *ProductRecordService) CreateProductRecord(param *com |
|
@@ -79,18 +99,31 @@ func (productRecordService *ProductRecordService) CreateProductRecord(param *com |
79
|
ProductGroupId: 0,
|
99
|
ProductGroupId: 0,
|
80
|
EmployeeId: param.WorkerId,
|
100
|
EmployeeId: param.WorkerId,
|
81
|
UnitConversionId: 0,
|
101
|
UnitConversionId: 0,
|
82
|
- Weigh: 0.0,
|
|
|
83
|
- CreatedAt: time.Time{},
|
102
|
+ Weigh: param.Weigh,
|
|
|
103
|
+ CreatedAt: dataTime,
|
84
|
}
|
104
|
}
|
85
|
- _, err = productRecordDomainService.SubmitProductRecord(domain.RecordTypeSecondLevelWeigh, tool_funs.SimpleStructToMap(submitProductRecordCommand))
|
105
|
+ productRecordData, err := productRecordDomainService.SubmitProductRecord(domain.RecordTypeSecondLevelWeigh, tool_funs.SimpleStructToMap(submitProductRecordCommand))
|
86
|
if err != nil {
|
106
|
if err != nil {
|
87
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
107
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
88
|
}
|
108
|
}
|
|
|
109
|
+ //保存并审核
|
|
|
110
|
+ if param.SaveAndApprove {
|
|
|
111
|
+ svr, _ := domainService.NewPGProductRecordService(transactionContext.(*pgTransaction.TransactionContext))
|
|
|
112
|
+ _, err = svr.Approve(
|
|
|
113
|
+ productRecordData.ProductRecordId,
|
|
|
114
|
+ operateInfo.UserId,
|
|
|
115
|
+ productRecordData.ProductRecordInfo.WeighBefore,
|
|
|
116
|
+ time.Now())
|
|
|
117
|
+ if err != nil {
|
|
|
118
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
119
|
+ }
|
|
|
120
|
+ }
|
89
|
if err := transactionContext.CommitTransaction(); err != nil {
|
121
|
if err := transactionContext.CommitTransaction(); err != nil {
|
90
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
122
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
91
|
}
|
123
|
}
|
92
|
- return nil, nil
|
|
|
93
|
-
|
124
|
+ return map[string]interface{}{
|
|
|
125
|
+ "productRecordId": productRecordData.ProductRecordId,
|
|
|
126
|
+ }, nil
|
94
|
}
|
127
|
}
|
95
|
|
128
|
|
96
|
// 返回生产记录服务
|
129
|
// 返回生产记录服务
|
|
@@ -380,3 +413,184 @@ func (productRecordService *ProductRecordService) CancelProductRecord(cmd *comma |
|
@@ -380,3 +413,184 @@ func (productRecordService *ProductRecordService) CancelProductRecord(cmd *comma |
380
|
}
|
413
|
}
|
381
|
return struct{}{}, nil
|
414
|
return struct{}{}, nil
|
382
|
}
|
415
|
}
|
|
|
416
|
+
|
|
|
417
|
+//BatchAddProductRecord 从文件导入的数据,批量添加生产记录
|
|
|
418
|
+func (productRecordService *ProductRecordService) BatchAddProductRecord(operate *domain.OperateInfo, param []command.BatchAddProductRecordCommand) (
|
|
|
419
|
+ failRows []interface{}, err error) {
|
|
|
420
|
+ transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
421
|
+ if err != nil {
|
|
|
422
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
423
|
+ }
|
|
|
424
|
+ if err := transactionContext.StartTransaction(); err != nil {
|
|
|
425
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
426
|
+ }
|
|
|
427
|
+ defer func() {
|
|
|
428
|
+ transactionContext.RollbackTransaction()
|
|
|
429
|
+ }()
|
|
|
430
|
+
|
|
|
431
|
+ //获取当前操作人
|
|
|
432
|
+ userSrv := domainService.NewUserService()
|
|
|
433
|
+ operateUser, err := userSrv.User(operate.UserId)
|
|
|
434
|
+ if err != nil {
|
|
|
435
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, "查询操作人数据失败。"+err.Error())
|
|
|
436
|
+ }
|
|
|
437
|
+ //获取当前操作人的组织
|
|
|
438
|
+ var org *domain.Org
|
|
|
439
|
+ org, err = userSrv.Organization(operate.OrgId)
|
|
|
440
|
+ if err != nil {
|
|
|
441
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
442
|
+ }
|
|
|
443
|
+
|
|
|
444
|
+ //生产班组 数据
|
|
|
445
|
+ productGroupRepo, _ := factory.CreateProductGroupRepository(map[string]interface{}{
|
|
|
446
|
+ "transactionContext": transactionContext,
|
|
|
447
|
+ })
|
|
|
448
|
+
|
|
|
449
|
+ //生产计划数据
|
|
|
450
|
+ productPlanRepo, _ := factory.CreateProductPlanRepository(map[string]interface{}{
|
|
|
451
|
+ "transactionContext": transactionContext,
|
|
|
452
|
+ })
|
|
|
453
|
+
|
|
|
454
|
+ _, productGroupList, err := productGroupRepo.Find(map[string]interface{}{
|
|
|
455
|
+ "companyId": operate.CompanyId,
|
|
|
456
|
+ "orgId": operate.OrgId,
|
|
|
457
|
+ })
|
|
|
458
|
+ if err != nil {
|
|
|
459
|
+ return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
460
|
+ }
|
|
|
461
|
+ //车间名称+/+线别名称+/+工段名称 作为键名
|
|
|
462
|
+ workStationMap := map[string]*domain.WorkStation{}
|
|
|
463
|
+ //车间名称+/+工人名 作为键名
|
|
|
464
|
+ workerMap := map[string][]*domain.User{}
|
|
|
465
|
+ //班组名称 作为键名
|
|
|
466
|
+ productGroupMap := map[string]*domain.ProductGroup{}
|
|
|
467
|
+ for _, v := range productGroupList {
|
|
|
468
|
+ workStationName := strings.Join([]string{
|
|
|
469
|
+ v.WorkStation.WorkshopName, v.WorkStation.LineName, v.WorkStation.SectionName,
|
|
|
470
|
+ }, "/")
|
|
|
471
|
+ workStationMap[workStationName] = v.WorkStation
|
|
|
472
|
+ productGroupMap[v.GroupName] = v
|
|
|
473
|
+ for _, vv := range v.GroupMembers {
|
|
|
474
|
+ k := v.WorkStation.WorkshopName + "/" + vv.UserName
|
|
|
475
|
+ isIn := false
|
|
|
476
|
+ for _, vvv := range workerMap[k] {
|
|
|
477
|
+ if vvv.UserId == vv.UserId {
|
|
|
478
|
+ isIn = true
|
|
|
479
|
+ break
|
|
|
480
|
+ }
|
|
|
481
|
+ }
|
|
|
482
|
+ if !isIn {
|
|
|
483
|
+ workerMap[k] = append(workerMap[k], vv)
|
|
|
484
|
+ }
|
|
|
485
|
+ }
|
|
|
486
|
+ }
|
|
|
487
|
+
|
|
|
488
|
+ productRecordList := []*domain.ProductRecord{}
|
|
|
489
|
+ nowTime := time.Now()
|
|
|
490
|
+ for i := range param {
|
|
|
491
|
+ //检查字段
|
|
|
492
|
+ err = param[i].ValidField()
|
|
|
493
|
+ if err != nil {
|
|
|
494
|
+ param[i].FailReason = err.Error()
|
|
|
495
|
+ failRows = append(failRows, param[i])
|
|
|
496
|
+ continue
|
|
|
497
|
+ }
|
|
|
498
|
+ //检查日期格式
|
|
|
499
|
+ productDate, err := time.ParseInLocation("2006-01-02", param[i].CreatedDate, time.Local)
|
|
|
500
|
+ if err != nil {
|
|
|
501
|
+ param[i].FailReason = "日期格式错误,例 2006-01-02。"
|
|
|
502
|
+ failRows = append(failRows, param[i])
|
|
|
503
|
+ continue
|
|
|
504
|
+ }
|
|
|
505
|
+ //检查工位
|
|
|
506
|
+ var workStation *domain.WorkStation
|
|
|
507
|
+ workStationName := param[i].WorkerName + "/" + param[i].LineName + "/" + param[i].SectionName
|
|
|
508
|
+ if v, ok := workStationMap[workStationName]; ok {
|
|
|
509
|
+ workStation = v
|
|
|
510
|
+ } else {
|
|
|
511
|
+ param[i].FailReason = "车间、线别、工段不存在"
|
|
|
512
|
+ failRows = append(failRows, param[i])
|
|
|
513
|
+ continue
|
|
|
514
|
+ }
|
|
|
515
|
+ //获取生产班组
|
|
|
516
|
+ var productGroup *domain.ProductGroup
|
|
|
517
|
+ if v, ok := productGroupMap[param[i].ProductGroupName]; ok {
|
|
|
518
|
+ productGroup = v
|
|
|
519
|
+ } else {
|
|
|
520
|
+ param[i].FailReason = "班组不存在"
|
|
|
521
|
+ failRows = append(failRows, param[i])
|
|
|
522
|
+ continue
|
|
|
523
|
+ }
|
|
|
524
|
+ //检查员工姓名
|
|
|
525
|
+ var worker *domain.User
|
|
|
526
|
+ workKey := param[i].WorkshopName + "/" + param[i].WorkerName
|
|
|
527
|
+ if u, ok := workerMap[workKey]; ok {
|
|
|
528
|
+ if len(u) > 1 {
|
|
|
529
|
+ param[i].FailReason = "当前车间存在重复的用户名"
|
|
|
530
|
+ failRows = append(failRows, param[i])
|
|
|
531
|
+ continue
|
|
|
532
|
+ }
|
|
|
533
|
+ worker = u[0]
|
|
|
534
|
+ } else {
|
|
|
535
|
+ param[i].FailReason = "当前车间不存在用户" + param[i].WorkerName
|
|
|
536
|
+ failRows = append(failRows, param[i])
|
|
|
537
|
+ continue
|
|
|
538
|
+ }
|
|
|
539
|
+ //二级品重量
|
|
|
540
|
+ weigh, err := strconv.ParseFloat(param[i].Weigh, 64)
|
|
|
541
|
+ if err != nil {
|
|
|
542
|
+ param[i].FailReason = "二级品重量填写错误"
|
|
|
543
|
+ failRows = append(failRows, param[i])
|
|
|
544
|
+ continue
|
|
|
545
|
+ }
|
|
|
546
|
+ //按批次获取生产计划
|
|
|
547
|
+ productPlanData, err := productPlanRepo.FindOne(map[string]interface{}{
|
|
|
548
|
+ "batchNumber": param[i].BatchNumber,
|
|
|
549
|
+ "companyId": operate.CompanyId,
|
|
|
550
|
+ })
|
|
|
551
|
+ if err != nil {
|
|
|
552
|
+ param[i].FailReason = "批次号不存在"
|
|
|
553
|
+ failRows = append(failRows, param[i])
|
|
|
554
|
+ continue
|
|
|
555
|
+ }
|
|
|
556
|
+
|
|
|
557
|
+ tempItem := &domain.ProductRecord{
|
|
|
558
|
+ ProductRecordId: 0,
|
|
|
559
|
+ CompanyId: operate.CompanyId,
|
|
|
560
|
+ OrgId: operate.OrgId,
|
|
|
561
|
+ ProductRecordType: domain.RecordTypeSecondLevelWeigh,
|
|
|
562
|
+ ProductWorker: worker,
|
|
|
563
|
+ WorkStation: workStation,
|
|
|
564
|
+ CreatedAt: productDate,
|
|
|
565
|
+ UpdatedAt: nowTime,
|
|
|
566
|
+ DeletedAt: time.Time{},
|
|
|
567
|
+ ProductRecordInfo: &domain.ProductRecordInfo{
|
|
|
568
|
+ ProductDate: productDate.Local().Format("2006-01-02"),
|
|
|
569
|
+ Original: weigh,
|
|
|
570
|
+ Weigh: weigh,
|
|
|
571
|
+ WeighBefore: weigh,
|
|
|
572
|
+ WeighAfter: weigh,
|
|
|
573
|
+ ApproveStatus: domain.ProductRecordAutoApproved,
|
|
|
574
|
+ ApproveAt: nowTime.Unix(),
|
|
|
575
|
+ ApproveUser: operateUser,
|
|
|
576
|
+ UnitConversionId: 0,
|
|
|
577
|
+ ProductPlanId: productPlanData.ProductPlanId,
|
|
|
578
|
+ PlanProductName: productPlanData.PlanProductName,
|
|
|
579
|
+ BatchNumber: productPlanData.BatchNumber,
|
|
|
580
|
+ ProductGroupId: productGroup.ProductGroupId,
|
|
|
581
|
+ WorkOn: productGroup.WorkOn,
|
|
|
582
|
+ },
|
|
|
583
|
+ Ext: &domain.Ext{
|
|
|
584
|
+ Operator: operateUser,
|
|
|
585
|
+ OrgName: org.OrgName,
|
|
|
586
|
+ },
|
|
|
587
|
+ PreRecord: &domain.ProductRecord{},
|
|
|
588
|
+ }
|
|
|
589
|
+ productRecordList = append(productRecordList, tempItem)
|
|
|
590
|
+ }
|
|
|
591
|
+
|
|
|
592
|
+ if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
593
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
594
|
+ }
|
|
|
595
|
+ return nil, nil
|
|
|
596
|
+} |