electricity_bill_calc_service/service/park.go

175 lines
4.7 KiB
Go

package service
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/exceptions"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model"
"fmt"
"github.com/uptrace/bun"
"go.uber.org/zap"
)
type _ParkService struct {
l *zap.Logger
}
var ParkService = _ParkService{
l: logger.Named("Service", "Park"),
}
func (_ParkService) SaveNewPark(park model.Park) error {
ctx, cancel := global.TimeoutContext()
defer cancel()
_, err := global.DB.NewInsert().Model(&park).Exec(ctx)
if err != nil {
return err
}
cache.AbolishRelation("park")
return nil
}
func (_ParkService) UpdateParkInfo(park *model.Park) error {
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, _ := res.RowsAffected(); rows == 0 {
return exceptions.NewNotFoundError("未能找到符合条件的园区。")
} else {
return err
}
}
cache.AbolishRelation(fmt.Sprintf("park:%s", park.Id))
return nil
}
func (_ParkService) ChangeParkState(uid, pid string, state bool) error {
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, _ := res.RowsAffected(); rows == 0 {
return exceptions.NewNotFoundError("未能找到符合条件的园区。")
} else {
return err
}
}
cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
return nil
}
func (_ParkService) DeletePark(uid, pid string) error {
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, _ := res.RowsAffected(); rows == 0 {
return exceptions.NewNotFoundError("未能找到符合条件的园区。")
} else {
return err
}
}
cache.AbolishRelation("park")
cache.AbolishRelation(fmt.Sprintf("park:%s", pid))
return nil
}
func (_ParkService) ListAllParkBelongsTo(uid, keyword string) ([]model.Park, error) {
if parks, _ := cache.RetreiveSearch[[]model.Park]("park", "belong", uid, keyword); parks != nil {
return *parks, nil
}
parks := make([]model.Park, 0)
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
}
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
}
func (_ParkService) FetchParkDetail(pid string) (*model.Park, error) {
if park, _ := cache.RetreiveEntity[model.Park]("park", pid); park != nil {
return park, nil
}
ctx, cancel := global.TimeoutContext()
defer cancel()
var park = new(model.Park)
err := global.DB.NewSelect().Model(park).
Where("id = ?", pid).
Scan(ctx)
if err != nil {
return nil, err
}
if park == nil {
return nil, exceptions.NewNotFoundError("未找到符合条件的园区记录。")
}
cache.CacheEntity(*park, []string{fmt.Sprintf("park:%s", pid)}, "park", pid)
return park, nil
}
func (_ParkService) EnsurePark(uid, pid string) (bool, error) {
if has, _ := cache.CheckExists("park", pid, uid); has {
return has, nil
}
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{fmt.Sprintf("park:%s", pid)}, "park", pid, uid)
}
return has, err
}
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.DB.NewSelect().Model((*model.Park)(nil)).
Where("user_id = ?", uid).
Column("id").
Scan(ctx, &ids)
if err != nil {
return make([]string, 0), err
}
cache.CacheSearch(ids, []string{"park"}, "park", "belong", uid)
return ids, nil
}