forked from free-lancers/electricity_bill_calc_service
		
	enhance(maintenance):为配电维护费部分接口增加缓存支持。
This commit is contained in:
		| @@ -1,9 +1,11 @@ | ||||
| package service | ||||
|  | ||||
| import ( | ||||
| 	"electricity_bill_calc/cache" | ||||
| 	"electricity_bill_calc/exceptions" | ||||
| 	"electricity_bill_calc/global" | ||||
| 	"electricity_bill_calc/model" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/google/uuid" | ||||
| 	"xorm.io/builder" | ||||
| @@ -20,11 +22,15 @@ func (_MaintenanceFeeService) ListMaintenanceFees(pid []string) ([]model.Mainten | ||||
| 	} else { | ||||
| 		return make([]model.MaintenanceFee, 0), exceptions.NewIllegalArgumentsError("必须给定所要请求的至少一个园区", "park_id") | ||||
| 	} | ||||
| 	if fees, _ := cache.RetreiveSearch[[]model.MaintenanceFee]("maintenance_fee", pid...); fees != nil { | ||||
| 		return *fees, nil | ||||
| 	} | ||||
| 	var fees = make([]model.MaintenanceFee, 0) | ||||
| 	err := global.DBConn.Where(cond).Desc("created_at").Find(&fees) | ||||
| 	if err != nil { | ||||
| 		return make([]model.MaintenanceFee, 0), err | ||||
| 	} | ||||
| 	cache.CacheSearch(fees, "maintenance_fee", "maintenance_fee", pid...) | ||||
| 	return fees, nil | ||||
| } | ||||
|  | ||||
| @@ -35,6 +41,7 @@ func (_MaintenanceFeeService) CreateMaintenanceFeeRecord(fee model.MaintenanceFe | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	cache.AbolishRelation("maintenance_fee") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @@ -47,6 +54,8 @@ func (_MaintenanceFeeService) ModifyMaintenanceFee(fee model.MaintenanceFee) err | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 	cache.AbolishRelation("maintenance_fee") | ||||
| 	cache.AbolishRelation(fmt.Sprintf("maintenance_fee_%s", fee.Id)) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @@ -59,6 +68,8 @@ func (_MaintenanceFeeService) ChangeMaintenanceFeeState(fid string, state bool) | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 	cache.AbolishRelation("maintenance_fee") | ||||
| 	cache.AbolishRelation(fmt.Sprintf("maintenance_fee_%s", fid)) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @@ -71,10 +82,15 @@ func (_MaintenanceFeeService) DeleteMaintenanceFee(fid string) error { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 	cache.AbolishRelation("maintenance_fee") | ||||
| 	cache.AbolishRelation(fmt.Sprintf("maintenance_fee_%s", fid)) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (_MaintenanceFeeService) EnsureFeeBelongs(uid, mid string) (bool, error) { | ||||
| 	if has, _ := cache.CheckExists("maintenance_fee", mid, uid); has { | ||||
| 		return true, nil | ||||
| 	} | ||||
| 	var fee = make([]model.MaintenanceFee, 0) | ||||
| 	err := global.DBConn. | ||||
| 		ID(mid).Limit(1).Find(&fee) | ||||
| @@ -93,5 +109,8 @@ func (_MaintenanceFeeService) EnsureFeeBelongs(uid, mid string) (bool, error) { | ||||
| 	if len(park) == 0 { | ||||
| 		return false, exceptions.NewNotFoundError("指定维护费所属园区未找到。") | ||||
| 	} | ||||
| 	if park[0].UserId == uid { | ||||
| 		cache.CacheExists("maintenance_fee", "maintenance_fee", mid, uid) | ||||
| 	} | ||||
| 	return park[0].UserId == uid, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user