zt_report_controller.rb 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # encoding:utf-8
  2. class ZtReportController < BaseController
  3. before_filter :authenticate_admin_user!
  4. def xccj
  5. render :layout => false
  6. end
  7. def fanhongbao
  8. time_0125 = Time.new(2017, 1, 27, 20, 0, 0)
  9. #合体成功总金额单位:分
  10. @success_total = 0
  11. records = ZtFanhongbaoRecord.where("state=?", 1)
  12. records.each do |record|
  13. @success_total += record.first_num*100 + record.last_num*10
  14. end
  15. #合体总次数
  16. @total_times = records.length#ZtFanhongbaoRecord.where("state=?", 1).length
  17. #绑定金额 balance
  18. @total_balance = Balance.where("source=? or source=?", "new_year_fanhongbao_", "new_year_fhb_2017").sum("count")
  19. #提现金额(金额小于50元的)
  20. @take_cash_total = Balance.where("source=? and count > -10000 and created_at > ?", "extract", time_0125).sum("count")
  21. #新增关注用户
  22. @new_wx_sub_users = WxUser.where("created_at > ? and subscribe = ?", time_0125, true).length
  23. #新注册用户
  24. @new_users = User.where("created_at>? and tel != ''", time_0125).length
  25. #新增绑定身份证用户
  26. @binding_id = User.where("created_at>? and identity_card != ''", time_0125).length
  27. #新增绑卡用户
  28. @binding_card = UserBankcard.where("created_at>?", time_0125).length
  29. render :layout => false
  30. end
  31. def share_info
  32. # sql = "select sum(first_num*100 + last_num*10) as total from d5c_zt_fanhongbao_records where state=1"
  33. # @success_total = ZtFanhongbaoRecord.find_by_sql(sql).first.total
  34. time_0125 = Time.new(2017, 1, 27, 20, 0, 0)
  35. #参与总人数
  36. @total_size = ZtFanhongbaoRecord.group("launcher_id").all.length
  37. #成功人数
  38. @success_size = ZtFanhongbaoRecord.where("state=?", 1).group("launcher_id").length
  39. #合体次数用满的人数
  40. # @full_time_people = ZtFanhongbaoRecord.where("state=?", 1).group("")
  41. #分享朋友圈
  42. @share_timeline = 0 #ShareInfo.where("share_code=? and share_to=?", "spring_szhb", "timeline").length
  43. #分享人或群
  44. @share_group = 0#ShareInfo.where("share_code=? and share_to=?", "spring_szhb", "group").length
  45. ShareInfo.where("share_code=?", "spring_szhb").each do |item|
  46. if item.share_to == "group"
  47. @share_group += 1
  48. elsif item.share_to == "timeline"
  49. @share_timeline += 1
  50. end
  51. end
  52. #分享人数
  53. @share_people = ShareInfo.where("share_code=?", "spring_szhb").group("wx_user_id").length
  54. end
  55. def top10
  56. #发起用户合体次数top10
  57. sql = "select count(*) as c, launcher_id, d5c_wx_users.nickname, d5c_wx_users.user_id from d5c_zt_fanhongbao_records
  58. left join d5c_wx_users on d5c_wx_users.id = launcher_id
  59. where state=1 group by launcher_id order by c DESC limit 10"
  60. @launch_top_10 = ZtFanhongbaoRecord.find_by_sql(sql)
  61. #被邀请合体次数top10
  62. sql = "select count(*) as c, partner_id, d5c_wx_users.nickname, d5c_wx_users.user_id from d5c_zt_fanhongbao_records
  63. left join d5c_wx_users on d5c_wx_users.id = partner_id
  64. where state=1 group by partner_id order by c DESC limit 10"
  65. @partner_top_10 = ZtFanhongbaoRecord.find_by_sql(sql)
  66. @money_top_10 = []
  67. sql = "select sum(count) as total, d5c_balances.user_id, d5c_users.nickname, d5c_users.tel from d5c_balances left join d5c_users on d5c_users.id = d5c_balances.user_id where (source='new_year_fanhongbao_' or source='new_year_fhb_2017') group by user_id order by total DESC limit 50"
  68. @money_top_10 = Balance.find_by_sql(sql)
  69. end
  70. def fhb_day
  71. @list = []
  72. ZtFanhongbaoRecord.select("date_format(open_time, '%Y-%m-%d') date, date_format(open_time, '%d') day, count(*) as c, sum(last_num*10 + first_num*100)/100 as total")
  73. .where("state=1")
  74. .group("day").order("day").each do |item|
  75. # p "*********** #{item.day}, #{item.c}, #{item.total}"
  76. @list << {:day => item.date, :count => item.c, :total => item.total}
  77. end
  78. @bk_list = []
  79. UserBankcard.select("date_format(created_at, '%Y-%m-%d') date, date_format(created_at, '%d') day, count(*) as c")
  80. .where("created_at > '2017-01-27'")
  81. .group("day").order("day").each do |item|
  82. @bk_list << {:day => item.date, :count => item.c}
  83. end
  84. end
  85. def kj_infos
  86. id = params[:id] || 0
  87. @act_infos =[]
  88. if id == 0
  89. # sql = "select id, title, purchase_price, open_ad_code, banner_ad_code, stock from d5c_zt_kj_infos where state = 1"
  90. sql = "select id,title from d5c_zt_kj_infos where state = 1"
  91. acts = ZtKjInfo.find_by_sql(sql)
  92. # p "----------------------------------------------------------------------------"
  93. # # p acts
  94. # p "----------------------------------------------------------------------------"
  95. acts.each do |u|
  96. act_info = get_act_statistic_info_by_act_id(u.id)
  97. @act_infos << act_info
  98. end
  99. else
  100. act_id = id.to_i
  101. act_info = get_act_statistic_info_by_act_id(act_id)
  102. @act_infos << act_info
  103. end
  104. render :layout => false
  105. end
  106. # 通过活动id 获取该活动统计信息
  107. def get_act_statistic_info_by_act_id(act_id)
  108. act = ZtKjInfo.where("id = ?", act_id).first
  109. if !act.blank?
  110. order_sql = "select id from d5c_zt_kj_orders where act_id = #{act_id}"
  111. join_user_count = ZtKjOrder.find_by_sql(order_sql).count()
  112. reduce_sql = "select id from d5c_zt_kj_reduces where act_id = #{act_id}"
  113. kj_count = ZtKjReduce.find_by_sql(reduce_sql).count()
  114. unit_price = 0
  115. success_kj_sql = "select id from d5c_zt_kj_orders where act_id = #{act_id} and left_price = #{act.min_price}"
  116. success_kj_count = ZtKjOrder.find_by_sql(success_kj_sql).count()
  117. # 成功下单数
  118. success_order_sql = "select id from d5c_zt_kj_orders where act_id = #{act_id} and left_price = #{act.min_price} and (status = 'processing' or status = 'complete' or status = 'dispatch')"
  119. success_order_count = ZtKjOrder.find_by_sql(success_order_sql).count()
  120. # 总成本
  121. cost = act.purchase_price * success_order_count
  122. # 分享朋友圈统计
  123. share_timeline_count = ShareInfo.where("relate_parent = ? and share_to = ? and share_code = ?", act.id, "timeline", "bargain_activity").count()
  124. share_group_count = ShareInfo.where("relate_parent = ? and share_to = ? and share_code = ?", act.id, "group", "bargain_activity").count()
  125. click_open_ad_count = 0
  126. show_open_ad_count = 0
  127. if !act.open_ad_code.blank?
  128. ad_items = get_ad_items_by_ad_position_code(act.open_ad_code)
  129. if !ad_items.blank?
  130. ad_items.each do |ad_item|
  131. click_count = get_click_times_by_ad_item(ad_item)
  132. click_open_ad_count = click_open_ad_count + click_count
  133. show_count = get_show_times_by_ad_item(ad_item)
  134. show_open_ad_count = show_open_ad_count + show_count
  135. end
  136. end
  137. end
  138. click_banner_ad_count = 0
  139. if !act.banner_ad_code.blank?
  140. ad_items = get_ad_items_by_ad_position_code(act.banner_ad_code)
  141. if !ad_items.blank?
  142. ad_items.each do |ad_item|
  143. click_count = get_click_times_by_ad_item(ad_item)
  144. click_banner_ad_count = click_banner_ad_count + click_count
  145. end
  146. end
  147. end
  148. unit_price = cost/((click_open_ad_count + click_banner_ad_count) * 1.0)
  149. unit_price = format("%.2f",unit_price).to_f
  150. act_info = Hash.new
  151. act_info = Hash[:title => act.title, :count => act.stock, :join_user_count => join_user_count, :kj_count =>kj_count, :show_open_ad_count => show_open_ad_count, :click_open_ad_count => click_open_ad_count, :click_banner_ad_count => click_banner_ad_count, :cost => cost, :unit_price => unit_price, :success_kj_count => success_kj_count, :success_order_count => success_order_count, :share_timeline_count => share_timeline_count, :share_group_count => share_group_count]
  152. return act_info
  153. end
  154. end
  155. # 通过广告位标识符获取所有广告项
  156. def get_ad_items_by_ad_position_code(code)
  157. ad_position = AdPosition.where("code = ?", code).first
  158. if !ad_position.blank?
  159. id = ad_position.id
  160. sql = "select id from d5c_ad_items where ad_position_id = #{id}"
  161. ad_items = AdItem.find_by_sql(sql)
  162. if !ad_items.blank?
  163. return ad_items
  164. end
  165. end
  166. end
  167. #通过广告项获取点击次数
  168. def get_click_times_by_ad_item(item)
  169. count = 0
  170. AdItemClickStatistic.select("ad_item_id, click_times").where("ad_item_id = ?", item.id).each do |u|
  171. count = count + u.click_times
  172. end
  173. return count
  174. end
  175. # 通过广告项获取展示次数
  176. def get_show_times_by_ad_item(item)
  177. count = 0
  178. AdItemShowStatistic.select("ad_item_id, show_times").where("ad_item_id = ?", item.id).each do |u|
  179. count = count + u.show_times
  180. end
  181. return count
  182. end
  183. end