...
|
...
|
@@ -2,6 +2,8 @@ package service |
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
...
|
...
|
@@ -54,7 +56,7 @@ func (productRecordService *ProductRecordService) ApproveProductRecord(cmd *comm |
|
|
}
|
|
|
|
|
|
// 创建生产记录服务
|
|
|
func (productRecordService *ProductRecordService) CreateProductRecord(param *command.CreateProductRecordCommand) (interface{}, error) {
|
|
|
func (productRecordService *ProductRecordService) CreateProductRecord(operateInfo *domain.OperateInfo, param *command.CreateProductRecordCommand) (interface{}, error) {
|
|
|
if err := param.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
...
|
...
|
@@ -68,10 +70,28 @@ func (productRecordService *ProductRecordService) CreateProductRecord(param *com |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
dataTime, err := time.ParseInLocation("2006-01-02", param.CreatedDate, time.Local)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, "日期格式错误")
|
|
|
}
|
|
|
|
|
|
productPlanRepo, _ := factory.CreateProductPlanRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
})
|
|
|
|
|
|
_, err = productPlanRepo.FindOne(map[string]interface{}{
|
|
|
"product_plan_id": param.ProductPlanId,
|
|
|
"company_id": operateInfo.CompanyId,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, "生产计划id错误,"+err.Error())
|
|
|
}
|
|
|
|
|
|
productRecordDomainService, _ := domainService.NewPGProductRecordService(transactionContext.(*pg.TransactionContext))
|
|
|
submitProductRecordCommand := domainService.SubmitOptions{
|
|
|
CompanyId: param.CompanyId,
|
|
|
OrgId: param.OrgId,
|
|
|
CompanyId: operateInfo.CompanyId,
|
|
|
OrgId: operateInfo.OrgId,
|
|
|
ProductPlanId: param.ProductPlanId,
|
|
|
WorkshopId: param.WorkshopId,
|
|
|
LineId: param.LineId,
|
...
|
...
|
@@ -79,18 +99,31 @@ func (productRecordService *ProductRecordService) CreateProductRecord(param *com |
|
|
ProductGroupId: 0,
|
|
|
EmployeeId: param.WorkerId,
|
|
|
UnitConversionId: 0,
|
|
|
Weigh: 0.0,
|
|
|
CreatedAt: time.Time{},
|
|
|
Weigh: param.Weigh,
|
|
|
CreatedAt: dataTime,
|
|
|
}
|
|
|
_, err = productRecordDomainService.SubmitProductRecord(domain.RecordTypeSecondLevelWeigh, tool_funs.SimpleStructToMap(submitProductRecordCommand))
|
|
|
productRecordData, err := productRecordDomainService.SubmitProductRecord(domain.RecordTypeSecondLevelWeigh, tool_funs.SimpleStructToMap(submitProductRecordCommand))
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
//保存并审核
|
|
|
if param.SaveAndApprove {
|
|
|
svr, _ := domainService.NewPGProductRecordService(transactionContext.(*pgTransaction.TransactionContext))
|
|
|
_, err = svr.Approve(
|
|
|
productRecordData.ProductRecordId,
|
|
|
operateInfo.UserId,
|
|
|
productRecordData.ProductRecordInfo.WeighBefore,
|
|
|
time.Now())
|
|
|
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())
|
|
|
}
|
|
|
return nil, nil
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
"productRecordId": productRecordData.ProductRecordId,
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
// 返回生产记录服务
|
...
|
...
|
@@ -380,3 +413,184 @@ func (productRecordService *ProductRecordService) CancelProductRecord(cmd *comma |
|
|
}
|
|
|
return struct{}{}, nil
|
|
|
}
|
|
|
|
|
|
//BatchAddProductRecord 从文件导入的数据,批量添加生产记录
|
|
|
func (productRecordService *ProductRecordService) BatchAddProductRecord(operate *domain.OperateInfo, param []command.BatchAddProductRecordCommand) (
|
|
|
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 param {
|
|
|
//检查字段
|
|
|
err = param[i].ValidField()
|
|
|
if err != nil {
|
|
|
param[i].FailReason = err.Error()
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//检查日期格式
|
|
|
productDate, err := time.ParseInLocation("2006-01-02", param[i].CreatedDate, time.Local)
|
|
|
if err != nil {
|
|
|
param[i].FailReason = "日期格式错误,例 2006-01-02。"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//检查工位
|
|
|
var workStation *domain.WorkStation
|
|
|
workStationName := param[i].WorkerName + "/" + param[i].LineName + "/" + param[i].SectionName
|
|
|
if v, ok := workStationMap[workStationName]; ok {
|
|
|
workStation = v
|
|
|
} else {
|
|
|
param[i].FailReason = "车间、线别、工段不存在"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//获取生产班组
|
|
|
var productGroup *domain.ProductGroup
|
|
|
if v, ok := productGroupMap[param[i].ProductGroupName]; ok {
|
|
|
productGroup = v
|
|
|
} else {
|
|
|
param[i].FailReason = "班组不存在"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//检查员工姓名
|
|
|
var worker *domain.User
|
|
|
workKey := param[i].WorkshopName + "/" + param[i].WorkerName
|
|
|
if u, ok := workerMap[workKey]; ok {
|
|
|
if len(u) > 1 {
|
|
|
param[i].FailReason = "当前车间存在重复的用户名"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
worker = u[0]
|
|
|
} else {
|
|
|
param[i].FailReason = "当前车间不存在用户" + param[i].WorkerName
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//二级品重量
|
|
|
weigh, err := strconv.ParseFloat(param[i].Weigh, 64)
|
|
|
if err != nil {
|
|
|
param[i].FailReason = "二级品重量填写错误"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
//按批次获取生产计划
|
|
|
productPlanData, err := productPlanRepo.FindOne(map[string]interface{}{
|
|
|
"batchNumber": param[i].BatchNumber,
|
|
|
"companyId": operate.CompanyId,
|
|
|
})
|
|
|
if err != nil {
|
|
|
param[i].FailReason = "批次号不存在"
|
|
|
failRows = append(failRows, param[i])
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
tempItem := &domain.ProductRecord{
|
|
|
ProductRecordId: 0,
|
|
|
CompanyId: operate.CompanyId,
|
|
|
OrgId: operate.OrgId,
|
|
|
ProductRecordType: domain.RecordTypeSecondLevelWeigh,
|
|
|
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: 0,
|
|
|
ProductPlanId: productPlanData.ProductPlanId,
|
|
|
PlanProductName: productPlanData.PlanProductName,
|
|
|
BatchNumber: productPlanData.BatchNumber,
|
|
|
ProductGroupId: productGroup.ProductGroupId,
|
|
|
WorkOn: productGroup.WorkOn,
|
|
|
},
|
|
|
Ext: &domain.Ext{
|
|
|
Operator: operateUser,
|
|
|
OrgName: org.OrgName,
|
|
|
},
|
|
|
PreRecord: &domain.ProductRecord{},
|
|
|
}
|
|
|
productRecordList = append(productRecordList, tempItem)
|
|
|
}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, nil
|
|
|
} |
...
|
...
|
|