作者 yangfu

产品管理

... ... @@ -9,6 +9,7 @@ require (
github.com/fatih/structs v1.1.0 // indirect
github.com/gavv/httpexpect v2.0.0+incompatible
github.com/go-pg/pg/v10 v10.9.0
github.com/go-redis/redis v6.15.7+incompatible
github.com/google/go-querystring v1.1.0 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7
... ... @@ -17,6 +18,7 @@ require (
github.com/onsi/gomega v1.11.0
github.com/sergi/go-diff v1.2.0 // indirect
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/tal-tech/go-zero v1.0.27
github.com/valyala/fasthttp v1.32.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
... ...
... ... @@ -5,6 +5,7 @@ import (
"github.com/beego/beego/v2/server/web"
"github.com/linmadan/egglib-go/log/logrus"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
... ... @@ -28,6 +29,7 @@ func main() {
MaxSize: 1024 * 1024 * 2,
})
log.Logger.AddHook(bw)
redis.InitRedis()
log.Logger.Info("server start!")
web.Run()
... ...
... ... @@ -14,23 +14,24 @@ type CreateProductCommand struct {
// 组织ID
OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`
// 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode" valid:"Required"`
ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode"`
// 产品名称
ProductName string `cname:"产品名称" json:"productName" valid:"Required"`
// 产品类别
ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"`
// 数量(保留两位小数)
Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"`
//Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"`
// 单位
Unit string `cname:"单位" json:"unit" valid:"Required"`
// 单份重量(原材料)
UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"`
// 重量
Weight float64 `cname:"重量" json:"weight" valid:"Required"`
//Weight float64 `cname:"重量" json:"weight" valid:"Required"`
}
func (createProductCommand *CreateProductCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (createProductCommand *CreateProductCommand) ValidateCommand() error {
... ...
... ... @@ -14,7 +14,7 @@ type RemoveProductCommand struct {
}
func (removeProductCommand *RemoveProductCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeProductCommand *RemoveProductCommand) ValidateCommand() error {
... ...
... ... @@ -12,23 +12,23 @@ type UpdateProductCommand struct {
// 产品ID
ProductId int `cname:"产品ID" json:"productId" valid:"Required"`
// 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode" valid:"Required"`
ProductCode string `cname:"产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001" json:"productCode"`
// 产品名称
ProductName string `cname:"产品名称" json:"productName" valid:"Required"`
// 产品类别
ProductCategory string `cname:"产品类别" json:"productCategory" valid:"Required"`
// 数量(保留两位小数)
Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"`
//Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"`
// 单位
Unit string `cname:"单位" json:"unit" valid:"Required"`
// 单份重量(原材料)
UnitWeight float64 `cname:"单份重量(原材料)" json:"unitWeight" valid:"Required"`
// 重量
Weight float64 `cname:"重量" json:"weight" valid:"Required"`
//Weight float64 `cname:"重量" json:"weight" valid:"Required"`
}
func (updateProductCommand *UpdateProductCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateProductCommand *UpdateProductCommand) ValidateCommand() error {
... ...
package dto
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
type ProductDto struct {
// 产品ID
ProductId int `json:"productId,omitempty"`
// 产品编号 编码规则为“CP”+2 位年+2 位月+2 位日+3 位流水码,如 CP211229001
ProductCode string `json:"productCode,omitempty"`
// 产品名称
ProductName string `json:"productName,omitempty"`
// 产品类别
ProductCategory string `json:"productCategory,omitempty"`
// 产品规格
*domain.UnitQuantity
}
func (d *ProductDto) LoadDto(m *domain.Product) *ProductDto {
d.ProductId = m.ProductId
d.ProductCode = m.ProductCode
d.ProductName = m.ProductName
d.ProductCategory = m.ProductCategory
d.UnitQuantity = m.ProductSpec
return d
}
... ...
... ... @@ -14,7 +14,7 @@ type GetProductQuery struct {
}
func (getProductQuery *GetProductQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getProductQuery *GetProductQuery) ValidateQuery() error {
... ...
... ... @@ -10,13 +10,13 @@ import (
type ListProductQuery struct {
// 查询偏离量
Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
Offset int `cname:"查询偏离量" json:"offset"`
// 查询限制
Limit int `cname:"查询限制" json:"limit" valid:"Required"`
}
func (listProductQuery *ListProductQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listProductQuery *ListProductQuery) ValidateQuery() error {
... ...
package query
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type SearchProductQuery struct {
// 查询偏离量
Offset int `cname:"查询偏离量" json:"offset"`
// 查询限制
Limit int `cname:"查询限制" json:"limit"`
// 当前公司
CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
// 当前登录的组织
OrgId int `cname:"当前登录的组织" json:"orgId,omitempty" valid:"Required"`
// 页码
PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
// 页数
PageSize int `cname:"页数" json:"pageSize,omitempty"`
// 产品名称
ProductName string `json:"productName,omitempty"`
// 产品类别
ProductCategory string `cname:"产品类别" json:"productCategory"`
}
func (cmd *SearchProductQuery) Valid(validation *validation.Validation) {
cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (cmd *SearchProductQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(cmd)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(cmd).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -6,8 +6,13 @@ import (
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/product/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"time"
)
// 产品服务
... ... @@ -29,22 +34,36 @@ func (productService *ProductService) CreateProduct(createProductCommand *comman
defer func() {
transactionContext.RollbackTransaction()
}()
if len(createProductCommand.ProductCode) == 0 {
generator := redis.NewProductCodeCache(createProductCommand.CompanyId)
code, err := redis.GenCode(generator)
if err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
}
createProductCommand.ProductCode = code
}
newProduct := &domain.Product{
CompanyId: createProductCommand.CompanyId,
OrgId: createProductCommand.OrgId,
ProductCode: createProductCommand.ProductCode,
ProductName: createProductCommand.ProductName,
ProductCategory: createProductCommand.ProductCategory,
//ProductSpec: createProductCommand.ProductSpec,
}
var productRepository domain.ProductRepository
if value, err := factory.CreateProductRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productRepository = value
ProductSpec: &domain.UnitQuantity{
Unit: createProductCommand.Unit,
UnitWeight: createProductCommand.UnitWeight,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
productRepository, _, _ := factory.FastPgProduct(transactionContext, 0)
if item, err := productRepository.FindOne(map[string]interface{}{"companyId": createProductCommand.CompanyId, "productCode": createProductCommand.ProductCode}); err == nil && item != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "产品编号重复,请重新提交")
}
if product, err := productRepository.Save(newProduct); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -88,7 +107,8 @@ func (productService *ProductService) GetProduct(getProductQuery *query.GetProdu
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return product, nil
result := &dto.ProductDto{}
return result.LoadDto(product), nil
}
}
... ... @@ -183,20 +203,23 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman
defer func() {
transactionContext.RollbackTransaction()
}()
var productRepository domain.ProductRepository
if value, err := factory.CreateProductRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productRepository = value
}
product, err := productRepository.FindOne(map[string]interface{}{"productId": updateProductCommand.ProductId})
productRepository, product, err := factory.FastPgProduct(transactionContext, updateProductCommand.ProductId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if product == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductCommand.ProductId)))
if len(updateProductCommand.ProductCode) == 0 {
generator := redis.NewProductCodeCache(product.CompanyId)
code, err := redis.GenCode(generator)
if err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
}
updateProductCommand.ProductCode = code
}
if updateProductCommand.ProductCode != product.ProductCode {
if item, err := productRepository.FindOne(map[string]interface{}{"companyId": product.CompanyId, "productCode": updateProductCommand.ProductCode}); err == nil && item != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "产品编号重复,请重新提交")
}
}
if err := product.Update(tool_funs.SimpleStructToMap(updateProductCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
... ... @@ -211,6 +234,47 @@ func (productService *ProductService) UpdateProduct(updateProductCommand *comman
}
}
// 返回产品服务列表
func (productService *ProductService) SearchProduct(operateInfo *domain.OperateInfo, listProductQuery *query.SearchProductQuery) (int64, interface{}, error) {
listProductQuery.OrgId = operateInfo.OrgId
listProductQuery.CompanyId = operateInfo.CompanyId
if err := listProductQuery.ValidateQuery(); err != nil {
return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var productRepository domain.ProductRepository
if value, err := factory.CreateProductRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
productRepository = value
}
count, products, err := productRepository.Find(utils.ObjectToMap(listProductQuery))
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var result = make([]*dto.ProductDto, 0)
for i := range products {
newItem := &dto.ProductDto{}
result = append(result, newItem.LoadDto(products[i]))
}
return count, result, nil
}
func NewProductService(options map[string]interface{}) *ProductService {
newProductService := &ProductService{}
return newProductService
... ...
... ... @@ -2,6 +2,7 @@ package command
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"reflect"
"strings"
... ... @@ -31,6 +32,10 @@ type CreateProductGroupCommand struct {
func (createProductGroupCommand *CreateProductGroupCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
if err := domain.ValidWorkOn(createProductGroupCommand.WorkOn); err != nil {
validation.SetError("workOn", err.Error())
return
}
}
func (createProductGroupCommand *CreateProductGroupCommand) ValidateCommand() error {
... ...
... ... @@ -4,6 +4,15 @@ import "fmt"
const MaxQueryRow = 10000
const (
WorkOnFullDay = 1 //全天
WorkOnDay = 2 //白班
WorkOnMidDay = 4 //中班
WorkOnNight = 8 //夜班
)
const WorkOn = WorkOnFullDay | WorkOnDay | WorkOnMidDay | WorkOnNight
var (
ErrorNotFound = fmt.Errorf("没有此资源")
)
... ... @@ -78,3 +87,10 @@ func Pagination(pageNumber, pageSize int) (offset int, limit int) {
}
return
}
func ValidWorkOn(workOn int) error {
if (workOn & WorkOn) == 0 {
return fmt.Errorf("班次有误")
}
return nil
}
... ...
... ... @@ -41,12 +41,6 @@ func (product *Product) Identify() interface{} {
}
func (product *Product) Update(data map[string]interface{}) error {
if companyId, ok := data["companyId"]; ok {
product.CompanyId = companyId.(int)
}
if orgId, ok := data["orgId"]; ok {
product.OrgId = orgId.(int)
}
if productCode, ok := data["productCode"]; ok {
product.ProductCode = productCode.(string)
}
... ... @@ -56,26 +50,15 @@ func (product *Product) Update(data map[string]interface{}) error {
if productCategory, ok := data["productCategory"]; ok {
product.ProductCategory = productCategory.(string)
}
if quantity, ok := data["quantity"]; ok {
product.ProductSpec.Quantity = quantity.(float64)
}
//if quantity, ok := data["quantity"]; ok {
// product.ProductSpec.Quantity = quantity.(float64)
//}
if unit, ok := data["unit"]; ok {
product.ProductSpec.Unit = unit.(string)
}
if unitWeight, ok := data["unitWeight"]; ok {
product.ProductSpec.UnitWeight = unitWeight.(float64)
}
if weight, ok := data["weight"]; ok {
product.ProductSpec.Weight = weight.(float64)
}
if createdAt, ok := data["createdAt"]; ok {
product.CreatedAt = createdAt.(time.Time)
}
if updatedAt, ok := data["updatedAt"]; ok {
product.UpdatedAt = updatedAt.(time.Time)
}
if deletedAt, ok := data["deletedAt"]; ok {
product.DeletedAt = deletedAt.(time.Time)
}
product.UpdatedAt = time.Now()
return nil
}
... ...
... ... @@ -2,7 +2,7 @@ package example
import (
"github.com/linmadan/egglib-go/utils/json"
"gitlab.fjmaimaimai.com/allied-creation/allied-lib/gateway/allied_creation_user"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway/allied_creation_user"
"testing"
)
... ...
package redis
import (
"fmt"
"github.com/go-redis/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"time"
)
func GenCode(ca CacheCode) (string, error) {
key := ca.CacheKey()
client := GetRedis()
result := client.Get(key)
index, err := result.Int()
if err == redis.Nil {
index = 1
err = nil
if ret := client.Set(key, index, ca.Expire()); ret.Err() != nil {
return "", ret.Err()
}
return ca.Format(index), nil
}
if err != nil {
return "", err
}
if ret := client.Incr(key); ret.Err() != nil {
return "", ret.Err()
} else {
index = int(ret.Val())
}
return ca.Format(index), nil
}
type CacheCode interface {
CacheKey() string
Expire() time.Duration
Format(index int) string
}
type ProductCodeCache struct {
CompanyId int
Time time.Time
}
func (ca ProductCodeCache) CacheKey() string {
str := fmt.Sprintf("%v:product-code:%v:%v", constant.CACHE_PREFIX, ca.CompanyId, ca.Time.Format("20060102"))
return str
}
func (ca ProductCodeCache) Format(index int) string {
if index <= 999 {
return fmt.Sprintf("CP%v%03d", ca.Time.Format("060102"), index)
}
return fmt.Sprintf("CP%v%d", ca.Time.Format("060102"), index)
}
func (ca ProductCodeCache) Expire() time.Duration {
return time.Hour * 24
}
func NewProductCodeCache(id int) ProductCodeCache {
return ProductCodeCache{
CompanyId: id,
Time: time.Now(),
}
}
... ...
package redis
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"github.com/go-redis/redis"
)
var clientRedis *redis.Client
func InitRedis() *redis.Client {
options := redis.Options{
Network: "tcp",
Addr: fmt.Sprintf("%s:%s", constant.REDIS_HOST, constant.REDIS_PORT),
Dialer: nil,
OnConnect: nil,
Password: "",
DB: 0,
MaxRetries: 0,
MinRetryBackoff: 0,
MaxRetryBackoff: 0,
DialTimeout: 0,
ReadTimeout: 0,
WriteTimeout: 0,
PoolSize: 0,
MinIdleConns: 0,
MaxConnAge: 0,
PoolTimeout: 0,
IdleTimeout: 0,
IdleCheckFrequency: 0,
TLSConfig: nil,
}
// 新建一个client
clientRedis = redis.NewClient(&options)
pong, err := clientRedis.Ping().Result()
if err != nil {
e := fmt.Errorf("redis 连接失败,%s,%w", pong, err)
panic(e)
}
return clientRedis
}
func GetRedis() *redis.Client {
return clientRedis
}
... ...
... ... @@ -122,6 +122,7 @@ func (repository *ProductGroupRepository) FindOne(queryOptions map[string]interf
query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId")
query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId")
query.SetWhereByQueryOption("work_station->>'lineId'='?'", "lineId")
query.AllWithDeleted()
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ...
... ... @@ -117,6 +117,7 @@ func (repository *ProductJobRepository) FindOne(queryOptions map[string]interfac
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("job_name=?", "jobName")
query.SetWhereByQueryOption("work_station->>'workStationId'=?", "workStationId")
query.AllWithDeleted()
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ...
... ... @@ -37,19 +37,13 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod
"updated_at",
"deleted_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_id", "deleted_at"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_id", "deleted_at"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_id")
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "product_id", "deleted_at")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if product.Identify() == nil {
productId, err := repository.nextIdentify()
if err != nil {
return product, err
} else {
product.ProductId = int(productId)
}
if _, err := tx.QueryOne(
pg.Scan(
&product.CompanyId,
... ... @@ -63,17 +57,17 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod
&product.UpdatedAt,
&product.DeletedAt,
),
fmt.Sprintf("INSERT INTO products (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
fmt.Sprintf("INSERT INTO manufacture.product (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
product.CompanyId,
product.OrgId,
product.ProductId,
//product.ProductId,
product.ProductCode,
product.ProductName,
product.ProductCategory,
product.ProductSpec,
product.CreatedAt,
product.UpdatedAt,
product.DeletedAt,
//product.DeletedAt,
); err != nil {
return product, err
}
... ... @@ -91,17 +85,17 @@ func (repository *ProductRepository) Save(product *domain.Product) (*domain.Prod
&product.UpdatedAt,
&product.DeletedAt,
),
fmt.Sprintf("UPDATE products SET %s WHERE product_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
fmt.Sprintf("UPDATE manufacture.product SET %s WHERE product_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
product.CompanyId,
product.OrgId,
product.ProductId,
//product.ProductId,
product.ProductCode,
product.ProductName,
product.ProductCategory,
product.ProductSpec,
product.CreatedAt,
product.UpdatedAt,
product.DeletedAt,
//product.DeletedAt,
product.Identify(),
); err != nil {
return product, err
... ... @@ -122,7 +116,11 @@ func (repository *ProductRepository) FindOne(queryOptions map[string]interface{}
tx := repository.transactionContext.PgTx
productModel := new(models.Product)
query := sqlbuilder.BuildQuery(tx.Model(productModel), queryOptions)
query.SetWhereByQueryOption("product.product_id = ?", "productId")
query.SetWhereByQueryOption("product_id = ?", "productId")
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("product_code = ?", "productCode")
query.AllWithDeleted()
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ...
... ... @@ -15,6 +15,9 @@ func (controller *ProductController) CreateProduct() {
productService := service.NewProductService(nil)
createProductCommand := &command.CreateProductCommand{}
controller.Unmarshal(createProductCommand)
op := ParseOperateInfo(controller.BaseController)
createProductCommand.CompanyId = op.CompanyId
createProductCommand.OrgId = op.OrgId
data, err := productService.CreateProduct(createProductCommand)
controller.Response(data, err)
}
... ... @@ -58,3 +61,11 @@ func (controller *ProductController) ListProduct() {
data, err := productService.ListProduct(listProductQuery)
controller.Response(data, err)
}
func (controller *ProductController) SearchProduct() {
productService := service.NewProductService(nil)
listProductQuery := &query.SearchProductQuery{}
controller.Unmarshal(listProductQuery)
total, data, err := productService.SearchProduct(ParseOperateInfo(controller.BaseController), listProductQuery)
ResponseGrid(controller.BaseController, total, data, err)
}
... ...
... ... @@ -11,4 +11,5 @@ func init() {
web.Router("/products/:productId", &controllers.ProductController{}, "Get:GetProduct")
web.Router("/products/:productId", &controllers.ProductController{}, "Delete:RemoveProduct")
web.Router("/products/", &controllers.ProductController{}, "Get:ListProduct")
web.Router("/products/search", &controllers.ProductController{}, "Post:SearchProduct")
}
... ...