作者 tangxvhui

更新

1 # 生产制造项目说明 1 # 生产制造项目说明
2 2
  3 +- 开发环境服务器地址:http://allied-creation-manufacture-dev.fjmaimaimai.com
  4 +- 测试环境服务器地址: http://allied-creation-manufacture-test.fjmaimaimai.com
3 ## 1.数据导入导出模块 5 ## 1.数据导入导出模块
4 6
5 ### 1.1.Excel导入流程 7 ### 1.1.Excel导入流程
  1 +package command
  2 +
  3 +import "errors"
  4 +
  5 +//批量添加生产记录
  6 +type BatchAddProductRecordCommand struct {
  7 + CreatedDate string `json:"createdDate"` // 日期
  8 + WorkshopName string `json:"workshopName"` // 车间
  9 + LineName string `json:"lineName"` // 生产线
  10 + SectionName string `json:"sectionName"` // 工段
  11 + WorkerName string `json:"workerName"` // 工人名称
  12 + BatchNumber string `json:"batchNumber"` // 批次
  13 + ProductGroupName string `json:"productGroupName"` // 班组名称
  14 + Weigh string `json:"weigh"` // 重量
  15 + FailReason string `json:"failReason"` // 数据检查失败的原因
  16 +}
  17 +
  18 +func (cmd *BatchAddProductRecordCommand) ValidField() error {
  19 + if len(cmd.CreatedDate) == 0 {
  20 + return errors.New("日期未填写")
  21 + }
  22 + if len(cmd.WorkshopName) == 0 {
  23 + return errors.New("车间未填写")
  24 + }
  25 + if len(cmd.LineName) == 0 {
  26 + return errors.New("生产线未填写")
  27 + }
  28 + if len(cmd.SectionName) == 0 {
  29 + return errors.New("工段未填写")
  30 + }
  31 + if len(cmd.WorkerName) == 0 {
  32 + return errors.New("姓名未填写")
  33 + }
  34 + if len(cmd.BatchNumber) == 0 {
  35 + return errors.New("批次未填写")
  36 + }
  37 + if len(cmd.ProductGroupName) == 0 {
  38 + return errors.New("班组名称未填写")
  39 + }
  40 + if len(cmd.Weigh) == 0 {
  41 + return errors.New("重量未填写")
  42 + }
  43 + return nil
  44 +}
@@ -9,10 +9,6 @@ import ( @@ -9,10 +9,6 @@ import (
9 ) 9 )
10 10
11 type CreateProductRecordCommand struct { 11 type CreateProductRecordCommand struct {
12 - // 企业id  
13 - CompanyId int `cname:"企业id" json:"companyId" valid:"Required"`  
14 - // 组织ID  
15 - OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`  
16 // 车间ID 12 // 车间ID
17 WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"` 13 WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
18 // 生产线ID 14 // 生产线ID
@@ -20,14 +16,16 @@ type CreateProductRecordCommand struct { @@ -20,14 +16,16 @@ type CreateProductRecordCommand struct {
20 // 工段ID 16 // 工段ID
21 SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"` 17 SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"`
22 18
23 - WorkerId int `cname:"工人ID" json:"workId" valid:"Required"` 19 + WorkerId int `cname:"工人ID" json:"workerId" valid:"Required"`
24 20
25 //生气计划id 21 //生气计划id
26 ProductPlanId int `cname:"生气计划id" json:"productPlanId" valid:"Required"` 22 ProductPlanId int `cname:"生气计划id" json:"productPlanId" valid:"Required"`
27 23
28 CreatedDate string `cname:"日期" json:"createdDate" valid:"Required"` 24 CreatedDate string `cname:"日期" json:"createdDate" valid:"Required"`
29 //重量 25 //重量
30 - Weigh float64 `` 26 + Weigh float64 `cname:"重量" json:"weigh" valid:"Required" `
  27 + //保存并审核
  28 + SaveAndApprove bool `json:"saveAndApprove"`
31 } 29 }
32 30
33 func (createProductRecordCommand *CreateProductRecordCommand) Valid(validation *validation.Validation) { 31 func (createProductRecordCommand *CreateProductRecordCommand) Valid(validation *validation.Validation) {
@@ -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 +}
@@ -43,7 +43,7 @@ type SubmitOptions struct { @@ -43,7 +43,7 @@ type SubmitOptions struct {
43 } 43 }
44 44
45 // SubmitProductRecord 提交生产记录 45 // SubmitProductRecord 提交生产记录
46 -func (ptr *PGProductRecordService) SubmitProductRecord(productRecordType int, queryOptions map[string]interface{}) (interface{}, error) { 46 +func (ptr *PGProductRecordService) SubmitProductRecord(productRecordType int, queryOptions map[string]interface{}) (*domain.ProductRecord, error) {
47 var request = &SubmitOptions{} 47 var request = &SubmitOptions{}
48 if err := utils.LoadQueryObject(queryOptions, request); err != nil { 48 if err := utils.LoadQueryObject(queryOptions, request); err != nil {
49 return nil, err 49 return nil, err
@@ -156,7 +156,7 @@ func (ptr *PGProductRecordService) SubmitProductRecord(productRecordType int, qu @@ -156,7 +156,7 @@ func (ptr *PGProductRecordService) SubmitProductRecord(productRecordType int, qu
156 // return nil, err 156 // return nil, err
157 // } 157 // }
158 //} 158 //}
159 - return nil, nil 159 + return record, nil
160 } 160 }
161 161
162 // Approve 审核生产记录 (二级品审核) 162 // Approve 审核生产记录 (二级品审核)
@@ -13,9 +13,10 @@ type ProductRecordController struct { @@ -13,9 +13,10 @@ type ProductRecordController struct {
13 13
14 func (controller *ProductRecordController) CreateProductRecord() { 14 func (controller *ProductRecordController) CreateProductRecord() {
15 productRecordService := service.NewProductRecordService(nil) 15 productRecordService := service.NewProductRecordService(nil)
16 - createProductRecordCommand := &command.CreateProductRecordCommand{}  
17 - controller.Unmarshal(createProductRecordCommand)  
18 - data, err := productRecordService.CreateProductRecord(createProductRecordCommand) 16 + createCommand := &command.CreateProductRecordCommand{}
  17 + Must(controller.Unmarshal(createCommand))
  18 + operateInfo := ParseOperateInfo(controller.BaseController)
  19 + data, err := productRecordService.CreateProductRecord(operateInfo, createCommand)
19 controller.Response(data, err) 20 controller.Response(data, err)
20 } 21 }
21 22