作者 yangfu

feat: 物料

正在显示 27 个修改的文件 包含 1394 行增加2 行删除
... ... @@ -413,6 +413,32 @@ func FastPgDeviceDailyRunningRecord(transactionContext application.TransactionCo
return rep, mod, err
}
// FastProductMaterialGroup 快速返回物料
//
// transactionContext 事务
// id 对象唯一标识
func FastProductMaterialGroup(transactionContext application.TransactionContext, id int, options ...option) (domain.ProductMaterialGroupRepository, *domain.ProductMaterialGroup, error) {
var rep domain.ProductMaterialGroupRepository
var mod *domain.ProductMaterialGroup
var err error
if value, err := CreateProductMaterialGroupRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if id > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"productMaterialGroupId": id}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该物料分组不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
/***** 2.配置 *****/
type FastOptions struct {
... ...
... ... @@ -141,3 +141,11 @@ func CreateDeviceDailyRunningRecordRepository(options map[string]interface{}) (d
}
return repository.NewDeviceDailyRunningRecordRepository(transactionContext)
}
func CreateProductMaterialGroupRepository(options map[string]interface{}) (domain.ProductMaterialGroupRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewProductMaterialGroupRepository(transactionContext)
}
... ...
package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type CreateProductMaterialGroupCommand struct {
// 企业id
CompanyId int `cname:"企业id" json:"companyId" valid:"Required"`
// 组织ID
OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`
// 父级ID
Pid int `cname:"父级ID" json:"pid"`
// 物料分组名称
MaterialGroupName string `cname:"物料分组名称" json:"materialGroupName" valid:"Required"`
// 物料分组编码
MaterialGroupNumber string `cname:"物料分组编码" json:"materialGroupNumber,omitempty"`
}
func (createProductMaterialGroupCommand *CreateProductMaterialGroupCommand) Valid(validation *validation.Validation) {
}
func (createProductMaterialGroupCommand *CreateProductMaterialGroupCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(createProductMaterialGroupCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(createProductMaterialGroupCommand).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 RemoveProductMaterialGroupCommand struct {
// 物料分组ID
ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"`
}
func (removeProductMaterialGroupCommand *RemoveProductMaterialGroupCommand) Valid(validation *validation.Validation) {
}
func (removeProductMaterialGroupCommand *RemoveProductMaterialGroupCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(removeProductMaterialGroupCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(removeProductMaterialGroupCommand).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 UpdateProductMaterialGroupCommand struct {
// 物料分组ID
ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"`
// 物料分组名称
MaterialGroupName string `cname:"物料分组名称" json:"materialGroupName" valid:"Required"`
// 物料分组编码
MaterialGroupNumber string `cname:"物料分组编码" json:"materialGroupNumber" valid:"Required"`
}
func (updateProductMaterialGroupCommand *UpdateProductMaterialGroupCommand) Valid(validation *validation.Validation) {
}
func (updateProductMaterialGroupCommand *UpdateProductMaterialGroupCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateProductMaterialGroupCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(updateProductMaterialGroupCommand).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 dto
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
type MaterialGroupDto struct {
// 物料分组ID
ProductMaterialGroupId int `json:"id"`
// 企业id
//CompanyId int `json:"companyId"`
// 组织ID
//OrgId int `json:"orgId"`
// 父级ID
Pid int `json:"pid"`
// 路径 (不使用,如果父级改变的话,子级的Path要做更新)
//Path string `json:"path"`
// 物料分组名称
MaterialGroupName string `json:"materialGroupName"`
// 物料分组编码
MaterialGroupNumber string `json:"materialGroupNumber"`
}
func (d *MaterialGroupDto) LoadDto(m *domain.ProductMaterialGroup, orgId int) *MaterialGroupDto {
d.ProductMaterialGroupId = m.ProductMaterialGroupId
d.Pid = m.Pid
d.MaterialGroupName = m.MaterialGroupName
d.MaterialGroupNumber = m.MaterialGroupNumber
return d
}
... ...
package query
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type GetProductMaterialGroupQuery struct {
// 物料分组ID
ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"`
}
func (getProductMaterialGroupQuery *GetProductMaterialGroupQuery) Valid(validation *validation.Validation) {
}
func (getProductMaterialGroupQuery *GetProductMaterialGroupQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getProductMaterialGroupQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(getProductMaterialGroupQuery).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 ListProductMaterialGroupQuery struct {
// 查询偏离量
Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
// 查询限制
Limit int `cname:"查询限制" json:"limit" valid:"Required"`
}
func (listProductMaterialGroupQuery *ListProductMaterialGroupQuery) Valid(validation *validation.Validation) {
}
func (listProductMaterialGroupQuery *ListProductMaterialGroupQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listProductMaterialGroupQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(listProductMaterialGroupQuery).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"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type SearchProductMaterialGroupQuery struct {
// 查询偏离量
Offset int `cname:"查询偏离量" json:"offset"`
// 查询限制
Limit int `cname:"查询限制" json:"limit"`
// 当前公司
CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
// 当前登录的组织
//OrgId int `cname:"当前登录的组织" json:"orgId,omitempty"`
// 匹配多个组织
//InOrgIds []int `cname:"匹配多个组织" json:"inOrgIds,omitempty" valid:"Required"`
// 页码
PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
// 页数
PageSize int `cname:"页数" json:"pageSize,omitempty"`
}
func (cmd *SearchProductMaterialGroupQuery) Valid(validation *validation.Validation) {
cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
}
func (cmd *SearchProductMaterialGroupQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(cmd)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(cmd).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/productMaterialGroup/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/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"
)
// 物料分组服务
type ProductMaterialGroupService struct {
}
// 创建物料分组服务
func (productMaterialGroupService *ProductMaterialGroupService) CreateProductMaterialGroup(operateInfo *domain.OperateInfo, createProductMaterialGroupCommand *command.CreateProductMaterialGroupCommand) (interface{}, error) {
if err := createProductMaterialGroupCommand.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()
}()
newProductMaterialGroup := &domain.ProductMaterialGroup{
Pid: createProductMaterialGroupCommand.Pid,
MaterialGroupName: createProductMaterialGroupCommand.MaterialGroupName,
MaterialGroupNumber: createProductMaterialGroupCommand.MaterialGroupNumber,
}
var productMaterialGroup *domain.ProductMaterialGroup
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
if productMaterialGroup, err = materialService.AddMaterialGroup(operateInfo, newProductMaterialGroup); 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 productMaterialGroup, nil
}
// 返回物料分组服务
func (productMaterialGroupService *ProductMaterialGroupService) GetProductMaterialGroup(getProductMaterialGroupQuery *query.GetProductMaterialGroupQuery) (interface{}, error) {
if err := getProductMaterialGroupQuery.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 productMaterialGroupRepository domain.ProductMaterialGroupRepository
if value, err := factory.CreateProductMaterialGroupRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productMaterialGroupRepository = value
}
productMaterialGroup, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"productMaterialGroupId": getProductMaterialGroupQuery.ProductMaterialGroupId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if productMaterialGroup == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductMaterialGroupQuery.ProductMaterialGroupId)))
}
//测试
//materialService,_:= domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
//nodes,child,_:=materialService.AllMaterialGroupChild(&domain.OperateInfo{CompanyId: productMaterialGroup.CompanyId},productMaterialGroup.ProductMaterialGroupId)
//log.Logger.Debug("节点的Child", map[string]interface{}{"data":child,"nodes":nodes})
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
materialGroupDto := dto.MaterialGroupDto{}
return materialGroupDto.LoadDto(productMaterialGroup, 0), nil
}
// 返回物料分组服务列表
func (productMaterialGroupService *ProductMaterialGroupService) ListProductMaterialGroup(listProductMaterialGroupQuery *query.ListProductMaterialGroupQuery) (interface{}, error) {
if err := listProductMaterialGroupQuery.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 productMaterialGroupRepository domain.ProductMaterialGroupRepository
if value, err := factory.CreateProductMaterialGroupRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productMaterialGroupRepository = value
}
if count, productMaterialGroups, err := productMaterialGroupRepository.Find(tool_funs.SimpleStructToMap(listProductMaterialGroupQuery)); 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,
"productMaterialGroups": productMaterialGroups,
}, nil
}
}
// 移除物料分组服务
func (productMaterialGroupService *ProductMaterialGroupService) RemoveProductMaterialGroup(removeProductMaterialGroupCommand *command.RemoveProductMaterialGroupCommand) (interface{}, error) {
if err := removeProductMaterialGroupCommand.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 productMaterialGroupRepository domain.ProductMaterialGroupRepository
if value, err := factory.CreateProductMaterialGroupRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productMaterialGroupRepository = value
}
productMaterialGroup, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"productMaterialGroupId": removeProductMaterialGroupCommand.ProductMaterialGroupId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if productMaterialGroup == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("已删除 %s", string(removeProductMaterialGroupCommand.ProductMaterialGroupId)))
}
if productMaterialGroup, err := productMaterialGroupRepository.Remove(productMaterialGroup); 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 productMaterialGroup, nil
}
}
// 更新物料分组服务
func (productMaterialGroupService *ProductMaterialGroupService) UpdateProductMaterialGroup(operateInfo *domain.OperateInfo, cmd *command.UpdateProductMaterialGroupCommand) (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()
}()
productMaterialGroup := &domain.ProductMaterialGroup{
ProductMaterialGroupId: cmd.ProductMaterialGroupId,
MaterialGroupName: cmd.MaterialGroupName,
MaterialGroupNumber: cmd.MaterialGroupNumber,
}
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
if productMaterialGroup, err := materialService.UpdateMaterialGroup(operateInfo, productMaterialGroup); 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())
}
materialGroupDto := dto.MaterialGroupDto{}
return materialGroupDto.LoadDto(productMaterialGroup, 0), nil
}
}
// 搜索物料分组服务列表
func (productMaterialGroupService *ProductMaterialGroupService) SearchProductMaterialGroup(operateInfo *domain.OperateInfo, cmd *query.SearchProductMaterialGroupQuery) (int64, interface{}, error) {
if err := cmd.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 productMaterialGroupRepository domain.ProductMaterialGroupRepository
productMaterialGroupRepository, _, _ = factory.FastProductMaterialGroup(transactionContext, 0)
queryOptions := utils.ObjectToMap(cmd)
count, productGroups, err := productMaterialGroupRepository.Find(queryOptions)
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var results = make([]*dto.MaterialGroupDto, 0)
for i := range productGroups {
newItem := &dto.MaterialGroupDto{}
newItem.LoadDto(productGroups[i], operateInfo.OrgId)
results = append(results, newItem)
}
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return count, results, nil
}
func NewProductMaterialGroupService(options map[string]interface{}) *ProductMaterialGroupService {
newProductMaterialGroupService := &ProductMaterialGroupService{}
return newProductMaterialGroupService
}
... ...
... ... @@ -7,6 +7,8 @@ import (
const MaxQueryRow = 10000
const PathSegment = ","
// 班次
const (
WorkOnFullDay = 1 //全天
... ...
... ... @@ -2,6 +2,9 @@ package domain
// 冗余附加数据
type Ext struct {
// 操作人
Operator *User `json:"operator,omitempty"`
// 组织名称
OrgName string `json:"orgName,omitempty"`
... ... @@ -35,3 +38,8 @@ func (e *Ext) WithProductPlanExt(ext *ProductPlanExt) *Ext {
e.ProductPlanExt = ext
return e
}
func (e *Ext) WithOperator(ext *User) *Ext {
e.Operator = ext
return e
}
... ...
package domain
import (
"fmt"
"time"
)
// 物料分组
type ProductMaterialGroup struct {
// 物料分组ID
ProductMaterialGroupId int `json:"productMaterialGroupId"`
// 企业id
CompanyId int `json:"companyId"`
// 组织ID
OrgId int `json:"orgId"`
// 父级ID
Pid int `json:"pid"`
// 路径 (不使用,如果父级改变的话,子级的Path要做更新)
//Path string `json:"path"`
// 物料分组名称
MaterialGroupName string `json:"materialGroupName"`
// 物料分组编码
MaterialGroupNumber string `json:"materialGroupNumber"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt"`
// 删除时间
DeletedAt time.Time `json:"deletedAt"`
// 扩展数据
Ext *Ext `json:"ext,omitempty"`
}
type ProductMaterialGroupRepository interface {
Save(productMaterialGroup *ProductMaterialGroup) (*ProductMaterialGroup, error)
Remove(productMaterialGroup *ProductMaterialGroup) (*ProductMaterialGroup, error)
FindOne(queryOptions map[string]interface{}) (*ProductMaterialGroup, error)
Find(queryOptions map[string]interface{}) (int64, []*ProductMaterialGroup, error)
}
func (productMaterialGroup *ProductMaterialGroup) Identify() interface{} {
if productMaterialGroup.ProductMaterialGroupId == 0 {
return nil
}
return productMaterialGroup.ProductMaterialGroupId
}
func (productMaterialGroup *ProductMaterialGroup) Update(data map[string]interface{}) error {
return nil
}
/***** 1.实现树 *****/
func (productMaterialGroup *ProductMaterialGroup) PID() string {
return fmt.Sprintf("%d", productMaterialGroup.Pid)
}
func (productMaterialGroup *ProductMaterialGroup) ID() string {
return fmt.Sprintf("%d", productMaterialGroup.ProductMaterialGroupId)
}
type ProductMaterialGroups []*ProductMaterialGroup
func (tree ProductMaterialGroups) Len() int {
return len(tree)
}
func (tree ProductMaterialGroups) Less(i, j int) bool {
if tree[i].Pid < tree[j].Pid {
return true
}
if tree[i].Pid == tree[j].Pid {
return tree[i].ProductMaterialGroupId < tree[j].ProductMaterialGroupId
}
return false
}
func (tree ProductMaterialGroups) Swap(i, j int) {
tree[i], tree[j] = tree[j], tree[i]
}
... ...
... ... @@ -3,6 +3,6 @@ package domainService
import "testing"
func TestSectionProductive(t *testing.T) {
svr := NewByteBankService()
svr.SectionProductive()
//svr := NewByteBankService()
//svr.SectionProductive()
}
... ...
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"sort"
"time"
)
type PGMaterialService struct {
transactionContext *pgTransaction.TransactionContext
}
func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
var (
user *domain.User
org *domain.Org
err error
parent *domain.ProductMaterialGroup
newProductMaterialGroup *domain.ProductMaterialGroup
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
}
if item.Pid != 0 {
if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
return nil, fmt.Errorf("上级物料分组不存在")
}
}
if newProductMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || newProductMaterialGroup != nil {
return nil, fmt.Errorf("物料分组编码已存在")
}
newProductMaterialGroup = &domain.ProductMaterialGroup{
CompanyId: opt.CompanyId,
OrgId: opt.OrgId,
Pid: item.Pid,
//Path: item.GetPath(parent),
MaterialGroupName: item.MaterialGroupName,
MaterialGroupNumber: item.MaterialGroupNumber,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Ext: domain.NewExt(org.OrgName).WithOperator(user),
}
newProductMaterialGroup, err = productMaterialGroupRepository.Save(newProductMaterialGroup)
return newProductMaterialGroup, err
}
func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
var (
user *domain.User
org *domain.Org
err error
parent *domain.ProductMaterialGroup
productMaterialGroup *domain.ProductMaterialGroup
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
}
if item.ProductMaterialGroupId == item.Pid {
return nil, fmt.Errorf("当前物料分组不能做为自己上级")
}
if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.ProductMaterialGroupId}); err != nil || productMaterialGroup == nil {
return nil, fmt.Errorf("物料分组不存在")
}
if item.Pid != productMaterialGroup.ProductMaterialGroupId && item.Pid != 0 {
if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
return nil, fmt.Errorf("上级物料分组不存在")
}
}
if productMaterialGroup.MaterialGroupNumber != item.MaterialGroupNumber {
if group, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || group != nil {
return nil, fmt.Errorf("物料分组编码已存在")
}
}
productMaterialGroup.MaterialGroupNumber = item.MaterialGroupNumber
productMaterialGroup.MaterialGroupName = item.MaterialGroupName
productMaterialGroup.UpdatedAt = item.UpdatedAt
productMaterialGroup.Ext = domain.NewExt(org.OrgName).WithOperator(user)
productMaterialGroup, err = productMaterialGroupRepository.Save(productMaterialGroup)
return productMaterialGroup, err
}
func (ptr *PGMaterialService) AllMaterialGroupChild(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) {
var (
err error
listId []int
productMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
groups domain.ProductMaterialGroups
)
if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": productMaterialGroupId}); err != nil || productMaterialGroup == nil {
return nil, listId, fmt.Errorf("物料分组不存在")
}
_, groups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": opt.CompanyId}) //,"orderByProductMaterialGroupId":"asc"
if len(groups) == 0 || err != nil {
return nil, listId, err
}
// 先排序 sort pid,id asc
sort.Stable(groups)
var treeNodes = make([]utils.TreeNode, len(groups))
for i := 0; i < len(groups); i++ {
treeNodes[i] = groups[i]
}
tree := utils.NewTree(treeNodes)
child := tree.AllChildNode(productMaterialGroup)
var result = make([]*domain.ProductMaterialGroup, 0)
for i := range child {
if v, ok := child[i].(*domain.ProductMaterialGroup); ok {
result = append(result, v)
listId = append(listId, v.ProductMaterialGroupId)
}
}
return result, listId, err
}
func (ptr *PGMaterialService) AllMaterialGroupParent(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) {
var (
err error
listId []int
productMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
groups domain.ProductMaterialGroups
)
if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": productMaterialGroupId}); err != nil || productMaterialGroup == nil {
return nil, listId, fmt.Errorf("物料分组不存在")
}
_, groups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": opt.CompanyId}) //,"orderByProductMaterialGroupId":"asc"
if len(groups) == 0 || err != nil {
return nil, listId, err
}
// 先排序 sort pid,id asc
sort.Stable(groups)
var treeNodes = make([]utils.TreeNode, len(groups))
for i := 0; i < len(groups); i++ {
treeNodes[i] = groups[i]
}
tree := utils.NewTree(treeNodes)
child := tree.TreeNodePaths(productMaterialGroup)
var result = make([]*domain.ProductMaterialGroup, 0)
for i := range child {
if v, ok := child[i].(*domain.ProductMaterialGroup); ok {
result = append(result, v)
listId = append(listId, v.ProductMaterialGroupId)
}
}
return result, listId, err
}
func NewPGMaterialService(transactionContext *pgTransaction.TransactionContext) (*PGMaterialService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PGMaterialService{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -48,6 +48,7 @@ func init() {
(*models.DeviceDailyRunningRecord)(nil),
(*models.DeviceRunningRecord)(nil),
(*models.WorkshopPlanCompletionRecord)(nil),
(*models.ProductMaterialGroup)(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 ProductMaterialGroup struct {
tableName string `comment:"物料分组" pg:"manufacture.product_material_group"`
// 物料分组ID
ProductMaterialGroupId int `comment:"物料分组ID" pg:"pk:product_material_group_id"`
// 企业id
CompanyId int `comment:"企业id"`
// 组织ID
OrgId int `comment:"组织ID"`
// 父级ID
Pid int `comment:"父级ID"`
// 路径
// Path string `comment:"路径"`
// 物料分组名称
MaterialGroupName string `comment:"物料分组名称"`
// 物料分组编码
MaterialGroupNumber string `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 TransformToProductMaterialGroupDomainModelFromPgModels(productMaterialGroupModel *models.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
return &domain.ProductMaterialGroup{
ProductMaterialGroupId: productMaterialGroupModel.ProductMaterialGroupId,
CompanyId: productMaterialGroupModel.CompanyId,
OrgId: productMaterialGroupModel.OrgId,
Pid: productMaterialGroupModel.Pid,
//Path: productMaterialGroupModel.Path,
MaterialGroupName: productMaterialGroupModel.MaterialGroupName,
MaterialGroupNumber: productMaterialGroupModel.MaterialGroupNumber,
CreatedAt: productMaterialGroupModel.CreatedAt,
UpdatedAt: productMaterialGroupModel.UpdatedAt,
DeletedAt: productMaterialGroupModel.DeletedAt,
Ext: productMaterialGroupModel.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 ProductMaterialGroupRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *ProductMaterialGroupRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *ProductMaterialGroupRepository) Save(productMaterialGroup *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
sqlBuildFields := []string{
"product_material_group_id",
"company_id",
"org_id",
"pid",
//"path",
"material_group_name",
"material_group_number",
"created_at",
"updated_at",
"deleted_at",
"ext",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_group_id", "deleted_at"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_group_id", "deleted_at"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_group_id", "deleted_at")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if productMaterialGroup.Identify() == nil {
if _, err := tx.QueryOne(
pg.Scan(
&productMaterialGroup.ProductMaterialGroupId,
&productMaterialGroup.CompanyId,
&productMaterialGroup.OrgId,
&productMaterialGroup.Pid,
//&productMaterialGroup.Path,
&productMaterialGroup.MaterialGroupName,
&productMaterialGroup.MaterialGroupNumber,
&productMaterialGroup.CreatedAt,
&productMaterialGroup.UpdatedAt,
&productMaterialGroup.DeletedAt,
&productMaterialGroup.Ext,
),
fmt.Sprintf("INSERT INTO manufacture.product_material_group (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
productMaterialGroup.CompanyId,
productMaterialGroup.OrgId,
productMaterialGroup.Pid,
//productMaterialGroup.Path,
productMaterialGroup.MaterialGroupName,
productMaterialGroup.MaterialGroupNumber,
productMaterialGroup.CreatedAt,
productMaterialGroup.UpdatedAt,
productMaterialGroup.Ext,
); err != nil {
return productMaterialGroup, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&productMaterialGroup.ProductMaterialGroupId,
&productMaterialGroup.CompanyId,
&productMaterialGroup.OrgId,
&productMaterialGroup.Pid,
//&productMaterialGroup.Path,
&productMaterialGroup.MaterialGroupName,
&productMaterialGroup.MaterialGroupNumber,
&productMaterialGroup.CreatedAt,
&productMaterialGroup.UpdatedAt,
&productMaterialGroup.DeletedAt,
&productMaterialGroup.Ext,
),
fmt.Sprintf("UPDATE manufacture.product_material_group SET %s WHERE product_material_group_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
productMaterialGroup.CompanyId,
productMaterialGroup.OrgId,
productMaterialGroup.Pid,
//productMaterialGroup.Path,
productMaterialGroup.MaterialGroupName,
productMaterialGroup.MaterialGroupNumber,
productMaterialGroup.CreatedAt,
productMaterialGroup.UpdatedAt,
productMaterialGroup.Ext,
productMaterialGroup.Identify(),
); err != nil {
return productMaterialGroup, err
}
}
return productMaterialGroup, nil
}
func (repository *ProductMaterialGroupRepository) Remove(productMaterialGroup *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
tx := repository.transactionContext.PgTx
productMaterialGroupModel := new(models.ProductMaterialGroup)
productMaterialGroupModel.ProductMaterialGroupId = productMaterialGroup.Identify().(int)
if _, err := tx.Model(productMaterialGroupModel).WherePK().Delete(); err != nil {
return productMaterialGroup, err
}
return productMaterialGroup, nil
}
func (repository *ProductMaterialGroupRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProductMaterialGroup, error) {
tx := repository.transactionContext.PgTx
productMaterialGroupModel := new(models.ProductMaterialGroup)
query := sqlbuilder.BuildQuery(tx.Model(productMaterialGroupModel), queryOptions)
query.SetWhereByQueryOption("product_material_group_id = ?", "productMaterialGroupId")
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("material_group_number = ?", "materialGroupNumber")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
if productMaterialGroupModel.ProductMaterialGroupId == 0 {
return nil, nil
} else {
return transform.TransformToProductMaterialGroupDomainModelFromPgModels(productMaterialGroupModel)
}
}
func (repository *ProductMaterialGroupRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProductMaterialGroup, error) {
tx := repository.transactionContext.PgTx
var productMaterialGroupModels []*models.ProductMaterialGroup
productMaterialGroups := make([]*domain.ProductMaterialGroup, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productMaterialGroupModels), queryOptions)
query.SetOffsetAndLimit(domain.MaxQueryRow)
if v, ok := queryOptions["orderByProductMaterialGroupId"]; ok {
query.SetOrderDirect("product_material_group_id", v.(string))
} else {
query.SetOrderDirect("product_material_group_id", "DESC")
}
if count, err := query.SelectAndCount(); err != nil {
return 0, productMaterialGroups, err
} else {
for _, productMaterialGroupModel := range productMaterialGroupModels {
if productMaterialGroup, err := transform.TransformToProductMaterialGroupDomainModelFromPgModels(productMaterialGroupModel); err != nil {
return 0, productMaterialGroups, err
} else {
productMaterialGroups = append(productMaterialGroups, productMaterialGroup)
}
}
return int64(count), productMaterialGroups, nil
}
}
func NewProductMaterialGroupRepository(transactionContext *pgTransaction.TransactionContext) (*ProductMaterialGroupRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ProductMaterialGroupRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterialGroup/service"
)
type ProductMaterialGroupController struct {
beego.BaseController
}
func (controller *ProductMaterialGroupController) CreateProductMaterialGroup() {
productMaterialGroupService := service.NewProductMaterialGroupService(nil)
cmd := &command.CreateProductMaterialGroupCommand{}
controller.Unmarshal(cmd)
operateInfo := ParseOperateInfo(controller.BaseController)
cmd.CompanyId = operateInfo.CompanyId
cmd.OrgId = operateInfo.OrgId
data, err := productMaterialGroupService.CreateProductMaterialGroup(operateInfo, cmd)
controller.Response(data, err)
}
func (controller *ProductMaterialGroupController) UpdateProductMaterialGroup() {
productMaterialGroupService := service.NewProductMaterialGroupService(nil)
updateProductMaterialGroupCommand := &command.UpdateProductMaterialGroupCommand{}
controller.Unmarshal(updateProductMaterialGroupCommand)
productMaterialGroupId, _ := controller.GetInt(":productMaterialGroupId")
updateProductMaterialGroupCommand.ProductMaterialGroupId = productMaterialGroupId
operateInfo := ParseOperateInfo(controller.BaseController)
data, err := productMaterialGroupService.UpdateProductMaterialGroup(operateInfo, updateProductMaterialGroupCommand)
controller.Response(data, err)
}
func (controller *ProductMaterialGroupController) GetProductMaterialGroup() {
productMaterialGroupService := service.NewProductMaterialGroupService(nil)
getProductMaterialGroupQuery := &query.GetProductMaterialGroupQuery{}
productMaterialGroupId, _ := controller.GetInt(":productMaterialGroupId")
getProductMaterialGroupQuery.ProductMaterialGroupId = productMaterialGroupId
data, err := productMaterialGroupService.GetProductMaterialGroup(getProductMaterialGroupQuery)
controller.Response(data, err)
}
func (controller *ProductMaterialGroupController) RemoveProductMaterialGroup() {
productMaterialGroupService := service.NewProductMaterialGroupService(nil)
removeProductMaterialGroupCommand := &command.RemoveProductMaterialGroupCommand{}
controller.Unmarshal(removeProductMaterialGroupCommand)
productMaterialGroupId, _ := controller.GetInt(":productMaterialGroupId")
removeProductMaterialGroupCommand.ProductMaterialGroupId = productMaterialGroupId
data, err := productMaterialGroupService.RemoveProductMaterialGroup(removeProductMaterialGroupCommand)
controller.Response(data, err)
}
func (controller *ProductMaterialGroupController) ListProductMaterialGroup() {
productMaterialGroupService := service.NewProductMaterialGroupService(nil)
listProductMaterialGroupQuery := &query.ListProductMaterialGroupQuery{}
offset, _ := controller.GetInt("offset")
listProductMaterialGroupQuery.Offset = offset
limit, _ := controller.GetInt("limit")
listProductMaterialGroupQuery.Limit = limit
data, err := productMaterialGroupService.ListProductMaterialGroup(listProductMaterialGroupQuery)
controller.Response(data, err)
}
func (controller *ProductMaterialGroupController) SearchProductMaterialGroup() {
productMaterialGroupService := service.NewProductMaterialGroupService(nil)
cmd := &query.SearchProductMaterialGroupQuery{}
offset, _ := controller.GetInt("offset")
cmd.Offset = offset
limit, _ := controller.GetInt("limit")
cmd.Limit = limit
operateInfo := ParseOperateInfo(controller.BaseController)
cmd.CompanyId = operateInfo.CompanyId
total, data, err := productMaterialGroupService.SearchProductMaterialGroup(ParseOperateInfo(controller.BaseController), cmd)
ResponseGrid(controller.BaseController, total, data, err)
}
func (controller *ProductMaterialGroupController) SelectorProductMaterialGroup() {
productMaterialGroupService := service.NewProductMaterialGroupService(nil)
cmd := &query.SearchProductMaterialGroupQuery{}
offset, _ := controller.GetInt("offset")
cmd.Offset = offset
limit, _ := controller.GetInt("limit")
cmd.Limit = limit
operateInfo := ParseOperateInfo(controller.BaseController)
cmd.CompanyId = operateInfo.CompanyId
count, data, err := productMaterialGroupService.SearchProductMaterialGroup(ParseOperateInfo(controller.BaseController), cmd)
controller.Response(map[string]interface{}{
"groups": data,
"count": count,
}, 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-material-groups/", &controllers.ProductMaterialGroupController{}, "Post:CreateProductMaterialGroup")
web.Router("/product-material-groups/:productMaterialGroupId", &controllers.ProductMaterialGroupController{}, "Put:UpdateProductMaterialGroup")
web.Router("/product-material-groups/:productMaterialGroupId", &controllers.ProductMaterialGroupController{}, "Get:GetProductMaterialGroup")
web.Router("/product-material-groups/:productMaterialGroupId", &controllers.ProductMaterialGroupController{}, "Delete:RemoveProductMaterialGroup")
web.Router("/product-material-groups/", &controllers.ProductMaterialGroupController{}, "Get:ListProductMaterialGroup")
web.Router("/product-material-groups/search", &controllers.ProductMaterialGroupController{}, "Post:SearchProductMaterialGroup")
web.Router("/product-material-groups/selector", &controllers.ProductMaterialGroupController{}, "Post:SelectorProductMaterialGroup")
}
... ...
package product_material_group
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{}{
"pid": "int",
"materialGroupName": "string",
"materialGroupNumber": "string",
}
httpExpect.POST("/product-material-groups/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("productMaterialGroupId").ValueNotEqual("productMaterialGroupId", BeZero())
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM product_material_groups WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package product_material_group
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 productMaterialGroupId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&productMaterialGroupId),
"INSERT INTO product_material_groups (product_material_group_id, company_id, org_id, pid, path, material_group_name, material_group_number, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_group_id",
"testProductMaterialGroupId", "testCompanyId", "testOrgId", "testPid", "testPath", "testMaterialGroupName", "testMaterialGroupNumber", "testCreatedAt", "testUpdatedAt", "testDeletedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据productMaterialGroupId参数返回物料分组", func() {
Context("传入有效的productMaterialGroupId", func() {
It("返回物料分组数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/product-material-groups/{productMaterialGroupId}").
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_material_groups WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package product_material_group
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 productMaterialGroupId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&productMaterialGroupId),
"INSERT INTO product_material_groups (product_material_group_id, company_id, org_id, pid, path, material_group_name, material_group_number, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_group_id",
"testProductMaterialGroupId", "testCompanyId", "testOrgId", "testPid", "testPath", "testMaterialGroupName", "testMaterialGroupNumber", "testCreatedAt", "testUpdatedAt", "testDeletedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回物料分组列表", func() {
Context("传入有效的参数", func() {
It("返回物料分组数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/product-material-groups/").
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("productMaterialGroups").Value("productMaterialGroups").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM product_material_groups WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package product_material_group
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 TestProductMaterialGroup(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port ProductMaterialGroup 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_group
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 productMaterialGroupId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&productMaterialGroupId),
"INSERT INTO product_material_groups (product_material_group_id, company_id, org_id, pid, path, material_group_name, material_group_number, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_group_id",
"testProductMaterialGroupId", "testCompanyId", "testOrgId", "testPid", "testPath", "testMaterialGroupName", "testMaterialGroupNumber", "testCreatedAt", "testUpdatedAt", "testDeletedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除物料分组服务", func() {
Context("传入有效的productMaterialGroupId", func() {
It("返回被移除物料分组的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/product-material-groups/{productMaterialGroupId}").
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_material_groups WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package product_material_group
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 productMaterialGroupId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&productMaterialGroupId),
"INSERT INTO product_material_groups (product_material_group_id, company_id, org_id, pid, path, material_group_name, material_group_number, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING product_material_group_id",
"testProductMaterialGroupId", "testCompanyId", "testOrgId", "testPid", "testPath", "testMaterialGroupName", "testMaterialGroupNumber", "testCreatedAt", "testUpdatedAt", "testDeletedAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新物料分组服务", func() {
Context("提交正确的物料分组数据", func() {
It("返回更新后的物料分组数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"materialGroupName": "string",
"materialGroupNumber": "string",
}
httpExpect.PUT("/product-material-groups/{productMaterialGroupId}").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("productMaterialGroupId").ValueEqual("productMaterialGroupId", productMaterialGroupId)
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM product_material_groups WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...