def get_user_activity(): for platform in Total.select(): UserActivity.create( source=platform.source, total=platform.total, active_count=platform.total - platform.default_good, active_percentage=calculate_percentage(platform.total, platform.total - platform.default_good), inactive_count=platform.default_good, inactive_percentage=calculate_percentage(platform.total, platform.default_good) )
def get_model_count(): # 苏宁和京东数据合并 all_color = ['国风雅灰', '钛银黑', '冰海蓝', '蜜桃金'] all_ram = ['8GB', '12GB'] all_rom = ['128GB', '256GB'] for color in all_color: for ram in all_ram: for rom in all_rom: all_ms = ModelSummary.select().where( (ModelSummary.product_color == color) & (ModelSummary.product_ram == ram) & (ModelSummary.product_rom == rom) ) # 没有12GB+128GB的奇葩内存组合 if len(all_ms) == 0: continue mc = ModelCount.create( product_color=color, product_ram=ram, product_rom=rom ) for ms in all_ms: if ms.source == '京东': mc.total += ms.default_good + ms.star_one + ms.star_two + \ ms.star_three + ms.star_four + ms.star_five mc.cal_total += ms.star_one + ms.star_two + ms.star_three + ms.star_four + ms.star_five mc.save() if ms.source == '苏宁': mc.total += ms.total mc.cal_total += ms.total mc.save() mc.default_good += ms.default_good mc.good_count += ms.star_four + ms.star_five mc.general_count += ms.star_two + ms.star_three mc.bad_count += ms.star_one mc.star_one += ms.star_one mc.star_two += ms.star_two mc.star_three += ms.star_three mc.star_four += ms.star_four mc.star_five += ms.star_five mc.save() # 计算占比和好评率 (京东计算规则) jd_total = Total.get(Total.source == '京东') sn_total = Total.get(Total.source == '苏宁') models_total = jd_total.all_models_total + sn_total.all_models_total for mc in ModelCount.select(): mc.percentage = calculate_percentage(models_total, mc.total) mc.good_rate = calculate_percentage(mc.cal_total, mc.good_count) mc.save()
def get_rom_count(): jd_total = Total.get(Total.source == '京东') sn_total = Total.get(Total.source == '苏宁') models_total = jd_total.all_models_total + sn_total.all_models_total all_rom = ['64GB', '128GB', '256GB'] for rom in all_rom: rom_total = 0 rom_cal_total = 0 rom_default_good = 0 rom_good_count = 0 rom_general_count = 0 rom_bad_count = 0 rom_star_one = 0 rom_star_two = 0 rom_star_three = 0 rom_star_four = 0 rom_star_five = 0 all_mc = ModelCount.select().where(ModelCount.rom == rom) for mc in all_mc: rom_total += mc.total rom_cal_total += mc.cal_total rom_default_good += mc.default_good rom_good_count += mc.good_count rom_general_count += mc.general_count rom_bad_count += mc.bad_count rom_star_one += mc.star_one rom_star_two += mc.star_two rom_star_three += mc.star_three rom_star_four += mc.star_four rom_star_five += mc.star_five RomCount.create(rom=rom, total=rom_total, percentage=calculate_percentage( models_total, rom_total), cal_total=rom_cal_total, good_rate=calculate_percentage(rom_cal_total, rom_good_count), default_good=rom_default_good, good_count=rom_good_count, general_count=rom_general_count, bad_count=rom_bad_count, star_one=rom_star_one, star_two=rom_star_two, star_three=rom_star_three, star_four=rom_star_four, star_five=rom_star_five)
def get_wh_self_per(): total_count = Commodity.select(fn.SUM( Commodity.total).alias('tc')).dicts()[0]['tc'] self_count = Commodity.select(fn.SUM(Commodity.total).alias('tc')).where( Commodity.is_self == True).dicts()[0]['tc'] non_self_count = Commodity.select(fn.SUM( Commodity.total).alias('tc')).where( Commodity.is_self == False).dicts()[0]['tc'] WHSelfPer.create( total_count=total_count, self_count=self_count, self_percentage=calculate_percentage(total_count, self_count), non_self_count=non_self_count, non_self_percentage=calculate_percentage(total_count, non_self_count))
def get_color_count(): jd_total = Total.get(Total.source == '京东') sn_total = Total.get(Total.source == '苏宁') models_total = jd_total.all_models_total + sn_total.all_models_total all_color = ['黑色', '白色', '红色', '黄色', '紫色', '绿色'] for color in all_color: color_total = 0 color_cal_total = 0 color_default_good = 0 color_good_count = 0 color_general_count = 0 color_bad_count = 0 color_star_one = 0 color_star_two = 0 color_star_three = 0 color_star_four = 0 color_star_five = 0 all_mc = ModelCount.select().where(ModelCount.color == color) for mc in all_mc: color_total += mc.total color_cal_total += mc.cal_total color_default_good += mc.default_good color_good_count += mc.good_count color_general_count += mc.general_count color_bad_count += mc.bad_count color_star_one += mc.star_one color_star_two += mc.star_two color_star_three += mc.star_three color_star_four += mc.star_four color_star_five += mc.star_five ColorCount.create( color=color, total=color_total, percentage=calculate_percentage(models_total, color_total), cal_total=color_cal_total, good_rate=calculate_percentage(color_cal_total, color_good_count), default_good=color_default_good, good_count=color_good_count, general_count=color_general_count, bad_count=color_bad_count, star_one=color_star_one, star_two=color_star_two, star_three=color_star_three, star_four=color_star_four, star_five=color_star_five)
def get_wh_price_and_sales(): for commodity in Commodity.select(): if 0 < commodity.price < 100: whpas = WHPriceAndSales.get_by_id('0-100元') whpas.total += commodity.total whpas.save() if 100 <= commodity.price < 200: whpas = WHPriceAndSales.get_by_id('100-200元') whpas.total += commodity.total whpas.save() if 200 <= commodity.price < 500: whpas = WHPriceAndSales.get_by_id('200-500元') whpas.total += commodity.total whpas.save() if 500 <= commodity.price < 900: whpas = WHPriceAndSales.get_by_id('500-900元') whpas.total += commodity.total whpas.save() if 900 <= commodity.price < 2000: whpas = WHPriceAndSales.get_by_id('900-2000元') whpas.total += commodity.total whpas.save() if commodity.price >= 2000: whpas = WHPriceAndSales.get_by_id('2000元以上') whpas.total += commodity.total whpas.save() total_count = WHPriceAndSales.select( fn.SUM(WHPriceAndSales.total).alias('tc')).dicts()[0]['tc'] print(total_count) for whpas in WHPriceAndSales.select(): whpas.percentage = calculate_percentage(total_count, whpas.total) whpas.save()
def get_brand_percentage(): # redmi = BrandPercentage.get( # BrandPercentage.main_brand == '小米', # BrandPercentage.sub_brand == '红米' # ) # xiaomi = BrandPercentage.get( # BrandPercentage.main_brand == '小米', # BrandPercentage.sub_brand == '小米' # ) # for phone in Phone.select().where(Phone.brand == '小米'): # if '小米' in phone.model: # xiaomi.total += phone.total # xiaomi.save() # if '红米' in phone.model: # redmi.total += phone.total # redmi.save() # # vivo = BrandPercentage.get( # BrandPercentage.main_brand == 'vivo', # BrandPercentage.sub_brand == 'vivo' # ) # iqoo = BrandPercentage.get( # BrandPercentage.main_brand == 'vivo', # BrandPercentage.sub_brand == 'iQOO' # ) # for phone in Phone.select().where(Phone.brand == 'vivo'): # if 'iQOO' in phone.model: # iqoo.total += phone.total # iqoo.save() # else: # vivo.total += phone.total # vivo.save() # # oppo = BrandPercentage.get( # BrandPercentage.main_brand == 'OPPO', # BrandPercentage.sub_brand == 'OPPO' # ) # realme = BrandPercentage.get( # BrandPercentage.main_brand == 'OPPO', # BrandPercentage.sub_brand == 'realme' # ) # oneplus = BrandPercentage.get( # BrandPercentage.main_brand == 'OPPO', # BrandPercentage.sub_brand == '一加' # ) # oppo.total = Phone.select(fn.SUM(Phone.total).alias('tc')).where(Phone.brand == 'OPPO').dicts()[0]['tc'] # oppo.save() # realme.total = Phone.select(fn.SUM(Phone.total).alias('tc')).where(Phone.brand == 'realme').dicts()[0]['tc'] # realme.save() # oneplus.total = Phone.select(fn.SUM(Phone.total).alias('tc')).where(Phone.brand == '一加').dicts()[0]['tc'] # oneplus.save() brand_list = ['小米', 'OPPO', 'vivo'] for brand in brand_list: total_count = BrandPercentage.select(fn.SUM(BrandPercentage.total).alias('tc')) \ .where(BrandPercentage.main_brand == brand).dicts()[0]['tc'] print(total_count) for bp in BrandPercentage.select().where(BrandPercentage.main_brand == brand): bp.percentage = calculate_percentage(total_count, bp.total) bp.save()
def get_jd_total(): jd_comments_summary = CommentSummary.get(CommentSummary.source == '京东') jd_total = jd_comments_summary.default_good + jd_comments_summary.star_one + jd_comments_summary.star_two + \ jd_comments_summary.star_three + jd_comments_summary.star_four + jd_comments_summary.star_five jd_good_count = jd_comments_summary.star_four + jd_comments_summary.star_five jd_general_cont = jd_comments_summary.star_two + jd_comments_summary.star_three jd_bad_count = jd_comments_summary.star_one jd_cal_total = jd_comments_summary.star_one + jd_comments_summary.star_two + jd_comments_summary.star_three + \ jd_comments_summary.star_four + jd_comments_summary.star_five jd_good_rate = calculate_percentage(jd_cal_total, jd_good_count) jd_models_total = 0 for ms in ModelSummary.select().where(ModelSummary.source == '京东'): jd_models_total += ms.default_good + ms.star_one + ms.star_two + ms.star_three + ms.star_four + ms.star_five Total.create( source='京东', total=jd_total, all_models_total=jd_models_total, good_rate=jd_good_rate, default_good=jd_comments_summary.default_good, good_count=jd_good_count, general_count=jd_general_cont, bad_count=jd_bad_count, star_one=jd_comments_summary.star_one, star_two=jd_comments_summary.star_two, star_three=jd_comments_summary.star_three, star_four=jd_comments_summary.star_four, star_five=jd_comments_summary.star_five )
def get_user_device_count(): total = Comment.select().where(Comment.user_device.is_null(False)).count() android = Comment.select().where(Comment.user_device == 'Android').count() ios = Comment.select().where(Comment.user_device == 'iOS').count() other = Comment.select().where(Comment.user_device == 'other').count() android_percentage = calculate_percentage(total, android) ios_percentage = calculate_percentage(total, ios) other_percentage = calculate_percentage(total, other) UserDeviceCount.create(total=total, android=android, ios=ios, other=other, android_percentage=android_percentage, ios_percentage=ios_percentage, other_percentage=other_percentage)
def get_phone_platform(): source_list = ['京东', '苏宁'] for source in source_list: total_count = Commodity.select(fn.SUM(Commodity.total).alias('tc')) \ .where(Commodity.source == source).dicts()[0]['tc'] commodity_count = Commodity.select().where(Commodity.source == source).count() self_count = Commodity.select(fn.SUM(Commodity.total).alias('tc')) \ .where((Commodity.source == source) & (Commodity.is_self == True)).dicts()[0]['tc'] non_self_count = Commodity.select(fn.SUM(Commodity.total).alias('tc')) \ .where((Commodity.source == source) & (Commodity.is_self == False)).dicts()[0]['tc'] PhonePlatform.create( source=source, total_count=total_count, commodity_count=commodity_count, self_percentage=calculate_percentage(total_count, self_count), non_self_percentage=calculate_percentage(total_count, non_self_count) )
def get_wh_price_and_brand(): # for commodity in Commodity.select(): # if 0 < commodity.price < 200: # try: # wh_pab = WHPriceAndBrand.get( # WHPriceAndBrand.price_range == '200元以下', # WHPriceAndBrand.brand == commodity.brand # ) # wh_pab.total += commodity.total # wh_pab.save() # except WHPriceAndBrand.DoesNotExist: # WHPriceAndBrand.create( # price_range='200元以下', # brand=commodity.brand, # total=commodity.total # ) # if 200 <= commodity.price < 900: # try: # wh_pab = WHPriceAndBrand.get( # WHPriceAndBrand.price_range == '200-900元', # WHPriceAndBrand.brand == commodity.brand # ) # wh_pab.total += commodity.total # wh_pab.save() # except WHPriceAndBrand.DoesNotExist: # WHPriceAndBrand.create( # price_range='200-900元', # brand=commodity.brand, # total=commodity.total # ) # if commodity.price >= 900: # try: # wh_pab = WHPriceAndBrand.get( # WHPriceAndBrand.price_range == '900元以上', # WHPriceAndBrand.brand == commodity.brand # ) # wh_pab.total += commodity.total # wh_pab.save() # except WHPriceAndBrand.DoesNotExist: # WHPriceAndBrand.create( # price_range='900元以上', # brand=commodity.brand, # total=commodity.total # ) range_list = ['200元以下', '200-900元', '900元以上'] for pr in range_list: total_count = WHPriceAndBrand.select(fn.SUM(WHPriceAndBrand.total).alias('tc')) \ .where(WHPriceAndBrand.price_range == pr).dicts()[0]['tc'] print(total_count) for wh_pab in WHPriceAndBrand.select().where( WHPriceAndBrand.price_range == pr): wh_pab.percentage = calculate_percentage(total_count, wh_pab.total) wh_pab.save()
def get_phone_price_and_brand(): # for commodity in Commodity.select().where(Commodity.os.in_(['Android', 'iOS'])): # if 0 < commodity.price < 2000: # try: # ppab = PhonePriceAndBrand.get( # PhonePriceAndBrand.price_range == '2000元以下', # PhonePriceAndBrand.brand == commodity.brand # ) # ppab.total += commodity.total # ppab.save() # except PhonePriceAndBrand.DoesNotExist: # PhonePriceAndBrand.create( # price_range='2000元以下', # brand=commodity.brand, # total=commodity.total # ) # if 2000 <= commodity.price < 5000: # try: # ppab = PhonePriceAndBrand.get( # PhonePriceAndBrand.price_range == '2000-5000元', # PhonePriceAndBrand.brand == commodity.brand # ) # ppab.total += commodity.total # ppab.save() # except PhonePriceAndBrand.DoesNotExist: # PhonePriceAndBrand.create( # price_range='2000-5000元', # brand=commodity.brand, # total=commodity.total # ) # if commodity.price >= 5000: # try: # ppab = PhonePriceAndBrand.get( # PhonePriceAndBrand.price_range == '5000元以上', # PhonePriceAndBrand.brand == commodity.brand # ) # ppab.total += commodity.total # ppab.save() # except PhonePriceAndBrand.DoesNotExist: # PhonePriceAndBrand.create( # price_range='5000元以上', # brand=commodity.brand, # total=commodity.total # ) range_list = ['2000元以下', '2000-5000元', '5000元以上'] for pr in range_list: total_count = PhonePriceAndBrand.select(fn.SUM(PhonePriceAndBrand.total).alias('tc')) \ .where(PhonePriceAndBrand.price_range == pr).dicts()[0]['tc'] print(total_count) for ppab in PhonePriceAndBrand.select().where(PhonePriceAndBrand.price_range == pr): ppab.percentage = calculate_percentage(total_count, ppab.total) ppab.save()
def get_model_count(): # 苏宁和京东数据合并 all_color = ['黑色', '白色', '红色', '黄色', '紫色', '绿色'] all_rom = ['64GB', '128GB', '256GB'] for color in all_color: for rom in all_rom: all_ms = ModelSummary.select().where((ModelSummary.color == color) & (ModelSummary.rom == rom)) mc = ModelCount.create(color=color, rom=rom) for ms in all_ms: if ms.source == '京东': mc.total += ms.default_good + ms.star_one + ms.star_two + \ ms.star_three + ms.star_four + ms.star_five mc.cal_total += ms.star_one + ms.star_two + ms.star_three + ms.star_four + ms.star_five mc.save() if ms.source == '苏宁': mc.total += ms.total mc.cal_total += ms.total mc.save() mc.default_good += ms.default_good mc.good_count += ms.star_four + ms.star_five mc.general_count += ms.star_two + ms.star_three mc.bad_count += ms.star_one mc.star_one += ms.star_one mc.star_two += ms.star_two mc.star_three += ms.star_three mc.star_four += ms.star_four mc.star_five += ms.star_five mc.save() # 计算占比和好评率 (京东计算规则) jd_total = Total.get(Total.source == '京东') sn_total = Total.get(Total.source == '苏宁') models_total = jd_total.all_models_total + sn_total.all_models_total for mc in ModelCount.select(): mc.percentage = calculate_percentage(models_total, mc.total) mc.good_rate = calculate_percentage(mc.cal_total, mc.good_count) mc.save()
def get_comment_date_count(): for comment in Comment.select(): year_month = str(comment.create_time)[0:7] try: cdc = CommentDateCount.get_by_id(year_month) cdc.total += 1 cdc.save() except CommentDateCount.DoesNotExist: CommentDateCount.create(year_month=year_month, total=1) comments_total = Comment.select().count() for cdc in CommentDateCount.select(): cdc.percentage = calculate_percentage(comments_total, cdc.total) cdc.save()
def get_phone_os(): # for commodity in Commodity.select(): # try: # phone_os = PhoneOS.get(PhoneOS.os == commodity.os) # phone_os.total += commodity.total # phone_os.save() # except PhoneOS.DoesNotExist: # PhoneOS.create( # os=commodity.os, # total=commodity.total # ) total_count = PhoneOS.select(fn.SUM(PhoneOS.total).alias('tc')).dicts()[0]['tc'] for phone_os in PhoneOS.select(): phone_os.percentage = calculate_percentage(total_count, phone_os.total) phone_os.save()
def get_after_days_count(): for comment in Comment.select().where(Comment.after_days.is_null(False)): after_days = comment.after_days try: adc = AfterDaysCount.get_by_id(after_days) adc.total += 1 adc.save() except AfterDaysCount.DoesNotExist: AfterDaysCount.create(after_days=after_days, total=1) after_total = Comment.select().where( Comment.after_days.is_null(False)).count() for adc in AfterDaysCount.select(): adc.percentage = calculate_percentage(after_total, adc.total) adc.save()
def get_order_date_count(): for comment in Comment.select().where(Comment.order_time.is_null(False)): year_month = str(comment.order_time)[0:7] try: odc = OrderDateCount.get_by_id(year_month) odc.total += 1 odc.save() except OrderDateCount.DoesNotExist: OrderDateCount.create(year_month=year_month, total=1) order_total = Comment.select().where( Comment.order_time.is_null(False)).count() for odc in OrderDateCount.select(): odc.percentage = calculate_percentage(order_total, odc.total) odc.save()
def get_order_days_count(): for comment in Comment.select().where(Comment.order_days.is_null(False)): order_days = comment.order_days try: odc = OrderDaysCount.get_by_id(order_days) odc.total += 1 odc.save() except OrderDaysCount.DoesNotExist: OrderDaysCount.create(order_days=order_days, total=1) order_total = Comment.select().where( Comment.order_days.is_null(False)).count() for odc in OrderDaysCount.select(): odc.percentage = calculate_percentage(order_total, odc.total) odc.save()
def get_feature_phone_soc_percentage(): # for commodity in Commodity.select().where(Commodity.os == '功能机'): # try: # fpsp = FeaturePhoneSoCPer.get(FeaturePhoneSoCPer.soc_mfrs == commodity.soc_mfrs) # fpsp.total += commodity.total # fpsp.save() # except FeaturePhoneSoCPer.DoesNotExist: # FeaturePhoneSoCPer.create( # soc_mfrs=commodity.soc_mfrs, # total=commodity.total # ) total_count = FeaturePhoneSoCPer.select(fn.SUM(FeaturePhoneSoCPer.total).alias('tc')).dicts()[0]['tc'] print(total_count) for fpsp in FeaturePhoneSoCPer.select(): fpsp.percentage = calculate_percentage(total_count, fpsp.total) fpsp.save()
def get_soc_mfrs(): for soc in SoC.select(): try: soc_mfrs = SoCMfrs.get_by_id(soc.soc_mfrs) soc_mfrs.total += soc.total soc_mfrs.save() except SoCMfrs.DoesNotExist: SoCMfrs.create( soc_mfrs=soc.soc_mfrs, total=soc.total ) total_count = SoCMfrs.select(fn.SUM(SoCMfrs.total).alias('tc')).dicts()[0]['tc'] print(total_count) for soc_mfrs in SoCMfrs.select(): soc_mfrs.percentage = calculate_percentage(total_count, soc_mfrs.total) soc_mfrs.save()
def get_feature_phone_percentage(): # for commodity in Commodity.select().where(Commodity.os == '功能机'): # try: # fpp = FeaturePhonePercentage.get(FeaturePhonePercentage.brand == commodity.brand) # fpp.total += commodity.total # fpp.save() # except FeaturePhonePercentage.DoesNotExist: # FeaturePhonePercentage.create( # brand=commodity.brand, # total=commodity.total # ) total_count = FeaturePhonePercentage.select(fn.SUM(FeaturePhonePercentage.total).alias('tc')).dicts()[0]['tc'] print(total_count) for fpp in FeaturePhonePercentage.select(): fpp.percentage = calculate_percentage(total_count, fpp.total) fpp.save()
def get_phone_brand(): # for commodity in Commodity.select(): # try: # phone_brand = PhoneBrand.get(PhoneBrand.brand == commodity.brand) # phone_brand.total += commodity.total # phone_brand.save() # except PhoneBrand.DoesNotExist: # PhoneBrand.create( # brand=commodity.brand, # total=commodity.total # ) total_count = PhoneBrand.select(fn.SUM(PhoneBrand.total).alias('tc')).dicts()[0]['tc'] print(total_count) for phone_brand in PhoneBrand.select(): phone_brand.percentage = calculate_percentage(total_count, phone_brand.total) phone_brand.save()
def get_wh_brand(): # for commodity in Commodity.select(): # try: # wh_brand = WHBrand.get(WHBrand.brand == commodity.brand) # wh_brand.total += commodity.total # wh_brand.save() # except WHBrand.DoesNotExist: # WHBrand.create( # brand=commodity.brand, # total=commodity.total # ) total_count = WHBrand.select(fn.SUM( WHBrand.total).alias('tc')).dicts()[0]['tc'] print(total_count) for wh_brand in WHBrand.select(): wh_brand.percentage = calculate_percentage(total_count, wh_brand.total) wh_brand.save()
def get_sn_total(): sn_good_count = 0 sn_general_count = 0 sn_bad_count = 0 sn_total = 0 sn_default_good = 0 sn_star_one = 0 sn_star_two = 0 sn_star_three = 0 sn_star_four = 0 sn_star_five = 0 for sn_comments_summary in CommentSummary.select().where( CommentSummary.source == '苏宁'): sn_good_count += sn_comments_summary.star_four + sn_comments_summary.star_five sn_general_count += sn_comments_summary.star_two + sn_comments_summary.star_three sn_bad_count += sn_comments_summary.star_one sn_total += sn_comments_summary.total sn_default_good += sn_comments_summary.default_good sn_star_one += sn_comments_summary.star_one sn_star_two += sn_comments_summary.star_two sn_star_three += sn_comments_summary.star_three sn_star_four += sn_comments_summary.star_four sn_star_five += sn_comments_summary.star_five sn_cal_total = sn_good_count + sn_general_count + sn_bad_count sn_good_rate = calculate_percentage(sn_cal_total, sn_good_count) sn_models_total = 0 for ms in ModelSummary.select().where(ModelSummary.source == '苏宁'): sn_models_total += ms.total Total.create(source='苏宁', total=sn_total, all_models_total=sn_models_total, good_rate=sn_good_rate, default_good=sn_default_good, good_count=sn_good_count, general_count=sn_general_count, bad_count=sn_bad_count, star_one=sn_star_one, star_two=sn_star_two, star_three=sn_star_three, star_four=sn_star_four, star_five=sn_star_five)
def get_soc_and_brand(): # for commodity in Commodity.select().where(Commodity.soc_mfrs != '未知'): # try: # sab = SoCAndBrand.get( # SoCAndBrand.soc_mfrs == commodity.soc_mfrs, # SoCAndBrand.brand == commodity.brand # ) # sab.total += commodity.total # sab.save() # except SoCAndBrand.DoesNotExist: # SoCAndBrand.create( # soc_mfrs=commodity.soc_mfrs, # brand=commodity.brand, # total=commodity.total # ) soc_mfrs_list = ['三星猎户座', '海思麒麟', '紫光展锐', '联发科', '高通骁龙'] for soc_mfrs in soc_mfrs_list: total_count = SoCAndBrand.select(fn.SUM(SoCAndBrand.total).alias('tc')) \ .where(SoCAndBrand.soc_mfrs == soc_mfrs).dicts()[0]['tc'] print(total_count) for sab in SoCAndBrand.select().where(SoCAndBrand.soc_mfrs == soc_mfrs): sab.percentage = calculate_percentage(total_count, sab.total) sab.save()
def get_phone_price_and_sales(): # PhonePriceAndSales.create( # type='功能机', # price_range='500-1000元', # total=0 # ) # for commodity in Commodity.select().where(Commodity.os.in_(['Android', 'iOS'])): # if 0 < commodity.price < 500: # ppas = PhonePriceAndSales.get_by_id(1) # ppas.total += commodity.total # ppas.save() # if 500 <= commodity.price < 1000: # ppas = PhonePriceAndSales.get_by_id(2) # ppas.total += commodity.total # ppas.save() # if 1000 <= commodity.price < 2000: # ppas = PhonePriceAndSales.get_by_id(3) # ppas.total += commodity.total # ppas.save() # if 2000 <= commodity.price < 3000: # ppas = PhonePriceAndSales.get_by_id(4) # ppas.total += commodity.total # ppas.save() # if 3000 <= commodity.price < 4000: # ppas = PhonePriceAndSales.get_by_id(5) # ppas.total += commodity.total # ppas.save() # if 4000 <= commodity.price < 5000: # ppas = PhonePriceAndSales.get_by_id(6) # ppas.total += commodity.total # ppas.save() # if 5000 <= commodity.price < 8000: # ppas = PhonePriceAndSales.get_by_id(7) # ppas.total += commodity.total # ppas.save() # if commodity.price >= 8000: # ppas = PhonePriceAndSales.get_by_id(8) # ppas.total += commodity.total # ppas.save() # total_count = PhonePriceAndSales.select(fn.SUM(PhonePriceAndSales.total).alias('tc')) \ # .where(PhonePriceAndSales.type == '智能手机').dicts()[0]['tc'] # print(total_count) # for ppas in PhonePriceAndSales.select().where(PhonePriceAndSales.type == '智能手机'): # ppas.percentage = calculate_percentage(total_count, ppas.total) # ppas.save() for commodity in Commodity.select().where(Commodity.os.in_(['功能机'])): if 0 < commodity.price < 100: ppas = PhonePriceAndSales.get_by_id(9) ppas.total += commodity.total ppas.save() if 100 <= commodity.price < 200: ppas = PhonePriceAndSales.get_by_id(10) ppas.total += commodity.total ppas.save() if 200 <= commodity.price < 300: ppas = PhonePriceAndSales.get_by_id(11) ppas.total += commodity.total ppas.save() if 300 <= commodity.price < 400: ppas = PhonePriceAndSales.get_by_id(12) ppas.total += commodity.total ppas.save() if 400 <= commodity.price < 500: ppas = PhonePriceAndSales.get_by_id(13) ppas.total += commodity.total ppas.save() if 500 <= commodity.price < 1000: ppas = PhonePriceAndSales.get_by_id(14) ppas.total += commodity.total ppas.save() total_count = PhonePriceAndSales.select(fn.SUM(PhonePriceAndSales.total).alias('tc')) \ .where(PhonePriceAndSales.type == '功能机').dicts()[0]['tc'] print(total_count) for ppas in PhonePriceAndSales.select().where(PhonePriceAndSales.type == '功能机'): ppas.percentage = calculate_percentage(total_count, ppas.total) ppas.save()