...
|
...
|
@@ -6,8 +6,13 @@ import ( |
|
|
"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/product/command"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/dto"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/query"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
|
|
|
"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"
|
|
|
)
|
|
|
|
|
|
// 产品服务
|
...
|
...
|
@@ -29,22 +34,36 @@ func (productService *ProductService) CreateProduct(createProductCommand *comman |
|
|
defer func() {
|
|
|
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
|
|
|
}
|
|
|
|
|
|
newProduct := &domain.Product{
|
|
|
CompanyId: createProductCommand.CompanyId,
|
|
|
OrgId: createProductCommand.OrgId,
|
|
|
ProductCode: createProductCommand.ProductCode,
|
|
|
ProductName: createProductCommand.ProductName,
|
|
|
ProductCategory: createProductCommand.ProductCategory,
|
|
|
//ProductSpec: createProductCommand.ProductSpec,
|
|
|
}
|
|
|
var productRepository domain.ProductRepository
|
|
|
if value, err := factory.CreateProductRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
productRepository = value
|
|
|
ProductSpec: &domain.UnitQuantity{
|
|
|
Unit: createProductCommand.Unit,
|
|
|
UnitWeight: createProductCommand.UnitWeight,
|
|
|
},
|
|
|
CreatedAt: time.Now(),
|
|
|
UpdatedAt: time.Now(),
|
|
|
}
|
|
|
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 product, err := productRepository.Save(newProduct); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
...
|
...
|
@@ -88,7 +107,8 @@ func (productService *ProductService) GetProduct(getProductQuery *query.GetProdu |
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return product, nil
|
|
|
result := &dto.ProductDto{}
|
|
|
return result.LoadDto(product), nil
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -183,20 +203,23 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var productRepository domain.ProductRepository
|
|
|
if value, err := factory.CreateProductRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
productRepository = value
|
|
|
}
|
|
|
product, err := productRepository.FindOne(map[string]interface{}{"productId": updateProductCommand.ProductId})
|
|
|
productRepository, product, err := factory.FastPgProduct(transactionContext, updateProductCommand.ProductId)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if product == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductCommand.ProductId)))
|
|
|
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, "产品编号重复,请重新提交")
|
|
|
}
|
|
|
}
|
|
|
if err := product.Update(tool_funs.SimpleStructToMap(updateProductCommand)); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
...
|
...
|
@@ -211,6 +234,47 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman |
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回产品服务列表
|
|
|
func (productService *ProductService) SearchProduct(operateInfo *domain.OperateInfo, listProductQuery *query.SearchProductQuery) (int64, interface{}, error) {
|
|
|
listProductQuery.OrgId = operateInfo.OrgId
|
|
|
listProductQuery.CompanyId = operateInfo.CompanyId
|
|
|
if err := listProductQuery.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 productRepository domain.ProductRepository
|
|
|
if value, err := factory.CreateProductRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
productRepository = value
|
|
|
}
|
|
|
count, products, err := productRepository.Find(utils.ObjectToMap(listProductQuery))
|
|
|
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.ProductDto, 0)
|
|
|
for i := range products {
|
|
|
newItem := &dto.ProductDto{}
|
|
|
result = append(result, newItem.LoadDto(products[i]))
|
|
|
}
|
|
|
return count, result, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
func NewProductService(options map[string]interface{}) *ProductService {
|
|
|
newProductService := &ProductService{}
|
|
|
return newProductService
|
...
|
...
|
|