作者 yangfu

feat: 添加物料

... ... @@ -149,3 +149,11 @@ func CreateProductMaterialGroupRepository(options map[string]interface{}) (domai
}
return repository.NewProductMaterialGroupRepository(transactionContext)
}
func CreateProductMaterialRepository(options map[string]interface{}) (domain.ProductMaterialRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewProductMaterialRepository(transactionContext)
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type CreateProductMaterialCommand struct {
// 企业id
CompanyId int `cname:"企业id" json:"companyId" valid:"Required"`
// 组织ID
OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`
// 物料分组ID
ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"`
// 物料编码
MaterialNumber string `cname:"物料编码" json:"materialNumber" valid:"Required"`
// 物料名称
MaterialName string `cname:"物料名称" json:"materialName" valid:"Required"`
// 物料属性
MaterialAttribute string `cname:"物料属性" json:"materialAttribute" valid:"Required"`
// 物料类别
MaterialCategory string `cname:"物料类别" json:"materialCategory" valid:"Required"`
// 规格
Specification string `cname:"规格" json:"specification"`
// 单位
Unit string `cname:"单位" json:"unit"`
// 保质期 单位:天
ExpiredDay int `cname:"保质期 单位:天" json:"expiredDay"`
// 备注
Remark string `cname:"备注" json:"remark"`
}
func (createProductMaterialCommand *CreateProductMaterialCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (createProductMaterialCommand *CreateProductMaterialCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(createProductMaterialCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(createProductMaterialCommand).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
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type RemoveProductMaterialCommand struct {
// 物料ID
ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"`
}
func (removeProductMaterialCommand *RemoveProductMaterialCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeProductMaterialCommand *RemoveProductMaterialCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(removeProductMaterialCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(removeProductMaterialCommand).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
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type UpdateProductMaterialCommand struct {
// 物料ID
ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"`
}
func (updateProductMaterialCommand *UpdateProductMaterialCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateProductMaterialCommand *UpdateProductMaterialCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateProductMaterialCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(updateProductMaterialCommand).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
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type GetProductMaterialQuery struct {
// 物料ID
ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"`
}
func (getProductMaterialQuery *GetProductMaterialQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getProductMaterialQuery *GetProductMaterialQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getProductMaterialQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(getProductMaterialQuery).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
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type ListProductMaterialQuery struct {
// 查询偏离量
Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
// 查询限制
Limit int `cname:"查询限制" json:"limit" valid:"Required"`
// 物料分组ID
ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"`
// 物料名称
MaterialName string `cname:"物料名称" json:"materialName" valid:"Required"`
// 物料类别
MaterialCategory string `cname:"物料类别" json:"materialCategory" valid:"Required"`
}
func (listProductMaterialQuery *ListProductMaterialQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listProductMaterialQuery *ListProductMaterialQuery) 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
}
... ...
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/productMaterial/command"
"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"
)
// 生产物料服务
type ProductMaterialService struct {
}
// 创建生产物料服务
func (productMaterialService *ProductMaterialService) CreateProductMaterial(operateInfo *domain.OperateInfo, cmd *command.CreateProductMaterialCommand) (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()
}()
newProductMaterial := &domain.ProductMaterial{
CompanyId: cmd.CompanyId,
OrgId: cmd.OrgId,
ProductMaterialGroupId: cmd.ProductMaterialGroupId,
MaterialNumber: cmd.MaterialNumber,
MaterialName: cmd.MaterialName,
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,
},
}
//var productMaterial *domain.ProductMaterial
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
if _, err = materialService.AddMaterial(operateInfo, newProductMaterial); 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 (productMaterialService *ProductMaterialService) GetProductMaterial(getProductMaterialQuery *query.GetProductMaterialQuery) (interface{}, error) {
if err := getProductMaterialQuery.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 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": getProductMaterialQuery.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(getProductMaterialQuery.ProductMaterialId)))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return productMaterial, nil
}
}
// 返回生产物料服务列表
func (productMaterialService *ProductMaterialService) ListProductMaterial(listProductMaterialQuery *query.ListProductMaterialQuery) (interface{}, error) {
if err := listProductMaterialQuery.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 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
}
if count, productMaterials, err := productMaterialRepository.Find(tool_funs.SimpleStructToMap(listProductMaterialQuery)); 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,
"productMaterials": productMaterials,
}, nil
}
}
// 移除生产物料服务
func (productMaterialService *ProductMaterialService) RemoveProductMaterial(removeProductMaterialCommand *command.RemoveProductMaterialCommand) (interface{}, error) {
if err := removeProductMaterialCommand.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 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": removeProductMaterialCommand.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(removeProductMaterialCommand.ProductMaterialId)))
}
if productMaterial, err := productMaterialRepository.Remove(productMaterial); 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 productMaterial, nil
}
}
// 更新生产物料服务
func (productMaterialService *ProductMaterialService) UpdateProductMaterial(updateProductMaterialCommand *command.UpdateProductMaterialCommand) (interface{}, error) {
if err := updateProductMaterialCommand.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 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": updateProductMaterialCommand.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(updateProductMaterialCommand.ProductMaterialId)))
}
if err := productMaterial.Update(tool_funs.SimpleStructToMap(updateProductMaterialCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if productMaterial, err := productMaterialRepository.Save(productMaterial); 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 productMaterial, nil
}
}
func NewProductMaterialService(options map[string]interface{}) *ProductMaterialService {
newProductMaterialService := &ProductMaterialService{}
return newProductMaterialService
}
... ...
... ... @@ -7,3 +7,27 @@ type Material struct {
// 物料类别
MaterialCategory string `json:"materialCategory,omitempty"`
}
// 物料类别
type MaterialCategory struct {
//Id int `json:"id"`
Category string `json:"category"`
}
// 物料属性
type MaterialAttribute struct {
//Id int `json:"id"`
Attribute string `json:"attribute"`
}
// 物料扩展数据
type MaterialExt struct {
// 规格
Specification string `json:"specification"`
// 单位
Unit string `json:"unit"`
// 保质期 单位:天
ExpiredDay int `json:"expiredDay"`
// 备注
Remark string `json:"remark"`
}
... ...
package domain
import "time"
// 生产物料
type ProductMaterial struct {
// 物料ID
ProductMaterialId int `json:"productMaterialId"`
// 企业id
CompanyId int `json:"companyId"`
// 组织ID
OrgId int `json:"orgId"`
// 物料分组ID
ProductMaterialGroupId int `json:"productMaterialGroupId"`
// 物料编码
MaterialNumber string `json:"materialNumber"`
// 物料名称
MaterialName string `json:"materialName"`
// 物料属性
MaterialAttribute *MaterialAttribute `json:"materialAttribute"`
// 物料类别
MaterialCategory *MaterialCategory `json:"materialCategory"`
// 物料扩展
ProductMaterialExt *MaterialExt `json:"productMaterialExt"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt"`
// 删除时间
DeletedAt time.Time `json:"deletedAt"`
// 扩展
Ext *Ext `json:"ext"`
}
type ProductMaterialRepository interface {
Save(productMaterial *ProductMaterial) (*ProductMaterial, error)
Remove(productMaterial *ProductMaterial) (*ProductMaterial, error)
FindOne(queryOptions map[string]interface{}) (*ProductMaterial, error)
Find(queryOptions map[string]interface{}) (int64, []*ProductMaterial, error)
}
func (productMaterial *ProductMaterial) Identify() interface{} {
if productMaterial.ProductMaterialId == 0 {
return nil
}
return productMaterial.ProductMaterialId
}
func (productMaterial *ProductMaterial) Update(data map[string]interface{}) error {
return nil
}
... ...
... ... @@ -14,6 +14,50 @@ type PGMaterialService struct {
transactionContext *pgTransaction.TransactionContext
}
// 物料
func (ptr *PGMaterialService) AddMaterial(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)
productGroupRepository, _ = repository.NewProductGroupRepository(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
}
_, err = productGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productGroupId": item.ProductMaterialGroupId})
if err != nil {
return nil, err
}
if newProductMaterial, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialNumber": item.MaterialNumber}); err == nil || newProductMaterial != nil {
return nil, fmt.Errorf("物料编码已存在")
}
newProductMaterial = &domain.ProductMaterial{
CompanyId: item.CompanyId,
OrgId: item.OrgId,
ProductMaterialGroupId: item.ProductMaterialGroupId,
MaterialName: item.MaterialName,
MaterialNumber: item.MaterialNumber,
MaterialAttribute: item.MaterialAttribute,
MaterialCategory: item.MaterialCategory,
ProductMaterialExt: item.ProductMaterialExt,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Ext: domain.NewExt(org.OrgName).WithOperator(user),
}
newProductMaterial, err = productMaterialRepository.Save(newProductMaterial)
return newProductMaterial, err
}
// 物料分组
func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
var (
user *domain.User
... ...
... ... @@ -49,6 +49,7 @@ func init() {
(*models.DeviceRunningRecord)(nil),
(*models.WorkshopPlanCompletionRecord)(nil),
(*models.ProductMaterialGroup)(nil),
(*models.ProductMaterial)(nil),
} {
err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
Temp: false,
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"time"
)
type ProductMaterial struct {
tableName string `comment:"生产物料" pg:"manufacture.product_material"`
// 物料ID
ProductMaterialId int `comment:"物料ID" pg:"pk:product_material_id"`
// 企业id
CompanyId int `comment:"企业id"`
// 组织ID
OrgId int `comment:"组织ID"`
// 物料分组ID
ProductMaterialGroupId int `comment:"物料分组ID"`
// 物料编码
MaterialNumber string `comment:"物料编码"`
// 物料名称
MaterialName string `comment:"物料名称"`
// 物料属性
MaterialAttribute *domain.MaterialAttribute `comment:"物料属性"`
// 物料类别
MaterialCategory *domain.MaterialCategory `comment:"物料类别"`
// 物料扩展
ProductMaterialExt *domain.MaterialExt `comment:"物料扩展"`
// 创建时间
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
UpdatedAt time.Time `comment:"更新时间"`
// 删除时间
DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"`
// 扩展
Ext *domain.Ext `comment:"扩展"`
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
func TransformToProductMaterialDomainModelFromPgModels(productMaterialModel *models.ProductMaterial) (*domain.ProductMaterial, error) {
return &domain.ProductMaterial{
ProductMaterialId: productMaterialModel.ProductMaterialId,
CompanyId: productMaterialModel.CompanyId,
OrgId: productMaterialModel.OrgId,
ProductMaterialGroupId: productMaterialModel.ProductMaterialGroupId,
MaterialNumber: productMaterialModel.MaterialNumber,
MaterialName: productMaterialModel.MaterialName,
MaterialAttribute: productMaterialModel.MaterialAttribute,
MaterialCategory: productMaterialModel.MaterialCategory,
ProductMaterialExt: productMaterialModel.ProductMaterialExt,
CreatedAt: productMaterialModel.CreatedAt,
UpdatedAt: productMaterialModel.UpdatedAt,
DeletedAt: productMaterialModel.DeletedAt,
Ext: productMaterialModel.Ext,
}, nil
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
)
type ProductMaterialRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *ProductMaterialRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *ProductMaterialRepository) Save(productMaterial *domain.ProductMaterial) (*domain.ProductMaterial, error) {
sqlBuildFields := []string{
"product_material_id",
"company_id",
"org_id",
"product_material_group_id",
"material_number",
"material_name",
"material_attribute",
"material_category",
"product_material_ext",
"created_at",
"updated_at",
"deleted_at",
"ext",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_id", "deleted_at"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_id", "deleted_at"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_id", "deleted_at")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if productMaterial.Identify() == nil {
if _, err := tx.QueryOne(
pg.Scan(
&productMaterial.ProductMaterialId,
&productMaterial.CompanyId,
&productMaterial.OrgId,
&productMaterial.ProductMaterialGroupId,
&productMaterial.MaterialNumber,
&productMaterial.MaterialName,
&productMaterial.MaterialAttribute,
&productMaterial.MaterialCategory,
&productMaterial.ProductMaterialExt,
&productMaterial.CreatedAt,
&productMaterial.UpdatedAt,
&productMaterial.DeletedAt,
&productMaterial.Ext,
),
fmt.Sprintf("INSERT INTO manufacture.product_material (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
productMaterial.CompanyId,
productMaterial.OrgId,
productMaterial.ProductMaterialGroupId,
productMaterial.MaterialNumber,
productMaterial.MaterialName,
productMaterial.MaterialAttribute,
productMaterial.MaterialCategory,
productMaterial.ProductMaterialExt,
productMaterial.CreatedAt,
productMaterial.UpdatedAt,
productMaterial.Ext,
); err != nil {
return productMaterial, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&productMaterial.ProductMaterialId,
&productMaterial.CompanyId,
&productMaterial.OrgId,
&productMaterial.ProductMaterialGroupId,
&productMaterial.MaterialNumber,
&productMaterial.MaterialName,
&productMaterial.MaterialAttribute,
&productMaterial.MaterialCategory,
&productMaterial.ProductMaterialExt,
&productMaterial.CreatedAt,
&productMaterial.UpdatedAt,
&productMaterial.DeletedAt,
&productMaterial.Ext,
),
fmt.Sprintf("UPDATE manufacture.product_material SET %s WHERE product_material_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
productMaterial.CompanyId,
productMaterial.OrgId,
productMaterial.ProductMaterialGroupId,
productMaterial.MaterialNumber,
productMaterial.MaterialName,
productMaterial.MaterialAttribute,
productMaterial.MaterialCategory,
productMaterial.ProductMaterialExt,
productMaterial.CreatedAt,
productMaterial.UpdatedAt,
productMaterial.Ext,
productMaterial.Identify(),
); err != nil {
return productMaterial, err
}
}
return productMaterial, nil
}
func (repository *ProductMaterialRepository) Remove(productMaterial *domain.ProductMaterial) (*domain.ProductMaterial, error) {
tx := repository.transactionContext.PgTx
productMaterialModel := new(models.ProductMaterial)
productMaterialModel.ProductMaterialId = productMaterial.Identify().(int)
if _, err := tx.Model(productMaterialModel).WherePK().Delete(); err != nil {
return productMaterial, err
}
return productMaterial, nil
}
// query compose
// 1. company_id,material_number
func (repository *ProductMaterialRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProductMaterial, error) {
tx := repository.transactionContext.PgTx
productMaterialModel := new(models.ProductMaterial)
query := sqlbuilder.BuildQuery(tx.Model(productMaterialModel), queryOptions)
query.SetWhereByQueryOption("product_material.product_material_id = ?", "productMaterialId")
query.SetWhereByQueryOption("product_material.company_id = ?", "companyId")
query.SetWhereByQueryOption("product_material.material_number = ?", "materialNumber")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if productMaterialModel.ProductMaterialId == 0 {
return nil, nil
} else {
return transform.TransformToProductMaterialDomainModelFromPgModels(productMaterialModel)
}
}
func (repository *ProductMaterialRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProductMaterial, error) {
tx := repository.transactionContext.PgTx
var productMaterialModels []*models.ProductMaterial
productMaterials := make([]*domain.ProductMaterial, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productMaterialModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("product_material_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, productMaterials, err
} else {
for _, productMaterialModel := range productMaterialModels {
if productMaterial, err := transform.TransformToProductMaterialDomainModelFromPgModels(productMaterialModel); err != nil {
return 0, productMaterials, err
} else {
productMaterials = append(productMaterials, productMaterial)
}
}
return int64(count), productMaterials, nil
}
}
func NewProductMaterialRepository(transactionContext *pgTransaction.TransactionContext) (*ProductMaterialRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ProductMaterialRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/service"
)
type ProductMaterialController struct {
beego.BaseController
}
func (controller *ProductMaterialController) CreateProductMaterial() {
productMaterialService := service.NewProductMaterialService(nil)
cmd := &command.CreateProductMaterialCommand{}
controller.Unmarshal(cmd)
operateInfo := ParseOperateInfo(controller.BaseController)
cmd.CompanyId = operateInfo.CompanyId
cmd.OrgId = operateInfo.OrgId
data, err := productMaterialService.CreateProductMaterial(operateInfo, cmd)
controller.Response(data, err)
}
func (controller *ProductMaterialController) UpdateProductMaterial() {
productMaterialService := service.NewProductMaterialService(nil)
updateProductMaterialCommand := &command.UpdateProductMaterialCommand{}
controller.Unmarshal(updateProductMaterialCommand)
productMaterialId, _ := controller.GetInt(":productMaterialId")
updateProductMaterialCommand.ProductMaterialId = productMaterialId
data, err := productMaterialService.UpdateProductMaterial(updateProductMaterialCommand)
controller.Response(data, err)
}
func (controller *ProductMaterialController) GetProductMaterial() {
productMaterialService := service.NewProductMaterialService(nil)
getProductMaterialQuery := &query.GetProductMaterialQuery{}
productMaterialId, _ := controller.GetInt(":productMaterialId")
getProductMaterialQuery.ProductMaterialId = productMaterialId
data, err := productMaterialService.GetProductMaterial(getProductMaterialQuery)
controller.Response(data, err)
}
func (controller *ProductMaterialController) RemoveProductMaterial() {
productMaterialService := service.NewProductMaterialService(nil)
removeProductMaterialCommand := &command.RemoveProductMaterialCommand{}
controller.Unmarshal(removeProductMaterialCommand)
productMaterialId, _ := controller.GetInt(":productMaterialId")
removeProductMaterialCommand.ProductMaterialId = productMaterialId
data, err := productMaterialService.RemoveProductMaterial(removeProductMaterialCommand)
controller.Response(data, err)
}
func (controller *ProductMaterialController) ListProductMaterial() {
productMaterialService := service.NewProductMaterialService(nil)
listProductMaterialQuery := &query.ListProductMaterialQuery{}
offset, _ := controller.GetInt("offset")
listProductMaterialQuery.Offset = offset
limit, _ := controller.GetInt("limit")
listProductMaterialQuery.Limit = limit
data, err := productMaterialService.ListProductMaterial(listProductMaterialQuery)
controller.Response(data, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers"
)
func init() {
web.Router("/product-materials/", &controllers.ProductMaterialController{}, "Post:CreateProductMaterial")
web.Router("/product-materials/:productMaterialId", &controllers.ProductMaterialController{}, "Put:UpdateProductMaterial")
web.Router("/product-materials/:productMaterialId", &controllers.ProductMaterialController{}, "Get:GetProductMaterial")
web.Router("/product-materials/:productMaterialId", &controllers.ProductMaterialController{}, "Delete:RemoveProductMaterial")
web.Router("/product-materials/", &controllers.ProductMaterialController{}, "Get:ListProductMaterial")
}
... ...
package product_material
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
)
var _ = Describe("创建生产物料服务", func() {
Describe("提交数据创建生产物料服务", func() {
Context("提交正确的新生产物料数据", func() {
It("返回生产物料数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"productMaterialGroupId": "int",
"materialNumber": "string",
"materialName": "string",
"materialAttribute": "string",
"materialCategory": "string",
"productMaterialExt": "string",
}
httpExpect.POST("/product-materials/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("productMaterialId").ValueNotEqual("productMaterialId", BeZero())
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM product_materials WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package product_material
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
)
var _ = Describe("返回生产物料服务", func() {
var productMaterialId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&productMaterialId),
"INSERT INTO product_materials (product_material_id, company_id, org_id, product_material_group_id, material_number, material_name, material_attribute, material_category, product_material_ext, created_at, updated_at, deleted_at, ext) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_id",
"testProductMaterialId", "testCompanyId", "testOrgId", "testProductMaterialGroupId", "testMaterialNumber", "testMaterialName", "testMaterialAttribute", "testMaterialCategory", "testProductMaterialExt", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testExt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据productMaterialId参数返回生产物料", func() {
Context("传入有效的productMaterialId", func() {
It("返回生产物料数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/product-materials/{productMaterialId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM product_materials WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package product_material
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
)
var _ = Describe("返回生产物料服务列表", func() {
var productMaterialId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&productMaterialId),
"INSERT INTO product_materials (product_material_id, company_id, org_id, product_material_group_id, material_number, material_name, material_attribute, material_category, product_material_ext, created_at, updated_at, deleted_at, ext) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_id",
"testProductMaterialId", "testCompanyId", "testOrgId", "testProductMaterialGroupId", "testMaterialNumber", "testMaterialName", "testMaterialAttribute", "testMaterialCategory", "testProductMaterialExt", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testExt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回生产物料列表", func() {
Context("传入有效的参数", func() {
It("返回生产物料数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/product-materials/").
WithQuery("offset", "int").
WithQuery("limit", "int").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("count").ValueEqual("count", 1).
ContainsKey("productMaterials").Value("productMaterials").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM product_materials WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package product_material
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/beego/beego/v2/server/web"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego"
)
func TestProductMaterial(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port ProductMaterial Correlations Test Case Suite")
}
var handler http.Handler
var server *httptest.Server
var _ = BeforeSuite(func() {
handler = web.BeeApp.Handlers
server = httptest.NewServer(handler)
})
var _ = AfterSuite(func() {
server.Close()
})
... ...
package product_material
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
)
var _ = Describe("移除生产物料服务", func() {
var productMaterialId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&productMaterialId),
"INSERT INTO product_materials (product_material_id, company_id, org_id, product_material_group_id, material_number, material_name, material_attribute, material_category, product_material_ext, created_at, updated_at, deleted_at, ext) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_id",
"testProductMaterialId", "testCompanyId", "testOrgId", "testProductMaterialGroupId", "testMaterialNumber", "testMaterialName", "testMaterialAttribute", "testMaterialCategory", "testProductMaterialExt", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testExt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除生产物料服务", func() {
Context("传入有效的productMaterialId", func() {
It("返回被移除生产物料的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/product-materials/{productMaterialId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM product_materials WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package product_material
import (
"github.com/go-pg/pg/v10"
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
)
var _ = Describe("更新生产物料服务", func() {
var productMaterialId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&productMaterialId),
"INSERT INTO product_materials (product_material_id, company_id, org_id, product_material_group_id, material_number, material_name, material_attribute, material_category, product_material_ext, created_at, updated_at, deleted_at, ext) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_id",
"testProductMaterialId", "testCompanyId", "testOrgId", "testProductMaterialGroupId", "testMaterialNumber", "testMaterialName", "testMaterialAttribute", "testMaterialCategory", "testProductMaterialExt", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testExt")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新生产物料服务", func() {
Context("提交正确的生产物料数据", func() {
It("返回更新后的生产物料数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{}
httpExpect.PUT("/product-materials/{productMaterialId}").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("productMaterialId").ValueEqual("productMaterialId", productMaterialId)
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM product_materials WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...