作者 yangfu

产品管理

@@ -9,6 +9,7 @@ require ( @@ -9,6 +9,7 @@ require (
9 github.com/fatih/structs v1.1.0 // indirect 9 github.com/fatih/structs v1.1.0 // indirect
10 github.com/gavv/httpexpect v2.0.0+incompatible 10 github.com/gavv/httpexpect v2.0.0+incompatible
11 github.com/go-pg/pg/v10 v10.9.0 11 github.com/go-pg/pg/v10 v10.9.0
  12 + github.com/go-redis/redis v6.15.7+incompatible
12 github.com/google/go-querystring v1.1.0 // indirect 13 github.com/google/go-querystring v1.1.0 // indirect
13 github.com/imkira/go-interpol v1.1.0 // indirect 14 github.com/imkira/go-interpol v1.1.0 // indirect
14 github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 15 github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7
@@ -17,6 +18,7 @@ require ( @@ -17,6 +18,7 @@ require (
17 github.com/onsi/gomega v1.11.0 18 github.com/onsi/gomega v1.11.0
18 github.com/sergi/go-diff v1.2.0 // indirect 19 github.com/sergi/go-diff v1.2.0 // indirect
19 github.com/smartystreets/goconvey v1.7.2 // indirect 20 github.com/smartystreets/goconvey v1.7.2 // indirect
  21 + github.com/tal-tech/go-zero v1.0.27
20 github.com/valyala/fasthttp v1.32.0 // indirect 22 github.com/valyala/fasthttp v1.32.0 // indirect
21 github.com/xeipuuv/gojsonschema v1.2.0 // indirect 23 github.com/xeipuuv/gojsonschema v1.2.0 // indirect
22 github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect 24 github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "github.com/beego/beego/v2/server/web" 5 "github.com/beego/beego/v2/server/web"
6 "github.com/linmadan/egglib-go/log/logrus" 6 "github.com/linmadan/egglib-go/log/logrus"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
9 10
10 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 11 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
@@ -28,6 +29,7 @@ func main() { @@ -28,6 +29,7 @@ func main() {
28 MaxSize: 1024 * 1024 * 2, 29 MaxSize: 1024 * 1024 * 2,
29 }) 30 })
30 log.Logger.AddHook(bw) 31 log.Logger.AddHook(bw)
  32 + redis.InitRedis()
31 33
32 log.Logger.Info("server start!") 34 log.Logger.Info("server start!")
33 web.Run() 35 web.Run()
@@ -14,23 +14,24 @@ type CreateProductCommand struct { @@ -14,23 +14,24 @@ type CreateProductCommand struct {
14 // 组织ID 14 // 组织ID
15 OrgId int `cname:"组织ID" json:"orgId" valid:"Required"` 15 OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`
16 // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001 16 // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
17 - ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode" valid:"Required"` 17 + ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode"`
18 // 产品名称 18 // 产品名称
19 ProductName string `cname:"产品名称" json:"productName" valid:"Required"` 19 ProductName string `cname:"产品名称" json:"productName" valid:"Required"`
20 // 产品类别 20 // 产品类别
21 ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"` 21 ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"`
22 // 数量(保留两位小数) 22 // 数量(保留两位小数)
23 - Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"` 23 + //Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"`
24 // 单位 24 // 单位
25 Unit string `cname:"单位" json:"unit" valid:"Required"` 25 Unit string `cname:"单位" json:"unit" valid:"Required"`
26 // 单份重量(原材料) 26 // 单份重量(原材料)
27 UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"` 27 UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"`
28 // 重量 28 // 重量
29 - Weight float64 `cname:"重量" json:"weight" valid:"Required"` 29 + //Weight float64 `cname:"重量" json:"weight" valid:"Required"`
30 } 30 }
31 31
32 func (createProductCommand *CreateProductCommand) Valid(validation *validation.Validation) { 32 func (createProductCommand *CreateProductCommand) Valid(validation *validation.Validation) {
33 - validation.SetError("CustomValid", "未实现的自定义认证") 33 +
  34 + //validation.SetError("CustomValid", "未实现的自定义认证")
34 } 35 }
35 36
36 func (createProductCommand *CreateProductCommand) ValidateCommand() error { 37 func (createProductCommand *CreateProductCommand) ValidateCommand() error {
@@ -14,7 +14,7 @@ type RemoveProductCommand struct { @@ -14,7 +14,7 @@ type RemoveProductCommand struct {
14 } 14 }
15 15
16 func (removeProductCommand *RemoveProductCommand) Valid(validation *validation.Validation) { 16 func (removeProductCommand *RemoveProductCommand) Valid(validation *validation.Validation) {
17 - validation.SetError("CustomValid", "未实现的自定义认证") 17 + //validation.SetError("CustomValid", "未实现的自定义认证")
18 } 18 }
19 19
20 func (removeProductCommand *RemoveProductCommand) ValidateCommand() error { 20 func (removeProductCommand *RemoveProductCommand) ValidateCommand() error {
@@ -12,23 +12,23 @@ type UpdateProductCommand struct { @@ -12,23 +12,23 @@ type UpdateProductCommand struct {
12 // 产品ID 12 // 产品ID
13 ProductId int `cname:"产品ID" json:"productId" valid:"Required"` 13 ProductId int `cname:"产品ID" json:"productId" valid:"Required"`
14 // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001 14 // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
15 - ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode" valid:"Required"` 15 + ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode"`
16 // 产品名称 16 // 产品名称
17 ProductName string `cname:"产品名称" json:"productName" valid:"Required"` 17 ProductName string `cname:"产品名称" json:"productName" valid:"Required"`
18 // 产品类别 18 // 产品类别
19 ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"` 19 ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"`
20 // 数量(保留两位小数) 20 // 数量(保留两位小数)
21 - Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"` 21 + //Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"`
22 // 单位 22 // 单位
23 Unit string `cname:"单位" json:"unit" valid:"Required"` 23 Unit string `cname:"单位" json:"unit" valid:"Required"`
24 // 单份重量(原材料) 24 // 单份重量(原材料)
25 UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"` 25 UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"`
26 // 重量 26 // 重量
27 - Weight float64 `cname:"重量" json:"weight" valid:"Required"` 27 + //Weight float64 `cname:"重量" json:"weight" valid:"Required"`
28 } 28 }
29 29
30 func (updateProductCommand *UpdateProductCommand) Valid(validation *validation.Validation) { 30 func (updateProductCommand *UpdateProductCommand) Valid(validation *validation.Validation) {
31 - validation.SetError("CustomValid", "未实现的自定义认证") 31 + //validation.SetError("CustomValid", "未实现的自定义认证")
32 } 32 }
33 33
34 func (updateProductCommand *UpdateProductCommand) ValidateCommand() error { 34 func (updateProductCommand *UpdateProductCommand) ValidateCommand() error {
  1 +package dto
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  4 +
  5 +type ProductDto struct {
  6 + // 产品ID
  7 + ProductId int `json:"productId,omitempty"`
  8 + // 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
  9 + ProductCode string `json:"productCode,omitempty"`
  10 + // 产品名称
  11 + ProductName string `json:"productName,omitempty"`
  12 + // 产品类别
  13 + ProductCategory string `json:"productCategory,omitempty"`
  14 + // 产品规格
  15 + *domain.UnitQuantity
  16 +}
  17 +
  18 +func (d *ProductDto) LoadDto(m *domain.Product) *ProductDto {
  19 + d.ProductId = m.ProductId
  20 + d.ProductCode = m.ProductCode
  21 + d.ProductName = m.ProductName
  22 + d.ProductCategory = m.ProductCategory
  23 + d.UnitQuantity = m.ProductSpec
  24 + return d
  25 +}
@@ -14,7 +14,7 @@ type GetProductQuery struct { @@ -14,7 +14,7 @@ type GetProductQuery struct {
14 } 14 }
15 15
16 func (getProductQuery *GetProductQuery) Valid(validation *validation.Validation) { 16 func (getProductQuery *GetProductQuery) Valid(validation *validation.Validation) {
17 - validation.SetError("CustomValid", "未实现的自定义认证") 17 + //validation.SetError("CustomValid", "未实现的自定义认证")
18 } 18 }
19 19
20 func (getProductQuery *GetProductQuery) ValidateQuery() error { 20 func (getProductQuery *GetProductQuery) ValidateQuery() error {
@@ -10,13 +10,13 @@ import ( @@ -10,13 +10,13 @@ import (
10 10
11 type ListProductQuery struct { 11 type ListProductQuery struct {
12 // 查询偏离量 12 // 查询偏离量
13 - Offset int `cname:"查询偏离量" json:"offset" valid:"Required"` 13 + Offset int `cname:"查询偏离量" json:"offset"`
14 // 查询限制 14 // 查询限制
15 Limit int `cname:"查询限制" json:"limit" valid:"Required"` 15 Limit int `cname:"查询限制" json:"limit" valid:"Required"`
16 } 16 }
17 17
18 func (listProductQuery *ListProductQuery) Valid(validation *validation.Validation) { 18 func (listProductQuery *ListProductQuery) Valid(validation *validation.Validation) {
19 - validation.SetError("CustomValid", "未实现的自定义认证") 19 + //validation.SetError("CustomValid", "未实现的自定义认证")
20 } 20 }
21 21
22 func (listProductQuery *ListProductQuery) ValidateQuery() error { 22 func (listProductQuery *ListProductQuery) ValidateQuery() error {
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type SearchProductQuery struct {
  13 + // 查询偏离量
  14 + Offset int `cname:"查询偏离量" json:"offset"`
  15 + // 查询限制
  16 + Limit int `cname:"查询限制" json:"limit"`
  17 + // 当前公司
  18 + CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
  19 + // 当前登录的组织
  20 + OrgId int `cname:"当前登录的组织" json:"orgId,omitempty" valid:"Required"`
  21 + // 页码
  22 + PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
  23 + // 页数
  24 + PageSize int `cname:"页数" json:"pageSize,omitempty"`
  25 + // 产品名称
  26 + ProductName string `json:"productName,omitempty"`
  27 + // 产品类别
  28 + ProductCategory string `cname:"产品类别" json:"productCategory"`
  29 +}
  30 +
  31 +func (cmd *SearchProductQuery) Valid(validation *validation.Validation) {
  32 + cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
  33 + //validation.SetError("CustomValid", "未实现的自定义认证")
  34 +}
  35 +
  36 +func (cmd *SearchProductQuery) ValidateQuery() error {
  37 + valid := validation.Validation{}
  38 + b, err := valid.Valid(cmd)
  39 + if err != nil {
  40 + return err
  41 + }
  42 + if !b {
  43 + elem := reflect.TypeOf(cmd).Elem()
  44 + for _, validErr := range valid.Errors {
  45 + field, isExist := elem.FieldByName(validErr.Field)
  46 + if isExist {
  47 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  48 + } else {
  49 + return fmt.Errorf(validErr.Message)
  50 + }
  51 + }
  52 + }
  53 + return nil
  54 +}
@@ -6,8 +6,13 @@ import ( @@ -6,8 +6,13 @@ import (
6 "github.com/linmadan/egglib-go/utils/tool_funs" 6 "github.com/linmadan/egglib-go/utils/tool_funs"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/command" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/dto"
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/query" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/query"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
  13 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
  14 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
  15 + "time"
11 ) 16 )
12 17
13 // 产品服务 18 // 产品服务
@@ -29,22 +34,36 @@ func (productService *ProductService) CreateProduct(createProductCommand *comman @@ -29,22 +34,36 @@ func (productService *ProductService) CreateProduct(createProductCommand *comman
29 defer func() { 34 defer func() {
30 transactionContext.RollbackTransaction() 35 transactionContext.RollbackTransaction()
31 }() 36 }()
  37 +
  38 + if len(createProductCommand.ProductCode) == 0 {
  39 + generator := redis.NewProductCodeCache(createProductCommand.CompanyId)
  40 + code, err := redis.GenCode(generator)
  41 + if err != nil {
  42 + log.Logger.Error(err.Error())
  43 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
  44 + }
  45 + createProductCommand.ProductCode = code
  46 + }
  47 +
32 newProduct := &domain.Product{ 48 newProduct := &domain.Product{
33 CompanyId: createProductCommand.CompanyId, 49 CompanyId: createProductCommand.CompanyId,
34 OrgId: createProductCommand.OrgId, 50 OrgId: createProductCommand.OrgId,
35 ProductCode: createProductCommand.ProductCode, 51 ProductCode: createProductCommand.ProductCode,
36 ProductName: createProductCommand.ProductName, 52 ProductName: createProductCommand.ProductName,
37 ProductCategory: createProductCommand.ProductCategory, 53 ProductCategory: createProductCommand.ProductCategory,
38 - //ProductSpec: createProductCommand.ProductSpec,  
39 - }  
40 - var productRepository domain.ProductRepository  
41 - if value, err := factory.CreateProductRepository(map[string]interface{}{  
42 - "transactionContext": transactionContext,  
43 - }); err != nil {  
44 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
45 - } else {  
46 - productRepository = value 54 + ProductSpec: &domain.UnitQuantity{
  55 + Unit: createProductCommand.Unit,
  56 + UnitWeight: createProductCommand.UnitWeight,
  57 + },
  58 + CreatedAt: time.Now(),
  59 + UpdatedAt: time.Now(),
  60 + }
  61 + productRepository, _, _ := factory.FastPgProduct(transactionContext, 0)
  62 +
  63 + if item, err := productRepository.FindOne(map[string]interface{}{"companyId": createProductCommand.CompanyId, "productCode": createProductCommand.ProductCode}); err == nil && item != nil {
  64 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "产品编号重复,请重新提交")
47 } 65 }
  66 +
48 if product, err := productRepository.Save(newProduct); err != nil { 67 if product, err := productRepository.Save(newProduct); err != nil {
49 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 68 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
50 } else { 69 } else {
@@ -88,7 +107,8 @@ func (productService *ProductService) GetProduct(getProductQuery *query.GetProdu @@ -88,7 +107,8 @@ func (productService *ProductService) GetProduct(getProductQuery *query.GetProdu
88 if err := transactionContext.CommitTransaction(); err != nil { 107 if err := transactionContext.CommitTransaction(); err != nil {
89 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 108 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
90 } 109 }
91 - return product, nil 110 + result := &dto.ProductDto{}
  111 + return result.LoadDto(product), nil
92 } 112 }
93 } 113 }
94 114
@@ -183,20 +203,23 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman @@ -183,20 +203,23 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman
183 defer func() { 203 defer func() {
184 transactionContext.RollbackTransaction() 204 transactionContext.RollbackTransaction()
185 }() 205 }()
186 - var productRepository domain.ProductRepository  
187 - if value, err := factory.CreateProductRepository(map[string]interface{}{  
188 - "transactionContext": transactionContext,  
189 - }); err != nil {  
190 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
191 - } else {  
192 - productRepository = value  
193 - }  
194 - product, err := productRepository.FindOne(map[string]interface{}{"productId": updateProductCommand.ProductId}) 206 + productRepository, product, err := factory.FastPgProduct(transactionContext, updateProductCommand.ProductId)
195 if err != nil { 207 if err != nil {
196 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 208 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
197 } 209 }
198 - if product == nil {  
199 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductCommand.ProductId))) 210 + if len(updateProductCommand.ProductCode) == 0 {
  211 + generator := redis.NewProductCodeCache(product.CompanyId)
  212 + code, err := redis.GenCode(generator)
  213 + if err != nil {
  214 + log.Logger.Error(err.Error())
  215 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
  216 + }
  217 + updateProductCommand.ProductCode = code
  218 + }
  219 + if updateProductCommand.ProductCode != product.ProductCode {
  220 + if item, err := productRepository.FindOne(map[string]interface{}{"companyId": product.CompanyId, "productCode": updateProductCommand.ProductCode}); err == nil && item != nil {
  221 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "产品编号重复,请重新提交")
  222 + }
200 } 223 }
201 if err := product.Update(tool_funs.SimpleStructToMap(updateProductCommand)); err != nil { 224 if err := product.Update(tool_funs.SimpleStructToMap(updateProductCommand)); err != nil {
202 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 225 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
@@ -211,6 +234,47 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman @@ -211,6 +234,47 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman
211 } 234 }
212 } 235 }
213 236
  237 +// 返回产品服务列表
  238 +func (productService *ProductService) SearchProduct(operateInfo *domain.OperateInfo, listProductQuery *query.SearchProductQuery) (int64, interface{}, error) {
  239 + listProductQuery.OrgId = operateInfo.OrgId
  240 + listProductQuery.CompanyId = operateInfo.CompanyId
  241 + if err := listProductQuery.ValidateQuery(); err != nil {
  242 + return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
  243 + }
  244 + transactionContext, err := factory.CreateTransactionContext(nil)
  245 + if err != nil {
  246 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  247 + }
  248 + if err := transactionContext.StartTransaction(); err != nil {
  249 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  250 + }
  251 + defer func() {
  252 + transactionContext.RollbackTransaction()
  253 + }()
  254 + var productRepository domain.ProductRepository
  255 + if value, err := factory.CreateProductRepository(map[string]interface{}{
  256 + "transactionContext": transactionContext,
  257 + }); err != nil {
  258 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  259 + } else {
  260 + productRepository = value
  261 + }
  262 + count, products, err := productRepository.Find(utils.ObjectToMap(listProductQuery))
  263 + if err != nil {
  264 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  265 + }
  266 + if err := transactionContext.CommitTransaction(); err != nil {
  267 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  268 + }
  269 + var result = make([]*dto.ProductDto, 0)
  270 + for i := range products {
  271 + newItem := &dto.ProductDto{}
  272 + result = append(result, newItem.LoadDto(products[i]))
  273 + }
  274 + return count, result, nil
  275 +
  276 +}
  277 +
214 func NewProductService(options map[string]interface{}) *ProductService { 278 func NewProductService(options map[string]interface{}) *ProductService {
215 newProductService := &ProductService{} 279 newProductService := &ProductService{}
216 return newProductService 280 return newProductService
@@ -2,6 +2,7 @@ package command @@ -2,6 +2,7 @@ package command
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
5 "reflect" 6 "reflect"
6 "strings" 7 "strings"
7 8
@@ -31,6 +32,10 @@ type CreateProductGroupCommand struct { @@ -31,6 +32,10 @@ type CreateProductGroupCommand struct {
31 32
32 func (createProductGroupCommand *CreateProductGroupCommand) Valid(validation *validation.Validation) { 33 func (createProductGroupCommand *CreateProductGroupCommand) Valid(validation *validation.Validation) {
33 //validation.SetError("CustomValid", "未实现的自定义认证") 34 //validation.SetError("CustomValid", "未实现的自定义认证")
  35 + if err := domain.ValidWorkOn(createProductGroupCommand.WorkOn); err != nil {
  36 + validation.SetError("workOn", err.Error())
  37 + return
  38 + }
34 } 39 }
35 40
36 func (createProductGroupCommand *CreateProductGroupCommand) ValidateCommand() error { 41 func (createProductGroupCommand *CreateProductGroupCommand) ValidateCommand() error {
@@ -4,6 +4,15 @@ import "fmt" @@ -4,6 +4,15 @@ import "fmt"
4 4
5 const MaxQueryRow = 10000 5 const MaxQueryRow = 10000
6 6
  7 +const (
  8 + WorkOnFullDay = 1 //全天
  9 + WorkOnDay = 2 //白班
  10 + WorkOnMidDay = 4 //中班
  11 + WorkOnNight = 8 //夜班
  12 +)
  13 +
  14 +const WorkOn = WorkOnFullDay | WorkOnDay | WorkOnMidDay | WorkOnNight
  15 +
7 var ( 16 var (
8 ErrorNotFound = fmt.Errorf("没有此资源") 17 ErrorNotFound = fmt.Errorf("没有此资源")
9 ) 18 )
@@ -78,3 +87,10 @@ func Pagination(pageNumber, pageSize int) (offset int, limit int) { @@ -78,3 +87,10 @@ func Pagination(pageNumber, pageSize int) (offset int, limit int) {
78 } 87 }
79 return 88 return
80 } 89 }
  90 +
  91 +func ValidWorkOn(workOn int) error {
  92 + if (workOn & WorkOn) == 0 {
  93 + return fmt.Errorf("班次有误")
  94 + }
  95 + return nil
  96 +}
@@ -41,12 +41,6 @@ func (product *Product) Identify() interface{} { @@ -41,12 +41,6 @@ func (product *Product) Identify() interface{} {
41 } 41 }
42 42
43 func (product *Product) Update(data map[string]interface{}) error { 43 func (product *Product) Update(data map[string]interface{}) error {
44 - if companyId, ok := data["companyId"]; ok {  
45 - product.CompanyId = companyId.(int)  
46 - }  
47 - if orgId, ok := data["orgId"]; ok {  
48 - product.OrgId = orgId.(int)  
49 - }  
50 if productCode, ok := data["productCode"]; ok { 44 if productCode, ok := data["productCode"]; ok {
51 product.ProductCode = productCode.(string) 45 product.ProductCode = productCode.(string)
52 } 46 }
@@ -56,26 +50,15 @@ func (product *Product) Update(data map[string]interface{}) error { @@ -56,26 +50,15 @@ func (product *Product) Update(data map[string]interface{}) error {
56 if productCategory, ok := data["productCategory"]; ok { 50 if productCategory, ok := data["productCategory"]; ok {
57 product.ProductCategory = productCategory.(string) 51 product.ProductCategory = productCategory.(string)
58 } 52 }
59 - if quantity, ok := data["quantity"]; ok {  
60 - product.ProductSpec.Quantity = quantity.(float64)  
61 - } 53 + //if quantity, ok := data["quantity"]; ok {
  54 + // product.ProductSpec.Quantity = quantity.(float64)
  55 + //}
62 if unit, ok := data["unit"]; ok { 56 if unit, ok := data["unit"]; ok {
63 product.ProductSpec.Unit = unit.(string) 57 product.ProductSpec.Unit = unit.(string)
64 } 58 }
65 if unitWeight, ok := data["unitWeight"]; ok { 59 if unitWeight, ok := data["unitWeight"]; ok {
66 product.ProductSpec.UnitWeight = unitWeight.(float64) 60 product.ProductSpec.UnitWeight = unitWeight.(float64)
67 } 61 }
68 - if weight, ok := data["weight"]; ok {  
69 - product.ProductSpec.Weight = weight.(float64)  
70 - }  
71 - if createdAt, ok := data["createdAt"]; ok {  
72 - product.CreatedAt = createdAt.(time.Time)  
73 - }  
74 - if updatedAt, ok := data["updatedAt"]; ok {  
75 - product.UpdatedAt = updatedAt.(time.Time)  
76 - }  
77 - if deletedAt, ok := data["deletedAt"]; ok {  
78 - product.DeletedAt = deletedAt.(time.Time)  
79 - } 62 + product.UpdatedAt = time.Now()
80 return nil 63 return nil
81 } 64 }
@@ -2,7 +2,7 @@ package example @@ -2,7 +2,7 @@ package example
2 2
3 import ( 3 import (
4 "github.com/linmadan/egglib-go/utils/json" 4 "github.com/linmadan/egglib-go/utils/json"
5 - "gitlab.fjmaimaimai.com/allied-creation/allied-lib/gateway/allied_creation_user" 5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway/allied_creation_user"
6 "testing" 6 "testing"
7 ) 7 )
8 8
  1 +package redis
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/go-redis/redis"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
  7 + "time"
  8 +)
  9 +
  10 +func GenCode(ca CacheCode) (string, error) {
  11 + key := ca.CacheKey()
  12 + client := GetRedis()
  13 + result := client.Get(key)
  14 + index, err := result.Int()
  15 + if err == redis.Nil {
  16 + index = 1
  17 + err = nil
  18 + if ret := client.Set(key, index, ca.Expire()); ret.Err() != nil {
  19 + return "", ret.Err()
  20 + }
  21 + return ca.Format(index), nil
  22 + }
  23 + if err != nil {
  24 + return "", err
  25 + }
  26 + if ret := client.Incr(key); ret.Err() != nil {
  27 + return "", ret.Err()
  28 + } else {
  29 + index = int(ret.Val())
  30 + }
  31 + return ca.Format(index), nil
  32 +}
  33 +
  34 +type CacheCode interface {
  35 + CacheKey() string
  36 + Expire() time.Duration
  37 + Format(index int) string
  38 +}
  39 +
  40 +type ProductCodeCache struct {
  41 + CompanyId int
  42 + Time time.Time
  43 +}
  44 +
  45 +func (ca ProductCodeCache) CacheKey() string {
  46 + str := fmt.Sprintf("%v:product-code:%v:%v", constant.CACHE_PREFIX, ca.CompanyId, ca.Time.Format("20060102"))
  47 + return str
  48 +}
  49 +func (ca ProductCodeCache) Format(index int) string {
  50 + if index <= 999 {
  51 + return fmt.Sprintf("CP%v%03d", ca.Time.Format("060102"), index)
  52 + }
  53 + return fmt.Sprintf("CP%v%d", ca.Time.Format("060102"), index)
  54 +}
  55 +func (ca ProductCodeCache) Expire() time.Duration {
  56 + return time.Hour * 24
  57 +}
  58 +func NewProductCodeCache(id int) ProductCodeCache {
  59 + return ProductCodeCache{
  60 + CompanyId: id,
  61 + Time: time.Now(),
  62 + }
  63 +}
  1 +package redis
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
  6 +
  7 + "github.com/go-redis/redis"
  8 +)
  9 +
  10 +var clientRedis *redis.Client
  11 +
  12 +func InitRedis() *redis.Client {
  13 + options := redis.Options{
  14 + Network: "tcp",
  15 + Addr: fmt.Sprintf("%s:%s", constant.REDIS_HOST, constant.REDIS_PORT),
  16 + Dialer: nil,
  17 + OnConnect: nil,
  18 + Password: "",
  19 + DB: 0,
  20 + MaxRetries: 0,
  21 + MinRetryBackoff: 0,
  22 + MaxRetryBackoff: 0,
  23 + DialTimeout: 0,
  24 + ReadTimeout: 0,
  25 + WriteTimeout: 0,
  26 + PoolSize: 0,
  27 + MinIdleConns: 0,
  28 + MaxConnAge: 0,
  29 + PoolTimeout: 0,
  30 + IdleTimeout: 0,
  31 + IdleCheckFrequency: 0,
  32 + TLSConfig: nil,
  33 + }
  34 + // 新建一个client
  35 + clientRedis = redis.NewClient(&options)
  36 + pong, err := clientRedis.Ping().Result()
  37 + if err != nil {
  38 + e := fmt.Errorf("redis 连接失败,%s,%w", pong, err)
  39 + panic(e)
  40 + }
  41 + return clientRedis
  42 +}
  43 +
  44 +func GetRedis() *redis.Client {
  45 + return clientRedis
  46 +}
@@ -122,6 +122,7 @@ func (repository *ProductGroupRepository) FindOne(queryOptions map[string]interf @@ -122,6 +122,7 @@ func (repository *ProductGroupRepository) FindOne(queryOptions map[string]interf
122 query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId") 122 query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId")
123 query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId") 123 query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId")
124 query.SetWhereByQueryOption("work_station->>'lineId'='?'", "lineId") 124 query.SetWhereByQueryOption("work_station->>'lineId'='?'", "lineId")
  125 + query.AllWithDeleted()
125 if err := query.First(); err != nil { 126 if err := query.First(); err != nil {
126 if err.Error() == "pg: no rows in result set" { 127 if err.Error() == "pg: no rows in result set" {
127 return nil, fmt.Errorf("没有此资源") 128 return nil, fmt.Errorf("没有此资源")
@@ -117,6 +117,7 @@ func (repository *ProductJobRepository) FindOne(queryOptions map[string]interfac @@ -117,6 +117,7 @@ func (repository *ProductJobRepository) FindOne(queryOptions map[string]interfac
117 query.SetWhereByQueryOption("org_id = ?", "orgId") 117 query.SetWhereByQueryOption("org_id = ?", "orgId")
118 query.SetWhereByQueryOption("job_name=?", "jobName") 118 query.SetWhereByQueryOption("job_name=?", "jobName")
119 query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId") 119 query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId")
  120 + query.AllWithDeleted()
120 if err := query.First(); err != nil { 121 if err := query.First(); err != nil {
121 if err.Error() == "pg: no rows in result set" { 122 if err.Error() == "pg: no rows in result set" {
122 return nil, fmt.Errorf("没有此资源") 123 return nil, fmt.Errorf("没有此资源")
@@ -37,19 +37,13 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod @@ -37,19 +37,13 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod
37 "updated_at", 37 "updated_at",
38 "deleted_at", 38 "deleted_at",
39 } 39 }
40 - insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)  
41 - insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields) 40 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_id", "deleted_at"))
  41 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_id", "deleted_at"))
42 returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) 42 returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
43 - updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_id") 43 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_id", "deleted_at")
44 updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) 44 updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
45 tx := repository.transactionContext.PgTx 45 tx := repository.transactionContext.PgTx
46 if product.Identify() == nil { 46 if product.Identify() == nil {
47 - productId, err := repository.nextIdentify()  
48 - if err != nil {  
49 - return product, err  
50 - } else {  
51 - product.ProductId = int(productId)  
52 - }  
53 if _, err := tx.QueryOne( 47 if _, err := tx.QueryOne(
54 pg.Scan( 48 pg.Scan(
55 &product.CompanyId, 49 &product.CompanyId,
@@ -63,17 +57,17 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod @@ -63,17 +57,17 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod
63 &product.UpdatedAt, 57 &product.UpdatedAt,
64 &product.DeletedAt, 58 &product.DeletedAt,
65 ), 59 ),
66 - fmt.Sprintf("INSERT INTO products (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), 60 + fmt.Sprintf("INSERT INTO manufacture.product (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
67 product.CompanyId, 61 product.CompanyId,
68 product.OrgId, 62 product.OrgId,
69 - product.ProductId, 63 + //product.ProductId,
70 product.ProductCode, 64 product.ProductCode,
71 product.ProductName, 65 product.ProductName,
72 product.ProductCategory, 66 product.ProductCategory,
73 product.ProductSpec, 67 product.ProductSpec,
74 product.CreatedAt, 68 product.CreatedAt,
75 product.UpdatedAt, 69 product.UpdatedAt,
76 - product.DeletedAt, 70 + //product.DeletedAt,
77 ); err != nil { 71 ); err != nil {
78 return product, err 72 return product, err
79 } 73 }
@@ -91,17 +85,17 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod @@ -91,17 +85,17 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod
91 &product.UpdatedAt, 85 &product.UpdatedAt,
92 &product.DeletedAt, 86 &product.DeletedAt,
93 ), 87 ),
94 - fmt.Sprintf("UPDATE products SET %s WHERE product_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), 88 + fmt.Sprintf("UPDATE manufacture.product SET %s WHERE product_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
95 product.CompanyId, 89 product.CompanyId,
96 product.OrgId, 90 product.OrgId,
97 - product.ProductId, 91 + //product.ProductId,
98 product.ProductCode, 92 product.ProductCode,
99 product.ProductName, 93 product.ProductName,
100 product.ProductCategory, 94 product.ProductCategory,
101 product.ProductSpec, 95 product.ProductSpec,
102 product.CreatedAt, 96 product.CreatedAt,
103 product.UpdatedAt, 97 product.UpdatedAt,
104 - product.DeletedAt, 98 + //product.DeletedAt,
105 product.Identify(), 99 product.Identify(),
106 ); err != nil { 100 ); err != nil {
107 return product, err 101 return product, err
@@ -122,7 +116,11 @@ func (repository *ProductRepository) FindOne(queryOptions map[string]interface{} @@ -122,7 +116,11 @@ func (repository *ProductRepository) FindOne(queryOptions map[string]interface{}
122 tx := repository.transactionContext.PgTx 116 tx := repository.transactionContext.PgTx
123 productModel := new(models.Product) 117 productModel := new(models.Product)
124 query := sqlbuilder.BuildQuery(tx.Model(productModel), queryOptions) 118 query := sqlbuilder.BuildQuery(tx.Model(productModel), queryOptions)
125 - query.SetWhereByQueryOption("product.product_id = ?", "productId") 119 + query.SetWhereByQueryOption("product_id = ?", "productId")
  120 + query.SetWhereByQueryOption("company_id = ?", "companyId")
  121 + query.SetWhereByQueryOption("org_id = ?", "orgId")
  122 + query.SetWhereByQueryOption("product_code = ?", "productCode")
  123 + query.AllWithDeleted()
126 if err := query.First(); err != nil { 124 if err := query.First(); err != nil {
127 if err.Error() == "pg: no rows in result set" { 125 if err.Error() == "pg: no rows in result set" {
128 return nil, fmt.Errorf("没有此资源") 126 return nil, fmt.Errorf("没有此资源")
@@ -15,6 +15,9 @@ func (controller *ProductController) CreateProduct() { @@ -15,6 +15,9 @@ func (controller *ProductController) CreateProduct() {
15 productService := service.NewProductService(nil) 15 productService := service.NewProductService(nil)
16 createProductCommand := &command.CreateProductCommand{} 16 createProductCommand := &command.CreateProductCommand{}
17 controller.Unmarshal(createProductCommand) 17 controller.Unmarshal(createProductCommand)
  18 + op := ParseOperateInfo(controller.BaseController)
  19 + createProductCommand.CompanyId = op.CompanyId
  20 + createProductCommand.OrgId = op.OrgId
18 data, err := productService.CreateProduct(createProductCommand) 21 data, err := productService.CreateProduct(createProductCommand)
19 controller.Response(data, err) 22 controller.Response(data, err)
20 } 23 }
@@ -58,3 +61,11 @@ func (controller *ProductController) ListProduct() { @@ -58,3 +61,11 @@ func (controller *ProductController) ListProduct() {
58 data, err := productService.ListProduct(listProductQuery) 61 data, err := productService.ListProduct(listProductQuery)
59 controller.Response(data, err) 62 controller.Response(data, err)
60 } 63 }
  64 +
  65 +func (controller *ProductController) SearchProduct() {
  66 + productService := service.NewProductService(nil)
  67 + listProductQuery := &query.SearchProductQuery{}
  68 + controller.Unmarshal(listProductQuery)
  69 + total, data, err := productService.SearchProduct(ParseOperateInfo(controller.BaseController), listProductQuery)
  70 + ResponseGrid(controller.BaseController, total, data, err)
  71 +}
@@ -11,4 +11,5 @@ func init() { @@ -11,4 +11,5 @@ func init() {
11 web.Router("/products/:productId", &controllers.ProductController{}, "Get:GetProduct") 11 web.Router("/products/:productId", &controllers.ProductController{}, "Get:GetProduct")
12 web.Router("/products/:productId", &controllers.ProductController{}, "Delete:RemoveProduct") 12 web.Router("/products/:productId", &controllers.ProductController{}, "Delete:RemoveProduct")
13 web.Router("/products/", &controllers.ProductController{}, "Get:ListProduct") 13 web.Router("/products/", &controllers.ProductController{}, "Get:ListProduct")
  14 + web.Router("/products/search", &controllers.ProductController{}, "Post:SearchProduct")
14 } 15 }