正在显示
22 个修改的文件
包含
1126 行增加
和
0 行删除
| @@ -149,3 +149,11 @@ func CreateProductMaterialGroupRepository(options map[string]interface{}) (domai | @@ -149,3 +149,11 @@ func CreateProductMaterialGroupRepository(options map[string]interface{}) (domai | ||
| 149 | } | 149 | } |
| 150 | return repository.NewProductMaterialGroupRepository(transactionContext) | 150 | return repository.NewProductMaterialGroupRepository(transactionContext) |
| 151 | } | 151 | } |
| 152 | + | ||
| 153 | +func CreateProductMaterialRepository(options map[string]interface{}) (domain.ProductMaterialRepository, error) { | ||
| 154 | + var transactionContext *pg.TransactionContext | ||
| 155 | + if value, ok := options["transactionContext"]; ok { | ||
| 156 | + transactionContext = value.(*pg.TransactionContext) | ||
| 157 | + } | ||
| 158 | + return repository.NewProductMaterialRepository(transactionContext) | ||
| 159 | +} |
| 1 | +package command | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "reflect" | ||
| 6 | + "strings" | ||
| 7 | + | ||
| 8 | + "github.com/beego/beego/v2/core/validation" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +type CreateProductMaterialCommand struct { | ||
| 12 | + // 企业id | ||
| 13 | + CompanyId int `cname:"企业id" json:"companyId" valid:"Required"` | ||
| 14 | + // 组织ID | ||
| 15 | + OrgId int `cname:"组织ID" json:"orgId" valid:"Required"` | ||
| 16 | + // 物料分组ID | ||
| 17 | + ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"` | ||
| 18 | + // 物料编码 | ||
| 19 | + MaterialNumber string `cname:"物料编码" json:"materialNumber" valid:"Required"` | ||
| 20 | + // 物料名称 | ||
| 21 | + MaterialName string `cname:"物料名称" json:"materialName" valid:"Required"` | ||
| 22 | + // 物料属性 | ||
| 23 | + MaterialAttribute string `cname:"物料属性" json:"materialAttribute" valid:"Required"` | ||
| 24 | + // 物料类别 | ||
| 25 | + MaterialCategory string `cname:"物料类别" json:"materialCategory" valid:"Required"` | ||
| 26 | + // 规格 | ||
| 27 | + Specification string `cname:"规格" json:"specification"` | ||
| 28 | + // 单位 | ||
| 29 | + Unit string `cname:"单位" json:"unit"` | ||
| 30 | + // 保质期 单位:天 | ||
| 31 | + ExpiredDay int `cname:"保质期 单位:天" json:"expiredDay"` | ||
| 32 | + // 备注 | ||
| 33 | + Remark string `cname:"备注" json:"remark"` | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +func (createProductMaterialCommand *CreateProductMaterialCommand) Valid(validation *validation.Validation) { | ||
| 37 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +func (createProductMaterialCommand *CreateProductMaterialCommand) ValidateCommand() error { | ||
| 41 | + valid := validation.Validation{} | ||
| 42 | + b, err := valid.Valid(createProductMaterialCommand) | ||
| 43 | + if err != nil { | ||
| 44 | + return err | ||
| 45 | + } | ||
| 46 | + if !b { | ||
| 47 | + elem := reflect.TypeOf(createProductMaterialCommand).Elem() | ||
| 48 | + for _, validErr := range valid.Errors { | ||
| 49 | + field, isExist := elem.FieldByName(validErr.Field) | ||
| 50 | + if isExist { | ||
| 51 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
| 52 | + } else { | ||
| 53 | + return fmt.Errorf(validErr.Message) | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + return nil | ||
| 58 | +} |
| 1 | +package command | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "reflect" | ||
| 6 | + "strings" | ||
| 7 | + | ||
| 8 | + "github.com/beego/beego/v2/core/validation" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +type RemoveProductMaterialCommand struct { | ||
| 12 | + // 物料ID | ||
| 13 | + ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"` | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +func (removeProductMaterialCommand *RemoveProductMaterialCommand) Valid(validation *validation.Validation) { | ||
| 17 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +func (removeProductMaterialCommand *RemoveProductMaterialCommand) ValidateCommand() error { | ||
| 21 | + valid := validation.Validation{} | ||
| 22 | + b, err := valid.Valid(removeProductMaterialCommand) | ||
| 23 | + if err != nil { | ||
| 24 | + return err | ||
| 25 | + } | ||
| 26 | + if !b { | ||
| 27 | + elem := reflect.TypeOf(removeProductMaterialCommand).Elem() | ||
| 28 | + for _, validErr := range valid.Errors { | ||
| 29 | + field, isExist := elem.FieldByName(validErr.Field) | ||
| 30 | + if isExist { | ||
| 31 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
| 32 | + } else { | ||
| 33 | + return fmt.Errorf(validErr.Message) | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + return nil | ||
| 38 | +} |
| 1 | +package command | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "reflect" | ||
| 6 | + "strings" | ||
| 7 | + | ||
| 8 | + "github.com/beego/beego/v2/core/validation" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +type UpdateProductMaterialCommand struct { | ||
| 12 | + // 物料ID | ||
| 13 | + ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"` | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +func (updateProductMaterialCommand *UpdateProductMaterialCommand) Valid(validation *validation.Validation) { | ||
| 17 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +func (updateProductMaterialCommand *UpdateProductMaterialCommand) ValidateCommand() error { | ||
| 21 | + valid := validation.Validation{} | ||
| 22 | + b, err := valid.Valid(updateProductMaterialCommand) | ||
| 23 | + if err != nil { | ||
| 24 | + return err | ||
| 25 | + } | ||
| 26 | + if !b { | ||
| 27 | + elem := reflect.TypeOf(updateProductMaterialCommand).Elem() | ||
| 28 | + for _, validErr := range valid.Errors { | ||
| 29 | + field, isExist := elem.FieldByName(validErr.Field) | ||
| 30 | + if isExist { | ||
| 31 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
| 32 | + } else { | ||
| 33 | + return fmt.Errorf(validErr.Message) | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + return nil | ||
| 38 | +} |
| 1 | +package query | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "reflect" | ||
| 6 | + "strings" | ||
| 7 | + | ||
| 8 | + "github.com/beego/beego/v2/core/validation" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +type GetProductMaterialQuery struct { | ||
| 12 | + // 物料ID | ||
| 13 | + ProductMaterialId int `cname:"物料ID" json:"productMaterialId" valid:"Required"` | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +func (getProductMaterialQuery *GetProductMaterialQuery) Valid(validation *validation.Validation) { | ||
| 17 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +func (getProductMaterialQuery *GetProductMaterialQuery) ValidateQuery() error { | ||
| 21 | + valid := validation.Validation{} | ||
| 22 | + b, err := valid.Valid(getProductMaterialQuery) | ||
| 23 | + if err != nil { | ||
| 24 | + return err | ||
| 25 | + } | ||
| 26 | + if !b { | ||
| 27 | + elem := reflect.TypeOf(getProductMaterialQuery).Elem() | ||
| 28 | + for _, validErr := range valid.Errors { | ||
| 29 | + field, isExist := elem.FieldByName(validErr.Field) | ||
| 30 | + if isExist { | ||
| 31 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
| 32 | + } else { | ||
| 33 | + return fmt.Errorf(validErr.Message) | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + return nil | ||
| 38 | +} |
| 1 | +package query | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "reflect" | ||
| 6 | + "strings" | ||
| 7 | + | ||
| 8 | + "github.com/beego/beego/v2/core/validation" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +type ListProductMaterialQuery struct { | ||
| 12 | + // 查询偏离量 | ||
| 13 | + Offset int `cname:"查询偏离量" json:"offset" valid:"Required"` | ||
| 14 | + // 查询限制 | ||
| 15 | + Limit int `cname:"查询限制" json:"limit" valid:"Required"` | ||
| 16 | + // 物料分组ID | ||
| 17 | + ProductMaterialGroupId int `cname:"物料分组ID" json:"productMaterialGroupId" valid:"Required"` | ||
| 18 | + // 物料名称 | ||
| 19 | + MaterialName string `cname:"物料名称" json:"materialName" valid:"Required"` | ||
| 20 | + // 物料类别 | ||
| 21 | + MaterialCategory string `cname:"物料类别" json:"materialCategory" valid:"Required"` | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +func (listProductMaterialQuery *ListProductMaterialQuery) Valid(validation *validation.Validation) { | ||
| 25 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +func (listProductMaterialQuery *ListProductMaterialQuery) ValidateQuery() error { | ||
| 29 | + valid := validation.Validation{} | ||
| 30 | + b, err := valid.Valid(listProductMaterialQuery) | ||
| 31 | + if err != nil { | ||
| 32 | + return err | ||
| 33 | + } | ||
| 34 | + if !b { | ||
| 35 | + elem := reflect.TypeOf(listProductMaterialQuery).Elem() | ||
| 36 | + for _, validErr := range valid.Errors { | ||
| 37 | + field, isExist := elem.FieldByName(validErr.Field) | ||
| 38 | + if isExist { | ||
| 39 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
| 40 | + } else { | ||
| 41 | + return fmt.Errorf(validErr.Message) | ||
| 42 | + } | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + return nil | ||
| 46 | +} |
| 1 | +package service | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "github.com/linmadan/egglib-go/core/application" | ||
| 6 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
| 7 | + "github.com/linmadan/egglib-go/utils/tool_funs" | ||
| 8 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory" | ||
| 9 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/command" | ||
| 10 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/query" | ||
| 11 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
| 12 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" | ||
| 13 | +) | ||
| 14 | + | ||
| 15 | +// 生产物料服务 | ||
| 16 | +type ProductMaterialService struct { | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +// 创建生产物料服务 | ||
| 20 | +func (productMaterialService *ProductMaterialService) CreateProductMaterial(operateInfo *domain.OperateInfo, cmd *command.CreateProductMaterialCommand) (interface{}, error) { | ||
| 21 | + if err := cmd.ValidateCommand(); err != nil { | ||
| 22 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 23 | + } | ||
| 24 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 25 | + if err != nil { | ||
| 26 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 27 | + } | ||
| 28 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 29 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 30 | + } | ||
| 31 | + defer func() { | ||
| 32 | + transactionContext.RollbackTransaction() | ||
| 33 | + }() | ||
| 34 | + | ||
| 35 | + newProductMaterial := &domain.ProductMaterial{ | ||
| 36 | + CompanyId: cmd.CompanyId, | ||
| 37 | + OrgId: cmd.OrgId, | ||
| 38 | + ProductMaterialGroupId: cmd.ProductMaterialGroupId, | ||
| 39 | + MaterialNumber: cmd.MaterialNumber, | ||
| 40 | + MaterialName: cmd.MaterialName, | ||
| 41 | + MaterialAttribute: &domain.MaterialAttribute{Attribute: cmd.MaterialAttribute}, | ||
| 42 | + MaterialCategory: &domain.MaterialCategory{Category: cmd.MaterialCategory}, | ||
| 43 | + ProductMaterialExt: &domain.MaterialExt{ | ||
| 44 | + Specification: cmd.Specification, | ||
| 45 | + Unit: cmd.Unit, | ||
| 46 | + ExpiredDay: cmd.ExpiredDay, | ||
| 47 | + Remark: cmd.Remark, | ||
| 48 | + }, | ||
| 49 | + } | ||
| 50 | + //var productMaterial *domain.ProductMaterial | ||
| 51 | + materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext)) | ||
| 52 | + if _, err = materialService.AddMaterial(operateInfo, newProductMaterial); err != nil { | ||
| 53 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 54 | + } | ||
| 55 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 56 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 57 | + } | ||
| 58 | + return struct{}{}, nil | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +// 返回生产物料服务 | ||
| 62 | +func (productMaterialService *ProductMaterialService) GetProductMaterial(getProductMaterialQuery *query.GetProductMaterialQuery) (interface{}, error) { | ||
| 63 | + if err := getProductMaterialQuery.ValidateQuery(); err != nil { | ||
| 64 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 65 | + } | ||
| 66 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 67 | + if err != nil { | ||
| 68 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 69 | + } | ||
| 70 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 71 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 72 | + } | ||
| 73 | + defer func() { | ||
| 74 | + transactionContext.RollbackTransaction() | ||
| 75 | + }() | ||
| 76 | + var productMaterialRepository domain.ProductMaterialRepository | ||
| 77 | + if value, err := factory.CreateProductMaterialRepository(map[string]interface{}{ | ||
| 78 | + "transactionContext": transactionContext, | ||
| 79 | + }); err != nil { | ||
| 80 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 81 | + } else { | ||
| 82 | + productMaterialRepository = value | ||
| 83 | + } | ||
| 84 | + productMaterial, err := productMaterialRepository.FindOne(map[string]interface{}{"productMaterialId": getProductMaterialQuery.ProductMaterialId}) | ||
| 85 | + if err != nil { | ||
| 86 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 87 | + } | ||
| 88 | + if productMaterial == nil { | ||
| 89 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductMaterialQuery.ProductMaterialId))) | ||
| 90 | + } else { | ||
| 91 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 92 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 93 | + } | ||
| 94 | + return productMaterial, nil | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +// 返回生产物料服务列表 | ||
| 99 | +func (productMaterialService *ProductMaterialService) ListProductMaterial(listProductMaterialQuery *query.ListProductMaterialQuery) (interface{}, error) { | ||
| 100 | + if err := listProductMaterialQuery.ValidateQuery(); err != nil { | ||
| 101 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 102 | + } | ||
| 103 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 104 | + if err != nil { | ||
| 105 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 106 | + } | ||
| 107 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 108 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 109 | + } | ||
| 110 | + defer func() { | ||
| 111 | + transactionContext.RollbackTransaction() | ||
| 112 | + }() | ||
| 113 | + var productMaterialRepository domain.ProductMaterialRepository | ||
| 114 | + if value, err := factory.CreateProductMaterialRepository(map[string]interface{}{ | ||
| 115 | + "transactionContext": transactionContext, | ||
| 116 | + }); err != nil { | ||
| 117 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 118 | + } else { | ||
| 119 | + productMaterialRepository = value | ||
| 120 | + } | ||
| 121 | + if count, productMaterials, err := productMaterialRepository.Find(tool_funs.SimpleStructToMap(listProductMaterialQuery)); err != nil { | ||
| 122 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 123 | + } else { | ||
| 124 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 125 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 126 | + } | ||
| 127 | + return map[string]interface{}{ | ||
| 128 | + "count": count, | ||
| 129 | + "productMaterials": productMaterials, | ||
| 130 | + }, nil | ||
| 131 | + } | ||
| 132 | +} | ||
| 133 | + | ||
| 134 | +// 移除生产物料服务 | ||
| 135 | +func (productMaterialService *ProductMaterialService) RemoveProductMaterial(removeProductMaterialCommand *command.RemoveProductMaterialCommand) (interface{}, error) { | ||
| 136 | + if err := removeProductMaterialCommand.ValidateCommand(); err != nil { | ||
| 137 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 138 | + } | ||
| 139 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 140 | + if err != nil { | ||
| 141 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 142 | + } | ||
| 143 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 144 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 145 | + } | ||
| 146 | + defer func() { | ||
| 147 | + transactionContext.RollbackTransaction() | ||
| 148 | + }() | ||
| 149 | + var productMaterialRepository domain.ProductMaterialRepository | ||
| 150 | + if value, err := factory.CreateProductMaterialRepository(map[string]interface{}{ | ||
| 151 | + "transactionContext": transactionContext, | ||
| 152 | + }); err != nil { | ||
| 153 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 154 | + } else { | ||
| 155 | + productMaterialRepository = value | ||
| 156 | + } | ||
| 157 | + productMaterial, err := productMaterialRepository.FindOne(map[string]interface{}{"productMaterialId": removeProductMaterialCommand.ProductMaterialId}) | ||
| 158 | + if err != nil { | ||
| 159 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 160 | + } | ||
| 161 | + if productMaterial == nil { | ||
| 162 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductMaterialCommand.ProductMaterialId))) | ||
| 163 | + } | ||
| 164 | + if productMaterial, err := productMaterialRepository.Remove(productMaterial); err != nil { | ||
| 165 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 166 | + } else { | ||
| 167 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 168 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 169 | + } | ||
| 170 | + return productMaterial, nil | ||
| 171 | + } | ||
| 172 | +} | ||
| 173 | + | ||
| 174 | +// 更新生产物料服务 | ||
| 175 | +func (productMaterialService *ProductMaterialService) UpdateProductMaterial(updateProductMaterialCommand *command.UpdateProductMaterialCommand) (interface{}, error) { | ||
| 176 | + if err := updateProductMaterialCommand.ValidateCommand(); err != nil { | ||
| 177 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 178 | + } | ||
| 179 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 180 | + if err != nil { | ||
| 181 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 182 | + } | ||
| 183 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 184 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 185 | + } | ||
| 186 | + defer func() { | ||
| 187 | + transactionContext.RollbackTransaction() | ||
| 188 | + }() | ||
| 189 | + var productMaterialRepository domain.ProductMaterialRepository | ||
| 190 | + if value, err := factory.CreateProductMaterialRepository(map[string]interface{}{ | ||
| 191 | + "transactionContext": transactionContext, | ||
| 192 | + }); err != nil { | ||
| 193 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 194 | + } else { | ||
| 195 | + productMaterialRepository = value | ||
| 196 | + } | ||
| 197 | + productMaterial, err := productMaterialRepository.FindOne(map[string]interface{}{"productMaterialId": updateProductMaterialCommand.ProductMaterialId}) | ||
| 198 | + if err != nil { | ||
| 199 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 200 | + } | ||
| 201 | + if productMaterial == nil { | ||
| 202 | + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductMaterialCommand.ProductMaterialId))) | ||
| 203 | + } | ||
| 204 | + if err := productMaterial.Update(tool_funs.SimpleStructToMap(updateProductMaterialCommand)); err != nil { | ||
| 205 | + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) | ||
| 206 | + } | ||
| 207 | + if productMaterial, err := productMaterialRepository.Save(productMaterial); err != nil { | ||
| 208 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 209 | + } else { | ||
| 210 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 211 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 212 | + } | ||
| 213 | + return productMaterial, nil | ||
| 214 | + } | ||
| 215 | +} | ||
| 216 | + | ||
| 217 | +func NewProductMaterialService(options map[string]interface{}) *ProductMaterialService { | ||
| 218 | + newProductMaterialService := &ProductMaterialService{} | ||
| 219 | + return newProductMaterialService | ||
| 220 | +} |
| @@ -7,3 +7,27 @@ type Material struct { | @@ -7,3 +7,27 @@ type Material struct { | ||
| 7 | // 物料类别 | 7 | // 物料类别 |
| 8 | MaterialCategory string `json:"materialCategory,omitempty"` | 8 | MaterialCategory string `json:"materialCategory,omitempty"` |
| 9 | } | 9 | } |
| 10 | + | ||
| 11 | +// 物料类别 | ||
| 12 | +type MaterialCategory struct { | ||
| 13 | + //Id int `json:"id"` | ||
| 14 | + Category string `json:"category"` | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +// 物料属性 | ||
| 18 | +type MaterialAttribute struct { | ||
| 19 | + //Id int `json:"id"` | ||
| 20 | + Attribute string `json:"attribute"` | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +// 物料扩展数据 | ||
| 24 | +type MaterialExt struct { | ||
| 25 | + // 规格 | ||
| 26 | + Specification string `json:"specification"` | ||
| 27 | + // 单位 | ||
| 28 | + Unit string `json:"unit"` | ||
| 29 | + // 保质期 单位:天 | ||
| 30 | + ExpiredDay int `json:"expiredDay"` | ||
| 31 | + // 备注 | ||
| 32 | + Remark string `json:"remark"` | ||
| 33 | +} |
pkg/domain/product_material.go
0 → 100644
| 1 | +package domain | ||
| 2 | + | ||
| 3 | +import "time" | ||
| 4 | + | ||
| 5 | +// 生产物料 | ||
| 6 | +type ProductMaterial struct { | ||
| 7 | + // 物料ID | ||
| 8 | + ProductMaterialId int `json:"productMaterialId"` | ||
| 9 | + // 企业id | ||
| 10 | + CompanyId int `json:"companyId"` | ||
| 11 | + // 组织ID | ||
| 12 | + OrgId int `json:"orgId"` | ||
| 13 | + // 物料分组ID | ||
| 14 | + ProductMaterialGroupId int `json:"productMaterialGroupId"` | ||
| 15 | + // 物料编码 | ||
| 16 | + MaterialNumber string `json:"materialNumber"` | ||
| 17 | + // 物料名称 | ||
| 18 | + MaterialName string `json:"materialName"` | ||
| 19 | + // 物料属性 | ||
| 20 | + MaterialAttribute *MaterialAttribute `json:"materialAttribute"` | ||
| 21 | + // 物料类别 | ||
| 22 | + MaterialCategory *MaterialCategory `json:"materialCategory"` | ||
| 23 | + // 物料扩展 | ||
| 24 | + ProductMaterialExt *MaterialExt `json:"productMaterialExt"` | ||
| 25 | + // 创建时间 | ||
| 26 | + CreatedAt time.Time `json:"createdAt"` | ||
| 27 | + // 更新时间 | ||
| 28 | + UpdatedAt time.Time `json:"updatedAt"` | ||
| 29 | + // 删除时间 | ||
| 30 | + DeletedAt time.Time `json:"deletedAt"` | ||
| 31 | + // 扩展 | ||
| 32 | + Ext *Ext `json:"ext"` | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +type ProductMaterialRepository interface { | ||
| 36 | + Save(productMaterial *ProductMaterial) (*ProductMaterial, error) | ||
| 37 | + Remove(productMaterial *ProductMaterial) (*ProductMaterial, error) | ||
| 38 | + FindOne(queryOptions map[string]interface{}) (*ProductMaterial, error) | ||
| 39 | + Find(queryOptions map[string]interface{}) (int64, []*ProductMaterial, error) | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +func (productMaterial *ProductMaterial) Identify() interface{} { | ||
| 43 | + if productMaterial.ProductMaterialId == 0 { | ||
| 44 | + return nil | ||
| 45 | + } | ||
| 46 | + return productMaterial.ProductMaterialId | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +func (productMaterial *ProductMaterial) Update(data map[string]interface{}) error { | ||
| 50 | + return nil | ||
| 51 | +} |
| @@ -14,6 +14,50 @@ type PGMaterialService struct { | @@ -14,6 +14,50 @@ type PGMaterialService struct { | ||
| 14 | transactionContext *pgTransaction.TransactionContext | 14 | transactionContext *pgTransaction.TransactionContext |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | +// 物料 | ||
| 18 | + | ||
| 19 | +func (ptr *PGMaterialService) AddMaterial(opt *domain.OperateInfo, item *domain.ProductMaterial) (*domain.ProductMaterial, error) { | ||
| 20 | + var ( | ||
| 21 | + user *domain.User | ||
| 22 | + org *domain.Org | ||
| 23 | + err error | ||
| 24 | + newProductMaterial *domain.ProductMaterial | ||
| 25 | + productMaterialRepository, _ = repository.NewProductMaterialRepository(ptr.transactionContext) | ||
| 26 | + productGroupRepository, _ = repository.NewProductGroupRepository(ptr.transactionContext) | ||
| 27 | + ) | ||
| 28 | + if user, err = NewUserService().User(opt.UserId); err != nil { | ||
| 29 | + return nil, err | ||
| 30 | + } | ||
| 31 | + if org, err = NewUserService().Organization(opt.OrgId); err != nil { | ||
| 32 | + return nil, err | ||
| 33 | + } | ||
| 34 | + _, err = productGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productGroupId": item.ProductMaterialGroupId}) | ||
| 35 | + if err != nil { | ||
| 36 | + return nil, err | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + if newProductMaterial, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialNumber": item.MaterialNumber}); err == nil || newProductMaterial != nil { | ||
| 40 | + return nil, fmt.Errorf("物料编码已存在") | ||
| 41 | + } | ||
| 42 | + newProductMaterial = &domain.ProductMaterial{ | ||
| 43 | + CompanyId: item.CompanyId, | ||
| 44 | + OrgId: item.OrgId, | ||
| 45 | + ProductMaterialGroupId: item.ProductMaterialGroupId, | ||
| 46 | + MaterialName: item.MaterialName, | ||
| 47 | + MaterialNumber: item.MaterialNumber, | ||
| 48 | + MaterialAttribute: item.MaterialAttribute, | ||
| 49 | + MaterialCategory: item.MaterialCategory, | ||
| 50 | + ProductMaterialExt: item.ProductMaterialExt, | ||
| 51 | + CreatedAt: time.Now(), | ||
| 52 | + UpdatedAt: time.Now(), | ||
| 53 | + Ext: domain.NewExt(org.OrgName).WithOperator(user), | ||
| 54 | + } | ||
| 55 | + newProductMaterial, err = productMaterialRepository.Save(newProductMaterial) | ||
| 56 | + return newProductMaterial, err | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +// 物料分组 | ||
| 60 | + | ||
| 17 | func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { | 61 | func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) { |
| 18 | var ( | 62 | var ( |
| 19 | user *domain.User | 63 | user *domain.User |
| @@ -49,6 +49,7 @@ func init() { | @@ -49,6 +49,7 @@ func init() { | ||
| 49 | (*models.DeviceRunningRecord)(nil), | 49 | (*models.DeviceRunningRecord)(nil), |
| 50 | (*models.WorkshopPlanCompletionRecord)(nil), | 50 | (*models.WorkshopPlanCompletionRecord)(nil), |
| 51 | (*models.ProductMaterialGroup)(nil), | 51 | (*models.ProductMaterialGroup)(nil), |
| 52 | + (*models.ProductMaterial)(nil), | ||
| 52 | } { | 53 | } { |
| 53 | err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ | 54 | err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ |
| 54 | Temp: false, | 55 | Temp: false, |
| 1 | +package models | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
| 5 | + "time" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +type ProductMaterial struct { | ||
| 9 | + tableName string `comment:"生产物料" pg:"manufacture.product_material"` | ||
| 10 | + // 物料ID | ||
| 11 | + ProductMaterialId int `comment:"物料ID" pg:"pk:product_material_id"` | ||
| 12 | + // 企业id | ||
| 13 | + CompanyId int `comment:"企业id"` | ||
| 14 | + // 组织ID | ||
| 15 | + OrgId int `comment:"组织ID"` | ||
| 16 | + // 物料分组ID | ||
| 17 | + ProductMaterialGroupId int `comment:"物料分组ID"` | ||
| 18 | + // 物料编码 | ||
| 19 | + MaterialNumber string `comment:"物料编码"` | ||
| 20 | + // 物料名称 | ||
| 21 | + MaterialName string `comment:"物料名称"` | ||
| 22 | + // 物料属性 | ||
| 23 | + MaterialAttribute *domain.MaterialAttribute `comment:"物料属性"` | ||
| 24 | + // 物料类别 | ||
| 25 | + MaterialCategory *domain.MaterialCategory `comment:"物料类别"` | ||
| 26 | + // 物料扩展 | ||
| 27 | + ProductMaterialExt *domain.MaterialExt `comment:"物料扩展"` | ||
| 28 | + // 创建时间 | ||
| 29 | + CreatedAt time.Time `comment:"创建时间"` | ||
| 30 | + // 更新时间 | ||
| 31 | + UpdatedAt time.Time `comment:"更新时间"` | ||
| 32 | + // 删除时间 | ||
| 33 | + DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"` | ||
| 34 | + // 扩展 | ||
| 35 | + Ext *domain.Ext `comment:"扩展"` | ||
| 36 | +} |
| 1 | +package transform | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
| 5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +func TransformToProductMaterialDomainModelFromPgModels(productMaterialModel *models.ProductMaterial) (*domain.ProductMaterial, error) { | ||
| 9 | + return &domain.ProductMaterial{ | ||
| 10 | + ProductMaterialId: productMaterialModel.ProductMaterialId, | ||
| 11 | + CompanyId: productMaterialModel.CompanyId, | ||
| 12 | + OrgId: productMaterialModel.OrgId, | ||
| 13 | + ProductMaterialGroupId: productMaterialModel.ProductMaterialGroupId, | ||
| 14 | + MaterialNumber: productMaterialModel.MaterialNumber, | ||
| 15 | + MaterialName: productMaterialModel.MaterialName, | ||
| 16 | + MaterialAttribute: productMaterialModel.MaterialAttribute, | ||
| 17 | + MaterialCategory: productMaterialModel.MaterialCategory, | ||
| 18 | + ProductMaterialExt: productMaterialModel.ProductMaterialExt, | ||
| 19 | + CreatedAt: productMaterialModel.CreatedAt, | ||
| 20 | + UpdatedAt: productMaterialModel.UpdatedAt, | ||
| 21 | + DeletedAt: productMaterialModel.DeletedAt, | ||
| 22 | + Ext: productMaterialModel.Ext, | ||
| 23 | + }, nil | ||
| 24 | +} |
| 1 | +package repository | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "github.com/go-pg/pg/v10" | ||
| 6 | + | ||
| 7 | + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | ||
| 8 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
| 9 | + "github.com/linmadan/egglib-go/utils/snowflake" | ||
| 10 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
| 11 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models" | ||
| 12 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform" | ||
| 13 | +) | ||
| 14 | + | ||
| 15 | +type ProductMaterialRepository struct { | ||
| 16 | + transactionContext *pgTransaction.TransactionContext | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +func (repository *ProductMaterialRepository) nextIdentify() (int64, error) { | ||
| 20 | + IdWorker, err := snowflake.NewIdWorker(1) | ||
| 21 | + if err != nil { | ||
| 22 | + return 0, err | ||
| 23 | + } | ||
| 24 | + id, err := IdWorker.NextId() | ||
| 25 | + return id, err | ||
| 26 | +} | ||
| 27 | +func (repository *ProductMaterialRepository) Save(productMaterial *domain.ProductMaterial) (*domain.ProductMaterial, error) { | ||
| 28 | + sqlBuildFields := []string{ | ||
| 29 | + "product_material_id", | ||
| 30 | + "company_id", | ||
| 31 | + "org_id", | ||
| 32 | + "product_material_group_id", | ||
| 33 | + "material_number", | ||
| 34 | + "material_name", | ||
| 35 | + "material_attribute", | ||
| 36 | + "material_category", | ||
| 37 | + "product_material_ext", | ||
| 38 | + "created_at", | ||
| 39 | + "updated_at", | ||
| 40 | + "deleted_at", | ||
| 41 | + "ext", | ||
| 42 | + } | ||
| 43 | + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_id", "deleted_at")) | ||
| 44 | + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_id", "deleted_at")) | ||
| 45 | + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) | ||
| 46 | + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_material_id", "deleted_at") | ||
| 47 | + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) | ||
| 48 | + tx := repository.transactionContext.PgTx | ||
| 49 | + if productMaterial.Identify() == nil { | ||
| 50 | + if _, err := tx.QueryOne( | ||
| 51 | + pg.Scan( | ||
| 52 | + &productMaterial.ProductMaterialId, | ||
| 53 | + &productMaterial.CompanyId, | ||
| 54 | + &productMaterial.OrgId, | ||
| 55 | + &productMaterial.ProductMaterialGroupId, | ||
| 56 | + &productMaterial.MaterialNumber, | ||
| 57 | + &productMaterial.MaterialName, | ||
| 58 | + &productMaterial.MaterialAttribute, | ||
| 59 | + &productMaterial.MaterialCategory, | ||
| 60 | + &productMaterial.ProductMaterialExt, | ||
| 61 | + &productMaterial.CreatedAt, | ||
| 62 | + &productMaterial.UpdatedAt, | ||
| 63 | + &productMaterial.DeletedAt, | ||
| 64 | + &productMaterial.Ext, | ||
| 65 | + ), | ||
| 66 | + fmt.Sprintf("INSERT INTO manufacture.product_material (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), | ||
| 67 | + productMaterial.CompanyId, | ||
| 68 | + productMaterial.OrgId, | ||
| 69 | + productMaterial.ProductMaterialGroupId, | ||
| 70 | + productMaterial.MaterialNumber, | ||
| 71 | + productMaterial.MaterialName, | ||
| 72 | + productMaterial.MaterialAttribute, | ||
| 73 | + productMaterial.MaterialCategory, | ||
| 74 | + productMaterial.ProductMaterialExt, | ||
| 75 | + productMaterial.CreatedAt, | ||
| 76 | + productMaterial.UpdatedAt, | ||
| 77 | + productMaterial.Ext, | ||
| 78 | + ); err != nil { | ||
| 79 | + return productMaterial, err | ||
| 80 | + } | ||
| 81 | + } else { | ||
| 82 | + if _, err := tx.QueryOne( | ||
| 83 | + pg.Scan( | ||
| 84 | + &productMaterial.ProductMaterialId, | ||
| 85 | + &productMaterial.CompanyId, | ||
| 86 | + &productMaterial.OrgId, | ||
| 87 | + &productMaterial.ProductMaterialGroupId, | ||
| 88 | + &productMaterial.MaterialNumber, | ||
| 89 | + &productMaterial.MaterialName, | ||
| 90 | + &productMaterial.MaterialAttribute, | ||
| 91 | + &productMaterial.MaterialCategory, | ||
| 92 | + &productMaterial.ProductMaterialExt, | ||
| 93 | + &productMaterial.CreatedAt, | ||
| 94 | + &productMaterial.UpdatedAt, | ||
| 95 | + &productMaterial.DeletedAt, | ||
| 96 | + &productMaterial.Ext, | ||
| 97 | + ), | ||
| 98 | + fmt.Sprintf("UPDATE manufacture.product_material SET %s WHERE product_material_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
| 99 | + productMaterial.CompanyId, | ||
| 100 | + productMaterial.OrgId, | ||
| 101 | + productMaterial.ProductMaterialGroupId, | ||
| 102 | + productMaterial.MaterialNumber, | ||
| 103 | + productMaterial.MaterialName, | ||
| 104 | + productMaterial.MaterialAttribute, | ||
| 105 | + productMaterial.MaterialCategory, | ||
| 106 | + productMaterial.ProductMaterialExt, | ||
| 107 | + productMaterial.CreatedAt, | ||
| 108 | + productMaterial.UpdatedAt, | ||
| 109 | + productMaterial.Ext, | ||
| 110 | + productMaterial.Identify(), | ||
| 111 | + ); err != nil { | ||
| 112 | + return productMaterial, err | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + return productMaterial, nil | ||
| 116 | +} | ||
| 117 | +func (repository *ProductMaterialRepository) Remove(productMaterial *domain.ProductMaterial) (*domain.ProductMaterial, error) { | ||
| 118 | + tx := repository.transactionContext.PgTx | ||
| 119 | + productMaterialModel := new(models.ProductMaterial) | ||
| 120 | + productMaterialModel.ProductMaterialId = productMaterial.Identify().(int) | ||
| 121 | + if _, err := tx.Model(productMaterialModel).WherePK().Delete(); err != nil { | ||
| 122 | + return productMaterial, err | ||
| 123 | + } | ||
| 124 | + return productMaterial, nil | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +// query compose | ||
| 128 | +// 1. company_id,material_number | ||
| 129 | + | ||
| 130 | +func (repository *ProductMaterialRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProductMaterial, error) { | ||
| 131 | + tx := repository.transactionContext.PgTx | ||
| 132 | + productMaterialModel := new(models.ProductMaterial) | ||
| 133 | + query := sqlbuilder.BuildQuery(tx.Model(productMaterialModel), queryOptions) | ||
| 134 | + query.SetWhereByQueryOption("product_material.product_material_id = ?", "productMaterialId") | ||
| 135 | + | ||
| 136 | + query.SetWhereByQueryOption("product_material.company_id = ?", "companyId") | ||
| 137 | + query.SetWhereByQueryOption("product_material.material_number = ?", "materialNumber") | ||
| 138 | + if err := query.First(); err != nil { | ||
| 139 | + if err.Error() == "pg: no rows in result set" { | ||
| 140 | + return nil, fmt.Errorf("没有此资源") | ||
| 141 | + } else { | ||
| 142 | + return nil, err | ||
| 143 | + } | ||
| 144 | + } | ||
| 145 | + if productMaterialModel.ProductMaterialId == 0 { | ||
| 146 | + return nil, nil | ||
| 147 | + } else { | ||
| 148 | + return transform.TransformToProductMaterialDomainModelFromPgModels(productMaterialModel) | ||
| 149 | + } | ||
| 150 | +} | ||
| 151 | +func (repository *ProductMaterialRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProductMaterial, error) { | ||
| 152 | + tx := repository.transactionContext.PgTx | ||
| 153 | + var productMaterialModels []*models.ProductMaterial | ||
| 154 | + productMaterials := make([]*domain.ProductMaterial, 0) | ||
| 155 | + query := sqlbuilder.BuildQuery(tx.Model(&productMaterialModels), queryOptions) | ||
| 156 | + query.SetOffsetAndLimit(20) | ||
| 157 | + query.SetOrderDirect("product_material_id", "DESC") | ||
| 158 | + if count, err := query.SelectAndCount(); err != nil { | ||
| 159 | + return 0, productMaterials, err | ||
| 160 | + } else { | ||
| 161 | + for _, productMaterialModel := range productMaterialModels { | ||
| 162 | + if productMaterial, err := transform.TransformToProductMaterialDomainModelFromPgModels(productMaterialModel); err != nil { | ||
| 163 | + return 0, productMaterials, err | ||
| 164 | + } else { | ||
| 165 | + productMaterials = append(productMaterials, productMaterial) | ||
| 166 | + } | ||
| 167 | + } | ||
| 168 | + return int64(count), productMaterials, nil | ||
| 169 | + } | ||
| 170 | +} | ||
| 171 | +func NewProductMaterialRepository(transactionContext *pgTransaction.TransactionContext) (*ProductMaterialRepository, error) { | ||
| 172 | + if transactionContext == nil { | ||
| 173 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
| 174 | + } else { | ||
| 175 | + return &ProductMaterialRepository{ | ||
| 176 | + transactionContext: transactionContext, | ||
| 177 | + }, nil | ||
| 178 | + } | ||
| 179 | +} |
| 1 | +package controllers | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/linmadan/egglib-go/web/beego" | ||
| 5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/command" | ||
| 6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/query" | ||
| 7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productMaterial/service" | ||
| 8 | +) | ||
| 9 | + | ||
| 10 | +type ProductMaterialController struct { | ||
| 11 | + beego.BaseController | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +func (controller *ProductMaterialController) CreateProductMaterial() { | ||
| 15 | + productMaterialService := service.NewProductMaterialService(nil) | ||
| 16 | + cmd := &command.CreateProductMaterialCommand{} | ||
| 17 | + controller.Unmarshal(cmd) | ||
| 18 | + operateInfo := ParseOperateInfo(controller.BaseController) | ||
| 19 | + cmd.CompanyId = operateInfo.CompanyId | ||
| 20 | + cmd.OrgId = operateInfo.OrgId | ||
| 21 | + data, err := productMaterialService.CreateProductMaterial(operateInfo, cmd) | ||
| 22 | + controller.Response(data, err) | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +func (controller *ProductMaterialController) UpdateProductMaterial() { | ||
| 26 | + productMaterialService := service.NewProductMaterialService(nil) | ||
| 27 | + updateProductMaterialCommand := &command.UpdateProductMaterialCommand{} | ||
| 28 | + controller.Unmarshal(updateProductMaterialCommand) | ||
| 29 | + productMaterialId, _ := controller.GetInt(":productMaterialId") | ||
| 30 | + updateProductMaterialCommand.ProductMaterialId = productMaterialId | ||
| 31 | + data, err := productMaterialService.UpdateProductMaterial(updateProductMaterialCommand) | ||
| 32 | + controller.Response(data, err) | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +func (controller *ProductMaterialController) GetProductMaterial() { | ||
| 36 | + productMaterialService := service.NewProductMaterialService(nil) | ||
| 37 | + getProductMaterialQuery := &query.GetProductMaterialQuery{} | ||
| 38 | + productMaterialId, _ := controller.GetInt(":productMaterialId") | ||
| 39 | + getProductMaterialQuery.ProductMaterialId = productMaterialId | ||
| 40 | + data, err := productMaterialService.GetProductMaterial(getProductMaterialQuery) | ||
| 41 | + controller.Response(data, err) | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +func (controller *ProductMaterialController) RemoveProductMaterial() { | ||
| 45 | + productMaterialService := service.NewProductMaterialService(nil) | ||
| 46 | + removeProductMaterialCommand := &command.RemoveProductMaterialCommand{} | ||
| 47 | + controller.Unmarshal(removeProductMaterialCommand) | ||
| 48 | + productMaterialId, _ := controller.GetInt(":productMaterialId") | ||
| 49 | + removeProductMaterialCommand.ProductMaterialId = productMaterialId | ||
| 50 | + data, err := productMaterialService.RemoveProductMaterial(removeProductMaterialCommand) | ||
| 51 | + controller.Response(data, err) | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +func (controller *ProductMaterialController) ListProductMaterial() { | ||
| 55 | + productMaterialService := service.NewProductMaterialService(nil) | ||
| 56 | + listProductMaterialQuery := &query.ListProductMaterialQuery{} | ||
| 57 | + offset, _ := controller.GetInt("offset") | ||
| 58 | + listProductMaterialQuery.Offset = offset | ||
| 59 | + limit, _ := controller.GetInt("limit") | ||
| 60 | + listProductMaterialQuery.Limit = limit | ||
| 61 | + data, err := productMaterialService.ListProductMaterial(listProductMaterialQuery) | ||
| 62 | + controller.Response(data, err) | ||
| 63 | +} |
| 1 | +package routers | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/beego/beego/v2/server/web" | ||
| 5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +func init() { | ||
| 9 | + web.Router("/product-materials/", &controllers.ProductMaterialController{}, "Post:CreateProductMaterial") | ||
| 10 | + web.Router("/product-materials/:productMaterialId", &controllers.ProductMaterialController{}, "Put:UpdateProductMaterial") | ||
| 11 | + web.Router("/product-materials/:productMaterialId", &controllers.ProductMaterialController{}, "Get:GetProductMaterial") | ||
| 12 | + web.Router("/product-materials/:productMaterialId", &controllers.ProductMaterialController{}, "Delete:RemoveProductMaterial") | ||
| 13 | + web.Router("/product-materials/", &controllers.ProductMaterialController{}, "Get:ListProductMaterial") | ||
| 14 | +} |
| 1 | +package product_material | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "net/http" | ||
| 5 | + | ||
| 6 | + "github.com/gavv/httpexpect" | ||
| 7 | + . "github.com/onsi/ginkgo" | ||
| 8 | + . "github.com/onsi/gomega" | ||
| 9 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
| 10 | +) | ||
| 11 | + | ||
| 12 | +var _ = Describe("创建生产物料服务", func() { | ||
| 13 | + Describe("提交数据创建生产物料服务", func() { | ||
| 14 | + Context("提交正确的新生产物料数据", func() { | ||
| 15 | + It("返回生产物料数据", func() { | ||
| 16 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 17 | + body := map[string]interface{}{ | ||
| 18 | + "productMaterialGroupId": "int", | ||
| 19 | + "materialNumber": "string", | ||
| 20 | + "materialName": "string", | ||
| 21 | + "materialAttribute": "string", | ||
| 22 | + "materialCategory": "string", | ||
| 23 | + "productMaterialExt": "string", | ||
| 24 | + } | ||
| 25 | + httpExpect.POST("/product-materials/"). | ||
| 26 | + WithJSON(body). | ||
| 27 | + Expect(). | ||
| 28 | + Status(http.StatusOK). | ||
| 29 | + JSON(). | ||
| 30 | + Object(). | ||
| 31 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 33 | + ContainsKey("data").Value("data").Object(). | ||
| 34 | + ContainsKey("productMaterialId").ValueNotEqual("productMaterialId", BeZero()) | ||
| 35 | + }) | ||
| 36 | + }) | ||
| 37 | + }) | ||
| 38 | + AfterEach(func() { | ||
| 39 | + _, err := pG.DB.Exec("DELETE FROM product_materials WHERE true") | ||
| 40 | + Expect(err).NotTo(HaveOccurred()) | ||
| 41 | + }) | ||
| 42 | +}) |
| 1 | +package product_material | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("返回生产物料服务", func() { | ||
| 14 | + var productMaterialId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&productMaterialId), | ||
| 18 | + "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", | ||
| 19 | + "testProductMaterialId", "testCompanyId", "testOrgId", "testProductMaterialGroupId", "testMaterialNumber", "testMaterialName", "testMaterialAttribute", "testMaterialCategory", "testProductMaterialExt", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testExt") | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("根据productMaterialId参数返回生产物料", func() { | ||
| 23 | + Context("传入有效的productMaterialId", func() { | ||
| 24 | + It("返回生产物料数据", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + httpExpect.GET("/product-materials/{productMaterialId}"). | ||
| 27 | + Expect(). | ||
| 28 | + Status(http.StatusOK). | ||
| 29 | + JSON(). | ||
| 30 | + Object(). | ||
| 31 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 33 | + ContainsKey("data").Value("data").Object() | ||
| 34 | + }) | ||
| 35 | + }) | ||
| 36 | + }) | ||
| 37 | + AfterEach(func() { | ||
| 38 | + _, err := pG.DB.Exec("DELETE FROM product_materials WHERE true") | ||
| 39 | + Expect(err).NotTo(HaveOccurred()) | ||
| 40 | + }) | ||
| 41 | +}) |
| 1 | +package product_material | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("返回生产物料服务列表", func() { | ||
| 14 | + var productMaterialId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&productMaterialId), | ||
| 18 | + "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", | ||
| 19 | + "testProductMaterialId", "testCompanyId", "testOrgId", "testProductMaterialGroupId", "testMaterialNumber", "testMaterialName", "testMaterialAttribute", "testMaterialCategory", "testProductMaterialExt", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testExt") | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("根据参数返回生产物料列表", func() { | ||
| 23 | + Context("传入有效的参数", func() { | ||
| 24 | + It("返回生产物料数据列表", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + httpExpect.GET("/product-materials/"). | ||
| 27 | + WithQuery("offset", "int"). | ||
| 28 | + WithQuery("limit", "int"). | ||
| 29 | + Expect(). | ||
| 30 | + Status(http.StatusOK). | ||
| 31 | + JSON(). | ||
| 32 | + Object(). | ||
| 33 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 34 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 35 | + ContainsKey("data").Value("data").Object(). | ||
| 36 | + ContainsKey("count").ValueEqual("count", 1). | ||
| 37 | + ContainsKey("productMaterials").Value("productMaterials").Array() | ||
| 38 | + }) | ||
| 39 | + }) | ||
| 40 | + }) | ||
| 41 | + AfterEach(func() { | ||
| 42 | + _, err := pG.DB.Exec("DELETE FROM product_materials WHERE true") | ||
| 43 | + Expect(err).NotTo(HaveOccurred()) | ||
| 44 | + }) | ||
| 45 | +}) |
| 1 | +package product_material | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "net/http" | ||
| 5 | + "net/http/httptest" | ||
| 6 | + "testing" | ||
| 7 | + | ||
| 8 | + "github.com/beego/beego/v2/server/web" | ||
| 9 | + . "github.com/onsi/ginkgo" | ||
| 10 | + . "github.com/onsi/gomega" | ||
| 11 | + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application" | ||
| 12 | + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
| 13 | + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego" | ||
| 14 | +) | ||
| 15 | + | ||
| 16 | +func TestProductMaterial(t *testing.T) { | ||
| 17 | + RegisterFailHandler(Fail) | ||
| 18 | + RunSpecs(t, "Beego Port ProductMaterial Correlations Test Case Suite") | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +var handler http.Handler | ||
| 22 | +var server *httptest.Server | ||
| 23 | + | ||
| 24 | +var _ = BeforeSuite(func() { | ||
| 25 | + handler = web.BeeApp.Handlers | ||
| 26 | + server = httptest.NewServer(handler) | ||
| 27 | +}) | ||
| 28 | + | ||
| 29 | +var _ = AfterSuite(func() { | ||
| 30 | + server.Close() | ||
| 31 | +}) |
| 1 | +package product_material | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("移除生产物料服务", func() { | ||
| 14 | + var productMaterialId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&productMaterialId), | ||
| 18 | + "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", | ||
| 19 | + "testProductMaterialId", "testCompanyId", "testOrgId", "testProductMaterialGroupId", "testMaterialNumber", "testMaterialName", "testMaterialAttribute", "testMaterialCategory", "testProductMaterialExt", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testExt") | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("根据参数移除生产物料服务", func() { | ||
| 23 | + Context("传入有效的productMaterialId", func() { | ||
| 24 | + It("返回被移除生产物料的数据", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + httpExpect.DELETE("/product-materials/{productMaterialId}"). | ||
| 27 | + Expect(). | ||
| 28 | + Status(http.StatusOK). | ||
| 29 | + JSON(). | ||
| 30 | + Object(). | ||
| 31 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 33 | + ContainsKey("data").Value("data").Object() | ||
| 34 | + }) | ||
| 35 | + }) | ||
| 36 | + }) | ||
| 37 | + AfterEach(func() { | ||
| 38 | + _, err := pG.DB.Exec("DELETE FROM product_materials WHERE true") | ||
| 39 | + Expect(err).NotTo(HaveOccurred()) | ||
| 40 | + }) | ||
| 41 | +}) |
| 1 | +package product_material | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "github.com/go-pg/pg/v10" | ||
| 5 | + "net/http" | ||
| 6 | + | ||
| 7 | + "github.com/gavv/httpexpect" | ||
| 8 | + . "github.com/onsi/ginkgo" | ||
| 9 | + . "github.com/onsi/gomega" | ||
| 10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +var _ = Describe("更新生产物料服务", func() { | ||
| 14 | + var productMaterialId int64 | ||
| 15 | + BeforeEach(func() { | ||
| 16 | + _, err := pG.DB.QueryOne( | ||
| 17 | + pg.Scan(&productMaterialId), | ||
| 18 | + "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", | ||
| 19 | + "testProductMaterialId", "testCompanyId", "testOrgId", "testProductMaterialGroupId", "testMaterialNumber", "testMaterialName", "testMaterialAttribute", "testMaterialCategory", "testProductMaterialExt", "testCreatedAt", "testUpdatedAt", "testDeletedAt", "testExt") | ||
| 20 | + Expect(err).NotTo(HaveOccurred()) | ||
| 21 | + }) | ||
| 22 | + Describe("提交数据更新生产物料服务", func() { | ||
| 23 | + Context("提交正确的生产物料数据", func() { | ||
| 24 | + It("返回更新后的生产物料数据", func() { | ||
| 25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
| 26 | + body := map[string]interface{}{} | ||
| 27 | + httpExpect.PUT("/product-materials/{productMaterialId}"). | ||
| 28 | + WithJSON(body). | ||
| 29 | + Expect(). | ||
| 30 | + Status(http.StatusOK). | ||
| 31 | + JSON(). | ||
| 32 | + Object(). | ||
| 33 | + ContainsKey("code").ValueEqual("code", 0). | ||
| 34 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
| 35 | + ContainsKey("data").Value("data").Object(). | ||
| 36 | + ContainsKey("productMaterialId").ValueEqual("productMaterialId", productMaterialId) | ||
| 37 | + }) | ||
| 38 | + }) | ||
| 39 | + }) | ||
| 40 | + AfterEach(func() { | ||
| 41 | + _, err := pG.DB.Exec("DELETE FROM product_materials WHERE true") | ||
| 42 | + Expect(err).NotTo(HaveOccurred()) | ||
| 43 | + }) | ||
| 44 | +}) |
-
请 注册 或 登录 后发表评论