You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
![]()
3 weeks ago
|
package price
|
||
|
|
||
|
import (
|
||
|
"cls-server/internal/domain/price"
|
||
|
"cls-server/pkg/util/page"
|
||
|
"cls-server/pkg/xorm_engine"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"xorm.io/builder"
|
||
|
)
|
||
|
|
||
|
// PriceRepositoryORM 价格管理仓储实现
|
||
|
type PriceRepositoryORM struct {
|
||
|
engine *xorm_engine.Engine
|
||
|
}
|
||
|
|
||
|
var _ price.PriceRepository = (*PriceRepositoryORM)(nil)
|
||
|
|
||
|
// NewRepository 创建价格管理仓储
|
||
|
func NewPriceRepositoryORM(engine *xorm_engine.Engine) price.PriceRepository {
|
||
|
return &PriceRepositoryORM{
|
||
|
engine: engine,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Save 保存价格记录
|
||
|
func (p *PriceRepositoryORM) Save(price *price.Price) error {
|
||
|
_, err := p.engine.Insert(price)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// FindByID 根据ID查找价格记录
|
||
|
func (p *PriceRepositoryORM) FindByID(id uint64) (*price.Price, error) {
|
||
|
price := &price.Price{}
|
||
|
has, err := p.engine.Where(builder.Eq{"id": id}).Get(price)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if !has {
|
||
|
return nil, errors.New("记录不存在")
|
||
|
}
|
||
|
return price, nil
|
||
|
}
|
||
|
|
||
|
// FindByTargetID 根据目标ID查找价格记录
|
||
|
func (p *PriceRepositoryORM) FindByTargetID(targetID uint64, priceType price.PriceType) (*price.Price, error) {
|
||
|
price := &price.Price{}
|
||
|
has, err := p.engine.Where(builder.Eq{
|
||
|
"target_id": targetID,
|
||
|
"type": priceType,
|
||
|
}).Get(price)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if !has {
|
||
|
return nil, errors.New(fmt.Sprintf("未找到相关数据【target_id: %d, type: %d】", targetID, priceType))
|
||
|
}
|
||
|
return price, nil
|
||
|
}
|
||
|
|
||
|
// FindAll 查询文章价格记录列表
|
||
|
func (p *PriceRepositoryORM) FindAll(pp *page.Page, conds []builder.Cond) error {
|
||
|
return p.engine.FindAll(pp, &price.Price{}, builder.And(conds...))
|
||
|
}
|
||
|
|
||
|
// FindColumnAll 查询专栏价格记录列表
|
||
|
func (p *PriceRepositoryORM) FindColumnAll(pp *page.Page, conds []builder.Cond) error {
|
||
|
return p.engine.FindAll(pp, "lc_price as p", builder.And(conds...), xorm_engine.Join{
|
||
|
JoinOperator: "left",
|
||
|
Tablename: "lc_column as c",
|
||
|
Condition: "p.target_id = c.id",
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// Update 更新价格记录
|
||
|
func (p *PriceRepositoryORM) Update(price *price.Price) error {
|
||
|
_, err := p.engine.Update(price)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Delete 删除价格记录
|
||
|
func (p *PriceRepositoryORM) Delete(id uint64) error {
|
||
|
_, err := p.engine.Delete(&price.Price{}, "id = ?", id)
|
||
|
return err
|
||
|
}
|