作者 tangxvhui

更新

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