refactor(park):园区部分基本完成迁移。
This commit is contained in:
parent
c6c1423364
commit
3d20ceb35a
128
service/park.go
128
service/park.go
|
@ -4,18 +4,26 @@ import (
|
|||
"electricity_bill_calc/cache"
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"fmt"
|
||||
|
||||
"xorm.io/builder"
|
||||
"github.com/uptrace/bun"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type _ParkService struct{}
|
||||
type _ParkService struct {
|
||||
l *zap.Logger
|
||||
}
|
||||
|
||||
var ParkService _ParkService
|
||||
var ParkService = _ParkService{
|
||||
l: logger.Named("Service", "Park"),
|
||||
}
|
||||
|
||||
func (_ParkService) SaveNewPark(park model.Park) error {
|
||||
_, err := global.DBConn.Insert(park)
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
_, err := global.DB.NewInsert().Model(&park).Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -24,52 +32,59 @@ func (_ParkService) SaveNewPark(park model.Park) error {
|
|||
}
|
||||
|
||||
func (_ParkService) UpdateParkInfo(park *model.Park) error {
|
||||
rows, err := global.DBConn.
|
||||
Where(builder.Eq{"id": park.Id, "user_id": park.UserId}).
|
||||
Cols("name", "abbr", "region", "address", "contact", "phone", "capacity", "tenement_quantity", "category", "meter_04kv_type").
|
||||
Update(park)
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
res, err := global.DB.NewUpdate().Model(&park).
|
||||
Where("id = ?", park.Id).
|
||||
Where("user_id = ?", park.UserId).
|
||||
Column("name", "abbr", "region", "address", "contact", "phone", "capacity", "tenement_quantity", "category", "meter_04kv_type").
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
if rows == 0 {
|
||||
if rows, _ := res.RowsAffected(); rows == 0 {
|
||||
return exceptions.NewNotFoundError("未能找到符合条件的园区。")
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
cache.AbolishRelation("park")
|
||||
cache.AbolishRelation(fmt.Sprintf("park_%s", park.Id))
|
||||
cache.AbolishRelation(fmt.Sprintf("park:%s", park.Id))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (_ParkService) ChangeParkState(uid, pid string, state bool) error {
|
||||
rows, err := global.DBConn.
|
||||
Table(&model.Park{}).
|
||||
Where(builder.Eq{"id": pid, "user_id": uid}).
|
||||
Update(map[string]interface{}{"enabled": state})
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
res, err := global.DB.NewUpdate().Model((*model.Park)(nil)).
|
||||
Where("id = ?", pid).
|
||||
Where("user_id = ?", uid).
|
||||
Set("enabled = ?", state).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
if rows == 0 {
|
||||
if rows, _ := res.RowsAffected(); rows == 0 {
|
||||
return exceptions.NewNotFoundError("未能找到符合条件的园区。")
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
cache.AbolishRelation("park")
|
||||
cache.AbolishRelation(fmt.Sprintf("park_%s", pid))
|
||||
cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (_ParkService) DeletePark(uid, pid string) error {
|
||||
rows, err := global.DBConn.
|
||||
Where(builder.Eq{"id": pid, "user_id": uid}).
|
||||
Delete(&model.Park{})
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
res, err := global.DB.NewDelete().Model((*model.Park)(nil)).
|
||||
Where("id = ?", pid).
|
||||
Where("user_id = ?", uid).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
if rows == 0 {
|
||||
if rows, _ := res.RowsAffected(); rows == 0 {
|
||||
return exceptions.NewNotFoundError("未能找到符合条件的园区。")
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
cache.AbolishRelation("park")
|
||||
cache.AbolishRelation(fmt.Sprintf("park_%s", pid))
|
||||
cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -77,25 +92,30 @@ func (_ParkService) ListAllParkBelongsTo(uid, keyword string) ([]model.Park, err
|
|||
if parks, _ := cache.RetreiveSearch[[]model.Park]("park", "belong", uid, keyword); parks != nil {
|
||||
return *parks, nil
|
||||
}
|
||||
cond := builder.NewCond().And(builder.Eq{"user_id": uid})
|
||||
if len(keyword) > 0 {
|
||||
cond = cond.And(
|
||||
builder.Like{"name", keyword}.
|
||||
Or(builder.Like{"abbr", keyword}).
|
||||
Or(builder.Like{"address", keyword}).
|
||||
Or(builder.Like{"contact", keyword}).
|
||||
Or(builder.Like{"phone", keyword}),
|
||||
)
|
||||
}
|
||||
parks := make([]model.Park, 0)
|
||||
err := global.DBConn.
|
||||
Where(cond).
|
||||
NoAutoCondition().
|
||||
Find(&parks)
|
||||
cond := global.DB.NewSelect().Model(&parks).
|
||||
Where("user_id = ?", uid)
|
||||
if len(keyword) > 0 {
|
||||
keywordCond := "%" + keyword + "%"
|
||||
cond = cond.WhereGroup(" and ", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||
return q.Where("name like ?", keywordCond).
|
||||
WhereOr("abbr like ?", keywordCond).
|
||||
WhereOr("address like ?", keywordCond).
|
||||
WhereOr("contact like ?", keywordCond).
|
||||
WhereOr("phone like ?", keywordCond)
|
||||
})
|
||||
}
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
err := cond.Scan(ctx)
|
||||
if err != nil {
|
||||
return make([]model.Park, 0), err
|
||||
}
|
||||
cache.CacheSearch(parks, []string{"park"}, "park", "belong", uid, keyword)
|
||||
relations := []string{"park"}
|
||||
for _, p := range parks {
|
||||
relations = append(relations, fmt.Sprintf("park:%s", p.Id))
|
||||
}
|
||||
cache.CacheSearch(parks, relations, "park", "belong", uid, keyword)
|
||||
return parks, nil
|
||||
}
|
||||
|
||||
|
@ -103,15 +123,19 @@ func (_ParkService) FetchParkDetail(pid string) (*model.Park, error) {
|
|||
if park, _ := cache.RetreiveEntity[model.Park]("park", pid); park != nil {
|
||||
return park, nil
|
||||
}
|
||||
var park = &model.Park{}
|
||||
has, err := global.DBConn.ID(pid).NoAutoCondition().Get(park)
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
var park *model.Park
|
||||
err := global.DB.NewSelect().Model(&park).
|
||||
Where("id = ?", pid).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !has {
|
||||
if park == nil {
|
||||
return nil, exceptions.NewNotFoundError("未找到符合条件的园区记录。")
|
||||
}
|
||||
cache.CacheEntity(park, []string{"park"}, "park", pid)
|
||||
cache.CacheEntity(*park, []string{fmt.Sprintf("park:%s", pid)}, "park", pid)
|
||||
return park, nil
|
||||
}
|
||||
|
||||
|
@ -119,9 +143,14 @@ func (_ParkService) EnsurePark(uid, pid string) (bool, error) {
|
|||
if has, _ := cache.CheckExists("park", pid, uid); has {
|
||||
return has, nil
|
||||
}
|
||||
has, err := global.DBConn.Table(&model.Park{}).Where(builder.Eq{"user_id": uid, "id": pid}).Exist()
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
has, err := global.DB.NewSelect().Model((*model.Park)(nil)).
|
||||
Where("id = ?", pid).
|
||||
Where("user_id = ?", uid).
|
||||
Exists(ctx)
|
||||
if has {
|
||||
cache.CacheExists([]string{"park"}, "park", pid, uid)
|
||||
cache.CacheExists([]string{fmt.Sprintf("park:%s", pid)}, "park", pid, uid)
|
||||
}
|
||||
return has, err
|
||||
}
|
||||
|
@ -130,12 +159,13 @@ func (_ParkService) AllParkIds(uid string) ([]string, error) {
|
|||
if ids, _ := cache.RetreiveSearch[[]string]("park", "belong", uid); ids != nil {
|
||||
return *ids, nil
|
||||
}
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
var ids = make([]string, 0)
|
||||
err := global.DBConn.
|
||||
Table(new(model.Park)).
|
||||
Where(builder.Eq{"user_id": uid}).
|
||||
Cols("id").
|
||||
Find(&ids)
|
||||
err := global.DB.NewSelect().Model((*model.Park)(nil)).
|
||||
Where("user_id = ?", uid).
|
||||
Column("id").
|
||||
Scan(ctx, &ids)
|
||||
if err != nil {
|
||||
return make([]string, 0), err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user