ActivityService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace App\Modules\Activity\Services;
  3. use App\Modules\Activity\Models\Activity;
  4. use App\Modules\Statistic\Services\WapVisitStatService;
  5. use App\Modules\Subscribe\Models\Order;
  6. use App\Modules\Trade\Services\OrderService;
  7. use DB;
  8. use Redis;
  9. class ActivityService
  10. {
  11. /**
  12. * 查询活动信息
  13. * @param array $params
  14. * @param bool $is_all
  15. * @return mixed
  16. */
  17. static function search($params = [], $is_all = false)
  18. {
  19. return Activity::search($params, $is_all);
  20. }
  21. /**
  22. * 查询活动id
  23. * @param array $params
  24. * @param bool $is_all
  25. * @return mixed
  26. */
  27. static function getActivityIds($params = [])
  28. {
  29. return Activity::getActivityIds($params);
  30. }
  31. /**
  32. * 查询活动的的充值信息
  33. * @param array $params
  34. * @param bool $is_all
  35. * @return mixed
  36. */
  37. static function getActivityRechargeInfo($params = [], $is_all = false)
  38. {
  39. return OrderService::search($params, $is_all);
  40. }
  41. /**
  42. * 更新活动控制开关
  43. * @param $activity_id
  44. * @param $params
  45. * @return mixed
  46. */
  47. static function updateShowSwitch($activity_id, $params)
  48. {
  49. return Activity::updateShowSwitch($activity_id, $params);
  50. }
  51. /**
  52. * 根据id获取
  53. * @param $id
  54. * @return mixed
  55. */
  56. static function getById($id)
  57. {
  58. return Activity::find($id);
  59. }
  60. /**
  61. * 根据token获取
  62. * @param $id
  63. * @return mixed
  64. */
  65. static function getByToken($token)
  66. {
  67. return Activity::where('token', $token)->first();
  68. }
  69. /**
  70. * 获取当前渠道的9.9元活动
  71. * @param $chanel_id
  72. * @return mixed
  73. */
  74. static function getActiveCustomActivity($chanel_id)
  75. {
  76. return Activity::where('distribution_channel_id', $chanel_id)
  77. ->where('start_time', '<=', date('Y-m-d H:i:s'))
  78. ->where('end_time', '>', date('Y-m-d H:i:s'))
  79. ->where('product_id', 1252)
  80. ->select('activity_page')
  81. ->first();
  82. }
  83. static function getActiveCustomActivityByType($chanel_id,$type='DEFAULT'){
  84. return Activity::where('distribution_channel_id', $chanel_id)
  85. ->where('start_time', '<=', date('Y-m-d H:i:s'))
  86. ->where('end_time', '>', date('Y-m-d H:i:s'))
  87. ->where('type', $type)
  88. ->select('activity_page')
  89. ->first();
  90. }
  91. /**
  92. * 活动统计
  93. * @param $activity_id
  94. * @param $distribution_channel_id
  95. * @return array
  96. */
  97. static function getActivityUvPv($activity_id, $distribution_channel_id)
  98. {
  99. $activity = self::getById($activity_id);
  100. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  101. $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
  102. $start = strtotime($activity->start_time);
  103. $end_time = strtotime($activity->end_time);
  104. $end = time() > $end_time ? $end_time : time();
  105. $page_pv = 0;
  106. $page_uv = 0;
  107. $visit_info = WapVisitStatService::getActivityUvAndPv($distribution_channel_id, $activity_id);
  108. $page_pv = $visit_info['pv'];
  109. $page_uv = $visit_info['uv'];
  110. /*while ($start<$end){
  111. $temp_day = date('Y-m-d',$start);
  112. $uv_key = sprintf($uv_key_format, $activity_id, $distribution_channel_id,$temp_day);
  113. $puv = Redis::scard($uv_key);
  114. if($puv) $page_uv += $puv;
  115. $start += 86400;
  116. }
  117. $pv_key = sprintf($pv_key_format, $activity_id, $distribution_channel_id);
  118. $pv_all = Redis::hgetAll($pv_key);
  119. if($pv_all){
  120. foreach ($pv_all as $v){
  121. $page_pv += $v;
  122. }
  123. }*/
  124. $button_pv = Order::where('activity_id', $activity_id)->where('distribution_channel_id', $distribution_channel_id)->count();
  125. $button_uv = DB::select('select ifnull(count(distinct uid),0) as count from orders where activity_id=' . $activity_id . ' and distribution_channel_id=' . $distribution_channel_id);
  126. $button_uv = $button_uv[0]->count;
  127. $order_num = Order::where('activity_id', $activity_id)->where('status', 'PAID')->where('distribution_channel_id', $distribution_channel_id)->count();
  128. $order_sum = Order::where('activity_id', $activity_id)->where('status', 'PAID')->where('distribution_channel_id', $distribution_channel_id)->sum('price');
  129. $sql = 'select ifnull(count(*),0) as count from orders a where activity_id=' . $activity_id . ' and status="PAID" and distribution_channel_id= ' . $distribution_channel_id . ' and EXISTS (select uid from orders b where created_at <="' . $activity->start_time . '" and b.uid=a.uid)';
  130. $re_order_info = DB::select($sql);
  131. $re_order = $re_order_info[0]->count;
  132. return compact('page_pv', 'page_uv', 'button_pv', 'button_uv', 'order_num', 're_order', 'order_sum');
  133. }
  134. static function ActivityStats($activity_id)
  135. {
  136. $sql = "select a.*,b.price,b.order_num as success_order from(
  137. select date(created_at) as `date`,distribution_channel_id,count(*) as order_num,count(DISTINCT uid) as uv
  138. from orders
  139. where activity_id={$activity_id} GROUP by date(created_at),distribution_channel_id
  140. ) a
  141. left join (
  142. select date(created_at) as `date`,distribution_channel_id,sum(price) as price,count(*) as order_num
  143. from orders
  144. where activity_id={$activity_id} and `status`='PAID' GROUP by date(created_at),distribution_channel_id
  145. ) b on a.`date`=b.`date` and a.distribution_channel_id=b.distribution_channel_id";
  146. $res = DB::select($sql);
  147. $activity = self::getById($activity_id);
  148. $data = [];
  149. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  150. foreach ($res as $v) {
  151. $temp = [];
  152. $temp['date'] = $v->date;
  153. $temp['distribution_channel_id'] = $v->distribution_channel_id;
  154. $temp['button_pv'] = $v->order_num;
  155. $temp['order_num'] = $v->success_order ? $v->success_order : 0;
  156. $sql1 = 'select ifnull(count(*),0) as count from orders a where activity_id=' . $activity_id . ' and status="PAID" and date(created_at)="' . $v->date . '" and distribution_channel_id= ' . $v->distribution_channel_id . ' and not EXISTS (select uid from orders b where created_at <="' . $activity->start_time . '" and b.uid=a.uid)';
  157. $sql2 = 'select ifnull(count(*),0) as count from orders a where activity_id=' . $activity_id . ' and status="PAID" and from_type="reader" and date(created_at)="' . $v->date . '" and distribution_channel_id= ' . $v->distribution_channel_id . ' and not EXISTS (select uid from orders b where created_at <="' . $activity->start_time . '" and b.uid=a.uid)';
  158. $re_order_info = DB::select($sql1);
  159. $re_order_info2 = DB::select($sql2);
  160. $temp['first_charge'] = $re_order_info[0]->count ? $re_order_info[0]->count : 0;
  161. $temp['first_charge_reader'] = $re_order_info2[0]->count ? $re_order_info2[0]->count : 0;
  162. $temp['button_uv'] = $v->uv;
  163. $uv_key = sprintf($uv_key_format, $activity_id, $v->distribution_channel_id, $v->date);
  164. $puv = Redis::scard($uv_key);
  165. $puv = $puv ? $puv : 0;
  166. $temp['page_uv'] = $puv;
  167. $from = 'reader';
  168. $reader_uv = Redis::scard('push:distribution_channel_id:' . $v->distribution_channel_id . 'from:' . $from . ':date:' . $v->date);
  169. $temp['reader_uv'] = $reader_uv ? $reader_uv : 0;
  170. $data[] = $temp;
  171. }
  172. $sql3 = "select a.*,b.price,b.order_num as success_order from(
  173. select date(created_at) as `date`,distribution_channel_id,count(*) as order_num,count(DISTINCT uid) as uv
  174. from orders
  175. where activity_id=$activity_id and from_type='reader' GROUP by date(created_at),distribution_channel_id
  176. ) a
  177. left join (
  178. select date(created_at) as `date`,distribution_channel_id,sum(price) as price,count(*) as order_num
  179. from orders
  180. where activity_id=$activity_id and `status`='PAID' and from_type='reader' GROUP by date(created_at),distribution_channel_id
  181. ) b on a.`date`=b.`date` and a.distribution_channel_id=b.distribution_channel_id";
  182. $res2 = DB::select($sql3);
  183. foreach ($data as &$v) {
  184. $v['reader_order_num'] = 0;
  185. $v['reader_order_success_num'] = 0;
  186. foreach ($res2 as $val) {
  187. if ($v['date'] == $val->date && $val->distribution_channel_id == $v['distribution_channel_id']) {
  188. $v['reader_order_num'] = $val->order_num ? $val->order_num : 0;
  189. $v['reader_order_success_num'] = $val->success_order ? $val->success_order : 0;
  190. }
  191. }
  192. }
  193. return $data;
  194. }
  195. public static function createActivity(array $param)
  196. {
  197. return Activity::create($param);
  198. }
  199. public static function getActivitySetting()
  200. {
  201. $activity_settng_key = 'activity:setting';
  202. $res = Redis::get($activity_settng_key);
  203. if ($res) {
  204. return json_decode($res, 1);
  205. }
  206. return null;
  207. }
  208. static function ActivityStatsI(int $activity_id): array
  209. {
  210. $activity_info = self::getById($activity_id);
  211. $sql_format = "select
  212. distribution_channel_id,date(created_at) as `day`,COUNT(*) as order_num,
  213. count(case when `status`='PAID' then `status` else null end) as success,
  214. GROUP_CONCAT(case when `status`='PAID' and `from_type`='reader' then uid else null end) as reader_uids,
  215. GROUP_CONCAT(case when `status`='PAID' and `from_type`='signcallback' then uid else null end) as signcallback_uids,
  216. sum(case when `status`='PAID' then `price` else null end) as sums,
  217. count(case when `from_type`='reader' then `uid` else null end) as reader_order,
  218. count(DISTINCT uid) as button_uv,
  219. count(case when `from_type`='reader' and `status`='PAID' then `uid` else null end) as reader_success_order,
  220. count(case when `from_type`='signcallback' then `uid` else null end) as signcallback_order,
  221. count(case when `from_type`='signcallback' and `status`='PAID' then `uid` else null end) as signcallback_success_order
  222. from orders where activity_id=%s
  223. and created_at BETWEEN '%s' and '%s'
  224. GROUP by distribution_channel_id,date(created_at)";
  225. $sql = sprintf($sql_format, $activity_id, $activity_info->start_time, $activity_info->end_time);
  226. $res = DB::select($sql);
  227. $data = [];
  228. foreach ($res as $v) {
  229. $temp['day'] = $v->day;
  230. $temp['siteid'] = $v->distribution_channel_id;
  231. $temp['button_uv'] = $v->button_uv;
  232. $temp['order_num'] = $v->order_num;
  233. $temp['success_order_num'] = $v->success;
  234. $temp['reader_order'] = $v->reader_order;
  235. $temp['reader_success_order'] = $v->reader_success_order;
  236. $temp['signcallback_order'] = $v->signcallback_order;
  237. $temp['signcallback_success_order'] = $v->signcallback_success_order;
  238. $sql1 = 'select ifnull(count(DISTINCT uid),0) as `count` from orders a where activity_id=' . $activity_id . ' and status="PAID" and date(created_at)="' . $v->day . '" and distribution_channel_id= ' . $v->distribution_channel_id . ' and not EXISTS (select uid from orders b where created_at <="' . $activity_info->start_time . '" and b.uid=a.uid and b.status="PAID")';
  239. $res1 = DB::select($sql1);
  240. $temp['first_charge_num'] = $res1[0]->count;
  241. $reader_uv_pv_info = WapVisitStatService::getSitePvAndUvOneDay($v->distribution_channel_id, 'reader', $v->day);
  242. $signcallback_uv_pv_info = WapVisitStatService::getSitePvAndUvOneDay($v->distribution_channel_id, 'signcallback', $v->day);
  243. $pv_uv_info = WapVisitStatService::getActivityUvAndPvByDay($v->distribution_channel_id, $activity_id, $v->day);
  244. $temp['uv'] = $pv_uv_info['uv'];
  245. $temp['reader_uv'] = $reader_uv_pv_info['uv'];
  246. $temp['signcallback_uv'] = $signcallback_uv_pv_info['uv'];
  247. $sq2 = "select count(distinct uid) as `count` from orders WHERE uid in (%s) and created_at<='%s' ";
  248. if ($v->reader_uids) {
  249. $v->reader_uids = rtrim($v->reader_uids, ',');
  250. $temp_reader_second = DB::select(sprintf($sq2, $v->reader_uids, $activity_info->start_time));
  251. $temp['reader_first_charge'] = $v->reader_success_order - (int)$temp_reader_second[0]->count;
  252. } else {
  253. $temp['reader_first_charge'] = 0;
  254. }
  255. if ($v->signcallback_uids) {
  256. $v->signcallback_uids = rtrim($v->signcallback_uids, ',');
  257. $temp_reader_second = DB::select(sprintf($sq2, $v->signcallback_uids, $activity_info->start_time));
  258. $temp['signcallback_first_charge'] = $v->signcallback_success_order - (int)$temp_reader_second[0]->count;
  259. } else {
  260. $temp['signcallback_first_charge'] = 0;
  261. }
  262. $data[] = $temp;
  263. }
  264. return $data;
  265. }
  266. static function yearActivityStats(int $activity_id): array
  267. {
  268. $activity_info = self::getById($activity_id);
  269. $sql_format = "select
  270. distribution_channel_id,date(created_at) as `day`,COUNT(*) as order_num,
  271. count(case when `status`='PAID' then `status` else null end) as success,
  272. GROUP_CONCAT(case when `status`='PAID' then uid else null end) as uids,
  273. sum(case when `status`='PAID' then `price` else null end) as sums,
  274. price from orders where activity_id=%s
  275. and
  276. created_at BETWEEN '%s' and '%s'
  277. GROUP by distribution_channel_id,date(created_at),price";
  278. $sql = sprintf($sql_format, $activity_id, $activity_info->start_time, $activity_info->end_time);
  279. $res = DB::select($sql);
  280. $data = [];
  281. foreach ($res as $v) {
  282. $temp['siteid'] = $v->distribution_channel_id;
  283. $temp['day'] = $v->day;
  284. $temp['price'] = $v->price;
  285. $temp['order_num'] = $v->order_num;
  286. $temp['success'] = $v->success;
  287. $temp['uv'] = 0;
  288. $second_sql_fromat = "SELECT COUNT(DISTINCT uid) as counts FROM orders WHERE uid in (%s) and `status` = 'PAID' and created_at<='%s'";
  289. if ($v->uids) {
  290. $v->uids = rtrim($v->uids, ',');
  291. $second_sql = sprintf($second_sql_fromat, $v->uids, $activity_info->start_time);
  292. $temp_res = DB::select($second_sql);
  293. $temp['first_charge'] = $v->success - (int)$temp_res[0]->counts;
  294. } else {
  295. $temp['first_charge'] = $v->success;
  296. }
  297. $pv_uv_info = WapVisitStatService::getActivityUvAndPvByDay($v->distribution_channel_id, $activity_id, $v->day);
  298. $temp['sums'] = $v->sums;
  299. $temp['uv'] = $pv_uv_info['uv'];
  300. $data[] = $temp;
  301. $temp = null;
  302. }
  303. return $data;
  304. }
  305. public static function getValidActivities()
  306. {
  307. return Activity::getValidActivities();
  308. }
  309. }