|
|
|
package price
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cls-server/internal/domain/price"
|
|
|
|
"cls-server/pkg/util/page"
|
|
|
|
"cls-server/pkg/xorm_engine"
|
|
|
|
"errors"
|
|
|
|
"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{}
|
|
|
|
_, err := p.engine.Where(builder.Eq{
|
|
|
|
"target_id": targetID,
|
|
|
|
"type": priceType,
|
|
|
|
}).Get(price)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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, &price.Price{}, builder.And(conds...), xorm_engine.Join{
|
|
|
|
JoinOperator: "left",
|
|
|
|
Tablename: "lc_column as c",
|
|
|
|
Condition: "lc_price.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PriceRepositoryORM) FindPriceByIds(id *[]uint64, t price.PriceType) (*[]*price.Price, error) {
|
|
|
|
data := make([]*price.Price, 0)
|
|
|
|
return &data, p.engine.Where(builder.Eq{"type": t}.And(builder.In("target_id", *id))).Find(&data)
|
|
|
|
}
|