report_controller.rb 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. # encoding:utf-8
  2. class ReportController < BaseController
  3. before_filter :authenticate_admin_user!
  4. def index
  5. item = Project.select("sum(total_crowd_funding)/100 as s").where("state='success'")
  6. @total_investment = item[0].blank? ? 0 : item[0].s
  7. @user_amount = User.count()
  8. item = ReportWeixinUser.where("appid='superd5c'").last
  9. @superd5c_weixin_user_amount = item.blank? ? 0 : item.amount
  10. item = ReportWeixinUser.where("appid='d5ctdyh'").last
  11. @d5ctdyh_weixin_user_amount = item.blank? ? 0 : item.amount
  12. item = ReportWeixinUser.where("appid='diwutegongdui'").last
  13. @tegongdui_weixin_user_amount = item.blank? ? 0 : item.amount
  14. @now = Time.now
  15. @user_total_counts = User.count()
  16. year()
  17. month()
  18. day()
  19. # if params[:t]!="123"
  20. # render :text => "sorry"
  21. # return
  22. # end
  23. render :layout => false
  24. end
  25. #按年统计
  26. def year
  27. current_month_first_day = Time.new(@now.year, @now.month, @now.day)
  28. current_month_first_month = Time.new(@now.year, @now.month)
  29. current_month = Time.new(@now.year, @now.month)
  30. first_month = current_month - 5.months
  31. @liucun = WxUser.where("subscribe_time>=? and subscribe=1",current_month_first_month.to_i).count()
  32. @total_sub = WxUser.where("subscribe_time>=?",current_month_first_month.to_i).count()
  33. @ms = []
  34. @months = []
  35. @month_users = {}
  36. @month_valid_users = {}
  37. @month_projects = {}
  38. @month_invest_users = {} #月投资
  39. @month_balance_money = {} #月充值
  40. @month_take_cash = {} #月提现
  41. @month_money = {}
  42. @month_valid_users = {} #新投资人
  43. 6.times do |i|
  44. m = (first_month + (i <2 ? i.month : i.months)).month
  45. @ms << m
  46. @months << "#{m}月"
  47. @month_users[m] = 0
  48. @month_valid_users[m] = 0
  49. @month_projects[m] = 0
  50. @month_invest_users[m] = 0
  51. @month_take_cash[m] = 0
  52. @month_balance_money[m] = 0
  53. @month_money[m] = 0
  54. p m
  55. end
  56. #用户增长
  57. sql = "select date_format(date_add(created_at,interval 8 hour),'%Y%m')ym ,count(*) as c,date_format(date_add(created_at,interval 8 hour), '%m')m from d5c_users where created_at < '#{current_month+1.month}' and created_at >='#{first_month}' group by ym order by ym asc "
  58. User.find_by_sql(sql).each do |u|
  59. @month_users[u.m.to_i] = u.c.to_i
  60. end
  61. # 新投资人增长
  62. sql = "select date_format(from_unixtime(pj.min_paied_at),'%Y%m')ym, date_format(from_unixtime(pj.min_paied_at),'%m')m, count(*) as c from (select j.user_id, min(j.paied_at) as min_paied_at from d5c_project_joins j, d5c_projects p where j.project_id = p.id and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0 and p.ptype = 'stock' group by user_id order by min_paied_at desc) pj where pj.min_paied_at >= #{first_month.to_i} and pj.min_paied_at < #{(current_month+1.month).to_i} group by ym order by ym desc"
  63. ProjectJoin.find_by_sql(sql).each do |u|
  64. @month_valid_users[u.m.to_i] = u.c.to_i
  65. end
  66. #粉丝净增
  67. @month_wx_users_fuwuhao = get_report_weixin_users_by_period("superd5c", first_month, current_month, false)
  68. @month_wx_users_dingyuehao = get_report_weixin_users_by_period("d5ctdyh", first_month, current_month, false)
  69. @month_wx_users_tegongdui = get_report_weixin_users_by_period("diwutegongdui", first_month, current_month, false)
  70. #粉丝新增
  71. @month_wx_users_fuwuhao_newly = get_report_weixin_users_by_period("superd5c", first_month, current_month, true)
  72. @month_wx_users_dingyuehao_newly = get_report_weixin_users_by_period("d5ctdyh", first_month, current_month, true)
  73. @month_wx_users_tegongdui_newly = get_report_weixin_users_by_period("diwutegongdui", first_month, current_month, true)
  74. #当月新增
  75. current_month_to_i = (@now.month).to_i
  76. @current_month_fuwuhao_increase_count = @month_wx_users_fuwuhao_newly[current_month_to_i].to_i
  77. @current_month_dingyuehao_increase_count = @month_wx_users_dingyuehao_newly[current_month_to_i].to_i
  78. @current_month_tegongdui_increase_count = @month_wx_users_tegongdui_newly[current_month_to_i].to_i
  79. @current_month_user_increase_count = @month_users[current_month_to_i].to_i
  80. @total_increase_user_count = @current_month_fuwuhao_increase_count + @current_month_dingyuehao_increase_count + @current_month_tegongdui_increase_count + @current_month_user_increase_count
  81. #上线项目
  82. # Project.select("date_format(invest_start_at, '%m') month, date_format(invest_start_at, '%Y%m') ym, count(*) as c")
  83. # .where("invest_start_at < ? and invest_start_at > ? and state='success'", current_month_first_day+1.month, first_month).group("ym").order("ym").each do |u|
  84. # @month_projects[u.month.to_i] = u.c.to_i
  85. # end
  86. # # 投资人数
  87. # ProjectJoin.select("date_format(created_at, '%m') month, count(distinct user_id) as c")
  88. # .where("paied_at < ? and paied_at > ? and state=1 and is_refunded=0 and is_deleted=0", (current_month_first_day+1.month).to_i, first_month.to_i).group("month").order("month").each do |u|
  89. # @month_invest_users[u.month.to_i] = u.c.to_i
  90. # end
  91. # 充值金额
  92. sql = "select date_format(from_unixtime(paied_at),'%Y%m')ym, date_format(from_unixtime(paied_at),'%m')m,sum(total_price) as c from d5c_balance_orders where state = 1 and paied_at >= #{first_month.to_i} and paied_at < #{(current_month+1.month).to_i} group by ym order by ym desc"
  93. BalanceOrder.find_by_sql(sql).each do |u|
  94. @month_balance_money[u.m.to_i] = u.c.to_f/100
  95. end
  96. # # 提现订单
  97. # TakeCashOrder.select("date_format(created_at, '%m') month, date_format(created_at, '%Y%m') ym, sum(count) as c")
  98. # .where("paied_at < ? and paied_at > ? and pay_state = 2 and paied_at >0", (current_month_first_day+1.month).to_i, first_month.to_i).group("ym").order("ym").each do |u|
  99. # @month_take_cash[u.month.to_i] = u.c.to_f/100
  100. # end
  101. # DATE_FORMAT(FROM_UNIXTIME(paied_at),'%Y%m%d')PYMD
  102. #融资总额情况
  103. sql = "select date_format(from_unixtime(j.paied_at),'%Y%m')ym, " +
  104. "date_format(from_unixtime(j.paied_at),'%m')m, sum(j.total_price) as c " +
  105. "from d5c_project_joins j inner join d5c_projects " +
  106. "on j.state=1 and j.is_refunded=0 and j.is_deleted=0 " +
  107. "and j.paied_at >= #{first_month.to_i} and j.paied_at < #{(current_month+1.month).to_i} "+
  108. "and d5c_projects.ptype='stock' and j.project_id=d5c_projects.id " +
  109. "group by ym order by ym desc"
  110. ProjectJoin.find_by_sql(sql).each do |u|
  111. @month_money[u.m.to_i] = u.c.to_f/100
  112. end
  113. # #数据太难看
  114. # if params[:full].to_i != 1
  115. # i = 11-@now.month.to_i
  116. # @months = @months[i..-1]
  117. # @month_users.each do |k,v|
  118. # if not @months.include?("#{k}月")
  119. # @month_users.delete(k)
  120. # end
  121. # end
  122. # @month_valid_users.each do |k,v|
  123. # if not @months.include?("#{k}月")
  124. # @month_valid_users.delete(k)
  125. # end
  126. # end
  127. # @month_projects.each do |k,v|
  128. # if not @months.include?("#{k}月")
  129. # @month_projects.delete(k)
  130. # end
  131. # end
  132. # @month_money.each do |k,v|
  133. # if not @months.include?("#{k}月")
  134. # @month_money.delete(k)
  135. # end
  136. # end
  137. # @month_wx_users_fuwuhao = @month_wx_users_fuwuhao[i..-1]
  138. # @month_wx_users_dingyuehao = @month_wx_users_dingyuehao[i..-1]
  139. # @month_wx_users_fuwuhao_newly = @month_wx_users_fuwuhao_newly[i..-1]
  140. # @month_wx_users_dingyuehao_newly = @month_wx_users_dingyuehao_newly[i..-1]
  141. # @month_wx_users_tegongdui_newly = @month_wx_users_tegongdui_newly[i..-1]
  142. # end
  143. end
  144. #按月统计
  145. def month
  146. @ds = []
  147. @days = []
  148. @day_users = {}
  149. @day_valid_users = {}
  150. @day_projects = {}
  151. @day_invest_users = {}
  152. @day_balance_money = {}
  153. @day_take_cash = {}
  154. @day_money = {}
  155. @now.day.to_i.times do |j|
  156. i = j + 1
  157. @ds << i
  158. @days << "#{i}号"
  159. @day_users[i] = 0
  160. @day_valid_users[i] = 0
  161. @day_invest_users[i] = 0
  162. @day_balance_money[i] = 0
  163. @day_take_cash[i] = 0
  164. @day_money[i] = 0
  165. end
  166. # #注册用户增长
  167. # # User.select("date_format(created_at, '%m') month, date_format(created_at, '%Y%m') ym, count(*) as c")
  168. # # .where("created_at < ? and created_at > ?", current_month_first_day+1.month, first_month).group("ym").order("ym").each do |u|
  169. # # @month_users[u.month.to_i] = u.c.to_i
  170. # # end
  171. # User.select("date_format(created_at, '%d') day, count(*) as c")
  172. # .where("date_format(created_at, '%Y%c') = ?", "#{@now.year}#{@now.month}").group("day").order("day").each do |u|
  173. # @day_users[u.day.to_i] = u.c.to_i
  174. # end
  175. # # 新投资人增长
  176. # valid_user_ids = ProjectJoin.where("state=1 and is_refunded =0 and is_deleted=0").collect {|e| e.user_id}
  177. # User.select("date_format(created_at, '%d') day, count(*) as c")
  178. # .where("date_format(created_at, '%Y%c') = ? and id in (?)", "#{@now.year}#{@now.month}",valid_user_ids).group("day").order("day").each do |u|
  179. # @day_valid_users[u.day.to_i] = u.c.to_i
  180. # end
  181. # #统计粉丝
  182. # @day_wx_users_fuwuhao = get_report_weixin_users_monthly("superd5c",@ds)
  183. # @day_wx_users_dingyuehao = get_report_weixin_users_monthly("d5ctdyh",@ds)
  184. # #粉丝新增长
  185. # @day_wx_users_fuwuhao_newly = get_report_weixin_users_monthly("superd5c", @ds, true)
  186. # @day_wx_users_dingyuehao_newly = get_report_weixin_users_monthly("d5ctdyh", @ds, true)
  187. # @day_wx_users_tegongdui_newly = get_report_weixin_users_monthly("diwutegongdui", @ds, true)
  188. # #上线项目
  189. # # Project.select("date_format(invest_start_at, '%m') month, date_format(invest_start_at, '%Y%m') ym, count(*) as c")
  190. # # .where("invest_start_at < ? and invest_start_at > ? and state='success'", current_month_first_day+1.month, first_month).group("ym").order("ym").each do |u|
  191. # # @month_projects[u.month.to_i] = u.c.to_i
  192. # # end
  193. # # 投资人数distinct user_id
  194. # ProjectJoin.select("date_format(created_at, '%d') day, count(*) as c")
  195. # .where("date_format(FROM_UNIXTIME(paied_at), '%Y%c') = ? and state=1 and is_refunded=0 and is_deleted=0", "#{@now.year}#{@now.month}").group("day").order("day").each do |u|
  196. # @day_invest_users[u.day.to_i] = u.c.to_i
  197. # end
  198. # # # 充值金额
  199. # # BalanceOrder.select("date_format(created_at, '%d') day, sum(total_price) as c")
  200. # # .where("date_format(created_at, '%Y%c') = ? and state = ?", "#{@now.year}#{@now.month}",true).group("day").order("day").each do |u|
  201. # # @day_balance_money[u.day.to_i] = u.c.to_f/100
  202. # # end
  203. # # # 提现订单
  204. # # TakeCashOrder.select("date_format(created_at, '%d') day, sum(count) as c")
  205. # # .where("date_format(created_at, '%Y%c') = ? and pay_state = 2 and paied_at > 0", "#{@now.year}#{@now.month}" ).group("day").order("day").each do |u|
  206. # # @month_take_cash[u.day.to_i] = u.c.to_f/100
  207. # # end
  208. # #融资总额情况
  209. # ProjectJoin.select("date_format(created_at, '%d') day, sum(total_price) as c")
  210. # .where("date_format(FROM_UNIXTIME(paied_at), '%Y%c') = ? and state=1 and is_refunded=0 and is_deleted=0", "#{@now.year}#{@now.month}").group("day").order("day").each do |u|
  211. # @day_money[u.day.to_i] = u.c.to_f/100
  212. # end
  213. end
  214. # 按日统计
  215. def day
  216. first = @now - 1.day
  217. # first_day = Time.new(@now.year, @now.month, @now.day-1)
  218. first_day = Time.new(first.year, first.month, first.day)
  219. end_day = Time.new(@now.year, @now.month, @now.day)
  220. first_hour = first_day + 16.hours
  221. end_hour = end_day +16.hours
  222. @hs = []
  223. @hours = []
  224. @hour_users = {}
  225. @hour_valid_users = {}
  226. @hour_projects = {}
  227. @hour_invest_users = {}
  228. @hour_balance_money = {}
  229. @hour_take_cash = {}
  230. @hour_money = {}
  231. t = 0
  232. if @now.hour !=24
  233. t = @now.hour.to_i+1
  234. else
  235. t = 1
  236. end
  237. t.times do |i|
  238. @hs << i
  239. @hours << "#{i}时"
  240. @hour_users[i] = 0
  241. @hour_valid_users[i] = 0
  242. @hour_invest_users[i] = 0
  243. @hour_balance_money[i] = 0
  244. @hour_take_cash[i] = 0
  245. @hour_money[i] = 0
  246. end
  247. # # @report_users_daily_data = ReportUser.where("year=? and month=? and day=?",year, month, day).order("hour").collect do |u|
  248. # # u.added_count
  249. # # end
  250. # #注册用户增长
  251. # User.select("date_format(created_at, '%H') hour, count(*) as c")
  252. # .where("UNIX_TIMESTAMP(created_at) >= ? and UNIX_TIMESTAMP(created_at) < ?", (first_hour).to_i,(end_hour).to_i).group("hour").order("hour").each do |u|
  253. # t = u.hour.to_i
  254. # t = t>16 ? (t-16):(t+8)
  255. # @hour_users[t] = u.c.to_i
  256. # end
  257. # #新投资人增长
  258. # valid_user_ids = ProjectJoin.where("state=1 and is_refunded =0 and is_deleted=0").collect {|e| e.user_id}
  259. # User.select("date_format(created_at, '%H') hour, count(*) as c")
  260. # .where("UNIX_TIMESTAMP(created_at) >= ? and UNIX_TIMESTAMP(created_at) < ? and id in (?)", (first_hour).to_i,(end_hour).to_i,valid_user_ids).group("hour").order("hour").each do |u|
  261. # t = u.hour.to_i
  262. # t = t>16 ? (t-16):(t+8)
  263. # @hour_valid_users[t] = u.c.to_i
  264. # end
  265. # # #投资人数(计算所有用户)
  266. # # ProjectJoin.select("date_format(created_at, '%H') hour, count(distinct user_id) as c")
  267. # # .where("date_format(FROM_UNIXTIME(paied_at), '%Y%c') = ? and date_format(FROM_UNIXTIME(paied_at), '%e') = ? and state=1 and is_refunded=0 and is_deleted=0", "#{@now.year}#{@now.month}","#{@now.day}").group("hour").order("hour").each do |u|
  268. # # @hour_invest_users[u.hour.to_i] = u.c.to_i
  269. # # end
  270. # ProjectJoin.select("date_format(created_at, '%H') hour, count(distinct user_id) as c")
  271. # .where("paied_at >= ? and paied_at < ? and state=1 and is_refunded=0 and is_deleted=0", (first_hour).to_i, (end_hour).to_i ).group("hour").order("hour").each do |u|
  272. # t = u.hour.to_i
  273. # t = t>16 ? (t-16):(t+8)
  274. # @hour_invest_users[t] = u.c.to_i
  275. # end
  276. # #充值金额
  277. # BalanceOrder.select("date_format(created_at, '%H') hour, sum(total_price) as c")
  278. # .where("UNIX_TIMESTAMP(created_at) >= ? and UNIX_TIMESTAMP(created_at) < ? and state = 1 and paied_at > 0", (first_hour).to_i,(end_hour).to_i).group("hour").order("hour").each do |u|
  279. # t = u.hour.to_i
  280. # t = t>16 ? (t-16):(t+8)
  281. # @hour_balance_money[t] = u.c.to_f/100
  282. # end
  283. # #提现金额
  284. # # TakeCashOrder.select("date_format(created_at, '%H') hour, sum(count) as c")
  285. # # .where("date_format(created_at, '%Y%c') = ? and date_format(created_at, '%e') = ? and pay_state=2 and paied_at > 0", "201612","6").group("hour").order("hour").each do |u|
  286. # # @hour_take_cash[u.hour.to_i] = u.c.to_f/100
  287. # # end
  288. # TakeCashOrder.select("date_format(created_at, '%H') hour, sum(count) as c")
  289. # .where("UNIX_TIMESTAMP(created_at) >= ? and UNIX_TIMESTAMP(created_at) < ? and pay_state=2 and paied_at > 0", (first_hour).to_i,(end_hour).to_i).group("hour").order("hour").each do |u|
  290. # t = u.hour.to_i
  291. # t = t>16 ? (t-16):(t+8)
  292. # @hour_take_cash[t] = u.c.to_f/100
  293. # end
  294. # # #投资金额
  295. # ProjectJoin.select("date_format(created_at, '%H') hour, sum(total_price) as c")
  296. # .where("paied_at >= ? and paied_at < ? and state=1 and is_refunded=0 and is_deleted=0", (first_hour).to_i,(end_hour).to_i).group("hour").order("hour").each do |u|
  297. # t = u.hour.to_i
  298. # t = t>16 ? (t-16):(t+8)
  299. # @hour_money[t] = u.c.to_f/100
  300. # end
  301. end
  302. # 注册、关注统计
  303. def user
  304. ##注:页面参数为time,输入格式"2016-12-12"或"2016-12"
  305. @now = Time.now
  306. @this_day = Time.new(@now.year, @now.month, @now.day)
  307. @last_day = @this_day - 1.day
  308. @check_day = Time.new(@last_day.year, @last_day.month, @last_day.day) #默认展示上一天
  309. @check_month = Time.new(@this_day.year,@this_day.month) #这一个月
  310. if !params[:time].blank?
  311. s = params[:time].split("-")
  312. if s.length < 3
  313. @check_day = Time.new(@last_day.year, @last_day.month, @last_day.day)
  314. else
  315. @check_day = Time.new(s[0],s[1],s[2])
  316. end
  317. if s.length > 1
  318. @check_month = Time.new(s[0],s[1])
  319. end
  320. end
  321. #总用户 --user
  322. @total_user = User.count()
  323. #当月用户增长月查询 --user
  324. @monthly_users = User.where("? <= created_at and created_at<?",@check_month,@check_month+1.month)
  325. @monthly_increase_users = @monthly_users.count()
  326. # 当日新增用户 --user
  327. @daily_users = User.where("? <= created_at and created_at<?",@check_day,@check_day+1.day)
  328. @daily_increase_users = @daily_users.count()
  329. #当日各渠道用户增长 --user
  330. @daily_user_groups = @daily_users.select("signup_channel_id, count(*) as c").group("signup_channel_id").order("c desc")
  331. #当月各渠道用户增长 --user
  332. @monthly_user_groups = @monthly_users.select("signup_channel_id, count(*) as c").group("signup_channel_id").order("c desc")
  333. # ------------------------微信月统计------------------------------
  334. # 总微信用户 --wx
  335. @total_wx_user = ReportWeixinUser.where("appid = ?","superd5c").last.amount
  336. #当月微信用户净增长--wx--report_wx_user
  337. @monthly_increase_wx_users = 0
  338. ReportWeixinUser.select("added_count-canceled_count as count").where("year= ? and month=? and appid=? ", @check_month.strftime("%Y").to_i, @check_month.strftime("%m").to_i,"superd5c").each do |u|
  339. @monthly_increase_wx_users += u.count
  340. end
  341. #当月新增--report_wx_user
  342. @monthly_wx_user = 0
  343. ReportWeixinUser.select("added_count as count").where("year= ? and month=? and appid=? ", @check_month.strftime("%Y").to_i, @check_month.strftime("%m").to_i,"superd5c").each do |u|
  344. @monthly_wx_user += u.count.to_i
  345. end
  346. #当月新增--wx_user
  347. m_sub_user = WxUser.where("(? <= subscribe_time and subscribe_time < ?) and (channel_qrcode_id is not null and channel_qrcode_id != 0)",@check_month.to_i, (@check_month+1.month).to_i)
  348. @m_sub_user_count = m_sub_user.count()
  349. m_sub_other = WxUser.where("(? <= subscribe_time and subscribe_time < ?) and (channel_qrcode_id is null or channel_qrcode_id = 0)",@check_month.to_i,(@check_month+1.month).to_i)
  350. @m_sub_other_count = m_sub_other.count()
  351. #当月取消关注人数
  352. #相关二维码ID为空或者0
  353. m_unsub_user = WxUser.where("(? <= unsubscribe_time and unsubscribe_time < ?) and (channel_qrcode_id is not null and channel_qrcode_id != 0)",@check_month.to_i,(@check_month+1.month).to_i)
  354. @m_unsub_user_count = m_unsub_user.count()
  355. m_unsub_other = WxUser.where("(? <= unsubscribe_time and unsubscribe_time < ?) and (channel_qrcode_id is null or channel_qrcode_id = 0)",@check_month.to_i,(@check_month+1.month).to_i)
  356. @m_unsub_other_count = m_unsub_other.count()
  357. #当月渠道“其他"净增
  358. @m_other_net_increase = @m_sub_other_count - @m_unsub_other_count
  359. #当月总净增
  360. @m_net_increase = @m_sub_user_count - @m_unsub_user_count + @m_other_net_increase
  361. # 当月取消关注人数--分渠道
  362. @m_unsub_groups = []
  363. @m_unsub_cq_ids = []
  364. m_unsub_user.select("channel_qrcode_id, count(*) as c").group("channel_qrcode_id").order("c desc").each do |u|
  365. r = getChannelQrcodeRemark(u.channel_qrcode_id)
  366. @m_unsub_cq_ids << u.channel_qrcode_id #取消关注的用户二维码ID分布
  367. @m_unsub_groups << {:channel_qrcode => r, :count => u.c, :channel_qrcode_id => u.channel_qrcode_id}
  368. end
  369. #当月各渠道关注--wx_user
  370. @m_sub_groups = []
  371. @m_sub_cq_ids = []
  372. m_sub_user.select("channel_qrcode_id, count(*) as c").group("channel_qrcode_id").order("c desc").each do |u|
  373. r = getChannelQrcodeRemark(u.channel_qrcode_id)
  374. net_increase = 0 #净增
  375. unsub = m_unsub_user.where("channel_qrcode_id = ?", u.channel_qrcode_id)
  376. net_increase = u.c - unsub.count()
  377. @m_sub_cq_ids << u.channel_qrcode_id
  378. @m_sub_groups << {:channel_qrcode => r, :count => u.c, :channel_qrcode_id => u.channel_qrcode_id, :net_increase => net_increase}
  379. end
  380. m_all_cq_ids = @m_unsub_cq_ids | @m_sub_cq_ids
  381. @m_res_cq_ids = m_all_cq_ids - @m_sub_cq_ids
  382. @m_res_groups = []
  383. @m_res_cq_ids.each do |c_id|
  384. r = getChannelQrcodeRemark(c_id)
  385. r_uncub = 0
  386. @m_unsub_groups.each do |u|
  387. if u[:channel_qrcode_id] == c_id
  388. r_uncub = 0 - u[:count]
  389. end
  390. end
  391. @m_res_groups << {:channel_qrcode => r, :count => r_uncub, :channel_qrcode_id => c_id}
  392. end
  393. # ------------------------微信日统计--------------------------------
  394. #当日取消关注人数--wx_user
  395. d_unsub_user = WxUser.where("(? <= unsubscribe_time and unsubscribe_time < ?) and (channel_qrcode_id is not null and channel_qrcode_id != 0)",@check_day.to_i,(@check_day+1.day).to_i)
  396. @d_unsub_user_count= d_unsub_user.count()
  397. d_unsub_other = WxUser.where("(? <= unsubscribe_time and unsubscribe_time < ?) and (channel_qrcode_id is null or channel_qrcode_id = 0)",@check_day.to_i, (@check_day+1.day).to_i)
  398. @d_unsub_other_count = d_unsub_other.count()
  399. #当日新增关注人数--wx_user
  400. d_sub_user = WxUser.where("(? <= subscribe_time and subscribe_time < ?) and (channel_qrcode_id is not null and channel_qrcode_id != 0)",@check_day.to_i, (@check_day+1.day).to_i)
  401. @d_sub_user_count = d_sub_user.count()
  402. d_sub_other = WxUser.where("(? <= subscribe_time and subscribe_time < ?) and (channel_qrcode_id is null or channel_qrcode_id = 0)",@check_day.to_i,(@check_day+1.day).to_i)
  403. @d_sub_other_count = d_sub_other.count()
  404. #当日渠道“其他”净增
  405. @d_other_net_increase = @d_sub_other_count - @d_unsub_other_count
  406. #当日净增--wx_user
  407. @d_net_increase = @d_sub_user_count - @d_unsub_user_count + @d_other_net_increase
  408. # 当天各渠道取消关注--wx_user(渠道--新增--渠道ID)
  409. @d_unsub_groups = []
  410. @d_unsub_cq_ids = []
  411. d_unsub_user.select("channel_qrcode_id, count(*) as c").group("channel_qrcode_id").order("c desc").each do |u|
  412. r = getChannelQrcodeRemark(u.channel_qrcode_id)
  413. @d_unsub_cq_ids << u.channel_qrcode_id
  414. @d_unsub_groups << {:channel_qrcode => r, :count => u.c, :channel_qrcode_id => u.channel_qrcode_id}
  415. end
  416. #当天各渠道关注--wx_user(渠道--新增--渠道ID)
  417. @d_sub_groups = []
  418. @d_sub_cq_ids = []
  419. d_sub_user.select("channel_qrcode_id, count(*) as c").group("channel_qrcode_id").order("c desc").each do |u|
  420. r = getChannelQrcodeRemark(u.channel_qrcode_id)
  421. net_increase = 0
  422. unsub = d_unsub_user.where("channel_qrcode_id = ?", u.channel_qrcode_id)
  423. net_increase = u.c - unsub.count()
  424. @d_sub_cq_ids << u.channel_qrcode_id
  425. @d_sub_groups <<{:channel_qrcode => r, :count => u.c, :channel_qrcode_id => u.channel_qrcode_id, :net_increase => net_increase}
  426. end
  427. d_all_cq_ids = @d_unsub_cq_ids | @d_sub_cq_ids
  428. @d_res_cq_ids = d_all_cq_ids - @d_sub_cq_ids
  429. @d_res_groups = []
  430. @d_res_cq_ids.each do |c_id|
  431. r = getChannelQrcodeRemark(c_id)
  432. r_uncub = 0
  433. @d_unsub_groups.each do |u|
  434. if u[:channel_qrcode_id] == c_id
  435. r_uncub = 0 - u[:count]
  436. end
  437. end
  438. @d_res_groups << {:channel_qrcode => r, :count => r_uncub, :channel_qrcode_id => c_id}
  439. end
  440. end
  441. #新手训练营参加者是否投资
  442. def investor_from_train
  443. p_id = params[:p_id].to_i || 0
  444. t_id = params[:t_id].to_i || 0
  445. @investor_list = []
  446. sql = "select distinct joins.user_id,u.real_name,u.tel from d5c_users u,d5c_projects pros, d5c_project_joins joins where u.id = joins.user_id and pros.id = joins.project_id and pros.ptype = 'stock' and joins.state = 1 and joins.is_deleted = 0 and joins.is_refunded = 0 and joins.user_id in (select distinct j.user_id from d5c_project_joins j, d5c_projects p where j.project_id = p.id and p.ptype = 'train' and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0) "
  447. if t_id > 0
  448. if p_id > 0
  449. sql = "select distinct joins.user_id,u.real_name,u.tel from d5c_users u,d5c_projects pros, d5c_project_joins joins where u.id = joins.user_id and pros.id = joins.project_id and pros.ptype = 'stock' and pros.id = #{p_id} and joins.state = 1 and joins.is_deleted = 0 and joins.is_refunded = 0 and joins.user_id in (select distinct j.user_id from d5c_project_joins j, d5c_projects p where j.project_id = p.id and p.ptype = 'train' and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0 and p.id = #{t_id}) "
  450. else
  451. sql = "select distinct joins.user_id,u.real_name,u.tel from d5c_users u,d5c_projects pros, d5c_project_joins joins where u.id = joins.user_id and pros.id = joins.project_id and pros.ptype = 'stock' and joins.state = 1 and joins.is_deleted = 0 and joins.is_refunded = 0 and joins.user_id in (select distinct j.user_id from d5c_project_joins j, d5c_projects p where j.project_id = p.id and p.ptype = 'train' and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0 and p.id = #{t_id}) "
  452. end
  453. else
  454. if p_id > 0
  455. sql = "select distinct joins.user_id,u.real_name,u.tel from d5c_users u,d5c_projects pros, d5c_project_joins joins where u.id = joins.user_id and pros.id = joins.project_id and pros.ptype = 'stock' and pros.id = #{p_id} and joins.state = 1 and joins.is_deleted = 0 and joins.is_refunded = 0 and joins.user_id in (select distinct j.user_id from d5c_project_joins j, d5c_projects p where j.project_id = p.id and p.ptype = 'train' and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0)"
  456. end
  457. end
  458. investors = User.find_by_sql(sql)
  459. investors.each do |u|
  460. # join_sql = "select distinct p.title from d5c_projects p,d5c_project_joins j where p.id = j.project_id and p.ptype = 'stock' and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0 and j.user_id = #{u.user_id}"
  461. # p_joins = Project.find_by_sql(join_sql).collect{|e| e.title}
  462. #投过的训练营项目
  463. train_sql = "select distinct j.project_id, p.title from d5c_projects p,d5c_project_joins j where p.id = j.project_id and p.ptype = 'train' and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0 and j.user_id = #{u.user_id}"
  464. invest_train = Project.find_by_sql(train_sql).collect{|e| "#{e.title}(#{e.project_id})"}
  465. @investor_list << {:name => u.real_name, :user_id => u.user_id, :tel => u.tel, :train_project => invest_train}
  466. end
  467. end
  468. # 客户关系列表
  469. def customer
  470. @url = "#{CONFIG_FILE["admin_host"]}/report/customer"
  471. @per_page = 200
  472. @page = 1
  473. if params[:page].to_i > 0
  474. @page = params[:page].to_i
  475. end
  476. if params[:per_page].to_i > 0
  477. @per_page = params[:per_page].to_i
  478. end
  479. # 四个参数客服微信ID,用户ID,用户微信ID,用户真实姓名,项目ID(投资记录)
  480. @cs_wx_uid = 0
  481. @user_id = 0
  482. @wx_uid = 0
  483. # @real_name = ""
  484. @p_id = 0
  485. if !params[:c].blank?
  486. if params[:c].to_i > 0
  487. @cs_wx_uid = params[:c].to_i
  488. end
  489. end
  490. if !params[:u_id].blank?
  491. if params[:u_id].to_i > 0
  492. @user_id = params[:u_id].to_i
  493. end
  494. end
  495. if !params[:wx_uid].blank?
  496. if params[:wx_uid].to_i > 0
  497. @wx_uid = params[:wx_uid].to_i
  498. end
  499. end
  500. # if !params[:name].blank?
  501. # @real_name = params[:name]
  502. # end
  503. if !params[:project_id].blank?
  504. @p_id = params[:project_id].to_i
  505. end
  506. # --------------------------------------------------------
  507. # sql = "select s.*,u.real_name,u.tel from (select c.*,w.nickname,w.user_id, wx.nickname as cs_nickname FROM d5c_customers c, d5c_wx_users w, d5c_wx_users wx where c.wx_uid = w.id and c.cs_wx_uid = wx.id) s left join d5c_users u on s.user_id = u.id"
  508. # sql = sql + " where s.cs_wx_uid >= 0"
  509. # if @cs_wx_uid > 0
  510. # sql = sql + " and s.cs_wx_uid = #{@cs_wx_uid}"
  511. # end
  512. # if @wx_uid > 0
  513. # sql = sql + " and s.wx_uid = #{@wx_uid}"
  514. # elsif @user_id > 0
  515. # sql = sql + " and u.id = #{@user_id}"
  516. # end
  517. # if @p_id > 0
  518. # sql = sql + " and s.user_id in (select distinct user_id from d5c_project_joins j where j.project_id = #{@p_id} and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0)"
  519. # end
  520. # --------------------------------------------------------
  521. sql = "select c.*,w.nickname,w.user_id, wx.nickname as cs_nickname from d5c_customers c, d5c_wx_users w, d5c_wx_users wx where c.wx_uid = w.id and c.cs_wx_uid = wx.id"
  522. if @cs_wx_uid > 0
  523. sql = sql + " and c.cs_wx_uid = #{@cs_wx_uid}"
  524. end
  525. if @wx_uid > 0
  526. sql = sql + " and c.wx_uid = #{@wx_uid}"
  527. elsif @user_id > 0
  528. sql = sql + " and w.user_id = #{@user_id}"
  529. end
  530. if @p_id > 0
  531. sql =sql + " and w.user_id in (select distinct user_id from d5c_project_joins j where j.project_id = #{@p_id} and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0)"
  532. end
  533. sql = "select info.*,u.real_name, u.tel from (#{sql}) info left join d5c_users u on info.user_id = u.id order by unix_timestamp(info.created_at) desc"
  534. # --------------------------------------------------------
  535. total_count_sql ="select count(*) as c from (#{sql}) table1"
  536. @total_count = 0
  537. t = Customer.find_by_sql(total_count_sql).first
  538. if !t.blank?
  539. @total_count = t.c.to_i
  540. end
  541. if @page > 0
  542. sql = sql + " limit #{(@page - 1) * @per_page} ,#{@per_page}"
  543. else
  544. sql = sql + " limit #{@per_page}"
  545. end
  546. @customer_list = Customer.find_by_sql(sql)
  547. # --------------------------分页------------------------
  548. # 总数量
  549. @current_page_count = @customer_list.length
  550. @total_pages = @total_count / @per_page
  551. if @total_count % @per_page > 0
  552. @total_pages += 1
  553. end
  554. @next_page = @page + 1
  555. @pre_page = @page - 1
  556. end
  557. def rhpf_survey
  558. #填写人数--------------------------------------------------
  559. @filled_user_count = 0
  560. #总星数----------------------------------------------------
  561. @total_star_nums = 0
  562. @survey = RhpfSurvey.where("push_time > ?",0).last
  563. @id = @survey.id
  564. if !params[:survey].blank?
  565. @id = params[:survey].to_i
  566. s = RhpfSurvey.where("id = ?", @id).first
  567. if !s.blank?
  568. @survey = s
  569. end
  570. end
  571. option_ids = @survey.options
  572. s = option_ids.split(",")
  573. #调查总星数
  574. @survey_total_stars = s.length * 5
  575. @option_info = []
  576. s.each do |u|
  577. id = u.to_i
  578. option = RhpfOption.where("id = ?", id).first
  579. ave = 0
  580. star_count = []
  581. (1..5).each do |i|
  582. count = 0
  583. result_by_star = RhpfResult.where("survey_id = ? and star_nums = ? and option_id = ?",@survey.id, i, id)
  584. count = result_by_star.count()
  585. star_count << count
  586. end
  587. total_star = star_count[0]*1+star_count[1]*2+star_count[2]*3+star_count[3]*4+star_count[4]*5
  588. @total_star_nums += total_star
  589. total_person = star_count[0]+star_count[1]+star_count[2]+star_count[3]+star_count[4]
  590. @filled_user_count = total_person
  591. ave = total_star/(total_person*1.0)
  592. ave = format("%.2f",ave).to_f
  593. @option_info << {:option => option.title, :survey => @survey.title, :one => star_count[0], :two => star_count[1], :three => star_count[2], :four => star_count[3], :five => star_count[4], :ave => ave}
  594. end
  595. #平均总星数------------------------------------------------
  596. @ave_total_stars = @total_star_nums/(@filled_user_count * 1.0)
  597. @ave_total_stars = format("%.2f", @ave_total_stars).to_f
  598. #推送人数--------------------------------------------------
  599. @push_user_count = 0
  600. pids = @survey.projects
  601. projectIds = pids.split(",")
  602. sql_p_ids = ""
  603. if !@survey.projects.blank?
  604. sql_p_ids = "("+@survey.projects+")"
  605. end
  606. if projectIds.length != 0
  607. sql = "select distinct user_id from d5c_project_joins where state=1 and is_refunded=0 and is_deleted=0 and project_id in #{sql_p_ids}"
  608. invest_user = ProjectJoin.find_by_sql(sql)
  609. @push_user_count = invest_user.count()
  610. else
  611. sql = "select distinct user_id from d5c_project_joins where state=1 and is_refunded=0 and is_deleted=0"
  612. invest_user = ProjectJoin.find_by_sql(sql)
  613. invest_user_count = invest_user.count()
  614. @push_user_count = invest_user_count
  615. end
  616. result_sql = "select sum(star_nums) as t,wx_uid from d5c_rhpf_results where survey_id = #{@survey.id} group by wx_uid"
  617. @users = RhpfResult.find_by_sql(result_sql)
  618. @not_null_comments = []
  619. @users.each do |u|
  620. pass = 0
  621. if u.t >= @survey.pass_stars
  622. pass = 1
  623. end
  624. t = ""
  625. result = RhpfResult.where("wx_uid = ? and survey_id = ?",u.wx_uid, @survey.id).first
  626. if !result.blank?
  627. t = result.comment
  628. end
  629. if !t.blank?
  630. wx_user = WxUser.where("id = ?",u.wx_uid).first
  631. if !wx_user.blank?
  632. usr = ""
  633. u_id = 0
  634. if !wx_user.user_id.blank? &&wx_user.user_id > 0
  635. u_id = wx_user.user_id
  636. user = User.where("id = ?",wx_user.user_id).first
  637. if !user.blank?
  638. usr = user.real_name
  639. end
  640. end
  641. @not_null_comments << {:wx_user => wx_user.nickname,:wx_uid => u.wx_uid, :comment => t,:pass => pass, :star => u.t, :u_id => wx_user.user_id, :usr => usr}
  642. end
  643. end
  644. end
  645. #-----------------------------分页----------------------------------------------------------
  646. #每页评论数量
  647. @total_pages = 0
  648. @per_page = 20
  649. @page = 1
  650. #总页数
  651. if !params[:per_page].blank?
  652. @per_page = params[:per_page].to_i
  653. end
  654. if !params[:page].blank?
  655. @page = params[:page].to_i
  656. end
  657. @next_page = @page + 1
  658. @pre_page = @page - 1
  659. if @pre_page < 1
  660. @pre_page = 1
  661. end
  662. @total_pages = (@not_null_comments.length/(@per_page * 1.0)).ceil
  663. @url = "#{CONFIG_FILE["admin_host"]}/report/rhpf_survey"
  664. @show_comments = []
  665. if @not_null_comments.length <= @per_page
  666. @show_comments = @not_null_comments
  667. elsif @not_null_comments.length <= (@per_page * (@page-1))
  668. @show_comments = @not_null_comments.first(@per_page)
  669. elsif @not_null_comments.length <= (@per_page * @page)
  670. @show_comments = @not_null_comments[(@per_page*(@page-1))..(@not_null_comments.length)]
  671. @next_page = 1
  672. else
  673. @show_comments = @not_null_comments[(@per_page*(@page-1)),@per_page]
  674. end
  675. end
  676. #成功项目,新老投资比
  677. def project_investor
  678. @p_id = 0
  679. if !params[:id].blank?
  680. @p_id = params[:id].to_i
  681. else
  682. #取最近众筹中或者众筹成功的项目
  683. states = ["underway","success"]
  684. @p_id = Project.where("state in (?)", states).last.id
  685. end
  686. p = Project.where("id = ?", @p_id).first
  687. @invest_user_infos_display = "block"
  688. @project_title = ""
  689. if !p.blank?
  690. @project_title = p.title
  691. if p.invest_version == 1
  692. @invest_user_infos_display = "none"
  693. end
  694. end
  695. #回报方式投资人
  696. sql = "select j.user_id,u.nickname,u.tel,u.email,u.real_name,u.tel,u.signup_channel_id,j.project_invest_way_id,w.title,sum(j.copies_count) as c, sum(j.total_price) as tp from d5c_project_joins j, d5c_users u,d5c_project_invest_ways w where j.state=1 and j.is_refunded=0 and j.is_deleted=0 and j.project_id=#{@p_id} and u.id=j.user_id and w.id=j.project_invest_way_id group by j.user_id, j.project_invest_way_id order by c desc,j.user_id asc;"
  697. @invest_user_infos = User.find_by_sql(sql)
  698. #该项目所有成功投资人
  699. investor_sql = "select DISTINCT user_id, project_id from d5c_project_joins where project_id = #{@p_id} and paied_at >0 and state = 1 and is_deleted = 0 and is_refunded = 0 order by user_id"
  700. investors = ProjectJoin.find_by_sql(investor_sql)
  701. investors_ids = investors.collect{|i| i.user_id}
  702. #总投资金额
  703. @total_invest_money = 0
  704. sql = "select sum(total_price) as c from d5c_project_joins where project_id = #{@p_id} and paied_at >0 and state = 1 and is_deleted = 0 and is_refunded = 0 "
  705. t = ProjectJoin.find_by_sql(sql).first
  706. if !t.blank?
  707. @total_invest_money = t.c.to_f/100.0
  708. end
  709. #所有相关注册渠道
  710. channel_ids = User.select("distinct signup_channel_id").where("id in (?)", investors_ids).collect{|i| i.signup_channel_id}
  711. #老投资人Id与数目
  712. @total_investor_count = investors.count()
  713. old_investor_ids = [] #老投资人id
  714. @old_investor_count = 0 #老投资人人数
  715. new_investor_ids = [] #新投资人id
  716. @new_investor_count = 0 #新投资人人数
  717. #新投资人
  718. investors_ids.each do |u|
  719. is = false
  720. is = is_old_investor(u, @p_id)
  721. if is
  722. old_investor_ids << u
  723. @old_investor_count += 1
  724. else
  725. new_investor_ids << u
  726. @new_investor_count += 1
  727. end
  728. end
  729. #老用户投资金额
  730. @old_invest_money = 0
  731. o = ProjectJoin.select("sum(total_price) as c").where("project_id = ? and paied_at >0 and state = 1 and is_deleted = 0 and is_refunded = 0 and user_id in (?)", @p_id, old_investor_ids).first
  732. if !o.blank?
  733. @old_invest_money = o.c.to_f/100.0
  734. end
  735. #新用户投资金额
  736. @new_invest_money = 0
  737. n = ProjectJoin.select("sum(total_price) as c").where("project_id = ? and paied_at >0 and state = 1 and is_deleted = 0 and is_refunded = 0 and user_id in (?)", @p_id, new_investor_ids).first
  738. if !n.blank?
  739. @new_invest_money = n.c.to_f/100.0
  740. end
  741. #根据每个渠道划分
  742. # 各渠道人数
  743. @total_investor_of_channel_count = []
  744. @rest_total_investor_count = @total_investor_count
  745. @old_investor_of_channel_count = []
  746. @rest_old_investor_count = @old_investor_count
  747. @new_investor_of_channel_count = []
  748. @rest_new_investor_count = @new_investor_count
  749. #各渠道投资金额
  750. @total_invest_money_of_channel = []
  751. @old_invest_money_of_channel = []
  752. @new_invest_money_of_channel = []
  753. @rest_total_invest_money = @total_invest_money
  754. @rest_old_invest_money = @old_invest_money
  755. @rest_new_invest_money = @new_invest_money
  756. @channels = []
  757. channel_ids.each do |u|
  758. c = SignUpChannel.where("id = ?", u).first
  759. if c.blank? || c.channel_value == nil
  760. next
  761. else
  762. @channels << c.channel_value
  763. end
  764. t = User.where("signup_channel_id = ? and id in (?)", u, investors_ids)
  765. t_count = t.count()
  766. t_ids = []
  767. if !t.blank?
  768. t.each do |u|
  769. t_ids << u.id
  770. end
  771. end
  772. t_invest = ProjectJoin.select("sum(total_price) as c").where("project_id = ? and paied_at >0 and state = 1 and is_deleted = 0 and is_refunded = 0 and user_id in (?)", @p_id, t_ids).first
  773. if !t_invest.blank?
  774. @total_invest_money_of_channel << t_invest.c.to_f/100.0
  775. @rest_total_invest_money -= t_invest.c.to_f/100.0
  776. end
  777. @total_investor_of_channel_count << t_count
  778. @rest_total_investor_count -= t_count
  779. o = User.where("signup_channel_id = ? and id in (?)", u, old_investor_ids)
  780. o_ids = []
  781. o_count = o.count()
  782. if !o.blank?
  783. o.each do |u|
  784. o_ids << u.id
  785. end
  786. end
  787. o_invest = ProjectJoin.select("sum(total_price) as c").where("project_id = ? and paied_at >0 and state = 1 and is_deleted = 0 and is_refunded = 0 and user_id in (?)", @p_id, o_ids).first
  788. if !o_invest.blank?
  789. @old_invest_money_of_channel << o_invest.c.to_f/100.0
  790. @rest_old_invest_money -= o_invest.c.to_f/100.0
  791. end
  792. @old_investor_of_channel_count << o_count
  793. @rest_old_investor_count -= o_count
  794. n = User.where("signup_channel_id = ? and id in (?)", u, new_investor_ids)
  795. n_ids = []
  796. n_count = n.count()
  797. if !n.blank?
  798. n.each do |u|
  799. n_ids << u.id
  800. end
  801. end
  802. n_invest = ProjectJoin.select("sum(total_price) as c").where("project_id = ? and paied_at >0 and state = 1 and is_deleted = 0 and is_refunded = 0 and user_id in (?)", @p_id, n_ids).first
  803. if !n_invest.blank?
  804. @new_invest_money_of_channel << n_invest.c.to_f/100.0
  805. @rest_new_invest_money -= n_invest.c.to_f/100.0
  806. end
  807. @new_investor_of_channel_count << n_count
  808. @rest_new_investor_count -= n_count
  809. end
  810. render :layout => false
  811. end
  812. #投资人渠道分布
  813. def investor_channel
  814. ##注:页面参数为time,输入格式"2016-12-12"或"2016-12"
  815. @now = Time.now
  816. @this_day = Time.new(@now.year, @now.month, @now.day)
  817. if !params[:time].blank?
  818. s = params[:time].split("-")
  819. if s.length < 3
  820. @this_day = Time.new(@this_day.year, @this_day.month, @this_day.day)
  821. else
  822. @this_day = Time.new(s[0],s[1],s[2])
  823. end
  824. end
  825. @final_day = @this_day + 1.day
  826. @total_investor_count = 0
  827. @users_channel_info = []
  828. sql = "select count(distinct s.user_id) as c, u.signup_channel_id from d5c_stock_balances s ,d5c_users u, d5c_projects p where s.user_id = u.id and s.project_id =p.id and p.ptype = 'stock' group by u.signup_channel_id order by c desc;"
  829. users = User.find_by_sql(sql)
  830. users.each do |u|
  831. @total_investor_count = @total_investor_count + u.c.to_i
  832. channel_value = get_channel_value_by_id(u.signup_channel_id)
  833. @users_channel_info << {:c_id => u.signup_channel_id, :c_value => channel_value, :count => u.c.to_i}
  834. end
  835. #7天体验金
  836. sql = "select signup_channel_id, count(*) as c from d5c_users where id in (select user_id from d5c_zt_invest_orders where state=1 and unix_timestamp(created_at) >= #{@this_day.to_i} and unix_timestamp(created_at) < #{@final_day.to_i} group by user_id) group by signup_channel_id order by c desc;"
  837. @current_seven_days_investors = User.find_by_sql(sql)
  838. sql = "select signup_channel_id, count(*) as c from d5c_users where id in (select user_id from d5c_zt_invest_orders where state=1 group by user_id) group by signup_channel_id order by c desc;"
  839. @seven_days_investors_info = []
  840. @seven_days_investors_count = 0
  841. User.find_by_sql(sql).each do |u|
  842. @seven_days_investors_count = @seven_days_investors_count + u.c.to_i
  843. channel_value = get_channel_value_by_id(u.signup_channel_id)
  844. current_investor_count = 0
  845. @current_seven_days_investors.each do |i|
  846. if i.signup_channel_id == u.signup_channel_id
  847. current_investor_count = i.c.to_i
  848. end
  849. end
  850. @seven_days_investors_info << {:c_id => u.signup_channel_id, :c_value => channel_value, :count => u.c.to_i, :current_investor_count => current_investor_count}
  851. end
  852. #1000元体验金
  853. sql = "select signup_channel_id, count(*) as c from d5c_users where id in (select user_id from d5c_project_joins where state=1 and pay_way = 'trial_coin' and (paied_at >= #{@this_day.to_i} and paied_at < #{@final_day.to_i}) group by user_id) group by signup_channel_id order by c desc;"
  854. @current_tyj_investors = User.find_by_sql(sql)
  855. sql = "select signup_channel_id, count(*) as c from d5c_users where id in (select user_id from d5c_project_joins where state=1 and pay_way = 'trial_coin' group by user_id) group by signup_channel_id order by c desc;"
  856. @tyj_investors_info = []
  857. @tyj_investors_count = 0
  858. User.find_by_sql(sql).each do |u|
  859. @tyj_investors_count = @tyj_investors_count + u.c.to_i
  860. channel_value = get_channel_value_by_id(u.signup_channel_id)
  861. current_investor_count = 0
  862. @current_tyj_investors.each do |i|
  863. if i.signup_channel_id == u.signup_channel_id
  864. current_investor_count = i.c.to_i
  865. end
  866. end
  867. @tyj_investors_info << {:c_id => u.signup_channel_id, :c_value => channel_value, :count => u.c.to_i, :current_investor_count => current_investor_count}
  868. end
  869. #新手训练营
  870. sql = "select u.signup_channel_id, count(distinct u.id) as c from d5c_users u, d5c_projects p, d5c_project_joins j where j.user_id = u.id and j.project_id = p.id and p.ptype = 'train' and j.paied_at >= #{@this_day.to_i} and j.paied_at < #{@final_day.to_i} group by u.signup_channel_id order by c desc"
  871. @current_day_train_investors = User.find_by_sql(sql)
  872. sql = "select u.signup_channel_id, count(distinct u.id) as c from d5c_users u, d5c_projects p, d5c_project_joins j where j.user_id = u.id and j.project_id = p.id and p.ptype = 'train' group by u.signup_channel_id order by c desc"
  873. @train_investors = []
  874. @train_investors_count = 0
  875. User.find_by_sql(sql).each do |u|
  876. @train_investors_count += u.c.to_i
  877. channel_value = get_channel_value_by_id(u.signup_channel_id)
  878. current_day_train_investor_count = 0
  879. @current_day_train_investors.each do |i|
  880. if i.signup_channel_id == u.signup_channel_id
  881. current_day_train_investor_count = i.c.to_i
  882. end
  883. end
  884. @train_investors << {:c_id => u.signup_channel_id, :c_value => channel_value, :count => u.c.to_i, :current_investor_count => current_day_train_investor_count}
  885. end
  886. #校验用户注册渠道
  887. #以二维码为准,根据时间先后,强制将注册渠道改为和二维码相匹配的渠道。如channel_qrcode_id=64是真功夫桌贴, signup_channel_id=41  是真功夫桌贴, u.created_at>w.created_at这个条件是关键
  888. sql = "select u.id,w.channel_qrcode_id from d5c_wx_users w,d5c_users u, d5c_sign_up_channels s where u.id=w.user_id and u.created_at>w.created_at and w.channel_qrcode_id >0 and (s.id!=u.signup_channel_id or u.signup_channel_id is null or u.signup_channel_id = 0) and s.channel_qrcode_id=w.channel_qrcode_id;"
  889. u = User.find_by_sql(sql)
  890. u.each do |i|
  891. signup_channel = SignUpChannel.where("channel_qrcode_id = ?", i.channel_qrcode_id).first
  892. if !signup_channel.blank?
  893. usr = get_user_by_id(i.id)
  894. if !usr.blank?
  895. usr.signup_channel_id = signup_channel.id
  896. usr.save
  897. end
  898. end
  899. end
  900. sql = "select u.id,w.channel_qrcode_id from d5c_wx_users w,d5c_users u where u.id=w.user_id and u.created_at>w.created_at and w.channel_qrcode_id >0 and (u.signup_channel_id=0 or u.signup_channel_id is NULL);"
  901. user_list = User.find_by_sql(sql)
  902. user_list.each do |i|
  903. c_q = ChannelQrcode.where("id = ?",i.channel_qrcode_id).first
  904. if !c_q.blank? && !c_q.sign_up_channel_id.blank? && c_q.sign_up_channel_id > 0
  905. usr = get_user_by_id(i.id)
  906. if !usr.blank?
  907. usr.signup_channel_id = c_q.sign_up_channel_id
  908. usr.save
  909. end
  910. end
  911. end
  912. end
  913. def monthly_cost
  914. @now = Time.now
  915. # current_month_first_day = Time.new(@now.year, @now.month, @now.day)
  916. current_month = Time.new(@now.year, @now.month)
  917. first_month = current_month - 11.months
  918. @ms = []
  919. @months = {}
  920. @month_article_cost = {}
  921. @month_train_club_cost = {}
  922. @month_trail_coin_cost = {}
  923. @month_zt_invest_cost = {}
  924. @month_zhuotie = {}
  925. #找到所有训练营项目
  926. sql = "select distinct p.id, p.total_crowd_funding, p.bonus_project_id, p.project_owner_has_crowd_funding from d5c_projects p, d5c_project_bonuses b where p.ptype = 'train' and b.project_id = p.id and t_rate > 100"
  927. train_projects = Project.find_by_sql(sql)
  928. 12.times do |i|
  929. mon = first_month + (i <2 ? i.month : i.months)
  930. m = mon.month
  931. @ms << m
  932. @months[m] = "#{mon.year}-#{m}"
  933. @month_article_cost[m] = 0
  934. @month_train_club_cost[m] = 0
  935. @month_trail_coin_cost[m] = 0
  936. @month_zt_invest_cost[m] = 0
  937. @month_zhuotie[m] = 0
  938. #计算训练营每月支出
  939. cost = 0
  940. train_projects.each do |u|
  941. b_id = u.id
  942. # if !u.bonus_project_id.blank? && u.bonus_project_id > 0
  943. # b_id = u.bonus_project_id
  944. # end
  945. rate = 0 * 1.0
  946. # 项目这个月累计分红大于100的最后一条记录
  947. sql = "select max(t_rate) as r from d5c_project_bonuses where project_id = #{b_id} and t_rate > 100 and date_add(b_time,interval 8 hour) >= '#{mon}' and date_add(b_time,interval 8 hour) < '#{mon + 1.month}'"
  948. bonus = ProjectBonus.find_by_sql(sql).first
  949. if !bonus.blank? && !bonus.nil? && !bonus.r.nil?
  950. sql = "select max(t_rate) as r from d5c_project_bonuses where project_id = #{b_id} and t_rate > 100 and date_add(b_time,interval 8 hour) < '#{mon}'"
  951. last_bonus = ProjectBonus.find_by_sql(sql).first
  952. if !last_bonus.blank? && !last_bonus.nil? && !last_bonus.r.nil?
  953. rate = bonus.r - last_bonus.r
  954. else
  955. rate = bonus.r - 100.0
  956. end
  957. end
  958. cost += (u.total_crowd_funding - u.project_owner_has_crowd_funding) * rate /100.0
  959. end
  960. @month_train_club_cost[m] = cost
  961. end
  962. # 文章每月支出
  963. sql = "select date_format(date_add(created_at,interval 8 hour),'%Y%m') as ym, sum(count) as c, date_format(date_add(created_at,interval 8 hour),'%m')as m from d5c_balances where count>0 and (date_add(created_at,interval 8 hour) >= '#{first_month}') and (date_add(created_at,interval 8 hour) < '#{current_month + 1.month}') and source='art_click_benefit' group by ym asc;"
  964. Article.find_by_sql(sql).each do |u|
  965. @month_article_cost[u.m.to_i] = u.c.to_i
  966. end
  967. #投资专题每月支出
  968. sql = "select date_format(date_add(benefit_at,interval 8 hour),'%Y%m') as ym, sum(b_count - total_price) as c, date_format(date_add(benefit_at,interval 8 hour),'%m')as m from d5c_zt_invest_orders where (b_count - total_price)>=0 and (date_add(benefit_at,interval 8 hour) >= '#{first_month}') and (date_add(benefit_at,interval 8 hour) < '#{current_month + 1.month}') group by ym asc;"
  969. ZtInvestOrder.find_by_sql(sql).each do |u|
  970. @month_zt_invest_cost[u.m.to_i] = u.c.to_i
  971. end
  972. #一天活动
  973. sql = "select date_format(from_unixtime(benefit_time),'%Y%m') ym, sum(benefit_amount) as c, date_format(from_unixtime(benefit_time),'%m') m from d5c_trial_coin_orders where benefit_state = 1 and benefit_time < #{(current_month+1.month).to_i} and benefit_time >= #{first_month.to_i} group by ym order by ym desc"
  974. TrialCoinOrder.find_by_sql(sql).each do |u|
  975. @month_trail_coin_cost[u.m.to_i] = u.c.to_i
  976. end
  977. #桌贴广告费
  978. sql = "select date_format(created_at,'%Y%m') ym, sum(count) as c, date_format(created_at,'%m') m from d5c_balances where source = 'zhuotie_guanggaofei' and created_at < '#{(current_month+1.month)}' and created_at >= '#{first_month}' group by ym order by ym desc"
  979. TrialCoinOrder.find_by_sql(sql).each do |u|
  980. @month_zhuotie[u.m.to_i] = u.c.to_i
  981. end
  982. end
  983. # 项目电子合同签署情况
  984. def tsign_project_info
  985. @unsigned = params[:not_sign_all] || false
  986. last_tsign_p_id = TsignConfig.last.project_id
  987. id = params[:project_id] || last_tsign_p_id
  988. @p_id = id.to_i
  989. @all_project_investors_info = []
  990. @p_title = get_project_title(@p_id)
  991. #投资该项目所有投资人用户ID
  992. sql = "select distinct user_id from d5c_project_joins where project_id = #{@p_id} and state = 1 and is_deleted = 0 and is_refunded = 0"
  993. investor_ids = ProjectJoin.find_by_sql(sql)
  994. investor_ids.each do |u|
  995. real_name = ""
  996. user_sex = ""
  997. user_tel = ""
  998. sub_info = ""
  999. sign_held_info = "未签署"
  1000. sign_invest_info = "未签署"
  1001. is_sign_all = 0
  1002. is_display = "table-row"
  1003. #单个投资用户签署信息
  1004. user_info_sql = "select u.id, u.tel, u.sex, u.real_name, w.nickname, w.subscribe from d5c_users u, d5c_wx_users w where u.id = #{u.user_id} and w.user_id = u.id"
  1005. user_info = User.find_by_sql(user_info_sql).first
  1006. if !user_info.blank?
  1007. real_name = user_info.real_name
  1008. if user_info.sex == 0
  1009. user_sex = "未知"
  1010. elsif user_info.sex == 1
  1011. user_sex = "男性"
  1012. elsif user_info.sex == 2
  1013. user_sex = "女性"
  1014. end
  1015. user_tel = user_info.tel
  1016. sub_info = "未关注公众号"
  1017. if user_info.subscribe
  1018. sub_info = "关注"
  1019. end
  1020. else
  1021. sql = "select u.id, u.tel, u.sex, u.real_name from d5c_users u where u.id = #{u.user_id}"
  1022. user_info = User.find_by_sql(sql).first
  1023. if !user_info.blank?
  1024. real_name = user_info.real_name
  1025. if user_info.sex == 0
  1026. user_sex = "未知"
  1027. elsif user_info.sex == 1
  1028. user_sex = "男性"
  1029. elsif user_info.sex == 2
  1030. user_sex = "女性"
  1031. end
  1032. user_tel = user_info.tel
  1033. sub_info = "未关注公众号"
  1034. end
  1035. end
  1036. signed_contract_sql = "select contract_type from d5c_tsign_user_file_signs where user_id = #{u.user_id} and project_id = #{@p_id}"
  1037. signed_contract = TsignUserFileSign.find_by_sql(signed_contract_sql)
  1038. signed_contract.each do |u|
  1039. if u.contract_type == "held"
  1040. sign_held_info = "签署"
  1041. elsif u.contract_type == "invest"
  1042. sign_invest_info = "签署"
  1043. end
  1044. end
  1045. if sign_invest_info == "签署" && sign_held_info == "签署"
  1046. is_sign_all = 1
  1047. end
  1048. if @unsigned == "true" && is_sign_all == 1
  1049. is_display = "none"
  1050. end
  1051. @all_project_investors_info << {:user_id => u.user_id, :user_name => real_name, :user_tel => user_tel,:sub_info => sub_info, :sign_held_info => sign_held_info, :sign_invest_info => sign_invest_info, :is_sign_all => is_sign_all, :is_display => is_display, :user_sex => user_sex}
  1052. end
  1053. end
  1054. # 充值订单新老投资人比例
  1055. def recharge
  1056. # 输入方式"2017-03"
  1057. # 确定查询月份
  1058. @now = Time.now
  1059. @current_month = Time.new(@now.year, @now.month)
  1060. @check_month = @current_month
  1061. if @now.month == 12
  1062. @check_next_month = Time.new(@now.year+1, 1)
  1063. else
  1064. @check_next_month = Time.new(@now.year, @now.month+1)
  1065. end
  1066. if !params[:time].blank?
  1067. m = params[:time].split("-")
  1068. @check_month = Time.new(m[0],m[1])
  1069. if m[1].to_i == 12
  1070. @check_next_month = Time.new(m[0].to_i+1, 1)
  1071. else
  1072. @check_next_month = Time.new(m[0], m[1].to_i+1)
  1073. end
  1074. end
  1075. # 充值区间
  1076. @se = [0, 1, 100000, 500000, 1000000, 5000000]
  1077. @section = ["0-1","1-1000", "1000-5000","5000-10000", "10000-50000", "50000以上"]
  1078. #每个区间充值金额
  1079. @se_recharges = []
  1080. @old_se_recharges = []
  1081. @new_se_recharges = []
  1082. @se_recharge_user_count = []
  1083. @old_se_recharge_user_count = []
  1084. @new_se_recharge_user_count = []
  1085. size = @se.size
  1086. se_sql = "select distinct user_id, sum(total_price) as recharge from d5c_balance_orders where paied_at < #{@check_next_month.to_i} and paied_at >= #{@check_month.to_i} and state = 1 group by user_id order by user_id"
  1087. s = BalanceOrder.find_by_sql(se_sql)
  1088. for i in 0..(size - 1)
  1089. @se_recharges[i] = 0 #总充值
  1090. @old_se_recharges[i] = 0 #老充值
  1091. @new_se_recharges[i] = 0 #新充值
  1092. @se_recharge_user_count[i]=0 #总用户充值人数
  1093. @old_se_recharge_user_count[i]= 0 #老用户充值人数
  1094. @new_se_recharge_user_count[i]= 0 #新用户充值人数
  1095. # if i == size-1
  1096. # se_sql = "select distinct user_id, sum(total_price) as recharge from d5c_balance_orders where paied_at < #{@check_next_month.to_i} and paied_at >= #{@check_month.to_i} and state = 1 and total_price > #{@se[i]} group by user_id order by id"
  1097. # else
  1098. # se_sql = "select distinct user_id, sum(total_price) as recharge from d5c_balance_orders where paied_at < #{@check_next_month.to_i} and paied_at >= #{@check_month.to_i} and state = 1 and total_price > #{@se[i]} and total_price <= #{@se[i+1]} group by user_id order by id"
  1099. # end
  1100. # 符合条件的记录,并计算充值总额
  1101. # se_re = BalanceOrder.find_by_sql(se_sql)
  1102. if i == size-1
  1103. s.each do |item|
  1104. t = item.recharge.to_i
  1105. if t > @se[i]
  1106. @se_recharge_user_count[i] +=1
  1107. @se_recharges[i] += t
  1108. has_recharged = is_recharge_before_current_month(item.user_id, @check_month)
  1109. if has_recharged == true
  1110. @old_se_recharges[i] += t
  1111. @old_se_recharge_user_count[i] +=1
  1112. else
  1113. @new_se_recharges[i] += t
  1114. @new_se_recharge_user_count[i] +=1
  1115. end
  1116. end
  1117. end
  1118. else
  1119. s.each do |item|
  1120. t = item.recharge.to_i
  1121. if t > @se[i] && t <= @se[i+1]
  1122. @se_recharges[i] += t
  1123. @se_recharge_user_count[i] +=1
  1124. has_recharged = is_recharge_before_current_month(item.user_id, @check_month)
  1125. if has_recharged == true
  1126. @old_se_recharges[i] += t
  1127. @old_se_recharge_user_count[i] +=1
  1128. else
  1129. @new_se_recharges[i] += t
  1130. @new_se_recharge_user_count[i] +=1
  1131. end
  1132. end
  1133. end
  1134. end
  1135. # se_re.each do |item|
  1136. # if item.recharge.to_i > 0
  1137. # @se_recharges[i] += item.recharge.to_i
  1138. # has_recharged = is_recharge_before_current_month(item.user_id, @check_month)
  1139. # if has_recharged == true
  1140. # @old_se_recharges[i] += item.recharge.to_i
  1141. # else
  1142. # @new_se_recharges[i] += item.recharge.to_i
  1143. # end
  1144. # end
  1145. # end
  1146. # @se_recharges[i] = s.first.recharge.to_i
  1147. if @se_recharges[i] !=nil && @se_recharges[i] > 0
  1148. @se_recharges[i] /= 100.0
  1149. end
  1150. if @old_se_recharges[i] != nil && @old_se_recharges[i] > 0
  1151. @old_se_recharges[i] /= 100.0
  1152. end
  1153. if @new_se_recharges[i] != nil && @new_se_recharges[i] > 0
  1154. @new_se_recharges[i] /= 100.0
  1155. end
  1156. end
  1157. # # 该月总充值金额
  1158. # @total_recharge = BalanceOrder.select("sum(total_price) as c")
  1159. # .where("paied_at < ? and paied_at >= ? and state= 1", (@check_next_month).to_i, @check_month.to_i).last.c.to_i/100.0
  1160. #该月充值用户
  1161. u_sql = "select distinct user_id, sum(total_price) as recharge from d5c_balance_orders where paied_at < #{@check_next_month.to_i} and paied_at >= #{@check_month.to_i} and state = 1 group by user_id order by user_id"
  1162. # 各用户充值总额
  1163. @user_recharge = BalanceOrder.find_by_sql(u_sql)
  1164. # 判断用户在查询月之前是否有冲过值,充过则将充值金额加在老用户,否则,为新用户
  1165. @total_recharge = 0
  1166. @old_user_recharge = 0
  1167. @new_user_recharge = 0
  1168. # 充值人数
  1169. @total_user_recharger = 0
  1170. @old_user_recharger = 0
  1171. @new_user_recharger = 0
  1172. @user_recharge.each do |item|
  1173. @total_recharge += item.recharge.to_i
  1174. @total_user_recharger +=1
  1175. has_recharged = is_recharge_before_current_month(item.user_id, @check_month)
  1176. if has_recharged == true
  1177. @old_user_recharge += item.recharge.to_i
  1178. @old_user_recharger += 1
  1179. else
  1180. @new_user_recharge += item.recharge.to_i
  1181. @new_user_recharger += 1
  1182. end
  1183. end
  1184. if @total_recharge != 0
  1185. @total_recharge /= 100.0
  1186. end
  1187. if @old_user_recharge != 0
  1188. @old_user_recharge /= 100.0
  1189. end
  1190. if @new_user_recharge != 0
  1191. @new_user_recharge /= 100.0
  1192. end
  1193. render :layout => false
  1194. end
  1195. #统计文章相关信息
  1196. def article
  1197. last_id = Article.last.id
  1198. id = params[:id] || last_id
  1199. @the_id = id
  1200. article = Article.where("id = ?",id).first
  1201. title = ""
  1202. @info = Hash.new
  1203. if !article.blank?
  1204. title = article.title
  1205. click_count = article.click
  1206. money_count_sql = "select sum(count) as c from d5c_article_click_benefits where article_id = #{id}"
  1207. money_count = 0
  1208. article_benefit = ArticleClickBenefit.find_by_sql(money_count_sql).first
  1209. if !article_benefit.blank?
  1210. money_count = article_benefit.c.to_i
  1211. end
  1212. # timeline_count_sql = "select * from d5c_share_infos where relate_id = #{id} and share_to = 'timeline'"
  1213. # timeline_count = ShareInfo.find_by_sql(timeline_count_sql).count()
  1214. timeline_count = ShareInfo.where("relate_id = ? and share_to = ?", "#{id}", "timeline").count()
  1215. # group_count_sql = "select * from d5c_share_infos where relate_id = #{id} and share_to = 'group'"
  1216. # group_count = ShareInfo.find_by_sql(group_count_sql).count()
  1217. group_count = ShareInfo.where("relate_id = ? and share_to = ?", "#{id}", "group").count()
  1218. click_url_times_count = 0
  1219. click_url_user_count = 0
  1220. if !article.url.blank?
  1221. s = article.url.split("/")
  1222. len = s.length
  1223. click_from_channel_id = s[len-1].to_i
  1224. click_from_channel = ClickFromChannel.where("id = ?", click_from_channel_id).first
  1225. if !click_from_channel.blank?
  1226. click_url_times_count = click_from_channel.click_times
  1227. end
  1228. click_url_user_count_sql = "select * from d5c_click_from_users where click_from_channel_id = #{click_from_channel_id}"
  1229. click_url_user_count = ClickFromUser.find_by_sql(click_url_user_count_sql).count()
  1230. end
  1231. @info = Hash[:title => title,:click_count => click_count, :money_count => money_count, :timeline_count => timeline_count, :group_count => group_count, :click_url_times_count => click_url_times_count, :click_url_user_count => click_url_user_count]
  1232. end
  1233. end
  1234. def stock_holder_list
  1235. @project_id = params[:project] || 0
  1236. end
  1237. private
  1238. #统计年粉丝情况
  1239. def get_report_weixin_users_yearly(appid, yearly_months, isnewly = false)
  1240. report_weixin_users_yearly_data = {}
  1241. yearly_months.each do |m|
  1242. report_weixin_users_yearly_data[m] = 0
  1243. end
  1244. if isnewly
  1245. ReportWeixinUser.select("month, sum(added_count) count").where("year=2016 and month>? and appid=? ", Time.now.month,appid).group("month").order("month").each do |u|
  1246. report_weixin_users_yearly_data[u.month] = u.count
  1247. end
  1248. else
  1249. ReportWeixinUser.select("month, sum(added_count-canceled_count) count").where("year=2016 and month>? and appid=? ", Time.now.month,appid).group("month").order("month").each do |u|
  1250. report_weixin_users_yearly_data[u.month] = u.count
  1251. end
  1252. end
  1253. if isnewly
  1254. ReportWeixinUser.select("month, sum(added_count) as count").where("year=2017 and appid=?",appid).group("month").order("month").each do |u|
  1255. report_weixin_users_yearly_data[u.month] = u.count
  1256. end
  1257. else
  1258. ReportWeixinUser.select("month, sum(added_count-canceled_count) as count").where("year=2017 and appid=?",appid).group("month").order("month").each do |u|
  1259. report_weixin_users_yearly_data[u.month] = u.count
  1260. end
  1261. end
  1262. return report_weixin_users_yearly_data.values
  1263. end
  1264. #统计月粉丝情况
  1265. def get_report_weixin_users_monthly(appid,monthly_days, isnewly = false)
  1266. report_weixin_users_monthly_data = {}
  1267. monthly_days.each do |m|
  1268. report_weixin_users_monthly_data[m] = 0
  1269. end
  1270. if isnewly
  1271. ReportWeixinUser.select("day, sum(added_count) as count").where("year=? and month=? and appid=?",Time.now.year,Time.now.month,appid).group("day").order("day").each do |u|
  1272. report_weixin_users_monthly_data[u.day] = u.count
  1273. end
  1274. else
  1275. ReportWeixinUser.select("day, sum(added_count-canceled_count) as count").where("year=? and month=? and appid=?",Time.now.year,Time.now.month,appid).group("day").order("day").each do |u|
  1276. report_weixin_users_monthly_data[u.day] = u.count
  1277. end
  1278. end
  1279. return report_weixin_users_monthly_data.values
  1280. end
  1281. #统计年粉丝,根据开始时间和结束时间
  1282. def get_report_weixin_users_by_period(appid,first_time,last_time, is_newly=false)
  1283. report_weixin_users = {}
  1284. if first_time.to_i >= last_time.to_i
  1285. return nil
  1286. end
  1287. first_year = first_time.year.to_i
  1288. first_month = first_time.month.to_i
  1289. last_year = last_time.year.to_i
  1290. last_month = last_time.month.to_i
  1291. if first_year = last_year
  1292. (first_month...last_month).each do |m|
  1293. report_weixin_users[m] = 0
  1294. end
  1295. if is_newly
  1296. sql = "select month,sum(added_count) as c from report_weixin_users where year = #{first_year} and month >= #{first_month} and month <= #{last_month} and appid = '#{appid}' group by month order by month desc"
  1297. ReportWeixinUser.find_by_sql(sql).each do |u|
  1298. report_weixin_users[u.month.to_i] = u.c.to_i
  1299. end
  1300. return report_weixin_users
  1301. else
  1302. sql = "select month,sum(added_count-canceled_count) as c from report_weixin_users where year = #{first_year} and month >= #{first_month} and month <= #{last_month} and appid = '#{appid}' group by month order by month desc"
  1303. ReportWeixinUser.find_by_sql(sql).each do |u|
  1304. report_weixin_users[u.month.to_i] = u.c.to_i
  1305. end
  1306. return report_weixin_users
  1307. end
  1308. elsif first_year < last_year
  1309. (first_month..12).each do |m|
  1310. report_weixin_users[m] = 0
  1311. end
  1312. (1...last_month).each do |m|
  1313. report_weixin_users[m] = 0
  1314. end
  1315. if is_newly
  1316. sql = "select month,sum(added_count) as c from report_weixin_users where year = #{first_year} and month >= #{first_month} and appid = '#{appid}' group by month order by month desc"
  1317. ReportWeixinUser.find_by_sql(sql).each do |u|
  1318. report_weixin_users[u.month.to_i] = u.c.to_i
  1319. end
  1320. sql = "select month,sum(added_count) as c from report_weixin_users where year = #{last_year} and month <= #{last_month} and appid = '#{appid}' group by month order by month desc"
  1321. ReportWeixinUser.find_by_sql(sql).each do |u|
  1322. report_weixin_users[u.month.to_i] = u.c.to_i
  1323. end
  1324. return report_weixin_users
  1325. else
  1326. sql = "select month,sum(added_count-canceled_count) as c from report_weixin_users where year = #{first_year} and month >= #{first_month} and appid = '#{appid}' group by month order by month desc"
  1327. ReportWeixinUser.find_by_sql(sql).each do |u|
  1328. report_weixin_users[u.month.to_i] = u.c.to_i
  1329. end
  1330. sql = "select month,sum(added_count-canceled_count) as c from report_weixin_users where year = #{last_year} and month <= #{last_month} and appid = '#{appid}' group by month order by month desc"
  1331. ReportWeixinUser.find_by_sql(sql).each do |u|
  1332. report_weixin_users[u.month.to_i] = u.c.to_i
  1333. end
  1334. return report_weixin_users
  1335. end
  1336. end
  1337. end
  1338. # 判断用户在查询月之前是否有充过值
  1339. def is_recharge_before_current_month(u_id, check_month)
  1340. is_recharge = false
  1341. sql = "select * from d5c_balance_orders where paied_at < #{check_month.to_i} and state = 1 and user_id = #{u_id}"
  1342. user_recharge = BalanceOrder.find_by_sql(sql)
  1343. if !user_recharge.blank?
  1344. is_recharge = true
  1345. end
  1346. return is_recharge
  1347. end
  1348. #判断用户是否为新投资用户
  1349. def is_old_investor(user_id, project_id)
  1350. is_old = false
  1351. #找出该用户所有该项目的最早时间点
  1352. invest_time = 0
  1353. t = ProjectJoin.where("state=1 and is_refunded =0 and is_deleted=0 and paied_at > 0 and user_id = ? and project_id = ?", user_id, project_id).first
  1354. invest_time = t.paied_at
  1355. #是否有体验金之外投资
  1356. sql = "select j.* from d5c_projects p, d5c_project_joins j where p.id = j.project_id and p.ptype = 'stock' and j.state = 1 and j.is_deleted = 0 and j.is_refunded = 0 and j.user_id = #{user_id} and j.paied_at < #{invest_time} limit 1"
  1357. # p = ProjectJoin.where("state=1 and is_refunded =0 and is_deleted=0 and paied_at > 0 and paied_at < ? and user_id = ? and pay_way != ?", invest_time, user_id, "trail_coin")
  1358. p = ProjectJoin.find_by_sql(sql).first
  1359. if !p.blank?
  1360. is_old = true
  1361. end
  1362. return is_old
  1363. end
  1364. def getChannelQrcodeRemark(channel_qrcode_id)
  1365. id = channel_qrcode_id
  1366. r = ""
  1367. cq = ChannelQrcode.where("id = ?",id).first
  1368. if !cq.blank?
  1369. r = cq.remark
  1370. if r.blank?
  1371. r = "二维码ID:#{channel_qrcode_id}"
  1372. end
  1373. else
  1374. r = "二维码ID:#{channel_qrcode_id}(已删除)"
  1375. end
  1376. return r
  1377. end
  1378. # 项目所有投资人通过项目ID
  1379. def get_all_project_investors(project_id)
  1380. sql = "select distinct user_id from d5c_project_joins where state=1 and is_refunded=0 and is_deleted=0 and project_id = #{project_id}"
  1381. all_project_investors = ProjectJoin.find_by_sql(sql)
  1382. return all_project_investors
  1383. end
  1384. #获得所有投资人
  1385. def get_all_investors
  1386. sql = "select distinct j.user_id from d5c_project_joins j, d5c_projects p where j.project_id = p.id and p.ptype = 'stock' and j.state = 1 and j.is_deleted = 0 and is_deleted = 0"
  1387. all_investors = ProjectJoin.find_by_sql(sql)
  1388. return all_investors
  1389. end
  1390. #获取用户
  1391. def get_user_by_id(user_id)
  1392. user = User.where("id = ?", user_id).first
  1393. return user
  1394. end
  1395. #获取项目名
  1396. def get_project_title(project_id)
  1397. title = ""
  1398. p = Project.where("id = ?", project_id).first
  1399. if !p.blank?
  1400. title = p.title
  1401. end
  1402. return title
  1403. end
  1404. # 是否关注公众号
  1405. def is_sub(user_id)
  1406. is_sub = false
  1407. wx_user = WxUser.where("user_id = ?", user_id).first
  1408. if !wx_user.blank? && wx_user.subscribe = true
  1409. is_sub = true
  1410. end
  1411. return is_sub
  1412. end
  1413. # 是否签署合同
  1414. def is_sign_contact(user_id, project_id, type)
  1415. is_sign = false
  1416. contact = TsignUserFileSign.where("user_id = ? and project_id = ? and contract_type = ?", user_id, project_id, type)
  1417. if !contact.blank?
  1418. is_sign = true
  1419. end
  1420. return is_sign
  1421. end
  1422. #包含
  1423. def is_contain(arr, n)
  1424. i = false
  1425. arr.each do |u|
  1426. u = n
  1427. i = true
  1428. break
  1429. end
  1430. return i
  1431. end
  1432. #获取渠道描述
  1433. def get_channel_value_by_id(channel_id)
  1434. v = ""
  1435. c = SignUpChannel.where("id = ?", channel_id).first
  1436. if !c.blank?
  1437. v = c.channel_value
  1438. end
  1439. return v
  1440. end
  1441. end