class UserStayedTime
  include Mongoid::Document
  field :starttime, type: Time
  field :endtime, type: Time
  field :stayedtime, type: Float, :default =>0.0
  field :created_at, type: Time
  field :rl_mac_id, type: String
  field :vd_mac_id, type: String
  field :register_flag, type: Integer, :default => 0
  field :stayed_flag, type: String
  field :return_flag, type: Integer, :default => 0
  belongs_to :visitor_detail

  index({"rl_mac_id" => 1, "starttime" => 1, "endtime" => 1}, {background: true})
  index({"return_flag"=>1, "register_flag"=>1},{background: true})
  before_save :set_stayed_flag
  
  def set_stayed_flag
    self.stayed_flag = self.id
  end  

  def self.register_ppl(rlids,start_date,end_date,offset)
    
    total_visits = self.collection.aggregate({"$match" => {'register_flag'=>{ "$exists"=> true},"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}},{ "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"},  "count"=> { "$sum" => 1 }}})

    never_visited = self.collection.aggregate({"$match" => {'register_flag'=>{ "$exists"=> true},"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}},{ "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"},  "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$lte" => 1}}})

    before_visited = self.collection.aggregate({"$match" => {'register_flag'=>{ "$exists"=> true},"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}},{ "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"},  "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$gt" => 1}}})

    return {:before_visited=>before_visited, :never_visited=>never_visited, :total_visits=>total_visits}
  end

  def self.tot_unique_users(vdids,start_date,end_date)
    self.collection.aggregate({"$match" => {"visitor_detail_id" => {"$in" => vdids},"starttime"=> { "$gte" => start_date.to_time.utc.beginning_of_day},"endtime" => {"$lte" => end_date.to_time.utc.end_of_day}, "stayedtime" => {"$gte" => APPLICATION['defaults']['timeinterval'] }  } }, { "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"}, "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$lte" => 1}}},"$group"=>{"_id"=>"$_id.day","count"=>{"$sum"=>"$count"}}) rescue []
  end

  def self.tot_before_visited(rlids,start_date,end_date,offset)
    self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}}, { "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"}, "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$gt" => 1}}},"$group"=>{"_id"=>"$_id.day","count"=>{"$sum"=>1}})
  end

  def self.tot_before_visited_in_store(rlids,start_date,end_date,offset)
    self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1, "stayedtime"=>1}}, { "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"},"stayedtime"=>{"$sum"=>"$stayedtime"} ,"count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$gt" => 1}, "stayedtime" => {"$gte" => APPLICATION['defaults']['timeinterval'] }}},"$group"=>{"_id"=>"$_id.day","count"=>{"$sum"=>1}})
  end

  def self.before_visited(vdids,start_date,end_date,offset)
    total_people = self.tot_before_visited(vdids,start_date,end_date,offset)
    in_store = self.tot_before_visited_in_store(vdids,start_date,end_date,offset)
    return {"total_people"=>total_people, "in_store"=>in_store}
  end
  
  def self.tot_never_visited(rlids,start_date,end_date,offset)
     self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}}, { "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"}, "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$lte" => 1}}},"$group"=>{"_id"=>"$_id.day","count"=>{"$sum"=>1}})
  end

  def self.total_people(rlids,start_date,end_date,offset)
   self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}},{ "$group"=> {"_id"=>{"year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}} ,"visitors"=> { "$addToSet" => "$visitor_detail_id" }}},{"$unwind" => '$visitors'},"$group"=>{"_id"=>"$_id.day","count"=>{"$sum"=>1}})
  end

  def self.tot_in_store(rlids,start_date,end_date,offset)
    self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1, "stayedtime"=>1}}, { "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"},"stayedtime"=>{"$sum"=>"$stayedtime"} ,"count"=> { "$sum" => 1 }}},{"$match" => {"stayedtime" => {"$gte" => APPLICATION['defaults']['timeinterval'] }}},"$group"=>{"_id"=>"$_id.day","count"=>{"$sum"=>1}})
  end

  def self.never_visited(rlids,start_date,end_date,offset)
    total_people = self.tot_never_visited(rlids,start_date,end_date,offset)
    in_store = self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1, "stayedtime"=>1}}, { "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"},"stayedtime"=>{"$sum"=>"$stayedtime"} ,"count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$lte" => 1}, "stayedtime" => {"$gte" => APPLICATION['defaults']['timeinterval'] }}},"$group"=>{"_id"=>"$_id.day","count"=>{"$sum"=>1}})
    return {"total_people"=>total_people, "in_store"=>in_store}
  end

  def self.total_traffic(rlids,start_date,end_date,offset)
    total_people = self.total_people(rlids,start_date,end_date,offset)
    in_store = self.tot_in_store(rlids,start_date,end_date,offset)
    return {"total_people"=>total_people, "in_store"=>in_store}
  end

  def self.store_hour_v2(rlids,start_date,end_date)
    tust = (0..23).inject({}) {|h, x| h.merge({"#{x} tust" => "$h.#{x}.tust" })}
    hs = tust.merge((0..23).inject({}) {|h, x| h.merge({"#{x} tv" => "$h.#{x}.tv"})})
    PresenceAnalytic.collection.aggregate({"$match" => { "ap"=>{"$in" => rlids},"d"=> { "$gte" => start_date.to_date, "$lte" => end_date.to_date} } }, {"$project"=>{"h"=>1, "ap"=>1}}, { "$group"=> {"_id"=> {}.merge(hs)}})

    # PresenceAnalytic.collection.aggregate({"$match" => { "ap"=>{"$in" => rlids},"d"=> { "$gte" => start_date.to_date, "$lte" => end_date.to_date} } }, {"$project"=>{"h"=>1, "ap"=>1}}, { "$group"=> {"_id"=> {"0 hr"=>"$h.0.tv","0 ust"=>"$h.0.tust","1 hr"=>"$h.1.tv","1 ust"=>"$h.1.tust","2 hr"=>"$h.2.tv","2 ust"=>"$h.2.tust","3 hr"=>"$h.3.tv", "3 ust"=>"$h.3.tust","4 hr"=>"$h.4.tv", "4 ust"=>"$h.4.tust","5 hr"=>"$h.5.tv", "5 ust"=>"$h.5.tust", "6 hr"=>"$h.6.tv", "6 ust"=>"$h.6.tust","7 hr"=>"$h.7.tv", "7 ust"=>"$h.7.tust","8 hr"=>"$h.8.tv", "8 ust"=>"$h.8.tust","9 hr"=>"$h.9.tv", "9 ust"=>"$h.9.tust","10 hr"=>"$h.10.tv", "10 ust"=>"$h.10.tust","11 hr"=>"$h.11.tv", "11 ust"=>"$h.11.tust","12 hr"=>"$h.12.tv","12 ust"=>"$h.12.tust","13 hr"=>"$h.13.tv", "13 ust"=>"$h.13.tust","14 hr"=>"$h.14.tv", "14 ust"=>"$h.14.tust",  "15 hr"=>"$h.15.tv", "15 ust"=>"$h.15.tust","16 hr"=>"$h.16.tv", "16 ust"=>"$h.16.tust", "17 hr"=>"$h.17.tv","17 ust"=>"$h.17.tust","18 hr"=>"$h.18.tv", "18 ust"=>"$h.18.tust","19 hr"=>"$h.19.tv", "19 ust"=>"$h.19.tust",  "20 hr"=>"$h.20.tv", "20 ust"=>"$h.20.tust","21 hr"=>"$h.21.tv", "21 ust"=>"$h.21.tust","22 hr"=>"$h.22.tv", "22 ust"=>"$h.22.tust", "23 hr"=>"$h.23.tv", "23 ust"=>"$h.23.tust"}}})
  end

  def self.ppl_bfr_afr_xmin_v2(rlids,start_date,end_date,offset)
    self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids}, "starttime"=> { "$gte" => start_date},"endtime" => {"$lt" => end_date} } },{"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "vd_mac_id"=>1, "stayedtime"=>1}},{"$group" => {"_id"=>{"vd"=>"$vd_mac_id", "day"=> {"$dayOfMonth"=>"$starttime"}}, "max_stm" => {"$max"=>"$stayedtime"}}})
  end

  def self.store_front_v2(rlids,start_date,end_date)
    PresenceAnalytic.collection.aggregate({"$match" => { "ap"=>{"$in" => rlids},"d"=> { "$gte" => start_date.to_date, "$lte" => end_date.to_date} } }, {"$project"=>{"d"=>1,"daily"=>1}}, { "$group"=> {"_id"=> {"day"=> {"$dayOfMonth"=>"$d"}}, "nvstore"=>{"$sum"=>"$daily.nv.store"},"nvwalk"=>{"$sum"=>"$daily.nv.walk"},"nvregv"=>{"$sum"=>"$daily.nv.regv"},"rvstore"=>{"$sum"=>"$daily.rv.store"},"rvwalk"=>{"$sum"=>"$daily.rv.walk"},"rvregv"=>{"$sum"=>"$daily.rv.regv"}}})
  end

  def self.customer_recency_v2(rlids,start_date,end_date,offset)
    return_vts = PresenceAnalytic.collection.aggregate({"$match" => { "ap"=>{"$in" => rlids},"d"=> { "$gte" => start_date.to_date, "$lte" => end_date.to_date} } }, {"$project"=>{"daily"=>1, "d"=>1}}, { "$group"=> {"_id"=> {"year"=> {"$year"=>"$d"}, "week"=>{"$week"=>"$d"}},"rvv"=>{"$sum"=>"$daily.rv.v"},"nvv"=>{"$sum"=>"$daily.nv.v"}}},{ "$sort" => { "_id.week" => 1 } })
    return_mbs = self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},  "starttime"=> { "$gte" => start_date}, "endtime"=>{"$lte"=>end_date}, "register_flag"=>{"$exists"=>true} }}, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]},"vd_mac_id"=>1}}, {"$match"=>{"register_flag" =>{"$eq" => 1}}} , {"$group"=> {"_id"=> {"year"=> {"$year"=>"$starttime"}, "week"=>{"$week"=>"$starttime"},"vd_mac_id"=> "$vd_mac_id"}, "count"=> { "$sum" => 1 }}},{ "$sort" => { "count"=> -1}})
    return {"return_vts"=>return_vts,"return_mbs"=>return_mbs}
  end

  def self.avg_ppl_by_hr_in_store(rlids,start_date,end_date,offset)
    
    self.collection.aggregate({"$match" => { "rl_mac_id"=>{"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date}, "stayedtime" => {"$gte" => APPLICATION['defaults']['timeinterval'] } } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}}, { "$group"=> {"_id"=> { "hour"=>{"$hour"=>"$starttime"}},  "visitors"=> { "$addToSet" => "$visitor_detail_id" }}},{"$unwind" => '$visitors'},"$group"=>{"_id"=>"$_id.hour","count"=>{"$sum"=>1}})
  end

  def self.avg_min_spent_in_store(rlids,start_date,end_date,offset)
    self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date}, "stayedtime" => {"$gte" => APPLICATION['defaults']['dwell_time']}} }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "stayedtime"=>1 }}, { "$group"=> {"_id"=> {"hour"=> {"$hour"=>"$starttime"}}, "sum"=> { "$sum" => "$stayedtime" }, "count"=> { "$sum" =>1 }}})
  end

  def self.avg_peple_by_hr(rl_mac_id,start_date,end_date,offset)
    self.collection.aggregate({"$match" => {"rl_mac_id" => rl_mac_id,"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "vd_mac_id"=>1}}, { "$group"=> {"_id"=> { "hour"=>{"$hour"=>"$starttime"}}, "visitors"=> { "$addToSet" => "$vd_mac_id" }}},{"$project"=>{"count"=>{"$size"=>"$visitors"}}})
  end

  def self.customer_recency_new(rlids,start_date,offset)
    self.collection.aggregate({"$match" => {'stayed_flag'=>{ "$exists"=> true}, "rl_mac_id" => {"$in" => rlids}, "starttime"=> { "$gte" => start_date} }}, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "stayedtime"=>1, "visitor_detail_id"=>1, "stayed_flag"=>1 }} , { "$group"=> {"_id"=> {"year"=> {"$year"=>"$starttime"}, "week"=>{"$week"=>"$starttime"},"stayed_flag"=>"$stayed_flag"},"visitors"=> { "$addToSet" => "$visitor_detail_id" }, "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$gt" => 1}}},{"$group"=> {"_id"=> {"visitors"=>"$visitors","week"=>"$_id.week","year"=>"$_id.year"}}},{ "$sort" => { "_id.week" => 1 }})
    # self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids}, "starttime"=> { "$gte" => start_date} }}, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "stayedtime"=>1, "visitor_detail_id"=>1 }} , { "$group"=> {"_id"=> {"year"=> {"$year"=>"$starttime"}, "week"=>{"$week"=>"$starttime"}},"visitors"=> { "$addToSet" => "$visitor_detail_id" }, "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$gt" => 1}}},{"$group"=> {"_id"=> {"visitors"=>"$visitors","week"=>"$_id.week","year"=>"$_id.year"}}},{ "$sort" => { "_id.week" => 1 } })
  end

  def self.members_customer_recency_new(rlids,start_date,offset)
    UserDetail.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"timestamp"=> { "$gte" => start_date} }}, {"$project"=>{"timestamp"=>{"$add" => ["$timestamp", offset.to_i]}, "visitor_detail_id"=>1 }} , { "$group"=> {"_id"=> { "year"=> {"$year"=>"$timestamp"}, "week"=>{"$week"=>"$timestamp"}},"visitors"=> { "$addToSet" => "$visitor_detail_id" }, "count"=> { "$sum" => 1 }}},{"$group"=> {"_id"=> {"visitors"=>"$visitors","week"=>"$_id.week","year"=>"$_id.year"}}},{ "$sort" => { "_id.week" => 1 } })
  end

  def self.members_customer_recency(rlids,start_date,end_date,offset)
    UserDetail.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"timestamp"=> { "$gte" => start_date, "$lte" => end_date} }}, {"$project"=>{"timestamp"=>{"$add" => ["$timestamp", offset.to_i]}, "visitor_detail_id"=>1}} , { "$group"=> {"_id"=> {},"visitors"=> { "$addToSet" => "$visitor_detail_id" }, "count"=> { "$sum" => 1 }}},{"$group"=> {"_id"=> {"visitors"=>"$visitors"}}})
  end

  def self.customer_recency(rlids,start_date,end_date,offset)
    self.collection.aggregate({"$match" => {'stayed_flag'=>{ "$exists"=> true},"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} }}, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "stayed_flag"=>1,"visitor_detail_id"=>1}} , { "$group"=> {"_id"=> {"stayed_flag"=>"$stayed_flag"},"visitors"=> { "$addToSet" => "$visitor_detail_id" }, "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$gt" => 1}}},{"$group"=> {"_id"=> {"visitors"=>"$visitors"}}})
    # self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} }}, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}} , { "$group"=> {"_id"=> {},"visitors"=> { "$addToSet" => "$visitor_detail_id" }, "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$gt" => 1}}},{"$group"=> {"_id"=> {"visitors"=>"$visitors"}}})
  end


  def self.top_6_visits(rlids,start_date,end_date,offset)
    UserDetail.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"timestamp"=> { "$gte" => start_date, "$lte" => end_date} }}, {"$project"=>{"timestamp"=>{"$add" => ["$timestamp", offset.to_i]}, "visitor_detail_id"=>1}} , { "$group"=> {"_id"=> {"hour"=> {"$hour"=>"$timestamp"}, "visitor_detail_id"=>"$visitor_detail_id" }, "count"=> { "$sum" => 1 }}}, { "$sort"=> { "count"=> -1 } })
    # self.collection.aggregate({"$match" => {"rl_mac_id" => {"$in" => rlids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date} } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1,"stayed_flag"=>1}}, { "$group"=> {"_id"=> { "visitor_detail_id"=>"$visitor_detail_id","stayed_flag"=>"$stayed_flag"}, "count"=> { "$sum" => 1 }}}, { "$sort"=> { "count"=> -1 }})
  end
  

  def self.tot_return_users(vdids,start_date,end_date)
    self.collection.aggregate({"$match" => {"visitor_detail_id" => {"$in" => vdids},"starttime"=> { "$gte" => start_date.to_time.utc.beginning_of_day},"endtime" => {"$lte" => end_date.to_time.utc.end_of_day}, "stayedtime" => {"$gte" => APPLICATION['defaults']['timeinterval'] } } }, { "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}, "visitor_detail_id"=>"$visitor_detail_id"}, "count"=> { "$sum" => 1 }}},{"$match" => {"count" =>{"$gt" => 1}}}, {"$group"=>{"_id"=> { "year"=> "$_id.year","month"=> "$_id.month", "day"=> "$_id.day" }, "count"=>{"$sum"=>1}}} )
  end

  def self.total_people_in_store(vdids,start_date,end_date,offset)
    self.collection.aggregate({"$match" => {"visitor_detail_id" => {"$in" => vdids},"starttime"=> { "$gte" => start_date},"endtime" => {"$lte" => end_date}, "stayedtime" => {"$gte" => APPLICATION['defaults']['timeinterval'] } } }, {"$project"=>{"starttime"=>{"$add" => ["$starttime", offset.to_i]}, "visitor_detail_id"=>1}}, { "$group"=> {"_id"=> { "year"=> {"$year"=>"$starttime"},"month"=> {"$month"=>"$starttime"},"day"=> {"$dayOfMonth"=>"$starttime"}},  "visitors"=> { "$addToSet" => "$visitor_detail_id" },"count"=>{"$sum"=>1}}},{"$group"=>{"_id"=>"$_id.day","count"=>{"$sum"=>1}} } )
  end

  def self.uniqe_tot_return(vdids,start_date,end_date)
    unique_users = self.tot_unique_users(vdids,start_date,end_date)
    return_users = self.tot_return_users(vdids,start_date,end_date)
    total_users = self.total_people_in_store(vdids,start_date,end_date)
    return {"total_users"=>total_users, "unique_users"=>unique_users, "return_users"=>return_users}
  end

  def self.get_visitors_count_n_sttime(starttime,endtime)

    starttime = starttime*1000
    endtime = endtime*1000
    map = %Q{
      function() {
        var st = this.starttime.getTime(), et = this.endtime.getTime()
        var stm = 0
          if(st >= #{starttime} && #{endtime} > st){
            if(#{endtime} > et)
               stm = (et-st)
            else
              stm = (#{endtime} - st)
          }
          else if(st < #{starttime} && #{endtime} > st){
            if(#{endtime} > et)
              stm = (et- #{starttime})
            else
              stm = (#{endtime} - #{starttime})
          }
          stm = stm/1000
        emit(this.rl_mac_id, {sttm: stm, tv: 1})
      }
    }
    reduce = %Q{
      function(key, values) {
        var result = {sttm:0, tv: 0}
        values.forEach(function(ust) {
          result.sttm +=  ust.sttm
          result.tv += ust.tv
        })
        return result;
      }
    }
    map_reduce(map,reduce).out(inline: true).to_a
  end
end