|
|
package service
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"github.com/linmadan/egglib-go/utils/tool_funs"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/command"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/dto"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/query"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
// 生产记录服务
|
|
|
type ProductRecordService struct {
|
|
|
}
|
|
|
|
|
|
// 生产记录审核
|
|
|
func (productRecordService *ProductRecordService) ApproveProductRecord(cmd *command.ApproveProductRecordCommand) (interface{}, error) {
|
|
|
if err := cmd.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, 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()
|
|
|
}()
|
|
|
|
|
|
svr, err := domainService.NewPGProductRecordService(transactionContext.(*pgTransaction.TransactionContext))
|
|
|
if _, err = svr.Approve(cmd.ProductRecordId, cmd.ApproveUserId, cmd.WeighAfter, time.Now()); 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 struct{}{}, nil
|
|
|
}
|
|
|
|
|
|
// 创建生产记录服务
|
|
|
func (productRecordService *ProductRecordService) CreateProductRecord(createProductRecordCommand *command.CreateProductRecordCommand) (interface{}, error) {
|
|
|
if err := createProductRecordCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, 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()
|
|
|
}()
|
|
|
newProductRecord := &domain.ProductRecord{
|
|
|
ProductRecordId: createProductRecordCommand.ProductRecordId,
|
|
|
}
|
|
|
var productRecordRepository domain.ProductRecordRepository
|
|
|
if value, err := factory.CreateProductRecordRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
productRecordRepository = value
|
|
|
}
|
|
|
if productRecord, err := productRecordRepository.Save(newProductRecord); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return productRecord, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回生产记录服务
|
|
|
func (productRecordService *ProductRecordService) GetProductRecord(getProductRecordQuery *query.GetProductRecordQuery) (interface{}, error) {
|
|
|
if err := getProductRecordQuery.ValidateQuery(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, 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()
|
|
|
}()
|
|
|
var productRecordRepository domain.ProductRecordRepository
|
|
|
if value, err := factory.CreateProductRecordRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
productRecordRepository = value
|
|
|
}
|
|
|
productRecord, err := productRecordRepository.FindOne(map[string]interface{}{"productRecordId": getProductRecordQuery.ProductRecordId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if productRecord == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductRecordQuery.ProductRecordId)))
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
var result = &dto.ProductLevelTwoRecord{}
|
|
|
return result.LoadDto(productRecord, productRecord.OrgId), nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回生产记录服务列表
|
|
|
func (productRecordService *ProductRecordService) ListProductRecord(listProductRecordQuery *query.ListProductRecordQuery) (interface{}, error) {
|
|
|
if err := listProductRecordQuery.ValidateQuery(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, 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()
|
|
|
}()
|
|
|
var productRecordRepository domain.ProductRecordRepository
|
|
|
if value, err := factory.CreateProductRecordRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
productRecordRepository = value
|
|
|
}
|
|
|
if count, productRecords, err := productRecordRepository.Find(tool_funs.SimpleStructToMap(listProductRecordQuery)); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return map[string]interface{}{
|
|
|
"count": count,
|
|
|
"productRecords": productRecords,
|
|
|
}, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 移除生产记录服务
|
|
|
func (productRecordService *ProductRecordService) RemoveProductRecord(removeProductRecordCommand *command.RemoveProductRecordCommand) (interface{}, error) {
|
|
|
if err := removeProductRecordCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, 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()
|
|
|
}()
|
|
|
var productRecordRepository domain.ProductRecordRepository
|
|
|
if value, err := factory.CreateProductRecordRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
productRecordRepository = value
|
|
|
}
|
|
|
productRecord, err := productRecordRepository.FindOne(map[string]interface{}{"productRecordId": removeProductRecordCommand.ProductRecordId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if productRecord == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductRecordCommand.ProductRecordId)))
|
|
|
}
|
|
|
if productRecord, err := productRecordRepository.Remove(productRecord); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return productRecord, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新生产记录服务
|
|
|
func (productRecordService *ProductRecordService) UpdateProductRecord(updateProductRecordCommand *command.UpdateProductRecordCommand) (interface{}, error) {
|
|
|
if err := updateProductRecordCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, 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()
|
|
|
}()
|
|
|
var productRecordRepository domain.ProductRecordRepository
|
|
|
if value, err := factory.CreateProductRecordRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
productRecordRepository = value
|
|
|
}
|
|
|
productRecord, err := productRecordRepository.FindOne(map[string]interface{}{"productRecordId": updateProductRecordCommand.ProductRecordId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if productRecord == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductRecordCommand.ProductRecordId)))
|
|
|
}
|
|
|
if err := productRecord.Update(tool_funs.SimpleStructToMap(updateProductRecordCommand)); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
if productRecord, err := productRecordRepository.Save(productRecord); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return productRecord, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回生产记录服务列表
|
|
|
func (productRecordService *ProductRecordService) SearchProductRecord(operateInfo *domain.OperateInfo, listProductRecordQuery *query.SearchProductRecordQuery) (int64, interface{}, error) {
|
|
|
if err := listProductRecordQuery.ValidateQuery(); err != nil {
|
|
|
return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var productRecordRepository domain.ProductRecordRepository
|
|
|
productRecordRepository, _, _ = factory.FastPgProductRecord(transactionContext, 0)
|
|
|
|
|
|
count, productRecords, err := productRecordRepository.Find(utils.ObjectToMap(listProductRecordQuery))
|
|
|
if err != nil {
|
|
|
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
var result = make([]*dto.ProductLevelTwoRecord, 0)
|
|
|
for i := range productRecords {
|
|
|
item := productRecords[i]
|
|
|
newItem := &dto.ProductLevelTwoRecord{}
|
|
|
newItem.LoadDto(item, operateInfo.OrgId)
|
|
|
result = append(result, newItem)
|
|
|
}
|
|
|
return count, result, nil
|
|
|
}
|
|
|
|
|
|
// 生产记录统计
|
|
|
func (productRecordService *ProductRecordService) ProductRecordStatics(cmd *command.ProductRecordStaticsCommand) (interface{}, error) {
|
|
|
if err := cmd.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, 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()
|
|
|
}()
|
|
|
var _ domain.ProductRecordRepository
|
|
|
var productRecord *domain.ProductRecord = cmd.ProductRecord
|
|
|
//_,productRecord,err = factory.FastPgProductRecord(transactionContext,cmd.ProductRecordId)
|
|
|
//if err!=nil{
|
|
|
// log.Logger.Error(err.Error())
|
|
|
// return nil, nil
|
|
|
//}
|
|
|
//
|
|
|
if productRecord == nil {
|
|
|
return nil, nil
|
|
|
}
|
|
|
svr, _ := domainService.NewPGProductRecordService(transactionContext.(*pgTransaction.TransactionContext))
|
|
|
if _, err = svr.EmployeeProductStatics(productRecord); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if _, err = svr.WorkshopProductStatics(productRecord); 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
|
|
|
}
|
|
|
|
|
|
func NewProductRecordService(options map[string]interface{}) *ProductRecordService {
|
|
|
newProductRecordService := &ProductRecordService{}
|
|
|
return newProductRecordService
|
|
|
} |
...
|
...
|
|