package service import ( "electricity_bill_calc/cache" "electricity_bill_calc/exceptions" "electricity_bill_calc/global" "electricity_bill_calc/model" "fmt" "xorm.io/builder" ) type _ParkService struct{} var ParkService _ParkService func (_ParkService) SaveNewPark(park model.Park) error { _, err := global.DBConn.Insert(park) if err != nil { return err } cache.AbolishRelation("park") return nil } 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) if err != nil { if rows == 0 { return exceptions.NewNotFoundError("未能找到符合条件的园区。") } else { return err } } cache.AbolishRelation("park") 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}) if err != nil { if rows == 0 { return exceptions.NewNotFoundError("未能找到符合条件的园区。") } else { return err } } cache.AbolishRelation("park") 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{}) if err != nil { if 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 } 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) if err != nil { return make([]model.Park, 0), err } cache.CacheSearch(parks, []string{"park"}, "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 } var park = &model.Park{} has, err := global.DBConn.ID(pid).NoAutoCondition().Get(park) if err != nil { return nil, err } if !has { return nil, exceptions.NewNotFoundError("未找到符合条件的园区记录。") } cache.CacheEntity(park, []string{"park"}, "park", pid) return park, nil } 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() if has { cache.CacheExists([]string{"park"}, "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 } var ids = make([]string, 0) err := global.DBConn. Table(new(model.Park)). Where(builder.Eq{"user_id": uid}). Cols("id"). Find(&ids) if err != nil { return make([]string, 0), err } cache.CacheSearch(ids, []string{"park"}, "park", "belong", uid) return ids, nil }