...
|
...
|
@@ -14,7 +14,6 @@ import ( |
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
// 产品服务
|
...
|
...
|
@@ -22,7 +21,7 @@ type ProductService struct { |
|
|
}
|
|
|
|
|
|
// 创建产品服务
|
|
|
func (productService *ProductService) CreateProduct(createProductCommand *command.CreateProductCommand) (interface{}, error) {
|
|
|
func (productService *ProductService) CreateProduct(opt *domain.OperateInfo, createProductCommand *command.CreateProductCommand) (interface{}, error) {
|
|
|
if err := createProductCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
...
|
...
|
@@ -37,50 +36,23 @@ func (productService *ProductService) CreateProduct(createProductCommand *comman |
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
if len(createProductCommand.ProductCode) == 0 {
|
|
|
generator := redis.NewProductCodeCache(createProductCommand.CompanyId)
|
|
|
code, err := redis.GenCode(generator)
|
|
|
if err != nil {
|
|
|
log.Logger.Error(err.Error())
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
|
|
|
}
|
|
|
createProductCommand.ProductCode = code
|
|
|
batchAddProductService, _ := domainService.NewPGBatchAddProductService(transactionContext.(*pgTransaction.TransactionContext))
|
|
|
item := &domain.ImportProductItem{
|
|
|
ProductCode: createProductCommand.ProductCode,
|
|
|
UnitWeight: utils.AssertString(createProductCommand.UnitWeight),
|
|
|
}
|
|
|
var userService = domainService.NewUserService()
|
|
|
var org *domain.Org
|
|
|
org, err = userService.Organization(createProductCommand.OrgId)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
if _, err = batchAddProductService.BatchAddProduct(opt, []*domain.ImportProductItem{
|
|
|
item,
|
|
|
}, false); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
newProduct := &domain.Product{
|
|
|
CompanyId: createProductCommand.CompanyId,
|
|
|
OrgId: createProductCommand.OrgId,
|
|
|
ProductCode: createProductCommand.ProductCode,
|
|
|
ProductName: createProductCommand.ProductName,
|
|
|
ProductCategory: createProductCommand.ProductCategory,
|
|
|
ProductSpec: &domain.UnitQuantity{
|
|
|
Unit: createProductCommand.Unit,
|
|
|
UnitWeight: createProductCommand.UnitWeight,
|
|
|
},
|
|
|
CreatedAt: time.Now(),
|
|
|
UpdatedAt: time.Now(),
|
|
|
Ext: domain.NewExt(org.OrgName),
|
|
|
}
|
|
|
productRepository, _, _ := factory.FastPgProduct(transactionContext, 0)
|
|
|
|
|
|
if item, err := productRepository.FindOne(map[string]interface{}{"companyId": createProductCommand.CompanyId, "productCode": createProductCommand.ProductCode}); err == nil && item != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "产品编号重复,请重新提交")
|
|
|
if len(item.FailReason) > 0 {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, item.FailReason)
|
|
|
}
|
|
|
|
|
|
if product, err := productRepository.Save(newProduct); 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 product, nil
|
|
|
}
|
|
|
return struct{}{}, nil
|
|
|
}
|
|
|
|
|
|
// 返回产品服务
|
...
|
...
|
@@ -242,7 +214,7 @@ func (productService *ProductService) BatchRemoveProduct(cmd *command.BatchRemov |
|
|
}
|
|
|
|
|
|
// 更新产品服务
|
|
|
func (productService *ProductService) UpdateProduct(updateProductCommand *command.UpdateProductCommand) (interface{}, error) {
|
|
|
func (productService *ProductService) UpdateProduct(opt *domain.OperateInfo, updateProductCommand *command.UpdateProductCommand) (interface{}, error) {
|
|
|
if err := updateProductCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
...
|
...
|
@@ -260,19 +232,11 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman |
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if len(updateProductCommand.ProductCode) == 0 {
|
|
|
generator := redis.NewProductCodeCache(product.CompanyId)
|
|
|
code, err := redis.GenCode(generator)
|
|
|
if err != nil {
|
|
|
log.Logger.Error(err.Error())
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
|
|
|
}
|
|
|
updateProductCommand.ProductCode = code
|
|
|
}
|
|
|
if updateProductCommand.ProductCode != product.ProductCode {
|
|
|
if item, err := productRepository.FindOne(map[string]interface{}{"companyId": product.CompanyId, "productCode": updateProductCommand.ProductCode}); err == nil && item != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "产品编号重复,请重新提交")
|
|
|
}
|
|
|
|
|
|
var material *domain.ProductMaterial
|
|
|
productMaterialRepository, _, _ := factory.FastProductMaterial(transactionContext, 0)
|
|
|
if material, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": product.CompanyId, "materialNumber": product.ProductCode}); err != nil || material == nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, "物料不存在")
|
|
|
}
|
|
|
|
|
|
var userService = domainService.NewUserService()
|
...
|
...
|
@@ -281,8 +245,13 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman |
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
updateProductCommand.ProductName = material.MaterialName
|
|
|
updateProductCommand.Unit = material.ProductMaterialExt.Unit
|
|
|
updateProductCommand.ProductCategory = material.MaterialCategory.Category
|
|
|
data := tool_funs.SimpleStructToMap(updateProductCommand)
|
|
|
data["orgName"] = org.OrgName
|
|
|
|
|
|
if err := product.Update(data); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
...
|
...
|
|