ActivityService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /**
  84. * 活动统计
  85. * @param $activity_id
  86. * @param $distribution_channel_id
  87. * @return array
  88. */
  89. static function getActivityUvPv($activity_id, $distribution_channel_id)
  90. {
  91. $activity = self::getById($activity_id);
  92. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  93. $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';
  94. $start = strtotime($activity->start_time);
  95. $end_time = strtotime($activity->end_time);
  96. $end = time() > $end_time ? $end_time : time();
  97. $page_pv = 0;
  98. $page_uv = 0;
  99. $visit_info = WapVisitStatService::getActivityUvAndPv($distribution_channel_id, $activity_id);
  100. $page_pv = $visit_info['pv'];
  101. $page_uv = $visit_info['uv'];
  102. /*while ($start<$end){
  103. $temp_day = date('Y-m-d',$start);
  104. $uv_key = sprintf($uv_key_format, $activity_id, $distribution_channel_id,$temp_day);
  105. $puv = Redis::scard($uv_key);
  106. if($puv) $page_uv += $puv;
  107. $start += 86400;
  108. }
  109. $pv_key = sprintf($pv_key_format, $activity_id, $distribution_channel_id);
  110. $pv_all = Redis::hgetAll($pv_key);
  111. if($pv_all){
  112. foreach ($pv_all as $v){
  113. $page_pv += $v;
  114. }
  115. }*/
  116. $button_pv = Order::where('activity_id', $activity_id)->where('distribution_channel_id', $distribution_channel_id)->count();
  117. $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);
  118. $button_uv = $button_uv[0]->count;
  119. $order_num = Order::where('activity_id', $activity_id)->where('status', 'PAID')->where('distribution_channel_id', $distribution_channel_id)->count();
  120. $order_sum = Order::where('activity_id', $activity_id)->where('status', 'PAID')->where('distribution_channel_id', $distribution_channel_id)->sum('price');
  121. $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)';
  122. $re_order_info = DB::select($sql);
  123. $re_order = $re_order_info[0]->count;
  124. return compact('page_pv', 'page_uv', 'button_pv', 'button_uv', 'order_num', 're_order', 'order_sum');
  125. }
  126. static function ActivityStats($activity_id)
  127. {
  128. $sql = "select a.*,b.price,b.order_num as success_order from(
  129. select date(created_at) as `date`,distribution_channel_id,count(*) as order_num,count(DISTINCT uid) as uv
  130. from orders
  131. where activity_id={$activity_id} GROUP by date(created_at),distribution_channel_id
  132. ) a
  133. left join (
  134. select date(created_at) as `date`,distribution_channel_id,sum(price) as price,count(*) as order_num
  135. from orders
  136. where activity_id={$activity_id} and `status`='PAID' GROUP by date(created_at),distribution_channel_id
  137. ) b on a.`date`=b.`date` and a.distribution_channel_id=b.distribution_channel_id";
  138. $res = DB::select($sql);
  139. $activity = self::getById($activity_id);
  140. $data = [];
  141. $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';
  142. foreach ($res as $v) {
  143. $temp = [];
  144. $temp['date'] = $v->date;
  145. $temp['distribution_channel_id'] = $v->distribution_channel_id;
  146. $temp['button_pv'] = $v->order_num;
  147. $temp['order_num'] = $v->success_order ? $v->success_order : 0;
  148. $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)';
  149. $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)';
  150. $re_order_info = DB::select($sql1);
  151. $re_order_info2 = DB::select($sql2);
  152. $temp['first_charge'] = $re_order_info[0]->count ? $re_order_info[0]->count : 0;
  153. $temp['first_charge_reader'] = $re_order_info2[0]->count ? $re_order_info2[0]->count : 0;
  154. $temp['button_uv'] = $v->uv;
  155. $uv_key = sprintf($uv_key_format, $activity_id, $v->distribution_channel_id, $v->date);
  156. $puv = Redis::scard($uv_key);
  157. $puv = $puv ? $puv : 0;
  158. $temp['page_uv'] = $puv;
  159. $from = 'reader';
  160. $reader_uv = Redis::scard('push:distribution_channel_id:' . $v->distribution_channel_id . 'from:' . $from . ':date:' . $v->date);
  161. $temp['reader_uv'] = $reader_uv ? $reader_uv : 0;
  162. $data[] = $temp;
  163. }
  164. $sql3 = "select a.*,b.price,b.order_num as success_order from(
  165. select date(created_at) as `date`,distribution_channel_id,count(*) as order_num,count(DISTINCT uid) as uv
  166. from orders
  167. where activity_id=$activity_id and from_type='reader' GROUP by date(created_at),distribution_channel_id
  168. ) a
  169. left join (
  170. select date(created_at) as `date`,distribution_channel_id,sum(price) as price,count(*) as order_num
  171. from orders
  172. where activity_id=$activity_id and `status`='PAID' and from_type='reader' GROUP by date(created_at),distribution_channel_id
  173. ) b on a.`date`=b.`date` and a.distribution_channel_id=b.distribution_channel_id";
  174. $res2 = DB::select($sql3);
  175. foreach ($data as &$v) {
  176. $v['reader_order_num'] = 0;
  177. $v['reader_order_success_num'] = 0;
  178. foreach ($res2 as $val) {
  179. if ($v['date'] == $val->date && $val->distribution_channel_id == $v['distribution_channel_id']) {
  180. $v['reader_order_num'] = $val->order_num ? $val->order_num : 0;
  181. $v['reader_order_success_num'] = $val->success_order ? $val->success_order : 0;
  182. }
  183. }
  184. }
  185. return $data;
  186. }
  187. public static function createActivity(array $param)
  188. {
  189. return Activity::create($param);
  190. }
  191. public static function getActivitySetting()
  192. {
  193. $activity_settng_key = 'activity:setting';
  194. $res = Redis::get($activity_settng_key);
  195. if ($res) {
  196. return json_decode($res, 1);
  197. }
  198. return null;
  199. }
  200. static function ActivityStatsI(int $activity_id): array
  201. {
  202. $activity_info = self::getById($activity_id);
  203. $sql_format = "select
  204. distribution_channel_id,date(created_at) as `day`,COUNT(*) as order_num,
  205. count(case when `status`='PAID' then `status` else null end) as success,
  206. GROUP_CONCAT(case when `status`='PAID' and `from_type`='reader' then uid else null end) as reader_uids,
  207. GROUP_CONCAT(case when `status`='PAID' and `from_type`='signcallback' then uid else null end) as signcallback_uids,
  208. sum(case when `status`='PAID' then `price` else null end) as sums,
  209. count(case when `from_type`='reader' then `uid` else null end) as reader_order,
  210. count(DISTINCT uid) as button_uv,
  211. count(case when `from_type`='reader' and `status`='PAID' then `uid` else null end) as reader_success_order,
  212. count(case when `from_type`='signcallback' then `uid` else null end) as signcallback_order,
  213. count(case when `from_type`='signcallback' and `status`='PAID' then `uid` else null end) as signcallback_success_order
  214. from orders where activity_id=%s
  215. and created_at BETWEEN '%s' and '%s'
  216. GROUP by distribution_channel_id,date(created_at)";
  217. $sql = sprintf($sql_format, $activity_id, $activity_info->start_time, $activity_info->end_time);
  218. $res = DB::select($sql);
  219. $data = [];
  220. foreach ($res as $v) {
  221. $temp['day'] = $v->day;
  222. $temp['siteid'] = $v->distribution_channel_id;
  223. $temp['button_uv'] = $v->button_uv;
  224. $temp['order_num'] = $v->order_num;
  225. $temp['success_order_num'] = $v->success;
  226. $temp['reader_order'] = $v->reader_order;
  227. $temp['reader_success_order'] = $v->reader_success_order;
  228. $temp['signcallback_order'] = $v->signcallback_order;
  229. $temp['signcallback_success_order'] = $v->signcallback_success_order;
  230. $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")';
  231. $res1 = DB::select($sql1);
  232. $temp['first_charge_num'] = $res1[0]->count;
  233. $reader_uv_pv_info = WapVisitStatService::getSitePvAndUvOneDay($v->distribution_channel_id, 'reader', $v->day);
  234. $signcallback_uv_pv_info = WapVisitStatService::getSitePvAndUvOneDay($v->distribution_channel_id, 'signcallback', $v->day);
  235. $pv_uv_info = WapVisitStatService::getActivityUvAndPvByDay($v->distribution_channel_id, $activity_id, $v->day);
  236. $temp['uv'] = $pv_uv_info['uv'];
  237. $temp['reader_uv'] = $reader_uv_pv_info['uv'];
  238. $temp['signcallback_uv'] = $signcallback_uv_pv_info['uv'];
  239. $sq2 = "select count(distinct uid) as `count` from orders WHERE uid in (%s) and created_at<='%s' ";
  240. if ($v->reader_uids) {
  241. $v->reader_uids = rtrim($v->reader_uids, ',');
  242. $temp_reader_second = DB::select(sprintf($sq2, $v->reader_uids, $activity_info->start_time));
  243. $temp['reader_first_charge'] = $v->reader_success_order - (int)$temp_reader_second[0]->count;
  244. } else {
  245. $temp['reader_first_charge'] = 0;
  246. }
  247. if ($v->signcallback_uids) {
  248. $v->signcallback_uids = rtrim($v->signcallback_uids, ',');
  249. $temp_reader_second = DB::select(sprintf($sq2, $v->signcallback_uids, $activity_info->start_time));
  250. $temp['signcallback_first_charge'] = $v->signcallback_success_order - (int)$temp_reader_second[0]->count;
  251. } else {
  252. $temp['signcallback_first_charge'] = 0;
  253. }
  254. $data[] = $temp;
  255. }
  256. return $data;
  257. }
  258. static function yearActivityStats(int $activity_id): array
  259. {
  260. $activity_info = self::getById($activity_id);
  261. $sql_format = "select
  262. distribution_channel_id,date(created_at) as `day`,COUNT(*) as order_num,
  263. count(case when `status`='PAID' then `status` else null end) as success,
  264. GROUP_CONCAT(case when `status`='PAID' then uid else null end) as uids,
  265. sum(case when `status`='PAID' then `price` else null end) as sums,
  266. price from orders where activity_id=%s
  267. and
  268. created_at BETWEEN '%s' and '%s'
  269. GROUP by distribution_channel_id,date(created_at),price";
  270. $sql = sprintf($sql_format, $activity_id, $activity_info->start_time, $activity_info->end_time);
  271. $res = DB::select($sql);
  272. $data = [];
  273. foreach ($res as $v) {
  274. $temp['siteid'] = $v->distribution_channel_id;
  275. $temp['day'] = $v->day;
  276. $temp['price'] = $v->price;
  277. $temp['order_num'] = $v->order_num;
  278. $temp['success'] = $v->success;
  279. $temp['uv'] = 0;
  280. $second_sql_fromat = "SELECT COUNT(DISTINCT uid) as counts FROM orders WHERE uid in (%s) and `status` = 'PAID' and created_at<='%s'";
  281. if ($v->uids) {
  282. $v->uids = rtrim($v->uids, ',');
  283. $second_sql = sprintf($second_sql_fromat, $v->uids, $activity_info->start_time);
  284. $temp_res = DB::select($second_sql);
  285. $temp['first_charge'] = $v->success - (int)$temp_res[0]->counts;
  286. } else {
  287. $temp['first_charge'] = $v->success;
  288. }
  289. $pv_uv_info = WapVisitStatService::getActivityUvAndPvByDay($v->distribution_channel_id, $activity_id, $v->day);
  290. $temp['sums'] = $v->sums;
  291. $temp['uv'] = $pv_uv_info['uv'];
  292. $data[] = $temp;
  293. $temp = null;
  294. }
  295. return $data;
  296. }
  297. }