|
|
|
package price
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cls/internal/domain/price"
|
|
|
|
"cls/pkg/logger"
|
|
|
|
"cls/pkg/util/page"
|
|
|
|
"cls/pkg/xorm_engine"
|
|
|
|
"errors"
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PriceRepositoryORM 价格管理仓储实现
|
|
|
|
type PriceRepositoryORM struct {
|
|
|
|
engine *xorm_engine.Engine
|
|
|
|
log logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ price.PriceRepository = (*PriceRepositoryORM)(nil)
|
|
|
|
|
|
|
|
// NewRepository 创建价格管理仓储
|
|
|
|
func NewPriceRepositoryORM(engine *xorm_engine.Engine, log logger.New) price.PriceRepository {
|
|
|
|
return &PriceRepositoryORM{
|
|
|
|
engine: engine,
|
|
|
|
log: log("cls:repository:price"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save 保存价格记录
|
|
|
|
func (p *PriceRepositoryORM) Save(price *price.Price) error {
|
|
|
|
_, err := p.engine.Insert(price)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error(err)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
p.log.Error(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
p.log.Errorf("未找到相关数据【%d】", id)
|
|
|
|
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 {
|
|
|
|
p.log.Error(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return price, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindAll 查询价格记录列表
|
|
|
|
func (p *PriceRepositoryORM) FindAll(page *page.Page, conds []builder.Cond) error {
|
|
|
|
return p.engine.FindAll(page, &price.Price{}, builder.And(conds...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update 更新价格记录
|
|
|
|
func (p *PriceRepositoryORM) Update(price *price.Price) error {
|
|
|
|
_, err := p.engine.Update(price)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete 删除价格记录
|
|
|
|
func (p *PriceRepositoryORM) Delete(id uint64) error {
|
|
|
|
_, err := p.engine.Delete(&price.Price{}, "id = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|