# 关于区间比例算法的实现总结_Ruby_编程开发_程序员俱乐部

### 关于区间比例算法的实现总结

2018/3/6 13:11:47  scholltop  程序员俱乐部  我要评论(0)
• 摘要：一：区间find／call选取PROFIT={0...10=>->(o){50},10...30=>->(o){40},30...50=>->(o){o},50...200=>->(o){o*0.5},200...1000=>->(o){o*0.4},1000..BigDecimal::INFINITY=>->(o){o*0.3}}defcompute_profit_priceresult=PROFIT.find{|k,_
• 标签：总结 实现 算法

```class="ruby" name="code">
PROFIT = {
0...10 => ->(o){ 50 },
10...30 => ->(o){ 40 },
30...50 => ->(o){ o },
50...200 => ->(o){ o * 0.5 },
200...1000 => ->(o){ o * 0.4 },
1000..BigDecimal::INFINITY => ->(o){ o * 0.3 }
}

def compute_profit_price
result = PROFIT.find { |k, _| k.include? import_price }
self.profit_price = result[1].call(import_price).to_d.ceil(2)
self.save
end
```

```GROSS_RATE =
["10000 <= _ && _ < 15000",0.03],
["15000 <= _  && _ < 20000",0.05],
["20000 <= _  && _ < 25000",0.07],
["25000 <= _ && _ < 30000",0.09],
["30000 <= _  && _< 35000",0.11],
["35000 <= _  && _< 40000",0.13],
["_ >= 40000",0.05,40000]],
["10000 <= _ && _ < 15000",0.04],
["15000 <= _  && _ < 20000",0.06],
["20000 <= _  && _ < 25000",0.08],
["25000 <= _ && _ < 30000",0.1],
["30000 <= _  && _< 35000",0.12],
["35000 <= _  && _< 40000",0.14],
["40000 <= _  && _< 45000",0.16],
["45000 <= _  && _< 50000",0.18],
["_ >= 50000",0.06,50000]] }

AMOUNT_RATE =
[["_ < 500",0.01],
["500 <= _ && _ <2000",0.009],
["2000 <= _ && _ <5000",0.008],
["5000 <= _ && _ < 10000",0.007],
["10000 <= _ && _ <50000",0.006],
["50000 <= _ && _ < 100000",0.005],
["100000 <= _ && _ < 500000",0.004],
["_ > 500000",0.003]]

```

# 销售金额毛利
```  def self.get_gross_rate(trader_type,amount)
return_rtb_rate = 0
base_trpe.each_with_index do |rtb_rate,index|
_ = amount
if eval(rtb_rate[0])
case_amount = (index + 1 == base_trpe.size) ? (amount - rtb_rate[2]) : (amount % 5000)
return_rtb_rate += rtb_rate[1] * case_amount
break
else
return_rtb_rate += rtb_rate[1] * 5000
end
end
return return_rtb_rate
end
```

# 毛利奖金
```  def self.get_amount_rate(amount)
return_rtb_rate = 0
AMOUNT_RATE.each do |rtb_rate|
_ = amount
if eval(rtb_rate[0])
return_rtb_rate = amount * rtb_rate[1]
break
end
end
return return_rtb_rate
end
```