| | |
| | | import ( |
| | | "fmt" |
| | | "gorm.io/gorm" |
| | | "wms/constvar" |
| | | "wms/pkg/mysqlx" |
| | | ) |
| | | |
| | | type ( |
| | | LocationProduct struct { |
| | | WmsModel |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | LocationId int `json:"locationId" gorm:"type:int;not null;comment:位置id"` //位置id |
| | | Location Location `json:"location" gorm:"foreignKey:LocationId;references:id"` |
| | | AreaId int `json:"areaId" grom:"type:int;not null;comment:区域id"` //区域id |
| | | Area Location `json:"area" gorm:"foreignKey:AreaId;references:id"` |
| | | ProductCategoryID int `json:"productCategoryId" gorm:"type:int;not null;comment:产品种类id"` //产品种类id |
| | | ProductCategory ProductCategory `json:"productCategory"` |
| | | ProductId string `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"` //产品id |
| | | Product Material `json:"product" gorm:"foreignKey:ProductId;references:ID"` |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | LocationId int `json:"locationId" gorm:"type:int;not null;comment:位置id"` //位置id |
| | | Location Location `json:"location" gorm:"foreignKey:LocationId;references:id"` |
| | | AreaId int `json:"areaId" grom:"type:int;not null;comment:区域id"` //区域id |
| | | Area Location `json:"area" gorm:"foreignKey:AreaId;references:id"` |
| | | ProductCategoryID int `json:"productCategoryId" gorm:"type:int;not null;comment:产品种类id"` //产品种类id |
| | | ProductCategory ProductCategory `json:"productCategory"` |
| | | ProductId string `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"` //产品id |
| | | Product Material `json:"product" gorm:"foreignKey:ProductId;references:ID"` |
| | | RuleType constvar.RuleType `json:"ruleType" gorm:"type:int(11);not null;comment:上架规则类型 1:产品类型;2:产品类别类型"` |
| | | } |
| | | |
| | | LocationProductSearch struct { |
| | | LocationProduct |
| | | Order string |
| | | PageNum int |
| | | PageSize int |
| | | Keyword string |
| | | Orm *gorm.DB |
| | | Preload bool |
| | | Order string |
| | | PageNum int |
| | | PageSize int |
| | | Keyword string |
| | | Orm *gorm.DB |
| | | Preload bool |
| | | LocationIds []int |
| | | } |
| | | ) |
| | | |
| | |
| | | return slf |
| | | } |
| | | |
| | | func (slf *LocationProductSearch) SetLocationIds(locationIds []int) *LocationProductSearch { |
| | | slf.LocationIds = locationIds |
| | | return slf |
| | | } |
| | | |
| | | func (slf *LocationProductSearch) SetAreaId(areaId int) *LocationProductSearch { |
| | | slf.AreaId = areaId |
| | | return slf |
| | | } |
| | | |
| | | func (slf *LocationProductSearch) SetProductCategoryId(productCategoryID int) *LocationProductSearch { |
| | | slf.ProductCategoryID = productCategoryID |
| | | return slf |
| | | } |
| | | |
| | | func (slf *LocationProductSearch) build() *gorm.DB { |
| | | var db = slf.Orm.Model(&LocationProduct{}) |
| | | |
| | |
| | | } |
| | | |
| | | if slf.Keyword != "" { |
| | | db = db.Where("product_name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)) |
| | | //db = db.Where("Product.name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)) |
| | | db.Joins("left join wms_location on wms_location_product.location_id = wms_location.id"). |
| | | Joins("left join material on wms_location_product.product_id = material.id"). |
| | | Joins("left join wms_product_category on wms_location_product.product_category_id = wms_product_category.id"). |
| | | Where("wms_location.name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)). |
| | | Or("material.name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)). |
| | | Or("wms_product_category.name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)) |
| | | } |
| | | |
| | | if slf.Preload { |
| | |
| | | |
| | | if slf.ProductId != "" { |
| | | db = db.Where("product_id=?", slf.ProductId) |
| | | } |
| | | |
| | | if len(slf.LocationIds) > 0 { |
| | | db = db.Where("location_id in (?)", slf.LocationIds) |
| | | } |
| | | |
| | | if slf.AreaId != 0 { |
| | | db = db.Where("area_id = ?", slf.AreaId) |
| | | } |
| | | |
| | | if slf.ProductCategoryID != 0 { |
| | | db = db.Where("product_category_id = ?", slf.ProductCategoryID) |
| | | } |
| | | |
| | | return db |
| | |
| | | if slf.PageNum*slf.PageSize > 0 { |
| | | db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) |
| | | } |
| | | if err := db.Find(&records).Error; err != nil { |
| | | if err := db.Preload("Location").Preload("ProductCategory").Preload("Product").Find(&records).Error; err != nil { |
| | | return records, total, fmt.Errorf("find records err: %v", err) |
| | | } |
| | | |
| | |
| | | db = slf.build() |
| | | ) |
| | | |
| | | if err := db.Find(&records).Error; err != nil { |
| | | if err := db.Preload("Location").Preload("ProductCategory").Preload("Product").Find(&records).Error; err != nil { |
| | | return records, fmt.Errorf("find records err: %v", err) |
| | | } |
| | | |
| | | return records, nil |
| | | } |
| | | |
| | | func (slf *LocationProductSearch) FirstRes() (*LocationProduct, *gorm.DB) { |
| | | var ( |
| | | record = new(LocationProduct) |
| | | db = slf.build() |
| | | ) |
| | | return record, db.First(record) |
| | | } |