refactor(park):园区部分基本完成迁移。

This commit is contained in:
徐涛 2022-09-16 09:26:48 +08:00
parent c6c1423364
commit 3d20ceb35a

View File

@ -4,18 +4,26 @@ import (
"electricity_bill_calc/cache" "electricity_bill_calc/cache"
"electricity_bill_calc/exceptions" "electricity_bill_calc/exceptions"
"electricity_bill_calc/global" "electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model" "electricity_bill_calc/model"
"fmt" "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 { 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 { if err != nil {
return err return err
} }
@ -24,52 +32,59 @@ func (_ParkService) SaveNewPark(park model.Park) error {
} }
func (_ParkService) UpdateParkInfo(park *model.Park) error { func (_ParkService) UpdateParkInfo(park *model.Park) error {
rows, err := global.DBConn. ctx, cancel := global.TimeoutContext()
Where(builder.Eq{"id": park.Id, "user_id": park.UserId}). defer cancel()
Cols("name", "abbr", "region", "address", "contact", "phone", "capacity", "tenement_quantity", "category", "meter_04kv_type"). res, err := global.DB.NewUpdate().Model(&park).
Update(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 err != nil {
if rows == 0 { if rows, _ := res.RowsAffected(); rows == 0 {
return exceptions.NewNotFoundError("未能找到符合条件的园区。") return exceptions.NewNotFoundError("未能找到符合条件的园区。")
} else { } else {
return err return err
} }
} }
cache.AbolishRelation("park") cache.AbolishRelation(fmt.Sprintf("park:%s", park.Id))
cache.AbolishRelation(fmt.Sprintf("park_%s", park.Id))
return nil return nil
} }
func (_ParkService) ChangeParkState(uid, pid string, state bool) error { func (_ParkService) ChangeParkState(uid, pid string, state bool) error {
rows, err := global.DBConn. ctx, cancel := global.TimeoutContext()
Table(&model.Park{}). defer cancel()
Where(builder.Eq{"id": pid, "user_id": uid}). res, err := global.DB.NewUpdate().Model((*model.Park)(nil)).
Update(map[string]interface{}{"enabled": state}) Where("id = ?", pid).
Where("user_id = ?", uid).
Set("enabled = ?", state).
Exec(ctx)
if err != nil { if err != nil {
if rows == 0 { if rows, _ := res.RowsAffected(); rows == 0 {
return exceptions.NewNotFoundError("未能找到符合条件的园区。") return exceptions.NewNotFoundError("未能找到符合条件的园区。")
} else { } else {
return err return err
} }
} }
cache.AbolishRelation("park") cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
cache.AbolishRelation(fmt.Sprintf("park_%s", pid))
return nil return nil
} }
func (_ParkService) DeletePark(uid, pid string) error { func (_ParkService) DeletePark(uid, pid string) error {
rows, err := global.DBConn. ctx, cancel := global.TimeoutContext()
Where(builder.Eq{"id": pid, "user_id": uid}). defer cancel()
Delete(&model.Park{}) res, err := global.DB.NewDelete().Model((*model.Park)(nil)).
Where("id = ?", pid).
Where("user_id = ?", uid).
Exec(ctx)
if err != nil { if err != nil {
if rows == 0 { if rows, _ := res.RowsAffected(); rows == 0 {
return exceptions.NewNotFoundError("未能找到符合条件的园区。") return exceptions.NewNotFoundError("未能找到符合条件的园区。")
} else { } else {
return err return err
} }
} }
cache.AbolishRelation("park") cache.AbolishRelation("park")
cache.AbolishRelation(fmt.Sprintf("park_%s", pid)) cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
return nil 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 { if parks, _ := cache.RetreiveSearch[[]model.Park]("park", "belong", uid, keyword); parks != nil {
return *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) parks := make([]model.Park, 0)
err := global.DBConn. cond := global.DB.NewSelect().Model(&parks).
Where(cond). Where("user_id = ?", uid)
NoAutoCondition(). if len(keyword) > 0 {
Find(&parks) 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 { if err != nil {
return make([]model.Park, 0), err 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 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 { if park, _ := cache.RetreiveEntity[model.Park]("park", pid); park != nil {
return park, nil return park, nil
} }
var park = &model.Park{} ctx, cancel := global.TimeoutContext()
has, err := global.DBConn.ID(pid).NoAutoCondition().Get(park) defer cancel()
var park *model.Park
err := global.DB.NewSelect().Model(&park).
Where("id = ?", pid).
Scan(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !has { if park == nil {
return nil, exceptions.NewNotFoundError("未找到符合条件的园区记录。") 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 return park, nil
} }
@ -119,9 +143,14 @@ func (_ParkService) EnsurePark(uid, pid string) (bool, error) {
if has, _ := cache.CheckExists("park", pid, uid); has { if has, _ := cache.CheckExists("park", pid, uid); has {
return has, nil 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 { if has {
cache.CacheExists([]string{"park"}, "park", pid, uid) cache.CacheExists([]string{fmt.Sprintf("park:%s", pid)}, "park", pid, uid)
} }
return has, err return has, err
} }
@ -130,12 +159,13 @@ func (_ParkService) AllParkIds(uid string) ([]string, error) {
if ids, _ := cache.RetreiveSearch[[]string]("park", "belong", uid); ids != nil { if ids, _ := cache.RetreiveSearch[[]string]("park", "belong", uid); ids != nil {
return *ids, nil return *ids, nil
} }
ctx, cancel := global.TimeoutContext()
defer cancel()
var ids = make([]string, 0) var ids = make([]string, 0)
err := global.DBConn. err := global.DB.NewSelect().Model((*model.Park)(nil)).
Table(new(model.Park)). Where("user_id = ?", uid).
Where(builder.Eq{"user_id": uid}). Column("id").
Cols("id"). Scan(ctx, &ids)
Find(&ids)
if err != nil { if err != nil {
return make([]string, 0), err return make([]string, 0), err
} }