fix 修复数据库查询bug

This commit is contained in:
timerzz 2024-09-01 16:49:35 +08:00
parent 402be86404
commit 14abeb24b5
5 changed files with 30 additions and 29 deletions

View File

@ -83,7 +83,7 @@ func (p *Product) BeforeSave(tx *gorm.DB) (err error) {
func (p *Product) fillPriceStatus(tx *gorm.DB) {
var lastPrice float64
tx.Model(&HistoryPrice{}).Where("pid = ?", p.Pid).Order("created_at desc").Limit(1).Pluck("original_price", &lastPrice)
tx.Model(&HistoryPrice{}).Where("pid=?", p.Pid).Order("created_at desc").Limit(1).Pluck("original_price", &lastPrice)
if p.OriginalPrice != lastPrice {
p.HistoryPrices = append(p.HistoryPrices, HistoryPrice{
OriginalPrice: p.OriginalPrice,

View File

@ -1,9 +1,10 @@
package productv1
import (
"testing"
"gitea.timerzz.com/kedaya_haitao/common/pkg/database"
"gorm.io/gorm/clause"
"testing"
)
func TestUpsert(t *testing.T) {
@ -51,7 +52,7 @@ func TestUpdate(t *testing.T) {
t.Fatal(err)
}
var p = Product{Pid: "CJ575-QBO4G", DWPrice: 3000, Freight: 150, Remark: "test", OriginalPrice: 209.3, ExchangeRate: 7.3}
err = db.Model(&p).Where("pid = ?", p.Pid).Select("dw_price", "freight", "remark", "rate", "cal_mark", "cny_price").Updates(&p).Error
err = db.Model(&p).Where("pid=?", p.Pid).Select("dw_price", "freight", "remark", "rate", "cal_mark", "cny_price").Updates(&p).Error
if err != nil {
t.Fatal(err)
}
@ -69,7 +70,7 @@ func TestGetConfig(t *testing.T) {
t.Fatal(err)
}
var exchangeRate float64
db.Table("options").Select("exchange_rate").Where("id = ?", 1).Scan(&exchangeRate)
db.Table("options").Select("exchange_rate").Where("id=?", 1).Scan(&exchangeRate)
t.Log(exchangeRate)
}

View File

@ -67,19 +67,19 @@ func (f *FindArticleQuery) SetAvailable(available bool) *FindArticleQuery {
func (l *FindArticleQuery) Scope(db *gorm.DB) *gorm.DB {
if l.ID != 0 {
db = db.Where("`id` = ?", l.ID)
db = db.Where("`id`=?", l.ID)
}
if l.Keyword != "" {
db = db.Where("(`name` LIKE ? OR `english_name LIKE ? OR `remark` LIKE ? )", "%"+l.Keyword+"%", "%"+l.Keyword+"%", "%"+l.Keyword+"%")
db = db.Where("(`name` ilike ? OR `english_name ilike ? OR `remark` ilike ? )", "%"+l.Keyword+"%", "%"+l.Keyword+"%", "%"+l.Keyword+"%")
}
if l.Brand != "" {
db = db.Where("`brand` = ?", l.Brand)
db = db.Where("`brand`=?", l.Brand)
}
if l.Pid != "" {
db = db.Where("`pid` = ?", l.Pid)
db = db.Where("`pid`=?", l.Pid)
}
if l.Available != nil {
db = db.Where("`available` = ?", *l.Available)
db = db.Where("`available`=?", *l.Available)
}
return db
}
@ -132,13 +132,13 @@ func NewGetArticleQuery() *GetArticleQuery {
func (g *GetArticleQuery) Scope(db *gorm.DB) *gorm.DB {
db = db.Preload("Providers").Preload("Sellers")
if g.ID != 0 {
db = db.Where("`id` = ?", g.ID)
db = db.Where("`id`=?", g.ID)
}
if g.Brand != "" {
db = db.Where("`brand` = ?", g.Brand)
db = db.Where("`brand`=?", g.Brand)
}
if g.Pid != "" {
db = db.Where("`pid` = ?", g.Pid)
db = db.Where("`pid`=?", g.Pid)
}
if g.History {
db = db.Preload("Providers.HistoryPrice").Preload("Sellers.HistoryPrice")

View File

@ -43,18 +43,18 @@ func (g *ListProviderQuery) Scope(db *gorm.DB) *gorm.DB {
db = db.Where("provider_id=?", g.ProviderId)
}
if g.Keyword != "" {
db = db.Where("name LIKE ?", "%"+g.Keyword+"%")
db = db.Where("name ilike ?", "%"+g.Keyword+"%")
}
return db
}
func (p *providerApi) Get(id uint) (provider v2.Provider, err error) {
err = p.db.Where("id = ?", id).Preload("CalculateProcess").First(&provider).Error
err = p.db.Where("id=?", id).Preload("CalculateProcess").First(&provider).Error
return
}
func (p *providerApi) GetByProvider(providerId v2.ProviderId) (provider v2.Provider, err error) {
err = p.db.Where("provider_id = ?", providerId).Preload("CalculateProcess").First(&provider).Error
err = p.db.Where("provider_id=?", providerId).Preload("CalculateProcess").First(&provider).Error
return
}
@ -88,13 +88,13 @@ func (p *providerApi) Update(provider v2.Provider) error {
})
// 删除已经没有的CalculateProcess
if err := tx.Not(ids).Where("owner_id = ? AND kind = ?", provider.ID, "provider").Delete(&v2.CalculateProcess{}).Error; err != nil {
if err := tx.Not(ids).Where("owner_id=? AND kind=?", provider.ID, "provider").Delete(&v2.CalculateProcess{}).Error; err != nil {
return err
}
// 更新方案的name和描述
return tx.Session(&gorm.Session{FullSaveAssociations: true}).Select("updated_at", "name", "config", "mark", "CalculateProcess").
Where("id = ?", provider.ID).Updates(&provider).Error
Where("id=?", provider.ID).Updates(&provider).Error
})
}
@ -102,15 +102,15 @@ func (p *providerApi) UpdateStatus(provider v2.Provider) error {
if provider.ID == 0 {
return fmt.Errorf("id为空")
}
return p.db.Model(&provider).Where("id = ?", provider.ID).Select("status", "msg", "pull_at").Updates(&provider).Error
return p.db.Model(&provider).Where("id=?", provider.ID).Select("status", "msg", "pull_at").Updates(&provider).Error
}
func (p *providerApi) Delete(id uint) (provider v2.Provider, err error) {
err = p.db.Transaction(func(tx *gorm.DB) error {
if err = tx.Where("owner_id = ? AND kind = ?", id, "provider").Delete(&v2.CalculateProcess{}).Error; err != nil {
if err = tx.Where("owner_id=? AND kind=?", id, "provider").Delete(&v2.CalculateProcess{}).Error; err != nil {
}
return p.db.Clauses(clause.Returning{}).Where("id = ?", id).Delete(&provider).Error
return p.db.Clauses(clause.Returning{}).Where("id=?", id).Delete(&provider).Error
})
return
}

View File

@ -40,21 +40,21 @@ func (g *ListSellerQuery) Scope(db *gorm.DB) *gorm.DB {
db = db.Where("id=?", g.ID)
}
if g.SellerId != "" {
db = db.Where("seller_id = ?", g.SellerId)
db = db.Where("seller_id=?", g.SellerId)
}
if g.Keyword != "" {
db = db.Where("name LIKE ?", "%"+g.Keyword+"%")
db = db.Where("name ilike ?", "%"+g.Keyword+"%")
}
return db
}
func (p *sellerApi) Get(id uint) (seller v2.Seller, err error) {
err = p.db.Where("id = ?", id).Preload("CalculateProcess").First(&seller).Error
err = p.db.Where("id=?", id).Preload("CalculateProcess").First(&seller).Error
return
}
func (p *sellerApi) GetBySellerId(sellerId v2.SellerId) (seller v2.Seller, err error) {
err = p.db.Where("seller_id = ?", sellerId).Preload("CalculateProcess").First(&seller).Error
err = p.db.Where("seller_id=?", sellerId).Preload("CalculateProcess").First(&seller).Error
return
}
@ -88,13 +88,13 @@ func (p *sellerApi) Update(seller v2.Seller) error {
})
// 删除已经没有的CalculateProcess
if err := tx.Not(ids).Where("owner_id = ? AND kind = ?", seller.ID, "seller").Delete(&v2.CalculateProcess{}).Error; err != nil {
if err := tx.Not(ids).Where("owner_id=? AND kind=?", seller.ID, "seller").Delete(&v2.CalculateProcess{}).Error; err != nil {
return err
}
// 更新方案的name和描述
return tx.Session(&gorm.Session{FullSaveAssociations: true}).Select("updated_at", "name", "config", "mark", "CalculateProcess").
Where("id = ?", seller.ID).Updates(&seller).Error
Where("id=?", seller.ID).Updates(&seller).Error
})
}
@ -102,15 +102,15 @@ func (p *sellerApi) UpdateStatus(seller v2.Seller) error {
if seller.ID == 0 {
return fmt.Errorf("id为空")
}
return p.db.Model(&seller).Where("id = ?", seller.ID).Select("status", "msg", "pull_at").Updates(&seller).Error
return p.db.Model(&seller).Where("id=?", seller.ID).Select("status", "msg", "pull_at").Updates(&seller).Error
}
func (p *sellerApi) Delete(id uint) (seller v2.Seller, err error) {
err = p.db.Transaction(func(tx *gorm.DB) error {
if err = tx.Where("owner_id = ? AND kind = ?", id, "seller").Delete(&v2.CalculateProcess{}).Error; err != nil {
if err = tx.Where("owner_id=? AND kind=?", id, "seller").Delete(&v2.CalculateProcess{}).Error; err != nil {
}
return p.db.Clauses(clause.Returning{}).Where("id = ?", id).Delete(&seller).Error
return p.db.Clauses(clause.Returning{}).Where("id=?", id).Delete(&seller).Error
})
return
}