| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| package controller |
|
|
| import ( |
| "github.com/gin-gonic/gin" |
| "veloera/middleware" |
| "veloera/model" |
| "veloera/setting" |
| "veloera/setting/operation_setting" |
| ) |
|
|
| func GetPricing(c *gin.Context) { |
| pricing := model.GetPricing() |
| userId, exists := c.Get("id") |
| usableGroup := map[string]string{} |
| groupRatio := map[string]float64{} |
| for s, f := range setting.GetGroupRatioCopy() { |
| groupRatio[s] = f |
| } |
| var group string |
| if exists { |
| user, err := model.GetUserCache(userId.(int)) |
| if err == nil { |
| group = user.Group |
| } |
| } |
|
|
| usableGroup = setting.GetUserUsableGroups(group) |
| |
| for group := range setting.GetGroupRatioCopy() { |
| if _, ok := usableGroup[group]; !ok { |
| delete(groupRatio, group) |
| } |
| } |
|
|
| |
| pricingWithPrefixes := enhancePricingWithPrefixes(pricing, group) |
|
|
| c.JSON(200, gin.H{ |
| "success": true, |
| "data": pricingWithPrefixes, |
| "group_ratio": groupRatio, |
| "usable_group": usableGroup, |
| }) |
| } |
|
|
| |
| |
| func enhancePricingWithPrefixes(pricing []model.Pricing, group string) []model.Pricing { |
| |
| prefixChannels := middleware.GetPrefixChannels(group) |
|
|
| |
| modelsWithPrefix := make(map[string]bool) |
|
|
| |
| var prefixedPricingModels []model.Pricing |
|
|
| for prefix, channels := range prefixChannels { |
| if prefix == "" { |
| continue |
| } |
|
|
| |
| for _, channel := range channels { |
| for _, channelModel := range channel.GetModels() { |
| |
| for _, baseModelPricing := range pricing { |
| if baseModelPricing.ModelName == channelModel { |
| |
| prefixedPricing := baseModelPricing |
| prefixedPricing.ModelName = prefix + channelModel |
|
|
| |
| prefixedPricingModels = append(prefixedPricingModels, prefixedPricing) |
|
|
| |
| modelsWithPrefix[channelModel] = true |
| break |
| } |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| var result []model.Pricing |
|
|
| |
| result = append(result, prefixedPricingModels...) |
|
|
| |
| nonPrefixedChannels := prefixChannels[""] |
| modelsFromNonPrefixedChannels := make(map[string]bool) |
|
|
| for _, channel := range nonPrefixedChannels { |
| for _, model := range channel.GetModels() { |
| modelsFromNonPrefixedChannels[model] = true |
| } |
| } |
|
|
| |
| |
| |
| for _, modelPricing := range pricing { |
| modelName := modelPricing.ModelName |
|
|
| |
| |
| if !modelsWithPrefix[modelName] && modelsFromNonPrefixedChannels[modelName] { |
| result = append(result, modelPricing) |
| } |
| } |
|
|
| return result |
| } |
|
|
| func ResetModelRatio(c *gin.Context) { |
| defaultStr := operation_setting.DefaultModelRatio2JSONString() |
| err := model.UpdateOption("ModelRatio", defaultStr) |
| if err != nil { |
| c.JSON(200, gin.H{ |
| "success": false, |
| "message": err.Error(), |
| }) |
| return |
| } |
| err = operation_setting.UpdateModelRatioByJSONString(defaultStr) |
| if err != nil { |
| c.JSON(200, gin.H{ |
| "success": false, |
| "message": err.Error(), |
| }) |
| return |
| } |
| c.JSON(200, gin.H{ |
| "success": true, |
| "message": "้็ฝฎๆจกๅๅ็ๆๅ", |
| }) |
| } |
|
|