# lower_limit = [7000, 2000] # 保底(年保底颖医,月保底)
# high_limit = [10000, 5000] # 上限(年上限,月上限)
# original_price = [[1000, 1600], [800, 800], [2000]] # 不同的價(jià)格的線索挨决,每個(gè)月的放在一個(gè)列表中
# append_price = [["1", 900],["2", 600]] # 補(bǔ)保底后追加的線索抽减,第X月追加一筆x元
# refund_type = "alread" # 是未扣保底前進(jìn)行退費(fèi),alread是扣保底后進(jìn)行退費(fèi)
# refund_place = [["0", "1"]] # 第X月第X條線索退費(fèi)
目的:通過(guò)接口傳遞上述參數(shù)例嘱,腳本進(jìn)行計(jì)算后,并輸出相應(yīng)結(jié)果
腳本1#price_step.py
#price_step.py
from datetime import datetime, timedelta
import calendar
def getMonthIter(start_month, end_month):
# 時(shí)間格式為 %Y-%m
out_month_arr = []
dt_date = datetime.strptime(start_month, '%Y-%m-%d')
dt_str = start_month
while dt_str <= end_month:
out_month_arr.append(dt_str)
dt_date = dt_date + timedelta(days=calendar.monthrange(dt_date.year, dt_date.month)[1])
dt_str = dt_date.strftime('%Y-%m-%d')
return out_month_arr
def day_guarantee(day_interval,day_date,count_type,minday,high_limit,lower_limit):
if count_type==1:
if minday<day_interval:
Month_lower = round((lower_limit[1] * day_interval / day_date),2)
else:
Month_lower=0
Month_high=round((high_limit[1] * day_interval / day_date),2)
else:
Month_lower=lower_limit[1]
Month_high=high_limit[1]
return Month_lower,Month_high
from datetime import datetime
def calculate_limit():
start_date = '2022-01-01'
end_date = '2022-04-01'
Month_high_limit = 400
Year_high_limit = 3000
Month_lower_limit = 200
Year_lower_limit = 1500
lower_limit = [Year_lower_limit, Month_lower_limit] # 保底(年保底宁舰,月保底)
high_limit = [Year_high_limit, Month_high_limit] # 上限(年上限拼卵,月上限)
minday = 0 # 不足X天的不需要計(jì)算月保底
year_minday = 0 # 不足X天不計(jì)算年保底
start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
days = int(str(end_datetime - start_datetime).split()[0]) +1 # 計(jì)算間隔天數(shù)
print(days)
Year_high = round((Year_high_limit * days / 365),2)
if days < year_minday:
Year_lower = 0
else:
Year_lower = round((lower_limit[0] * days / 365),2)
print("年保底和上限分別是{},{}".format(Year_lower, Year_high))
getMonthdata=getMonthIter(start_date, end_date)
# print(getMonthdata)
guarantee_list=[]
guarantee_data_list=[]
for index in range(0, len(getMonthdata)):
start_date = getMonthdata[index]
# print(index,len(getMonthdata))
start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
a=start_datetime.month
date=start_datetime.day
if a in (1,5,3,7,8,10,12):
day_date=31
elif a in (4,6,9,11):
day_date = 30
else:day_date = 28
if index ==0: #不足滿月的,需要計(jì)算月保底和上限
day_interval=day_date-date+1
guarantee_data=day_guarantee(day_interval,day_date,1,minday,high_limit,lower_limit) #計(jì)算保底上限
elif index+1 ==len(getMonthdata): #不足滿月的蛮艰,需要計(jì)算月保底和上限
day_interval=end_datetime.day
guarantee_data=day_guarantee(day_interval,day_date,1,minday,high_limit,lower_limit) #計(jì)算保底上限
else:
day_interval=day_date
guarantee_data=day_guarantee(day_interval,day_date, 0,minday,high_limit,lower_limit)
print("{}月,使用天數(shù){}腋腮,月保底{},月上限{}".format(a,day_interval,guarantee_data[0],guarantee_data[1]))
guarantee_list.append([index,day_interval,guarantee_data[0],guarantee_data[1]])
return guarantee_list,Year_lower, Year_high
# print(day_interval,guarantee_data) #月天數(shù),月保底即寡,上限
#計(jì)算年保底:
# start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
# end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
# delta = (end_datetime -start_datetimestr).split()[0])
# days = delta.days
# print("日期之間的天數(shù):", days)
calculate_data=calculate_limit()
print(calculate_data[0]) #月保底上限
print(calculate_data[1]) #年保底
print(calculate_data[2]) #年上限
if __name__ == '__main__':
# day_difference()
# day_guarantee(22)
pass
腳本2#account.py
from flask import Flask, jsonify, request
import re
app = Flask(__name__)
# 使通過(guò)jsonify返回的中文顯示正常徊哑,否則顯示為ASCII碼
app.json.ensure_ascii = False
# 保底lower_limit 月保底Month_lower_limit 年保底Year_lower_limit
# 上限high_limit 月上限Month_high_limit 年上限Year_high_limit
# 價(jià)格price [0-2]200 [3-]100
# 折扣價(jià)discount_price 折扣discount 原價(jià)original_price
# 本次預(yù)扣withhold 本次實(shí)扣Actual_deduction 累計(jì)計(jì)價(jià)accumulate_pricing 累計(jì)實(shí)扣accumulate_price 累計(jì)數(shù)量count
def accumulate_original_price(lower_limit, high_limit, original_price,describe,accumulate_original_money):
# lower_limit = [7000, 2000] # 保底(年保底,月保底)
# high_limit = [10000, 5000] # 上限(年上限聪富,月上限)
# original_price = [[1000, 1600], [800, 800], [2000]] # 不同的價(jià)格的線索莺丑,每個(gè)月的放在一個(gè)列表中
# append_price = [["1", 900],["2", 600]] # 補(bǔ)保底后追加的線索,第X月追加一筆x元
# refund_type = "alread" # 是未扣保底前進(jìn)行退費(fèi)墩蔓,alread是扣保底后進(jìn)行退費(fèi)
# refund_place = [["0", "1"]] # 第X月第X條線索退費(fèi)
original_price_day=[]
price_refunding_total = 0
Month_high_limit = high_limit[1]
Year_high_limit = high_limit[0]
Month_lower_limit = lower_limit[1]
Year_lower_limit = lower_limit[0]
accumulate_pricing_list = []
accumulate_price_list = []
total_accumulate_price = 0
total_accumulate_pricing = 0
month_bu_total = 0
exceed_Monthhigh_total = 0
month_data = []
print(original_price)
for index, i in enumerate(original_price):
accumulate_pricing = sum(i)
if accumulate_original_money ==0:
accumulate_pricing_data=accumulate_pricing
else:
accumulate_pricing_data=sum(accumulate_original_money[index])
index_accumulate_pricing = "第{}個(gè)月計(jì)價(jià):{}".format(index, accumulate_pricing_data)
accumulate_pricing_list.append(accumulate_pricing_data)
accumulate_pricing_total = accumulate_pricing
if total_accumulate_price + accumulate_pricing > Year_high_limit: # 加上新線索的計(jì)價(jià)已大于年上限
accumulate_price = round(Year_high_limit - total_accumulate_price,2)
else:
if accumulate_pricing >= Month_high_limit: # 大于月上限
accumulate_price = Month_high_limit
month_bu_lower = 0
exceed_Month_high = accumulate_pricing - Month_high_limit
elif accumulate_pricing <= Month_lower_limit: # 小于月保底
accumulate_price = Month_lower_limit
month_bu_lower = round(accumulate_price - accumulate_pricing,2) #補(bǔ)保底
exceed_Month_high = 0
else:
accumulate_price = accumulate_pricing
month_bu_lower = 0
exceed_Month_high = 0
month_bu_total += month_bu_lower
exceed_Monthhigh_total += exceed_Month_high
if month_bu_lower !=0 or exceed_Month_high !=0:
lower_exceed_record="{}月補(bǔ)保底金額{}梢莽,超出月上限的金額為{}".format(index, month_bu_lower, exceed_Month_high)
print(lower_exceed_record)
month_data.append(lower_exceed_record)
total_money=accumulate_price
print(total_money)
day_list=[]
for day_money in i:
day_money_last=total_money
total_money = round(total_money - int(day_money),2)
day_money=round(day_money,2)
if total_money <0:
day_money=day_money_last #余額扣為負(fù)數(shù)的時(shí)候不再扣費(fèi),只是把上一輪剩余的錢支出
if day_money_last <0:
day_money=0
day_list.append(day_money)
original_price_day.append(day_list)
# day_money=accumulate_price-i
index_accumulate_price = "{}月實(shí)扣:{}奸披,金額分別是{}".format(index, accumulate_price,day_list)
total_accumulate_pricing += accumulate_pricing_data # 月累計(jì)計(jì)價(jià)
total_accumulate_price += accumulate_price # 月累計(jì)實(shí)扣
index_totalaccumulate_pricing = "{}月累計(jì)預(yù)扣:{}".format(index, total_accumulate_pricing)
index_totalaccumulate_price = "{}月累計(jì)實(shí)扣:{}".format(index, total_accumulate_price)
accumulate_price_list.append(accumulate_price)
month_data.append([index_accumulate_pricing, index_accumulate_price, index_totalaccumulate_pricing,
index_totalaccumulate_price])
# print(original_price_day)
print(month_data)
pricing_total = sum(accumulate_pricing_list)
price_total = sum(accumulate_price_list)
if price_total > Year_high_limit:
price_total = Year_high_limit
else:
price_total = price_total
pricing_total_describe = "綜合整個(gè)周期累計(jì)計(jì)價(jià):{}".format(pricing_total) # 累計(jì)計(jì)價(jià)
# price_total_describe="根據(jù)每月上限{}和年上限{}昏名,綜合累計(jì)實(shí)扣:{}".format(Month_high_limit, Year_high_limit, price_total)# 累計(jì)扣價(jià)
price_total_describe = "綜合累計(jì)實(shí)扣:{}".format(price_total) # 累計(jì)扣價(jià)
if price_total <= Year_lower_limit: # 小于年保底
bu_lower_limit = Year_lower_limit - price_total
accumulate_price_data = Year_lower_limit
else:
accumulate_price_data = price_total
bu_lower_limit = 0
bu_lower_total = month_bu_total + bu_lower_limit
bu_lower_limitdescrible = "還需要補(bǔ)年保底金額:{}".format(round(bu_lower_limit)) # 初次補(bǔ)年保底
accumulate_price_datadescrible = "綜合扣費(fèi)(保底+實(shí)扣):{}".format(accumulate_price_data) # 綜合扣費(fèi)(保底+實(shí)扣)
# print("月已補(bǔ)保底金額+年補(bǔ)保底金額合計(jì):{}".format(bu_lower_total))
accumulate_describle = [pricing_total_describe, price_total_describe, bu_lower_limitdescrible,
accumulate_price_datadescrible, "************************************************************************************************************"]
return describe,str(original_price), month_data, accumulate_describle,original_price_day,pricing_total,price_total
def price_data(lower_limit, high_limit, original_price, append_price, refund_place):
Year_high_limit = high_limit[0]
accumulate_original_money = 0
# 按線索計(jì)算
if original_price !=[]:
accumulate_count_price=accumulate_original_price(lower_limit, high_limit, original_price,"按線索計(jì)算",accumulate_original_money)
accumulate_money =accumulate_count_price[:4] # 計(jì)算滿足月保底,又滿足年保底
original_price_day=accumulate_count_price[4]
pricing_total=accumulate_count_price[5]
price_total=accumulate_count_price[6]
print(original_price_day)
else:
accumulate_money="============================不進(jìn)行計(jì)算"
# 補(bǔ)保底后又追加的線索計(jì)算
if append_price != []:
for append_money in append_price:
# print(append_money)
append_place = int(append_money[0])
original_price_day[append_place].append(append_money[1])
accumulate_append_money = accumulate_original_price(lower_limit, high_limit, original_price_day,"新追加線索后阵面,重新計(jì)算",accumulate_original_money)
else:
accumulate_append_money ="============================無(wú)新追加的線索"
# 退費(fèi)計(jì)算
print("111",refund_place)
if refund_place != []:
for i in refund_place:
retfund_place = int(i[0])
retfund_times = int(i[1])
renfund_money=int(original_price_day[retfund_place][retfund_times])
renfund_money_total=round(pricing_total,2)-renfund_money
print(renfund_money_total)
if renfund_money_total <Year_high_limit:
refund_price_data=round(price_total -renfund_money_total,2) #實(shí)際退還的金額
change_money=renfund_money-refund_price_data
original_price_day[retfund_place][retfund_times]=change_money
accumulate_original_money=original_price
accumulate_original_money[retfund_place].pop(retfund_times) #計(jì)價(jià)計(jì)算
print("-----------",accumulate_original_money,"-----------")
accumulate_refund_day=accumulate_original_price(lower_limit, high_limit, original_price_day,"退費(fèi)",accumulate_original_money)
accumulate_refund_money = accumulate_refund_day[:4]
else:
accumulate_refund_money="============================不進(jìn)行退費(fèi)計(jì)算"
return accumulate_money,accumulate_append_money,accumulate_refund_money
@app.route("/settlement", methods=['POST'])
def settlement():
clue = request.json.get("clue")
limit = request.json.get("limit")
lower_limit = limit["lower_limit"]
high_limit = limit["high_limit"]
original_price = clue["original_price"]
append_price = clue["append_price"]
refund_place = request.json.get("refund_place")
data = lower_limit, high_limit, original_price, append_price, refund_place
# print(lower_limit,high_limit,original_price,append_price,refund_place)
settlement_data = price_data(lower_limit, high_limit, original_price, append_price, refund_place)
return jsonify(
{"code": 0, "settlement_data": settlement_data})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=9089)
# lower_limit = [373.97, 200] # 保底(年保底轻局,月保底)
# high_limit = [747.95, 300] # 上限(年上限,月上限)
# original_price = [[100, 80], [100, 80, 80],[100,80,80,80]] # 不同的價(jià)格的線索膜钓,每個(gè)月的放在一個(gè)列表中
# append_price = [] # 補(bǔ)保底后追加的線索嗽交,第X月追加一筆x元
# refund_type = "alread" # 是未扣保底前進(jìn)行退費(fèi)卿嘲,alread是扣保底后進(jìn)行退費(fèi)
# refund_place = [["2", "0"]] # 第X月第X條線索退費(fèi)
# price_data(lower_limit,high_limit,original_price,append_price,refund_place)
使用:
運(yùn)行account.py腳本颂斜,打開postman,通過(guò)輸入不同的保底拾枣,上限金額沃疮,以及每個(gè)月分別的扣費(fèi)情況,扣費(fèi)后又追加的金額梅肤,以及各月退費(fèi)的金額計(jì)算出最后會(huì)扣除的金額
舉例:接口和入?yún)⑷缦?/p>
http://xx.xx.xx.xx:9089/settlement
headers:Content-Type=application/json;charset=utf8
入?yún)ⅲ?
{
"clue":{
"original_price":[[1000, 1600], [800, 800], [2000]], //不同的價(jià)格的線索司蔬,每個(gè)月的放在一個(gè)列表中
"append_price":[["1", 900],["2", 3000]] //補(bǔ)保底后追加的線索,第X月追加一筆x元
},
"limit":{
"lower_limit":[7000, 2000], //保底(年保底姨蝴,月保底)
"high_limit":[8000, 3000] //上限(年上限俊啼,月上限)
},
"refund_place":[["0", "1"],["1", "1"]] //第X月第X條線索退費(fèi)
}
響應(yīng)結(jié)果輸出如下:
{
"code": 0,
"settlement_data": [
[
"按線索計(jì)算",
"[[100, 80], [100, 80, 80], [100, 80, 80, 80]]",
[
"0月補(bǔ)保底金額20,超出月上限的金額為0",
[
"第0個(gè)月計(jì)價(jià):180",
"0月實(shí)扣:200左医,金額分別是[100, 80]",
"0月累計(jì)預(yù)扣:180",
"0月累計(jì)實(shí)扣:200"
],
[
"第1個(gè)月計(jì)價(jià):260",
"1月實(shí)扣:260授帕,金額分別是[100, 80, 80]",
"1月累計(jì)預(yù)扣:440",
"1月累計(jì)實(shí)扣:460"
],
[
"第2個(gè)月計(jì)價(jià):340",
"2月實(shí)扣:287.95,金額分別是[100, 80, 80, 27.95]",
"2月累計(jì)預(yù)扣:780",
"2月累計(jì)實(shí)扣:747.95"
]
],
[
"綜合整個(gè)周期累計(jì)計(jì)價(jià):780",
"綜合累計(jì)實(shí)扣:747.95",
"還需要補(bǔ)年保底金額:0",
"綜合扣費(fèi)(保底+實(shí)扣):747.95",
"************************************************************************************************************"
]
],
"============================無(wú)新追加的線索",
[
"退費(fèi)",
"[[100, 80], [100, 80, 80], [100, 32.05, 80, 27.95]]",
[
"0月補(bǔ)保底金額20浮梢,超出月上限的金額為0",
[
"第0個(gè)月計(jì)價(jià):180",
"0月實(shí)扣:200跛十,金額分別是[100, 80]",
"0月累計(jì)預(yù)扣:180",
"0月累計(jì)實(shí)扣:200"
],
[
"第1個(gè)月計(jì)價(jià):260",
"1月實(shí)扣:260,金額分別是[100, 80, 80]",
"1月累計(jì)預(yù)扣:440",
"1月累計(jì)實(shí)扣:460"
],
[
"第2個(gè)月計(jì)價(jià):260",
"2月實(shí)扣:240.0秕硝,金額分別是[100, 32.05, 80, 27.95]",
"2月累計(jì)預(yù)扣:700",
"2月累計(jì)實(shí)扣:700.0"
]
],
[
"綜合整個(gè)周期累計(jì)計(jì)價(jià):700",
"綜合累計(jì)實(shí)扣:700.0",
"還需要補(bǔ)年保底金額:0",
"綜合扣費(fèi)(保底+實(shí)扣):700.0",
"************************************************************************************************************"
]
]
]
}