public_analysis.py 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. from model.DateUtils import DateUtils
  2. from model.DataBaseUtils import *
  3. from model.log import logger
  4. from model.CommonUtils import *
  5. from data_manage.operate import get_pitcher, get_user_name_by_id
  6. from model.UserAuthUtils import super_auth
  7. from model import UserAuthUtils
  8. du = DateUtils()
  9. log = logger()
  10. def book_rank(start, end, book, stage, type, page, page_size, order_by, order):
  11. db = MysqlUtils()
  12. op1 = f" and book='{book}'" if book else ''
  13. op2 = f" and dt>='{start}' " if start else ''
  14. op3 = f" and dt<='{end}' " if end else ''
  15. op4 = f" order by {order_by} {order}" if order_by and order else ''
  16. op5 = f" and stage='{stage}'" if stage else ''
  17. op6 = f" and type='{type}'" if type else ''
  18. sql = f"""select
  19. concat(DATE_FORMAT(min(dt),'%Y/%m/%d') ,'~',DATE_FORMAT(max(dt),'%Y/%m/%d')) date,
  20. pitcher,book,stage,type,channel,
  21. sum(cost) cost,
  22. sum(view_count) view_count,
  23. sum(click_count) click_count,
  24. round(sum(click_count)/sum(view_count),4) click_rate,
  25. round(sum(cost)*1000/sum(view_count),2) thousand_view_cost,
  26. sum(reg_order_user) reg_user,
  27. sum(reg_order_count) reg_count,
  28. sum(first_order_amount) first_amount,
  29. sum(reg_order_amount) reg_amount,
  30. round(sum(first_order_amount)/sum(cost),4) first_roi,
  31. round(sum(reg_order_amount)/sum(cost),4) roi
  32. from dw_channel where 1=1 {op1} {op2} {op3} {op5} {op6} GROUP BY channel,book,pitcher,stage,type
  33. {op4}"""
  34. print(sql)
  35. return getLimitData(db.dm, sql, page, page_size)
  36. def book_trend(start, end, book, book_type, platform, page, page_size, order_by, order):
  37. op1 = f" and book='{book}'" if book else ''
  38. op2 = f" and type='{book_type}' " if book_type else ''
  39. # op3 = f" and platform='{platform}' " if platform else ''
  40. op4 = f" and dt>='{start}' " if start else ''
  41. op5 = f" and dt<='{end}' " if end else ''
  42. op6 = f" order by {order_by} {order}" if order_by and order else ''
  43. db = MysqlUtils()
  44. sql = f"""
  45. select dt,book,type,cost,first_order_amount,reg_order_amount,
  46. round(first_order_amount/cost,4) first_roi,
  47. a7,a30,
  48. round(a7/cost,4) roi7,
  49. round(a30/cost,4) roi30,
  50. round(reg_order_amount/cost,4) roi,
  51. order_amount,
  52. reg_order_amount-cost profit,
  53. round(click_count/view_count,4) click_rate,
  54. round(follow_user/click_count,4) follow_rate,
  55. round(first_order_user/click_count,4) first_order_rate,
  56. view_count,
  57. click_count,
  58. follow_user,
  59. reg_order_user,
  60. first_order_user,
  61. round(cost/follow_user,2) follow_cost,
  62. round(cost/reg_order_user,2) order_cost,
  63. round(cost/first_order_user,2) first_order_cost,
  64. concat(a1,',',0,',',a1/cost,',',1) d1,
  65. concat(a2-a1,',',(a2-a1)/cost,',',a2/cost,',',a2/a1) d2,
  66. concat(a3-a2,',',(a3-a2)/cost,',',a3/cost,',',a3/a1) d3,
  67. concat(a4-a3,',',(a4-a3)/cost,',',a4/cost,',',a4/a1) d4,
  68. concat(a5-a4,',',(a5-a4)/cost,',',a5/cost,',',a5/a1) d5,
  69. concat(a6-a5,',',(a6-a5)/cost,',',a6/cost,',',a6/a1) d6,
  70. concat(a7-a6,',',(a7-a6)/cost,',',a7/cost,',',a7/a1) d7,
  71. concat(a8-a7,',',(a8-a7)/cost,',',a8/cost,',',a8/a1) d8,
  72. concat(a9-a8,',',(a9-a8)/cost,',',a9/cost,',',a9/a1) d9,
  73. concat(a10-a9,',',(a10-a9)/cost,',',a10/cost,',',a10/a1) d10,
  74. concat(a11-a10,',',(a11-a10)/cost,',',a11/cost,',',a11/a1) d11,
  75. concat(a12-a11,',',(a12-a11)/cost,',',a12/cost,',',a12/a1) d12,
  76. concat(a13-a12,',',(a13-a12)/cost,',',a13/cost,',',a13/a1) d13,
  77. concat(a14-a13,',',(a14-a13)/cost,',',a14/cost,',',a14/a1) d14,
  78. concat(a15-a14,',',(a15-a14)/cost,',',a15/cost,',',a15/a1) d15,
  79. concat(a16-a15,',',(a16-a15)/cost,',',a16/cost,',',a16/a1) d16,
  80. concat(a17-a16,',',(a17-a16)/cost,',',a17/cost,',',a17/a1) d17,
  81. concat(a18-a17,',',(a18-a17)/cost,',',a18/cost,',',a18/a1) d18,
  82. concat(a19-a18,',',(a19-a18)/cost,',',a19/cost,',',a19/a1) d19,
  83. concat(a20-a19,',',(a20-a19)/cost,',',a20/cost,',',a20/a1) d20,
  84. concat(a21-a20,',',(a21-a20)/cost,',',a21/cost,',',a21/a1) d21,
  85. concat(a22-a21,',',(a22-a21)/cost,',',a22/cost,',',a22/a1) d22,
  86. concat(a23-a22,',',(a23-a22)/cost,',',a23/cost,',',a23/a1) d23,
  87. concat(a24-a23,',',(a24-a23)/cost,',',a24/cost,',',a24/a1) d24,
  88. concat(a25-a24,',',(a25-a24)/cost,',',a25/cost,',',a25/a1) d25,
  89. concat(a26-a25,',',(a26-a25)/cost,',',a26/cost,',',a26/a1) d26,
  90. concat(a27-a26,',',(a27-a26)/cost,',',a27/cost,',',a27/a1) d27,
  91. concat(a28-a27,',',(a28-a27)/cost,',',a28/cost,',',a28/a1) d28,
  92. concat(a29-a28,',',(a29-a28)/cost,',',a29/cost,',',a29/a1) d29,
  93. concat(a30-a29,',',(a30-a29)/cost,',',a30/cost,',',a30/a1) d30,
  94. concat(a31-a30,',',(a31-a30)/cost,',',a31/cost,',',a31/a1) d31,
  95. concat(a32-a31,',',(a32-a31)/cost,',',a32/cost,',',a32/a1) d32,
  96. concat(a33-a32,',',(a33-a32)/cost,',',a33/cost,',',a33/a1) d33,
  97. concat(a34-a33,',',(a34-a33)/cost,',',a34/cost,',',a34/a1) d34,
  98. concat(a35-a34,',',(a35-a34)/cost,',',a35/cost,',',a35/a1) d35,
  99. concat(a36-a35,',',(a36-a35)/cost,',',a36/cost,',',a36/a1) d36,
  100. concat(a37-a36,',',(a37-a36)/cost,',',a37/cost,',',a37/a1) d37,
  101. concat(a38-a37,',',(a38-a37)/cost,',',a38/cost,',',a38/a1) d38,
  102. concat(a39-a38,',',(a39-a38)/cost,',',a39/cost,',',a39/a1) d39,
  103. concat(a40-a39,',',(a40-a39)/cost,',',a40/cost,',',a40/a1) d40,
  104. concat(a41-a40,',',(a41-a40)/cost,',',a41/cost,',',a41/a1) d41,
  105. concat(a42-a41,',',(a42-a41)/cost,',',a42/cost,',',a42/a1) d42,
  106. concat(a43-a42,',',(a43-a42)/cost,',',a43/cost,',',a43/a1) d43,
  107. concat(a44-a43,',',(a44-a43)/cost,',',a44/cost,',',a44/a1) d44,
  108. concat(a45-a44,',',(a45-a44)/cost,',',a45/cost,',',a45/a1) d45,
  109. concat(a46-a45,',',(a46-a45)/cost,',',a46/cost,',',a46/a1) d46,
  110. concat(a47-a46,',',(a47-a46)/cost,',',a47/cost,',',a47/a1) d47,
  111. concat(a48-a47,',',(a48-a47)/cost,',',a48/cost,',',a48/a1) d48,
  112. concat(a49-a48,',',(a49-a48)/cost,',',a49/cost,',',a49/a1) d49,
  113. concat(a50-a49,',',(a50-a49)/cost,',',a50/cost,',',a50/a1) d50,
  114. concat(a51-a50,',',(a51-a50)/cost,',',a51/cost,',',a51/a1) d51,
  115. concat(a52-a51,',',(a52-a51)/cost,',',a52/cost,',',a52/a1) d52,
  116. concat(a53-a52,',',(a53-a52)/cost,',',a53/cost,',',a53/a1) d53,
  117. concat(a54-a53,',',(a54-a53)/cost,',',a54/cost,',',a54/a1) d54,
  118. concat(a55-a54,',',(a55-a54)/cost,',',a55/cost,',',a55/a1) d55,
  119. concat(a56-a55,',',(a56-a55)/cost,',',a56/cost,',',a56/a1) d56,
  120. concat(a57-a56,',',(a57-a56)/cost,',',a57/cost,',',a57/a1) d57,
  121. concat(a58-a57,',',(a58-a57)/cost,',',a58/cost,',',a58/a1) d58,
  122. concat(a59-a58,',',(a59-a58)/cost,',',a59/cost,',',a59/a1) d59,
  123. concat(a60-a59,',',(a60-a59)/cost,',',a60/cost,',',a60/a1) d60,
  124. concat(m3-a60,',',(m3-a60)/cost,',',m3/cost,',',m3/a1) m3,
  125. concat(m4-m3,',',(m4-m3)/cost,',',m4/cost,',',m4/a1) m4,
  126. concat(m5-m4,',',(m5-m4)/cost,',',m5/cost,',',m5/a1) m5,
  127. a1 as da1,a2 as da2,a3 as da3,a4 as da4,a5 as da5,a6 as da6,a7 as da7,
  128. a8 as da8,a9 as da9,a10 as da10,a11 as da11,a12 as da12,a13 as da13,
  129. a14 as da14,a15 as da15,a16 as da16,a17 as da17,a18 as da18,a19 as da19,
  130. a20 as da20,a21 as da21,a22 as da22,a23 as da23,a24 as da24,a25 as da25,
  131. a26 as da26,a27 as da27,a28 as da28,a29 as da29,a30 as da30,a31 as da31,
  132. a32 as da32,a33 as da33,a34 as da34,a35 as da35,a36 as da36,a37 as da37,
  133. a38 as da38,a39 as da39,a40 as da40,a41 as da41,a42 as da42,a43 as da43,
  134. a44 as da44,a45 as da45,a46 as da46,a47 as da47,a48 as da48,a49 as da49,
  135. a50 as da50,a51 as da51,a52 as da52,a53 as da53,a54 as da54,a55 as da55,
  136. a56 as da56,a57 as da57,a58 as da58,a59 as da59,a60 as da60,
  137. m3 as dm3,m4 as dm4,m5 as dm5
  138. from book_trend where 1=1 {op1} {op2} {op4} {op5} {op6}
  139. """
  140. sum_sql = f"""select concat(date_format(min(dt),'%Y/%m/%d'),'~',date_format(max(dt),'%Y/%m/%d')) dt,
  141. sum(cost) cost,
  142. sum(first_order_amount) first_order_amount,
  143. sum(reg_order_amount) reg_order_amount,
  144. round(sum(first_order_amount)/sum(cost),4) first_roi,
  145. round(sum(a7)/sum(cost),4) roi7,
  146. round(sum(a30)/sum(cost),4) roi30,
  147. round(sum(reg_order_amount)/sum(cost),4) roi,
  148. sum(order_amount) order_amount,
  149. sum(reg_order_amount-cost) profit,
  150. round(sum(click_count)/sum(view_count),4) click_rate,
  151. round(sum(follow_user)/sum(click_count),4) follow_rate,
  152. round(sum(first_order_user)/sum(click_count),4) first_order_rate,
  153. sum(view_count) view_count,
  154. sum(click_count) click_count,
  155. sum(follow_user) follow_user,
  156. sum(reg_order_user) reg_order_user,
  157. sum(first_order_user) first_order_user,
  158. round(sum(cost)/sum(follow_user),2) follow_cost,
  159. round(sum(cost)/sum(reg_order_user)) order_cost,
  160. round(sum(cost)/sum(first_order_user)) first_order_cost,
  161. concat(sum(da1),',',sum(da1)/sum(cost),',', 0,',',1) d1,
  162. concat(sum(da2)-sum(if (da2,da1,0)),',',sum(da2)/sum(if (da2,cost,0)),',', (sum(da2)-sum(if (da2,da1,0)))/sum(if (da2,cost,0)),',',if(sum(if (da2,da1,0))=0,1,sum(da2)/sum(if (da2,da1,0))) ) d2,
  163. concat(sum(da3)-sum(if (da3,da2,0)),',',sum(da3)/sum(if (da3,cost,0)),',', (sum(da3)-sum(if (da3,da2,0)))/sum(if (da3,cost,0)),',',if(sum(if (da3,da1,0))=0,1,sum(da3)/sum(if (da3,da1,0))) ) d3,
  164. concat(sum(da4)-sum(if (da4,da3,0)),',',sum(da4)/sum(if (da4,cost,0)),',', (sum(da4)-sum(if (da4,da3,0)))/sum(if (da4,cost,0)),',',if(sum(if (da4,da1,0))=0,1,sum(da4)/sum(if (da4,da1,0))) ) d4,
  165. concat(sum(da5)-sum(if (da5,da4,0)),',',sum(da5)/sum(if (da5,cost,0)),',', (sum(da5)-sum(if (da5,da4,0)))/sum(if (da5,cost,0)),',',if(sum(if (da5,da1,0))=0,1,sum(da5)/sum(if (da5,da1,0))) ) d5,
  166. concat(sum(da6)-sum(if (da6,da5,0)),',',sum(da6)/sum(if (da6,cost,0)),',', (sum(da6)-sum(if (da6,da5,0)))/sum(if (da6,cost,0)),',',if(sum(if (da6,da1,0))=0,1,sum(da6)/sum(if (da6,da1,0))) ) d6,
  167. concat(sum(da7)-sum(if (da7,da6,0)),',',sum(da7)/sum(if (da7,cost,0)),',', (sum(da7)-sum(if (da7,da6,0)))/sum(if (da7,cost,0)),',',if(sum(if (da7,da1,0))=0,1,sum(da7)/sum(if (da7,da1,0))) ) d7,
  168. concat(sum(da8)-sum(if (da8,da7,0)),',',sum(da8)/sum(if (da8,cost,0)),',', (sum(da8)-sum(if (da8,da7,0)))/sum(if (da8,cost,0)),',',if(sum(if (da8,da1,0))=0,1,sum(da8)/sum(if (da8,da1,0))) ) d8,
  169. concat(sum(da9)-sum(if (da9,da8,0)),',',sum(da9)/sum(if (da9,cost,0)),',', (sum(da9)-sum(if (da9,da8,0)))/sum(if (da9,cost,0)),',',if(sum(if (da9,da1,0))=0,1,sum(da9)/sum(if (da9,da1,0))) ) d9,
  170. concat(sum(da10)-sum(if (da10,da9,0)),',',sum(da10)/sum(if (da10,cost,0)),',', (sum(da10)-sum(if (da10,da9,0)))/sum(if (da10,cost,0)),',',if(sum(if (da10,da1,0))=0,1,sum(da10)/sum(if (da10,da1,0))) ) d10,
  171. concat(sum(da11)-sum(if (da11,da10,0)),',',sum(da11)/sum(if (da11,cost,0)),',', (sum(da11)-sum(if (da11,da10,0)))/sum(if (da11,cost,0)),',',if(sum(if (da11,da1,0))=0,1,sum(da11)/sum(if (da11,da1,0))) ) d11,
  172. concat(sum(da12)-sum(if (da12,da11,0)),',',sum(da12)/sum(if (da12,cost,0)),',', (sum(da12)-sum(if (da12,da11,0)))/sum(if (da12,cost,0)),',',if(sum(if (da12,da1,0))=0,1,sum(da12)/sum(if (da12,da1,0))) ) d12,
  173. concat(sum(da13)-sum(if (da13,da12,0)),',',sum(da13)/sum(if (da13,cost,0)),',', (sum(da13)-sum(if (da13,da12,0)))/sum(if (da13,cost,0)),',',if(sum(if (da13,da1,0))=0,1,sum(da13)/sum(if (da13,da1,0))) ) d13,
  174. concat(sum(da14)-sum(if (da14,da13,0)),',',sum(da14)/sum(if (da14,cost,0)),',', (sum(da14)-sum(if (da14,da13,0)))/sum(if (da14,cost,0)),',',if(sum(if (da14,da1,0))=0,1,sum(da14)/sum(if (da14,da1,0))) ) d14,
  175. concat(sum(da15)-sum(if (da15,da14,0)),',',sum(da15)/sum(if (da15,cost,0)),',', (sum(da15)-sum(if (da15,da14,0)))/sum(if (da15,cost,0)),',',if(sum(if (da15,da1,0))=0,1,sum(da15)/sum(if (da15,da1,0))) ) d15,
  176. concat(sum(da16)-sum(if (da16,da15,0)),',',sum(da16)/sum(if (da16,cost,0)),',', (sum(da16)-sum(if (da16,da15,0)))/sum(if (da16,cost,0)),',',if(sum(if (da16,da1,0))=0,1,sum(da16)/sum(if (da16,da1,0))) ) d16,
  177. concat(sum(da17)-sum(if (da17,da16,0)),',',sum(da17)/sum(if (da17,cost,0)),',', (sum(da17)-sum(if (da17,da16,0)))/sum(if (da17,cost,0)),',',if(sum(if (da17,da1,0))=0,1,sum(da17)/sum(if (da17,da1,0))) ) d17,
  178. concat(sum(da18)-sum(if (da18,da17,0)),',',sum(da18)/sum(if (da18,cost,0)),',', (sum(da18)-sum(if (da18,da17,0)))/sum(if (da18,cost,0)),',',if(sum(if (da18,da1,0))=0,1,sum(da18)/sum(if (da18,da1,0))) ) d18,
  179. concat(sum(da19)-sum(if (da19,da18,0)),',',sum(da19)/sum(if (da19,cost,0)),',', (sum(da19)-sum(if (da19,da18,0)))/sum(if (da19,cost,0)),',',if(sum(if (da19,da1,0))=0,1,sum(da19)/sum(if (da19,da1,0))) ) d19,
  180. concat(sum(da20)-sum(if (da20,da19,0)),',',sum(da20)/sum(if (da20,cost,0)),',', (sum(da20)-sum(if (da20,da19,0)))/sum(if (da20,cost,0)),',',if(sum(if (da20,da1,0))=0,1,sum(da20)/sum(if (da20,da1,0))) ) d20,
  181. concat(sum(da21)-sum(if (da21,da20,0)),',',sum(da21)/sum(if (da21,cost,0)),',', (sum(da21)-sum(if (da21,da20,0)))/sum(if (da21,cost,0)),',',if(sum(if (da21,da1,0))=0,1,sum(da21)/sum(if (da21,da1,0))) ) d21,
  182. concat(sum(da22)-sum(if (da22,da21,0)),',',sum(da22)/sum(if (da22,cost,0)),',', (sum(da22)-sum(if (da22,da21,0)))/sum(if (da22,cost,0)),',',if(sum(if (da22,da1,0))=0,1,sum(da22)/sum(if (da22,da1,0))) ) d22,
  183. concat(sum(da23)-sum(if (da23,da22,0)),',',sum(da23)/sum(if (da23,cost,0)),',', (sum(da23)-sum(if (da23,da22,0)))/sum(if (da23,cost,0)),',',if(sum(if (da23,da1,0))=0,1,sum(da23)/sum(if (da23,da1,0))) ) d23,
  184. concat(sum(da24)-sum(if (da24,da23,0)),',',sum(da24)/sum(if (da24,cost,0)),',', (sum(da24)-sum(if (da24,da23,0)))/sum(if (da24,cost,0)),',',if(sum(if (da24,da1,0))=0,1,sum(da24)/sum(if (da24,da1,0))) ) d24,
  185. concat(sum(da25)-sum(if (da25,da24,0)),',',sum(da25)/sum(if (da25,cost,0)),',', (sum(da25)-sum(if (da25,da24,0)))/sum(if (da25,cost,0)),',',if(sum(if (da25,da1,0))=0,1,sum(da25)/sum(if (da25,da1,0))) ) d25,
  186. concat(sum(da26)-sum(if (da26,da25,0)),',',sum(da26)/sum(if (da26,cost,0)),',', (sum(da26)-sum(if (da26,da25,0)))/sum(if (da26,cost,0)),',',if(sum(if (da26,da1,0))=0,1,sum(da26)/sum(if (da26,da1,0))) ) d26,
  187. concat(sum(da27)-sum(if (da27,da26,0)),',',sum(da27)/sum(if (da27,cost,0)),',', (sum(da27)-sum(if (da27,da26,0)))/sum(if (da27,cost,0)),',',if(sum(if (da27,da1,0))=0,1,sum(da27)/sum(if (da27,da1,0))) ) d27,
  188. concat(sum(da28)-sum(if (da28,da27,0)),',',sum(da28)/sum(if (da28,cost,0)),',', (sum(da28)-sum(if (da28,da27,0)))/sum(if (da28,cost,0)),',',if(sum(if (da28,da1,0))=0,1,sum(da28)/sum(if (da28,da1,0))) ) d28,
  189. concat(sum(da29)-sum(if (da29,da28,0)),',',sum(da29)/sum(if (da29,cost,0)),',', (sum(da29)-sum(if (da29,da28,0)))/sum(if (da29,cost,0)),',',if(sum(if (da29,da1,0))=0,1,sum(da29)/sum(if (da29,da1,0))) ) d29,
  190. concat(sum(da30)-sum(if (da30,da29,0)),',',sum(da30)/sum(if (da30,cost,0)),',', (sum(da30)-sum(if (da30,da29,0)))/sum(if (da30,cost,0)),',',if(sum(if (da30,da1,0))=0,1,sum(da30)/sum(if (da30,da1,0))) ) d30,
  191. concat(sum(da31)-sum(if (da31,da30,0)),',',sum(da31)/sum(if (da31,cost,0)),',', (sum(da31)-sum(if (da31,da30,0)))/sum(if (da31,cost,0)),',',if(sum(if (da31,da1,0))=0,1,sum(da31)/sum(if (da31,da1,0))) ) d31,
  192. concat(sum(da32)-sum(if (da32,da31,0)),',',sum(da32)/sum(if (da32,cost,0)),',', (sum(da32)-sum(if (da32,da31,0)))/sum(if (da32,cost,0)),',',if(sum(if (da32,da1,0))=0,1,sum(da32)/sum(if (da32,da1,0))) ) d32,
  193. concat(sum(da33)-sum(if (da33,da32,0)),',',sum(da33)/sum(if (da33,cost,0)),',', (sum(da33)-sum(if (da33,da32,0)))/sum(if (da33,cost,0)),',',if(sum(if (da33,da1,0))=0,1,sum(da33)/sum(if (da33,da1,0))) ) d33,
  194. concat(sum(da34)-sum(if (da34,da33,0)),',',sum(da34)/sum(if (da34,cost,0)),',', (sum(da34)-sum(if (da34,da33,0)))/sum(if (da34,cost,0)),',',if(sum(if (da34,da1,0))=0,1,sum(da34)/sum(if (da34,da1,0))) ) d34,
  195. concat(sum(da35)-sum(if (da35,da34,0)),',',sum(da35)/sum(if (da35,cost,0)),',', (sum(da35)-sum(if (da35,da34,0)))/sum(if (da35,cost,0)),',',if(sum(if (da35,da1,0))=0,1,sum(da35)/sum(if (da35,da1,0))) ) d35,
  196. concat(sum(da36)-sum(if (da36,da35,0)),',',sum(da36)/sum(if (da36,cost,0)),',', (sum(da36)-sum(if (da36,da35,0)))/sum(if (da36,cost,0)),',',if(sum(if (da36,da1,0))=0,1,sum(da36)/sum(if (da36,da1,0))) ) d36,
  197. concat(sum(da37)-sum(if (da37,da36,0)),',',sum(da37)/sum(if (da37,cost,0)),',', (sum(da37)-sum(if (da37,da36,0)))/sum(if (da37,cost,0)),',',if(sum(if (da37,da1,0))=0,1,sum(da37)/sum(if (da37,da1,0))) ) d37,
  198. concat(sum(da38)-sum(if (da38,da37,0)),',',sum(da38)/sum(if (da38,cost,0)),',', (sum(da38)-sum(if (da38,da37,0)))/sum(if (da38,cost,0)),',',if(sum(if (da38,da1,0))=0,1,sum(da38)/sum(if (da38,da1,0))) ) d38,
  199. concat(sum(da39)-sum(if (da39,da38,0)),',',sum(da39)/sum(if (da39,cost,0)),',', (sum(da39)-sum(if (da39,da38,0)))/sum(if (da39,cost,0)),',',if(sum(if (da39,da1,0))=0,1,sum(da39)/sum(if (da39,da1,0))) ) d39,
  200. concat(sum(da40)-sum(if (da40,da39,0)),',',sum(da40)/sum(if (da40,cost,0)),',', (sum(da40)-sum(if (da40,da39,0)))/sum(if (da40,cost,0)),',',if(sum(if (da40,da1,0))=0,1,sum(da40)/sum(if (da40,da1,0))) ) d40,
  201. concat(sum(da41)-sum(if (da41,da40,0)),',',sum(da41)/sum(if (da41,cost,0)),',', (sum(da41)-sum(if (da41,da40,0)))/sum(if (da41,cost,0)),',',if(sum(if (da41,da1,0))=0,1,sum(da41)/sum(if (da41,da1,0))) ) d41,
  202. concat(sum(da42)-sum(if (da42,da41,0)),',',sum(da42)/sum(if (da42,cost,0)),',', (sum(da42)-sum(if (da42,da41,0)))/sum(if (da42,cost,0)),',',if(sum(if (da42,da1,0))=0,1,sum(da42)/sum(if (da42,da1,0))) ) d42,
  203. concat(sum(da43)-sum(if (da43,da42,0)),',',sum(da43)/sum(if (da43,cost,0)),',', (sum(da43)-sum(if (da43,da42,0)))/sum(if (da43,cost,0)),',',if(sum(if (da43,da1,0))=0,1,sum(da43)/sum(if (da43,da1,0))) ) d43,
  204. concat(sum(da44)-sum(if (da44,da43,0)),',',sum(da44)/sum(if (da44,cost,0)),',', (sum(da44)-sum(if (da44,da43,0)))/sum(if (da44,cost,0)),',',if(sum(if (da44,da1,0))=0,1,sum(da44)/sum(if (da44,da1,0))) ) d44,
  205. concat(sum(da45)-sum(if (da45,da44,0)),',',sum(da45)/sum(if (da45,cost,0)),',', (sum(da45)-sum(if (da45,da44,0)))/sum(if (da45,cost,0)),',',if(sum(if (da45,da1,0))=0,1,sum(da45)/sum(if (da45,da1,0))) ) d45,
  206. concat(sum(da46)-sum(if (da46,da45,0)),',',sum(da46)/sum(if (da46,cost,0)),',', (sum(da46)-sum(if (da46,da45,0)))/sum(if (da46,cost,0)),',',if(sum(if (da46,da1,0))=0,1,sum(da46)/sum(if (da46,da1,0))) ) d46,
  207. concat(sum(da47)-sum(if (da47,da46,0)),',',sum(da47)/sum(if (da47,cost,0)),',', (sum(da47)-sum(if (da47,da46,0)))/sum(if (da47,cost,0)),',',if(sum(if (da47,da1,0))=0,1,sum(da47)/sum(if (da47,da1,0))) ) d47,
  208. concat(sum(da48)-sum(if (da48,da47,0)),',',sum(da48)/sum(if (da48,cost,0)),',', (sum(da48)-sum(if (da48,da47,0)))/sum(if (da48,cost,0)),',',if(sum(if (da48,da1,0))=0,1,sum(da48)/sum(if (da48,da1,0))) ) d48,
  209. concat(sum(da49)-sum(if (da49,da48,0)),',',sum(da49)/sum(if (da49,cost,0)),',', (sum(da49)-sum(if (da49,da48,0)))/sum(if (da49,cost,0)),',',if(sum(if (da49,da1,0))=0,1,sum(da49)/sum(if (da49,da1,0))) ) d49,
  210. concat(sum(da50)-sum(if (da50,da49,0)),',',sum(da50)/sum(if (da50,cost,0)),',', (sum(da50)-sum(if (da50,da49,0)))/sum(if (da50,cost,0)),',',if(sum(if (da50,da1,0))=0,1,sum(da50)/sum(if (da50,da1,0))) ) d50,
  211. concat(sum(da51)-sum(if (da51,da50,0)),',',sum(da51)/sum(if (da51,cost,0)),',', (sum(da51)-sum(if (da51,da50,0)))/sum(if (da51,cost,0)),',',if(sum(if (da51,da1,0))=0,1,sum(da51)/sum(if (da51,da1,0))) ) d51,
  212. concat(sum(da52)-sum(if (da52,da51,0)),',',sum(da52)/sum(if (da52,cost,0)),',', (sum(da52)-sum(if (da52,da51,0)))/sum(if (da52,cost,0)),',',if(sum(if (da52,da1,0))=0,1,sum(da52)/sum(if (da52,da1,0))) ) d52,
  213. concat(sum(da53)-sum(if (da53,da52,0)),',',sum(da53)/sum(if (da53,cost,0)),',', (sum(da53)-sum(if (da53,da52,0)))/sum(if (da53,cost,0)),',',if(sum(if (da53,da1,0))=0,1,sum(da53)/sum(if (da53,da1,0))) ) d53,
  214. concat(sum(da54)-sum(if (da54,da53,0)),',',sum(da54)/sum(if (da54,cost,0)),',', (sum(da54)-sum(if (da54,da53,0)))/sum(if (da54,cost,0)),',',if(sum(if (da54,da1,0))=0,1,sum(da54)/sum(if (da54,da1,0))) ) d54,
  215. concat(sum(da55)-sum(if (da55,da54,0)),',',sum(da55)/sum(if (da55,cost,0)),',', (sum(da55)-sum(if (da55,da54,0)))/sum(if (da55,cost,0)),',',if(sum(if (da55,da1,0))=0,1,sum(da55)/sum(if (da55,da1,0))) ) d55,
  216. concat(sum(da56)-sum(if (da56,da55,0)),',',sum(da56)/sum(if (da56,cost,0)),',', (sum(da56)-sum(if (da56,da55,0)))/sum(if (da56,cost,0)),',',if(sum(if (da56,da1,0))=0,1,sum(da56)/sum(if (da56,da1,0))) ) d56,
  217. concat(sum(da57)-sum(if (da57,da56,0)),',',sum(da57)/sum(if (da57,cost,0)),',', (sum(da57)-sum(if (da57,da56,0)))/sum(if (da57,cost,0)),',',if(sum(if (da57,da1,0))=0,1,sum(da57)/sum(if (da57,da1,0))) ) d57,
  218. concat(sum(da58)-sum(if (da58,da57,0)),',',sum(da58)/sum(if (da58,cost,0)),',', (sum(da58)-sum(if (da58,da57,0)))/sum(if (da58,cost,0)),',',if(sum(if (da58,da1,0))=0,1,sum(da58)/sum(if (da58,da1,0))) ) d58,
  219. concat(sum(da59)-sum(if (da59,da58,0)),',',sum(da59)/sum(if (da59,cost,0)),',', (sum(da59)-sum(if (da59,da58,0)))/sum(if (da59,cost,0)),',',if(sum(if (da59,da1,0))=0,1,sum(da59)/sum(if (da59,da1,0))) ) d59,
  220. concat(sum(da60)-sum(if (da60,da59,0)),',',sum(da60)/sum(if (da60,cost,0)),',', (sum(da60)-sum(if (da60,da59,0)))/sum(if (da60,cost,0)),',',if(sum(if (da60,da1,0))=0,1,sum(da60)/sum(if (da60,da1,0))) ) d60,
  221. concat(sum(dm3)-sum(if (dm3,da60,0)) ,',' ,sum(dm3)/sum(if(dm3,cost,0)) ,',', (sum(dm3)-sum(if (dm3,da60,0)))/sum(if(dm3,cost,0)) ,',' ,if(sum(if(dm3,da1,0))=0,1,sum(dm3)/sum(if(dm3,da1,0))) ) m3,
  222. concat(sum(dm4)-sum(if (dm4,dm3,0)),',',sum(dm4)/sum(if (dm4,cost,0)),',', (sum(dm4)-sum(if (dm4,dm3,0)))/sum(if (dm4,cost,0)),',',if(sum(if (dm4,da1,0))=0,1,sum(dm4)/sum(if (dm4,da1,0))) ) m4,
  223. concat(sum(dm5)-sum(if (dm5,dm4,0)),',',sum(dm5)/sum(if (dm5,cost,0)),',', (sum(dm5)-sum(if (dm5,dm4,0)))/sum(if (dm5,cost,0)),',',if(sum(if (dm5,da1,0))=0,1,sum(dm5)/sum(if (dm5,da1,0))) ) m5
  224. from ({sql}) a
  225. """
  226. data, total, total_data = getLimitSumData(db.dm, sql, sum_sql, page, page_size)
  227. def parse(key_str):
  228. if type(key_str) is not str:
  229. key_str = key_str.decode('utf-8')
  230. li = key_str.split(',')
  231. li[0] = round(float(li[0]), 2)
  232. li[1] = round(float(li[1]), 4)
  233. li[2] = round(float(li[2]), 4)
  234. li[3] = round(float(li[3]), 2)
  235. return dict(zip(['amount', 'add', 'roi', 'mult'], li))
  236. # total已经写错更改过于
  237. def parse_total(key_str):
  238. if type(key_str) is not str:
  239. key_str = key_str.decode('utf-8')
  240. li = key_str.split(',')
  241. li[0] = round(float(li[0]), 2)
  242. li[1] = round(float(li[1]), 4)
  243. li[2] = round(float(li[2]), 4)
  244. li[3] = round(float(li[3]), 2)
  245. return dict(zip(['amount', 'roi', 'add', 'mult'], li))
  246. for k, v in total_data.items():
  247. if k in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'd11', 'd12', 'd13', 'd14', 'd15',
  248. 'd16', 'd17', 'd18',
  249. 'd19', 'd20', 'd21', 'd22', 'd23', 'd24', 'd25', 'd26', 'd27', 'd28', 'd29', 'd30', 'd31', 'd32',
  250. 'd33', 'd34', 'd35', 'd36', 'd37', 'd38',
  251. 'd39', 'd40', 'd41', 'd42', 'd43', 'd44', 'd45', 'd46', 'd47', 'd48', 'd49', 'd50', 'd51', 'd52',
  252. 'd53', 'd54', 'd55', 'd56', 'd57', 'd58', 'd59',
  253. 'd60', 'm3', 'm4', 'm5']:
  254. total_data[k] = parse_total(v) if v else {}
  255. # print(data)
  256. for i in data:
  257. for k, v in i.items():
  258. if k in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'd11', 'd12', 'd13', 'd14', 'd15',
  259. 'd16', 'd17', 'd18',
  260. 'd19', 'd20', 'd21', 'd22', 'd23', 'd24', 'd25', 'd26', 'd27', 'd28', 'd29', 'd30', 'd31', 'd32',
  261. 'd33', 'd34', 'd35', 'd36', 'd37', 'd38',
  262. 'd39', 'd40', 'd41', 'd42', 'd43', 'd44', 'd45', 'd46', 'd47', 'd48', 'd49', 'd50', 'd51', 'd52',
  263. 'd53', 'd54', 'd55', 'd56', 'd57', 'd58', 'd59',
  264. 'd60', 'm3', 'm4', 'm5']:
  265. i[k] = parse(v) if v else {}
  266. return data, total, total_data
  267. def book_overview(start, end, book, type, page, page_size, order_by, order):
  268. op1 = f" and book='{book}'" if book else ''
  269. op2 = f" and type='{type}' " if type else ''
  270. op3 = f" and dt>='{start}' " if start else ''
  271. op4 = f" and dt<='{end}' " if end else ''
  272. op5 = f" order by {order_by} {order}" if order_by and order else ''
  273. db = MysqlUtils()
  274. sql = f"""
  275. select book,type,
  276. DATE_FORMAT(min(dt),'%Y%m%d') start_date,
  277. DATE_FORMAT(max(dt),'%Y%m%d') end_date,
  278. sum(cost) cost,
  279. sum(reg_order_amount) amount,
  280. sum(first_order_amount) first_amount,
  281. round(sum(reg_order_amount)/sum(cost),4) roi,
  282. round(sum(first_order_amount)/sum(cost),4) first_roi,
  283. round(sum(a7)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 5 day),0,cost)),4) roi7,
  284. round(sum(a30)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 28 day),0,cost)),4) roi30,
  285. sum(reg_order_amount-cost) profit,
  286. round(sum(click_count)/sum(view_count),4) click_rate,
  287. round(sum(follow_user)/sum(click_count),4) follow_rate,
  288. round(sum(reg_order_user)/sum(click_count),4) order_rate,
  289. round(sum(first_order_user)/sum(click_count),4) first_order_rate,
  290. sum(view_count) view_count,
  291. sum(click_count) click_count,
  292. sum(follow_user) follow_user,
  293. sum(reg_order_user) order_user,
  294. sum(first_order_user) first_order_user,
  295. round(sum(cost)/sum(follow_user),2) follow_cost,
  296. round(sum(cost)/sum(reg_order_user),2) order_cost,
  297. round(sum(cost)/sum(first_order_user),2) first_order_cost,
  298. round(sum(reg_order_amount)/sum(reg_order_user),2) avg_amount,
  299. sum(ba1) td_amount,
  300. sum(ba2) yd_amount,
  301. sum(ba3) byd_amount,
  302. concat(sum(a1),',',0,',',sum(a1)/sum(cost),',',1) d1,
  303. concat(sum(a2-a1),',',sum(a2-a1)/sum(if(dt=CURRENT_DATE,0,cost)),',',sum(a2)/sum(if(dt=CURRENT_DATE,0,cost)),',',sum(a2)/sum(if(dt=CURRENT_DATE,0,a1))) d2,
  304. concat(sum(a3-a2),',',sum(a3-a2)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 1 day),0,cost)),',',sum(a3)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 1 day),0,cost)),',',sum(a3)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 1 day),0,a1))) d3,
  305. concat(sum(a4-a3),',',sum(a4-a3)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 2 day),0,cost)),',',sum(a4)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 2 day),0,cost)),',',sum(a4)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 2 day),0,a1))) d4,
  306. concat(sum(a5-a4),',',sum(a5-a4)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 3 day),0,cost)),',',sum(a5)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 3 day),0,cost)),',',sum(a5)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 3 day),0,a1))) d5,
  307. concat(sum(a6-a5),',',sum(a6-a5)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 4 day),0,cost)),',',sum(a6)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 4 day),0,cost)),',',sum(a6)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 4 day),0,a1))) d6,
  308. concat(sum(a7-a6),',',sum(a7-a6)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 5 day),0,cost)),',',sum(a7)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 5 day),0,cost)),',',sum(a7)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 5 day),0,a1))) d7,
  309. concat(sum(a8-a7),',',sum(a8-a7)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 6 day),0,cost)),',',sum(a8)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 6 day),0,cost)),',',sum(a8)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 6 day),0,a1))) d8,
  310. concat(sum(a9-a8),',',sum(a9-a8)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 7 day),0,cost)),',',sum(a9)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 7 day),0,cost)),',',sum(a9)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 7 day),0,a1))) d9,
  311. concat(sum(a10-a9),',',sum(a10-a9)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 8 day),0,cost)),',',sum(a10)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 8 day),0,cost)),',',sum(a10)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 8 day),0,a1))) d10,
  312. concat(sum(a11-a10),',',sum(a11-a10)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 9 day),0,cost)),',',sum(a11)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 9 day),0,cost)),',',sum(a11)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 9 day),0,a1))) d11,
  313. concat(sum(a12-a11),',',sum(a12-a11)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 10 day),0,cost)),',',sum(a12)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 10 day),0,cost)),',',sum(a12)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 10 day),0,a1))) d12,
  314. concat(sum(a13-a12),',',sum(a13-a12)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 11 day),0,cost)),',',sum(a13)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 11 day),0,cost)),',',sum(a13)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 11 day),0,a1))) d13,
  315. concat(sum(a14-a13),',',sum(a14-a13)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 12 day),0,cost)),',',sum(a14)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 12 day),0,cost)),',',sum(a14)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 12 day),0,a1))) d14,
  316. concat(sum(a15-a14),',',sum(a15-a14)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 13 day),0,cost)),',',sum(a15)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 13 day),0,cost)),',',sum(a15)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 13 day),0,a1))) d15,
  317. concat(sum(a16-a15),',',sum(a16-a15)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 14 day),0,cost)),',',sum(a16)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 14 day),0,cost)),',',sum(a16)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 14 day),0,a1))) d16,
  318. concat(sum(a17-a16),',',sum(a17-a16)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 15 day),0,cost)),',',sum(a17)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 15 day),0,cost)),',',sum(a17)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 15 day),0,a1))) d17,
  319. concat(sum(a18-a17),',',sum(a18-a17)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 16 day),0,cost)),',',sum(a18)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 16 day),0,cost)),',',sum(a18)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 16 day),0,a1))) d18,
  320. concat(sum(a19-a18),',',sum(a19-a18)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 17 day),0,cost)),',',sum(a19)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 17 day),0,cost)),',',sum(a19)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 17 day),0,a1))) d19,
  321. concat(sum(a20-a19),',',sum(a20-a19)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 18 day),0,cost)),',',sum(a20)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 18 day),0,cost)),',',sum(a20)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 18 day),0,a1))) d20,
  322. concat(sum(a21-a20),',',sum(a21-a20)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 19 day),0,cost)),',',sum(a21)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 19 day),0,cost)),',',sum(a21)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 19 day),0,a1))) d21,
  323. concat(sum(a22-a21),',',sum(a22-a21)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 20 day),0,cost)),',',sum(a22)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 20 day),0,cost)),',',sum(a22)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 20 day),0,a1))) d22,
  324. concat(sum(a23-a22),',',sum(a23-a22)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 21 day),0,cost)),',',sum(a23)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 21 day),0,cost)),',',sum(a23)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 21 day),0,a1))) d23,
  325. concat(sum(a24-a23),',',sum(a24-a23)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 22 day),0,cost)),',',sum(a24)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 22 day),0,cost)),',',sum(a24)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 22 day),0,a1))) d24,
  326. concat(sum(a25-a24),',',sum(a25-a24)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 23 day),0,cost)),',',sum(a25)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 23 day),0,cost)),',',sum(a25)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 23 day),0,a1))) d25,
  327. concat(sum(a26-a25),',',sum(a26-a25)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 24 day),0,cost)),',',sum(a26)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 24 day),0,cost)),',',sum(a26)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 24 day),0,a1))) d26,
  328. concat(sum(a27-a26),',',sum(a27-a26)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 25 day),0,cost)),',',sum(a27)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 25 day),0,cost)),',',sum(a27)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 25 day),0,a1))) d27,
  329. concat(sum(a28-a27),',',sum(a28-a27)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 26 day),0,cost)),',',sum(a28)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 26 day),0,cost)),',',sum(a28)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 26 day),0,a1))) d28,
  330. concat(sum(a29-a28),',',sum(a29-a28)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 27 day),0,cost)),',',sum(a29)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 27 day),0,cost)),',',sum(a29)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 27 day),0,a1))) d29,
  331. concat(sum(a30-a29),',',sum(a30-a29)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 28 day),0,cost)),',',sum(a30)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 28 day),0,cost)),',',sum(a30)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 28 day),0,a1))) d30,
  332. concat(sum(a31-a30),',',sum(a31-a30)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 29 day),0,cost)),',',sum(a31)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 29 day),0,cost)),',',sum(a31)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 29 day),0,a1))) d31,
  333. concat(sum(a32-a31),',',sum(a32-a31)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 30 day),0,cost)),',',sum(a32)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 30 day),0,cost)),',',sum(a32)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 30 day),0,a1))) d32,
  334. concat(sum(a33-a32),',',sum(a33-a32)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 31 day),0,cost)),',',sum(a33)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 31 day),0,cost)),',',sum(a33)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 31 day),0,a1))) d33,
  335. concat(sum(a34-a33),',',sum(a34-a33)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 32 day),0,cost)),',',sum(a34)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 32 day),0,cost)),',',sum(a34)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 32 day),0,a1))) d34,
  336. concat(sum(a35-a34),',',sum(a35-a34)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 33 day),0,cost)),',',sum(a35)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 33 day),0,cost)),',',sum(a35)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 33 day),0,a1))) d35,
  337. concat(sum(a36-a35),',',sum(a36-a35)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 34 day),0,cost)),',',sum(a36)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 34 day),0,cost)),',',sum(a36)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 34 day),0,a1))) d36,
  338. concat(sum(a37-a36),',',sum(a37-a36)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 35 day),0,cost)),',',sum(a37)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 35 day),0,cost)),',',sum(a37)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 35 day),0,a1))) d37,
  339. concat(sum(a38-a37),',',sum(a38-a37)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 36 day),0,cost)),',',sum(a38)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 36 day),0,cost)),',',sum(a38)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 36 day),0,a1))) d38,
  340. concat(sum(a39-a38),',',sum(a39-a38)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 37 day),0,cost)),',',sum(a39)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 37 day),0,cost)),',',sum(a39)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 37 day),0,a1))) d39,
  341. concat(sum(a40-a39),',',sum(a40-a39)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 38 day),0,cost)),',',sum(a40)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 38 day),0,cost)),',',sum(a40)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 38 day),0,a1))) d40,
  342. concat(sum(a41-a40),',',sum(a41-a40)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 39 day),0,cost)),',',sum(a41)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 39 day),0,cost)),',',sum(a41)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 39 day),0,a1))) d41,
  343. concat(sum(a42-a41),',',sum(a42-a41)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 40 day),0,cost)),',',sum(a42)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 40 day),0,cost)),',',sum(a42)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 40 day),0,a1))) d42,
  344. concat(sum(a43-a42),',',sum(a43-a42)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 41 day),0,cost)),',',sum(a43)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 41 day),0,cost)),',',sum(a43)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 41 day),0,a1))) d43,
  345. concat(sum(a44-a43),',',sum(a44-a43)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 42 day),0,cost)),',',sum(a44)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 42 day),0,cost)),',',sum(a44)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 42 day),0,a1))) d44,
  346. concat(sum(a45-a44),',',sum(a45-a44)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 43 day),0,cost)),',',sum(a45)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 43 day),0,cost)),',',sum(a45)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 43 day),0,a1))) d45,
  347. concat(sum(a46-a45),',',sum(a46-a45)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 44 day),0,cost)),',',sum(a46)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 44 day),0,cost)),',',sum(a46)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 44 day),0,a1))) d46,
  348. concat(sum(a47-a46),',',sum(a47-a46)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 45 day),0,cost)),',',sum(a47)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 45 day),0,cost)),',',sum(a47)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 45 day),0,a1))) d47,
  349. concat(sum(a48-a47),',',sum(a48-a47)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 46 day),0,cost)),',',sum(a48)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 46 day),0,cost)),',',sum(a48)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 46 day),0,a1))) d48,
  350. concat(sum(a49-a48),',',sum(a49-a48)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 47 day),0,cost)),',',sum(a49)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 47 day),0,cost)),',',sum(a49)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 47 day),0,a1))) d49,
  351. concat(sum(a50-a49),',',sum(a50-a49)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 48 day),0,cost)),',',sum(a50)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 48 day),0,cost)),',',sum(a50)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 48 day),0,a1))) d50,
  352. concat(sum(a51-a50),',',sum(a51-a50)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 49 day),0,cost)),',',sum(a51)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 49 day),0,cost)),',',sum(a51)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 49 day),0,a1))) d51,
  353. concat(sum(a52-a51),',',sum(a52-a51)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 50 day),0,cost)),',',sum(a52)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 50 day),0,cost)),',',sum(a52)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 50 day),0,a1))) d52,
  354. concat(sum(a53-a52),',',sum(a53-a52)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 51 day),0,cost)),',',sum(a53)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 51 day),0,cost)),',',sum(a53)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 51 day),0,a1))) d53,
  355. concat(sum(a54-a53),',',sum(a54-a53)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 52 day),0,cost)),',',sum(a54)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 52 day),0,cost)),',',sum(a54)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 52 day),0,a1))) d54,
  356. concat(sum(a55-a54),',',sum(a55-a54)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 53 day),0,cost)),',',sum(a55)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 53 day),0,cost)),',',sum(a55)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 53 day),0,a1))) d55,
  357. concat(sum(a56-a55),',',sum(a56-a55)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 54 day),0,cost)),',',sum(a56)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 54 day),0,cost)),',',sum(a56)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 54 day),0,a1))) d56,
  358. concat(sum(a57-a56),',',sum(a57-a56)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 55 day),0,cost)),',',sum(a57)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 55 day),0,cost)),',',sum(a57)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 55 day),0,a1))) d57,
  359. concat(sum(a58-a57),',',sum(a58-a57)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 56 day),0,cost)),',',sum(a58)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 56 day),0,cost)),',',sum(a58)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 56 day),0,a1))) d58,
  360. concat(sum(a59-a58),',',sum(a59-a58)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 57 day),0,cost)),',',sum(a59)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 57 day),0,cost)),',',sum(a59)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 57 day),0,a1))) d59,
  361. concat(sum(a60-a59),',',sum(a60-a59)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 58 day),0,cost)),',',sum(a60)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 58 day),0,cost)),',',sum(a60)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 58 day),0,a1))) d60,
  362. concat(sum(m3-a60),',',sum(m3-a60)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 88 day),0,cost)),',',sum(m3)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 88 day),0,cost)),',',sum(m3)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 88 day),0,a1))) m3,
  363. concat(sum(m4-m3),',',sum(m4-m3)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 118 day),0,cost)),',',sum(m4)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 118 day),0,cost)),',',sum(m4)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 118 day),0,a1))) m4,
  364. concat(sum(m5-m4),',',sum(m5-m4)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 148 day),0,cost)),',',sum(m5)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 148 day),0,cost)),',',sum(m5)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 148 day),0,a1))) m5
  365. from book_trend where cost>0 {op1} {op2} {op3} {op4} group by book,type {op5}
  366. """
  367. data, total = getLimitData(db.dm, sql, page, page_size)
  368. def parse(str):
  369. li = str.decode('utf-8').split(',')
  370. li[0] = round(float(li[0]), 2)
  371. li[1] = round(float(li[1]), 4)
  372. li[2] = round(float(li[2]), 4)
  373. li[3] = round(float(li[3]), 2)
  374. return dict(zip(['amount', 'add', 'roi', 'mult'], li))
  375. # print(data)
  376. for i in data:
  377. for k, v in i.items():
  378. if k in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'd11', 'd12', 'd13', 'd14', 'd15',
  379. 'd16', 'd17', 'd18',
  380. 'd19', 'd20', 'd21', 'd22', 'd23', 'd24', 'd25', 'd26', 'd27', 'd28', 'd29', 'd30', 'd31', 'd32',
  381. 'd33', 'd34', 'd35', 'd36', 'd37', 'd38',
  382. 'd39', 'd40', 'd41', 'd42', 'd43', 'd44', 'd45', 'd46', 'd47', 'd48', 'd49', 'd50', 'd51', 'd52',
  383. 'd53', 'd54', 'd55', 'd56', 'd57', 'd58', 'd59',
  384. 'd60', 'm3', 'm4', 'm5']:
  385. i[k] = parse(v) if v else {}
  386. return data, total
  387. def pitcher_overview(user_id, start, end, pitcher, page, page_size, order_by, order):
  388. if user_id in super_auth():
  389. op = ''
  390. else:
  391. user = tuple([i['nick_name'] for i in get_pitcher({'user_id': user_id})] + [get_user_name_by_id(user_id)])
  392. if len(user) == 1:
  393. op = f" and pitcher ='{user[0]}'"
  394. else:
  395. op = f" and pitcher in {str(user)}"
  396. op1 = f" and pitcher='{pitcher}' " if pitcher else ''
  397. op2 = f" and dt>='{start}' " if start else ''
  398. op3 = f" and dt<='{end}' " if end else ''
  399. op4 = f" order by {order_by} {order}" if order_by and order else ''
  400. db = MysqlUtils()
  401. sql = f""" select pitcher,
  402. DATE_FORMAT(min(dt),'%Y%m%d') start_date,
  403. DATE_FORMAT(max(dt),'%Y%m%d') end_date,
  404. sum(cost) cost,
  405. sum(reg_amount) amount,
  406. sum(first_order_amount) first_amount,
  407. round(sum(reg_amount)/sum(cost),4) roi,
  408. round(sum(first_order_amount)/sum(cost),4) first_roi,
  409. round(sum(d7)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 5 day),0,cost)),4) roi7,
  410. round(sum(d30)/sum(if(dt>=SUBDATE(CURRENT_DATE,INTERVAL 5 day),0,cost)),4) roi30,
  411. sum(reg_amount-cost) profit,
  412. round(sum(click_count)/sum(view_count),4) click_rate,
  413. round(sum(follow_user)/sum(click_count),4) follow_rate,
  414. round(sum(reg_order_user)/sum(click_count),4) order_rate,
  415. round(sum(first_order_user)/sum(click_count),4) first_order_rate,
  416. sum(view_count) view_count,
  417. sum(click_count) click_count,
  418. sum(follow_user) follow_user,
  419. sum(reg_order_user) order_user,
  420. sum(first_order_user) first_order_user,
  421. round(sum(cost)/sum(follow_user),2) follow_cost,
  422. round(sum(cost)/sum(reg_order_user),2) order_cost,
  423. round(sum(cost)/sum(first_order_user),2) first_order_cost,
  424. round(sum(reg_amount)/sum(reg_order_user),2) avg_amount,
  425. sum(ba1) td_amount,
  426. sum(ba2) yd_amount,
  427. sum(ba3) byd_amount,sum(d7) d7 ,sum(d30) d30
  428. from dw_pitcher_trend where cost>0 and pitcher!='' {op} {op1} {op2} {op3} group by pitcher {op4}
  429. """
  430. sum_sql = f"""select '总计' as pitcher,
  431. sum(cost) cost,
  432. sum(amount) amount,
  433. sum(first_amount) first_amount,
  434. round(sum(amount)/sum(cost),4) roi,
  435. round(sum(first_amount)/sum(cost),4) first_roi,
  436. round(sum(d7)/sum(cost),4) roi7,
  437. round(sum(d30)/sum(cost),4) roi30,
  438. sum(amount-cost) profit,
  439. round(sum(click_count)/sum(view_count),4) click_rate,
  440. round(sum(follow_user)/sum(click_count),4) follow_rate,
  441. round(sum(order_user)/sum(click_count),4) order_rate,
  442. round(sum(first_order_user)/sum(click_count),4) first_order_rate,
  443. sum(view_count) view_count,
  444. sum(click_count) click_count,
  445. sum(follow_user) follow_user,
  446. sum(order_user) order_user,
  447. sum(first_order_user) first_order_user,
  448. round(sum(cost)/sum(follow_user),2) follow_cost,
  449. round(sum(cost)/sum(order_user),2) order_cost,
  450. round(sum(cost)/sum(first_order_user),2) first_order_cost,
  451. round(sum(amount)/sum(order_user),2) avg_amount,
  452. sum(td_amount) td_amount,
  453. sum(yd_amount) yd_amount,
  454. sum(byd_amount) byd_amount from ({sql}) a
  455. """
  456. return getLimitSumDataV2(db.dm, sql, sum_sql, page, page_size)
  457. def boss_panel_summary(user_id, start, end, channel, pitcher, type, stage, page, page_size, order_by, order):
  458. if user_id in super_auth():
  459. op = ''
  460. else:
  461. x = UserAuthUtils.get_auth_channel(user_id)
  462. if len(x) == 0:
  463. return None, None
  464. elif len(x) == 1:
  465. op = f" and channel ='{x[0]}'"
  466. else:
  467. op = f" and channel in {str(UserAuthUtils.get_auth_channel(user_id))}"
  468. op1 = f" and pitcher='{pitcher}' " if pitcher else ''
  469. op2 = f" and dt>='{start}' " if start else ''
  470. op3 = f" and dt<='{end}' " if end else ''
  471. op4 = f" and type='{type}' " if type else ''
  472. op5 = f" and stage='{stage}' " if stage else ''
  473. op6 = f" and channel='{channel}' " if channel else ''
  474. op7 = f" order by {order_by} {order}" if order_by and order else 'order by dt desc'
  475. dm = MysqlUtils().dm
  476. sql = f"""select sum(order_amount) order_amount,sum(cost) cost,round(sum(reg_order_amount)/sum(cost),4) roi,dt from dw_channel
  477. where 1=1 {op} {op1} {op2} {op3} {op4} {op5} {op6}
  478. GROUP BY dt HAVING order_amount+cost>0 {op7} """
  479. # print(sql)
  480. return getLimitData(dm, sql, page, page_size)
  481. def image_rank(start, end, type, book, page, page_size, order_by, order):
  482. op1 = f" and book='{book}' " if book else ''
  483. op2 = f" and dt>='{start}' " if start else ''
  484. op3 = f" and dt<='{end}' " if end else ''
  485. op4 = f" and type='{type}' " if type else ''
  486. op5 = f" order by {order_by} {order}" if order_by and order else 'order by dt desc'
  487. db = MysqlUtils().quchen_text
  488. sql = f"""select preview_url,
  489. sum(cost),
  490. round(sum(click_count)/sum(view_count),4) ctr,
  491. round(sum(cost)/sum(click_count),2) cpc
  492. from dm_image_cost_day where preview_url!='' {op1} {op2} {op3} {op4} GROUP BY signature,preview_url
  493. {op5} """
  494. return getLimitData(db, sql, page, page_size)
  495. """广告排行榜"""
  496. def advertisement_rank(user_id, start, end, type, page, page_size, order, order_by, book, channel, pitcher, has_order,
  497. is_video, show_type):
  498. # TODO:修改为clickhouse来进行数据访问
  499. if user_id in super_auth():
  500. op1 = ''
  501. else:
  502. user = tuple([i['nick_name'] for i in get_pitcher({'user_id': user_id})] + [get_user_name_by_id(user_id)])
  503. if len(user) == 1:
  504. op1 = f" and pitcher ='{user[0]}'"
  505. else:
  506. op1 = f" and pitcher in {str(user)}"
  507. op4 = f" and channel='{channel}'" if channel else ''
  508. op5 = f" and pitcher ='{pitcher}' " if pitcher else ''
  509. op8 = f" and type='{type}'" if type else ''
  510. op10 = f" and book='{book}'" if book else ''
  511. op11 = f" and order_count>0" if has_order else ''
  512. op12 = f" and is_video" if is_video else ''
  513. # 公共数据,和素材库一样,个人只显示个人(小组)数据
  514. op_or1 = f' or (dt<date_add(now(),interval -5 day) or cost>5000) ' if show_type == 'public' else ''
  515. op_order = f" order by {order_by} {order}" if order_by and order else ''
  516. # 时间为基底限制,必须遵守
  517. op_time_bigger = f" and dt>='{start}' " if start else ''
  518. op_time_small = f" and dt<='{end}' " if end else ''
  519. db = MysqlUtils().dm
  520. sql = f"""
  521. select
  522. campaign_id,
  523. dt,stage,platform,book,`type`, cost,channel,pitcher,owner,
  524. view_count,
  525. click_count,
  526. follow_count,
  527. round((cost/view_count)*1000,2) cpm,
  528. round(click_count/view_count,4) ctr,
  529. round(cost/click_count,2) cpc,
  530. order_count,
  531. order_amount,
  532. round(order_count/click_count,4) order_rate,
  533. round(order_amount/order_count,2) unit_price,
  534. round(cost/follow_count) follow_cost,
  535. round(cost/order_count,2) order_cost,
  536. round(order_amount/cost,4) roi,
  537. title,
  538. description,
  539. image_id,
  540. is_video,
  541. use_times,
  542. preview_url
  543. from
  544. ( select
  545. campaign_id,
  546. min(dt) as dt,
  547. max(stage) as stage,
  548. max(platform) as platform,
  549. max(book) as book,
  550. max(`type`) as type,
  551. max(channel) as channel,
  552. max(pitcher) as pitcher,
  553. max(owner) as owner,
  554. sum(cost) as cost,
  555. sum(view_count) as view_count,
  556. sum(click_count) as click_count,
  557. sum(follow_count) as follow_count,
  558. sum(order_count) as order_count,
  559. sum(order_amount) as order_amount,
  560. sum(use_times) as use_times,
  561. max(title) as title,
  562. max(description) as description,
  563. max(image_id) as image_id,
  564. max(is_video) as is_video,
  565. max(preview_url) as preview_url
  566. from dw_image_cost_day
  567. where (1=1 {op1} {op_or1})
  568. {op4} {op5} {op8} {op10} {op11} {op12}
  569. {op_time_bigger} {op_time_small}
  570. group by campaign_id
  571. ) as a
  572. {op_order}
  573. """
  574. print(sql)
  575. sum_sql = f"""select '总计' as pitcher,
  576. sum(cost) cost,
  577. sum(view_count) view_count,
  578. sum(click_count) click_count,
  579. sum(follow_count) follow_count,
  580. round((sum(cost)/sum(view_count))*1000,4) cpm,
  581. round(sum(click_count)/sum(view_count),4) ctr,
  582. round(sum(cost)/sum(click_count),2) cpc,
  583. sum(order_count),
  584. sum(order_amount),
  585. round(sum(order_count)/sum(click_count),4) order_rate,
  586. round(sum(order_amount)/sum(order_count),2) unit_price,
  587. round(sum(cost)/sum(follow_count),2) follow_cost,
  588. round(sum(cost)/sum(order_count),2) order_cost,
  589. sum(use_times) use_times,
  590. round(sum(order_amount)/sum(cost),4) ROI
  591. from ({sql}) a
  592. """
  593. return getLimitSumData(db, sql, sum_sql, page, page_size)
  594. """创意展示"""
  595. def idea_rank(user_id, start, end, page, page_size, order, order_by, book, channel, is_singleimg,
  596. is_video, data_type, campaign_ids):
  597. # TODO:修改为clickhouse来进行数据访问
  598. # 时间的归因-----获取到所有这段时间内的记录,并进行聚合(聚合周末再做,先把数据拿出来)
  599. # 认为素材消耗的数据,已经是一条数据的所有归因直接根据dt来就可以
  600. if user_id in super_auth():
  601. op1 = ''
  602. else:
  603. user = tuple([i['nick_name'] for i in get_pitcher({'user_id': user_id})] + [get_user_name_by_id(user_id)])
  604. if len(user) == 1:
  605. op1 = f" and pitcher ='{user[0]}'"
  606. else:
  607. op1 = f" and pitcher in {str(user)}"
  608. channel = channel.replace('朋友圈信息流', 'MP')
  609. channel = channel.replace('广点通', 'GDT')
  610. channel = "','".join(channel.split(','))
  611. channel = "'{}'".format(channel)
  612. op4 = f" and type in ({channel}) " if channel else ''
  613. op10 = f" and book='{book}'" if book else ''
  614. op11 = f" and image_id like '%,%' " if not is_singleimg else ''
  615. op12 = f" and is_video" if is_video == 2 else ' and not is_video' # 进行对应修改1----图片
  616. if campaign_ids:
  617. campaign_ids = campaign_ids.split(',')
  618. campaign_ids = "','".join([str(i) for i in campaign_ids])
  619. campaign_ids = "'" + campaign_ids + "'"
  620. op_campaign_id = f'and campaign_id in ({campaign_ids})' if campaign_ids else ''
  621. # 公共数据,和素材库一样,个人只显示个人(小组)数据
  622. op_or1 = f' or (dt<date_add(now(),interval -5 day) or cost>5000) ' if data_type == 'all' else ''
  623. # clicktimes,view_times,consume_amount,click_rate---------数据进行一一对应
  624. if order_by == 'click_times':
  625. order_by = 'click_count'
  626. if order_by == 'view_times':
  627. order_by = 'view_count'
  628. if order_by == 'consume_amount':
  629. order_by = 'cost'
  630. if order_by == 'click_rate':
  631. order_by = 'if(click_count=0 or view_count =0 or view_count is null or click_count is null,0,round(click_count / view_count,2)) '
  632. if order_by == 'create_time' or order_by == 'start_date':
  633. order_by = 'cost'
  634. op_order = f" order by {order_by} {order}" if order_by and order else ''
  635. # 时间为基底限制,必须遵守
  636. op_time_bigger = f" and dt>='{start}' " if start else ''
  637. op_time_small = f" and dt<='{end}' " if end else ''
  638. db = MysqlUtils().dm
  639. sql = f'''
  640. select
  641. campaign_id as id,
  642. book as novels,
  643. dt as startDate,
  644. date_format( now(),'%Y-%m-%d') as endDate,
  645. `type` as channel,
  646. 'all' as dataType,
  647. owner as creator,
  648. 0 as delFlag,
  649. False as isCollected,
  650. download_path as downloadPath,
  651. height,
  652. width,
  653. preview_url as media,
  654. format as mediaFormat,
  655. size as mediaSize,
  656. if(click_count=0 or view_count =0 or view_count is null or click_count is null,0,round(click_count / view_count,2)) as clickRate,
  657. round(width/if(height,height,1),2) aspect_ratio,
  658. cost as consumeAmount,
  659. view_count as viewTimes,
  660. click_count as clickTimes,
  661. round((cost/view_count)*1000,2) cpm,
  662. round(click_count/view_count,4) ctr,
  663. round(cost/click_count,2) cpc,
  664. title,
  665. description as article,
  666. date_format( now(),'%Y-%m-%d %H:%i:%S') as upateTime,
  667. null as updateBy,
  668. if(is_video,2,1) as type,
  669. video_bit_rate as videoBitRate,
  670. null as videoFirstPage,
  671. video_length as videoLength,
  672. use_times as userTimes
  673. from ( select
  674. max(campaign_id) as campaign_id ,
  675. max(book) as book,
  676. min(dt) as dt,
  677. max(image_id) as image_id,
  678. max(`type`) as type,
  679. max(owner) as owner,
  680. max(download_path) as download_path,
  681. max(height) as height,
  682. max(width) as width,
  683. max(preview_url) as preview_url,
  684. max(format) as format,
  685. max(size) as size,
  686. sum(cost) as cost,
  687. sum(view_count) as view_count,
  688. sum(click_count) as click_count,
  689. max(title) as title,
  690. max(is_video) as is_video,
  691. max(description) as description,
  692. max(video_bit_rate) as video_bit_rate,
  693. max(video_length) as video_length,
  694. sum(use_times) as use_times
  695. from dw_image_cost_day
  696. where replace (preview_url,' ,','') !=''
  697. and (1=1 {op1} {op_or1})
  698. {op4} {op10} {op11} {op12} {op_campaign_id}
  699. {op_time_bigger} {op_time_small}
  700. group by campaign_id) as foo
  701. {op_order}
  702. '''
  703. print(sql)
  704. data, total = getLimitData(db, sql, page, page_size)
  705. data = {'records': data, 'total': total, 'size': page_size, 'current': page, 'pages': int(total / page_size) + 1}
  706. return data
  707. def media_rank(user_id, start, end, page, page_size, order, order_by, book, channel, is_singleimg,
  708. is_video, data_type, campaign_ids):
  709. # TODO:修改为clickhouse来进行数据访问
  710. # 时间的归因-----获取到所有这段时间内的记录,并进行聚合(聚合周末再做,先把数据拿出来)
  711. # 认为素材消耗的数据,已经是一条数据的所有归因直接根据dt来就可以
  712. if user_id in super_auth():
  713. op1 = ''
  714. else:
  715. user = tuple([i['nick_name'] for i in get_pitcher({'user_id': user_id})] + [get_user_name_by_id(user_id)])
  716. if len(user) == 1:
  717. op1 = f" and pitcher ='{user[0]}'"
  718. else:
  719. op1 = f" and pitcher in {str(user)}"
  720. channel = channel.replace('朋友圈信息流', 'MP')
  721. channel = channel.replace('广点通', 'GDT')
  722. channel = "','".join(channel.split(','))
  723. channel = "'{}'".format(channel)
  724. op4 = f" and type in ({channel}) " if channel else ''
  725. op10 = f" and book='{book}'" if book else ''
  726. op11 = f" and image_id like '%,%' " if not is_singleimg else ''
  727. op12 = f" and is_video" if is_video == 2 else ' and not is_video' # 进行对应修改1----图片
  728. if campaign_ids:
  729. campaign_ids = campaign_ids.split(',')
  730. campaign_ids = "','".join([str(i) for i in campaign_ids])
  731. campaign_ids = "'" + campaign_ids + "'"
  732. op_campaign_id = f'and campaign_id in ({campaign_ids})' if campaign_ids else ''
  733. # 公共数据,和素材库一样,个人只显示个人(小组)数据
  734. op_or1 = f' or (dt<date_add(now(),interval -5 day) or cost>5000) ' if data_type == 'all' else ''
  735. # clicktimes,view_times,consume_amount,click_rate---------数据进行一一对应
  736. if order_by == 'click_times':
  737. order_by = 'click_count'
  738. if order_by == 'view_times':
  739. order_by = 'view_count'
  740. if order_by == 'consume_amount':
  741. order_by = 'cost'
  742. if order_by == 'click_rate':
  743. order_by = 'if(click_count=0 or view_count =0 or view_count is null or click_count is null,0,round(click_count / view_count,2)) '
  744. if order_by == 'create_time' or order_by == 'start_date':
  745. order_by = 'cost'
  746. op_order = f" order by {order_by} {order}" if order_by and order else ''
  747. # 时间为基底限制,必须遵守
  748. op_time_bigger = f" and dt>='{start}' " if start else ''
  749. op_time_small = f" and dt<='{end}' " if end else ''
  750. db = MysqlUtils().dm
  751. sql = f'''
  752. select
  753. campaign_id as id,
  754. book as novels,
  755. dt as startDate,
  756. date_format( now(),'%Y-%m-%d') as endDate,
  757. `type` as channel,
  758. 'all' as dataType,
  759. owner as creator,
  760. 0 as delFlag,
  761. False as isCollected,
  762. download_path as downloadPath,
  763. height,
  764. width,
  765. preview_url as content,
  766. format as mediaFormat,
  767. size as mediaSize,
  768. if(click_count=0 or view_count =0 or view_count is null or click_count is null,0,round(click_count / view_count,2)) as clickRate,
  769. round(width/if(height,height,1),2) aspect_ratio,
  770. cost as consumeAmount,
  771. view_count as viewTimes,
  772. click_count as clickTimes,
  773. round((cost/view_count)*1000,2) cpm,
  774. round(click_count/view_count,4) ctr,
  775. round(cost/click_count,2) cpc,
  776. date_format( now(),'%Y-%m-%d %H:%i:%S') as upateTime,
  777. null as updateBy,
  778. if (image_id not like '%,%' ,true,false) as singleImg,
  779. if(is_video,2,1) as type,
  780. video_bit_rate as videoBitRate,
  781. null as videoFirstPage,
  782. video_length as videoLength,
  783. use_times as userTimes
  784. from
  785. ( select
  786. max(campaign_id) as campaign_id ,
  787. max(book) as book,
  788. min(dt) as dt,
  789. max(image_id) as image_id,
  790. max(`type`) as type,
  791. max(owner) as owner,
  792. max(download_path) as download_path,
  793. max(height) as height,
  794. max(width) as width,
  795. max(preview_url) as preview_url,
  796. max(format) as format,
  797. max(size) as size,
  798. sum(cost) as cost,
  799. sum(view_count) as view_count,
  800. sum(click_count) as click_count,
  801. max(title) as title,
  802. max(is_video) as is_video,
  803. max(description) as description,
  804. max(video_bit_rate) as video_bit_rate,
  805. max(video_length) as video_length,
  806. sum(use_times) as use_times
  807. from dw_image_cost_day
  808. where replace (preview_url,' ,','') !=''
  809. and (1=1 {op1} {op_or1})
  810. {op4} {op10} {op11} {op12} {op_campaign_id}
  811. {op_time_bigger} {op_time_small}
  812. group by campaign_id) as foo
  813. {op_order}
  814. '''
  815. print(sql)
  816. data, total = getLimitData(db, sql, page, page_size)
  817. data = {'records': data, 'total': total, 'size': page_size, 'current': page, 'pages': int(total / page_size) + 1}
  818. return data
  819. def content_rank(user_id, start, end, page, page_size, order, order_by, book, channel, data_type, campaign_ids):
  820. # TODO:修改为clickhouse来进行数据访问
  821. # 时间的归因-----获取到所有这段时间内的记录,并进行聚合(聚合周末再做,先把数据拿出来)
  822. # 认为素材消耗的数据,已经是一条数据的所有归因直接根据dt来就可以
  823. if user_id in super_auth():
  824. op1 = ''
  825. else:
  826. user = tuple([i['nick_name'] for i in get_pitcher({'user_id': user_id})] + [get_user_name_by_id(user_id)])
  827. if len(user) == 1:
  828. op1 = f" and pitcher ='{user[0]}'"
  829. else:
  830. op1 = f" and pitcher in {str(user)}"
  831. channel = channel.replace('朋友圈信息流', 'MP')
  832. channel = channel.replace('广点通', 'GDT')
  833. channel = "','".join(channel.split(','))
  834. channel = "'{}'".format(channel)
  835. op4 = f" and type in ({channel}) " if channel else ''
  836. op10 = f" and book='{book}'" if book else ''
  837. if campaign_ids:
  838. campaign_ids = campaign_ids.split(',')
  839. campaign_ids = "','".join([str(i) for i in campaign_ids])
  840. campaign_ids = "'" + campaign_ids + "'"
  841. op_campaign_id = f'and campaign_id in ({campaign_ids})' if campaign_ids else ''
  842. # 公共数据,和素材库一样,个人只显示个人(小组)数据
  843. op_or1 = f' or (dt<date_add(now(),interval -5 day) or cost>5000) ' if data_type == 'all' else ''
  844. # clicktimes,view_times,consume_amount,click_rate---------数据进行一一对应
  845. if order_by == 'click_times':
  846. order_by = 'click_count'
  847. if order_by == 'view_times':
  848. order_by = 'view_count'
  849. if order_by == 'consume_amount':
  850. order_by = 'cost'
  851. if order_by == 'click_rate':
  852. order_by = 'if(click_count=0 or view_count =0 or view_count is null or click_count is null,0,round(click_count / view_count,2)) '
  853. if order_by == 'create_time' or order_by == 'start_date':
  854. order_by = 'cost'
  855. op_order = f" order by {order_by} {order}" if order_by and order else ''
  856. # 时间为基底限制,必须遵守
  857. op_time_bigger = f" and dt>='{start}' " if start else ''
  858. op_time_small = f" and dt<='{end}' " if end else ''
  859. db = MysqlUtils().dm
  860. sql = f"""
  861. select
  862. campaign_id as id,
  863. book as novels,
  864. dt as startDate,
  865. date_format( now(),'%Y-%m-%d') as endDate,
  866. 'all' as dataType,
  867. owner as creator,
  868. False as isCollected,
  869. if(click_count=0 or view_count =0 or view_count is null or click_count is null,0,round(click_count / view_count,2)) as clickRate,
  870. round(width/if(height,height,1),2) aspect_ratio,
  871. cost as consumeAmount,
  872. view_count as viewTimes,
  873. click_count as clickTimes,
  874. round((cost/view_count)*1000,2) cpm,
  875. round(click_count/view_count,4) ctr,
  876. round(cost/click_count,2) cpc,
  877. REPLACE(REPLACE(description , CHAR(10), ''), CHAR(13), '') as content,
  878. REPLACE(REPLACE(title, CHAR(10), ''), CHAR(13), '') as title,
  879. date_format( now(),'%Y-%m-%d %H:%i:%S') as upateTime,
  880. null as updateBy,
  881. use_times as userTimes
  882. from (
  883. select
  884. max(campaign_id) as campaign_id ,
  885. max(book) as book,
  886. min(dt) as dt,
  887. max(image_id) as image_id,
  888. max(`type`) as type,
  889. max(owner) as owner,
  890. max(download_path) as download_path,
  891. max(height) as height,
  892. max(width) as width,
  893. max(preview_url) as preview_url,
  894. max(format) as format,
  895. max(size) as size,
  896. sum(cost) as cost,
  897. sum(view_count) as view_count,
  898. sum(click_count) as click_count,
  899. max(title) as title,
  900. max(is_video) as is_video,
  901. max(description) as description,
  902. max(video_bit_rate) as video_bit_rate,
  903. max(video_length) as video_length,
  904. sum(use_times) as use_times
  905. from dw_image_cost_day
  906. where (REPLACE(REPLACE(description, CHAR(10), ''), CHAR(13), '') is not null
  907. or REPLACE(REPLACE(title, CHAR(10), ''), CHAR(13), '') is not null )
  908. and (1=1 {op1} {op_or1})
  909. {op4} {op10} {op_campaign_id}
  910. {op_time_bigger} {op_time_small}
  911. group by campaign_id ) as foo
  912. {op_order}
  913. """
  914. print(sql)
  915. data, total = getLimitData(db, sql, page, page_size)
  916. data = {'records': data, 'total': total, 'size': page_size, 'current': page, 'pages': int(total / page_size) + 1}
  917. return data
  918. def label_list(page, page_size):
  919. db = MysqlUtils().dm
  920. sql = '''
  921. select id,label,date_format( update_time,'%Y-%m-%d') as updateTime,
  922. date_format( create_time,'%Y-%m-%d') as createTime
  923. from ads_label al
  924. where del_flag=0
  925. '''
  926. data = db.getData_json(sql)
  927. total_sql = '''
  928. select count(*) from ads_label al where del_flag=0;
  929. '''
  930. data_total = db.getData(total_sql)
  931. total = int(data_total[0][0])
  932. data = {'records': data, 'total': total, 'size': page_size, 'current': page, 'pages': int(total / page_size) + 1}
  933. print(data)
  934. return data
  935. def label_update():
  936. pass
  937. def label_del(label_id, user_id):
  938. if label_id is None:
  939. return
  940. sql = f'''
  941. update ads_label
  942. set del_flag=1 ,update_by ={user_id}
  943. where id ='{label_id}'
  944. '''
  945. db = MysqlUtils().dm
  946. db.execute(sql)
  947. def label_add(label, user_id):
  948. if label is None:
  949. return
  950. sql = f'''
  951. insert into ads_label
  952. (label,create_by) values ('{label}',{user_id})
  953. '''
  954. db = MysqlUtils().dm
  955. db.execute(sql)
  956. if __name__ == '__main__':
  957. print(get_pitcher({"user_id": 78}))