作者 13655977079

feat:增加物料管理

... ... @@ -2,17 +2,16 @@ package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type CreateProductMaterialCommand struct {
// 企业id
CompanyId int `cname:"企业id" json:"companyId" valid:"Required"`
CompanyId int `cname:"企业id" json:"companyId"`
// 组织ID
OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`
OrgId int `cname:"组织ID" json:"orgId"`
// 物料分组ID
ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"`
// 物料编码
... ... @@ -24,9 +23,9 @@ type CreateProductMaterialCommand struct {
// 物料类别
MaterialCategory string `cname:"物料类别" json:"materialCategory" valid:"Required"`
// 规格
Specification string `cname:"规格" json:"specification"`
Specification string `cname:"规格" json:"specification" valid:"Required"`
// 单位
Unit string `cname:"单位" json:"unit"`
Unit string `cname:"单位" json:"unit" valid:"Required"`
// 保质期 单位:天
ExpiredDay int `cname:"保质期 单位:天" json:"expiredDay"`
// 备注
... ...
... ... @@ -10,9 +10,9 @@ import (
type UpdateProductMaterialCommand struct {
// 企业id
CompanyId int `cname:"企业id" json:"companyId"`
//CompanyId int `cname:"企业id" json:"companyId"`
// 组织ID
OrgId int `cname:"组织ID" json:"orgId"`
//OrgId int `cname:"组织ID" json:"orgId"`
// 物料ID
ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"`
// 物料分组ID
... ...
package dto
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
type DtoMaterial struct {
ProductMaterialId int `json:"productMaterialId"` // 物料ID
MaterialNumber string `json:"materialNumber"` //物料编码
MaterialName string `json:"materialName"` //物料名称
Specification string `json:"specification"` //规格型号
MaterialCategory string `json:"materialCategory"` //物料类别
Unit string `json:"unit"` //单位
MaterialAttribute string `json:"materialAttribute"` //物料属性
ProductMaterialGroupId int `json:"productMaterialGroupId"` //物料分组 string/string/string
ExpiredDay int `json:"expiredDay"` //保质期
Remark string `json:"remark"` //备注
ProductMaterialGroupIdName []*domain.ProductMaterialGroup
ProductMaterialId int `json:"productMaterialId"` // 物料ID
MaterialNumber string `json:"materialNumber"` //物料编码
MaterialName string `json:"materialName"` //物料名称
Specification string `json:"specification"` //规格型号
MaterialCategory string `json:"materialCategory"` //物料类别
Unit string `json:"unit"` //单位
MaterialAttribute string `json:"materialAttribute"` //物料属性
ProductMaterialGroupId int `json:"productMaterialGroupId"` //物料分组 string/string/string
ExpiredDay int `json:"expiredDay"` //保质期
Remark string `json:"remark"` //备注
ProductMaterialGroups []int `json:"productMaterialGroups"`
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type SearchProductMaterialQuery struct {
PageNumber int64 `json:"pageNumber"`
PageSize int64 `json:"pageSize"`
// 物料分组ID
ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId"`
// 物料分组ID数组
ProductMaterialGroupIds []int
// 物料名称
MaterialName string `cname:"物料名称" json:"materialName"`
// 物料类别
MaterialCategory string `cname:"物料类别" json:"materialCategory"`
CompanyId int
}
func (listProductMaterialQuery *SearchProductMaterialQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listProductMaterialQuery *SearchProductMaterialQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listProductMaterialQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(listProductMaterialQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -11,6 +11,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
"time"
)
// 生产物料服务
... ... @@ -90,15 +91,19 @@ func (productMaterialService *ProductMaterialService) GetProductMaterial(operate
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductMaterialQuery.ProductMaterialId)))
} else {
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
ProductMaterialGroupIdName, _, err := materialService.AllMaterialGroupParent(operateInfo, productMaterial.ProductMaterialGroupId)
productMaterialGroupIdNames, _, err := materialService.AllMaterialGroupParent(operateInfo, productMaterial.ProductMaterialGroupId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(ProductMaterialGroupIdName) == 0 {
if len(productMaterialGroupIdNames) == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var productMaterialGroups []int
for _, productMaterialGroupIdName := range productMaterialGroupIdNames {
productMaterialGroups = append(productMaterialGroups, productMaterialGroupIdName.ProductMaterialGroupId)
}
one := dto.DtoProductMaterial(productMaterial)
one.ProductMaterialGroupIdName = ProductMaterialGroupIdName
one.ProductMaterialGroups = productMaterialGroups
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ... @@ -156,7 +161,7 @@ func (productMaterialService *ProductMaterialService) ListProductMaterial(operat
}
// 返回生产物料服务列表
func (productMaterialService *ProductMaterialService) SearchProductMaterial(operateInfo *domain.OperateInfo, listProductMaterialQuery *query.ListProductMaterialQuery) (int64, []*dto.DtoMaterial, error) {
func (productMaterialService *ProductMaterialService) SearchProductMaterial(operateInfo *domain.OperateInfo, listProductMaterialQuery *query.SearchProductMaterialQuery) (int64, []*dto.DtoMaterial, error) {
if err := listProductMaterialQuery.ValidateQuery(); err != nil {
return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
... ... @@ -178,7 +183,7 @@ func (productMaterialService *ProductMaterialService) SearchProductMaterial(oper
} else {
productMaterialRepository = value
}
var results []*dto.DtoMaterial
results := make([]*dto.DtoMaterial, 0)
if listProductMaterialQuery.ProductMaterialGroupId != 0 {
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
_, ProductMaterialGroupIds, err := materialService.AllMaterialGroupChild(operateInfo, listProductMaterialQuery.ProductMaterialGroupId)
... ... @@ -256,45 +261,30 @@ func (productMaterialService *ProductMaterialService) UpdateProductMaterial(oper
defer func() {
transactionContext.RollbackTransaction()
}()
var productMaterialRepository domain.ProductMaterialRepository
if value, err := factory.CreateProductMaterialRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productMaterialRepository = value
}
productMaterial, err := productMaterialRepository.FindOne(map[string]interface{}{"productMaterialId": cmd.ProductMaterialId})
productMaterialRepository, productMaterial, err := factory.FastProductMaterial(transactionContext, cmd.ProductMaterialId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if productMaterial == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(cmd.ProductMaterialId)))
}
newProductMaterial := &domain.ProductMaterial{ //赋值
ProductMaterialId: cmd.ProductMaterialId,
ProductMaterialGroupId: cmd.ProductMaterialGroupId,
MaterialName: cmd.MaterialName,
MaterialNumber: productMaterial.MaterialNumber,
MaterialAttribute: &domain.MaterialAttribute{Attribute: cmd.MaterialAttribute},
MaterialCategory: &domain.MaterialCategory{Category: cmd.MaterialCategory},
ProductMaterialExt: &domain.MaterialExt{
Specification: cmd.Specification,
Unit: cmd.Unit,
ExpiredDay: cmd.ExpiredDay,
Remark: cmd.Remark,
},
CreatedAt: productMaterial.CreatedAt,
}
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
data, err := materialService.UpdateMaterial(operateInfo, newProductMaterial)
if err != nil {
productMaterial.ProductMaterialGroupId = cmd.ProductMaterialGroupId
productMaterial.MaterialName = cmd.MaterialName
productMaterial.MaterialAttribute.Attribute = cmd.MaterialAttribute
productMaterial.MaterialCategory.Category = cmd.MaterialCategory
productMaterial.ProductMaterialExt = &domain.MaterialExt{
Specification: cmd.Specification,
Unit: cmd.Unit,
ExpiredDay: cmd.ExpiredDay,
Remark: cmd.Remark,
}
productMaterial.UpdatedAt = time.Now()
if _, err := productMaterialRepository.Save(productMaterial); 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 data, nil
return struct{}{}, nil
}
// 批量移除生产物料服务
... ...
... ... @@ -56,39 +56,6 @@ func (ptr *PGMaterialService) AddMaterial(opt *domain.OperateInfo, item *domain.
return aaa, err
}
func (ptr *PGMaterialService) UpdateMaterial(opt *domain.OperateInfo, item *domain.ProductMaterial) (*domain.ProductMaterial, error) {
var (
user *domain.User
org *domain.Org
err error
newProductMaterial *domain.ProductMaterial
productMaterialRepository, _ = repository.NewProductMaterialRepository(ptr.transactionContext)
//productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
)
if user, err = NewUserService().User(opt.UserId); err != nil {
return nil, err
}
if org, err = NewUserService().Organization(opt.OrgId); err != nil {
return nil, err
}
newProductMaterial = &domain.ProductMaterial{
CompanyId: opt.CompanyId,
OrgId: opt.OrgId,
ProductMaterialId: item.ProductMaterialId,
ProductMaterialGroupId: item.ProductMaterialGroupId,
MaterialName: item.MaterialName,
MaterialNumber: item.MaterialNumber,
MaterialAttribute: item.MaterialAttribute,
MaterialCategory: item.MaterialCategory,
ProductMaterialExt: item.ProductMaterialExt,
CreatedAt: item.CreatedAt,
UpdatedAt: time.Now(),
Ext: domain.NewExt(org.OrgName).WithOperator(user),
}
aaa, err := productMaterialRepository.Save(newProductMaterial)
return aaa, err
}
// 物料分组
func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
... ...
... ... @@ -164,7 +164,7 @@ func (repository *ProductMaterialRepository) Find(queryOptions map[string]interf
query.Where("material_name like ?", fmt.Sprintf("%%%v%%", v))
}
if v, ok := queryOptions["materialCategory"]; ok && v != "" {
query.Where("material_category like ?", fmt.Sprintf("%%%v%%", v))
query.Where(fmt.Sprintf(`material_category->>'category' like '%%%v%%'`, v))
}
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_material_id", "DESC")
... ...
... ... @@ -29,8 +29,8 @@ func (controller *ProductMaterialController) UpdateProductMaterial() {
productMaterialId, _ := controller.GetInt(":productMaterialId")
updateProductMaterialCommand.ProductMaterialId = productMaterialId
operateInfo := ParseOperateInfo(controller.BaseController)
updateProductMaterialCommand.CompanyId = operateInfo.CompanyId
updateProductMaterialCommand.OrgId = operateInfo.OrgId
//updateProductMaterialCommand.CompanyId = operateInfo.CompanyId
//updateProductMaterialCommand.OrgId = operateInfo.OrgId
data, err := productMaterialService.UpdateProductMaterial(operateInfo, updateProductMaterialCommand)
controller.Response(data, err)
}
... ... @@ -67,7 +67,7 @@ func (controller *ProductMaterialController) ListProductMaterial() {
func (controller *ProductMaterialController) SearchProductMaterial() {
productMaterialService := service.NewProductMaterialService(nil)
listProductMaterialQuery := &query.ListProductMaterialQuery{}
listProductMaterialQuery := &query.SearchProductMaterialQuery{}
controller.Unmarshal(listProductMaterialQuery)
operateInfo := ParseOperateInfo(controller.BaseController)
total, data, err := productMaterialService.SearchProductMaterial(operateInfo, listProductMaterialQuery)
... ...