OrderDayStatService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/21
  6. * Time: 10:42
  7. */
  8. namespace App\Modules\Trade\Services;
  9. use App\Modules\Statistic\Services\WapVisitStatService;
  10. use App\Modules\Trade\Services\OrderService;
  11. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  12. use App\Modules\SendOrder\Services\SendOrderService;
  13. use App\Modules\User\Services\UserService;
  14. use App\Modules\Trade\Models\OrderDayStat;
  15. use App\Modules\Trade\Models\Order;
  16. use DB;
  17. class OrderDayStatService
  18. {
  19. //创建日统计数据
  20. //'distribution_channel_id', 'date','pay_success_user_num', 'ticket_recharge_user_num', 'year_recharge_user_num', 'total_recharge_amount','unpaid_num','paid_num','current_month_unpaid_num','promotion_total_uv','promotion_total_pv','force_user_num'
  21. static function add($distribution_channel_id, $channel_user_id, $date, $distribution_channel_name,$real_reg_num)
  22. {
  23. $data = [];
  24. $data['channel_user_id'] = $channel_user_id;
  25. $data['distribution_channel_id'] = $distribution_channel_id;
  26. $data['distribution_channel_name'] = $distribution_channel_name;
  27. $data['date'] = $date;
  28. $data['month'] = date("Y-m",strtotime($date));
  29. $data['pay_success_user_num'] = OrderService::getDayPayUserNumByChannelId($distribution_channel_id, $date);//付费总用户数
  30. $data['ticket_recharge_user_num'] = OrderService::getDayRechargePayUserNumByChannelId($distribution_channel_id, $date);//ticket_recharge_user_num
  31. $data['year_recharge_user_num'] = OrderService::getDayVipPayUserNumByChannelId($distribution_channel_id, $date);//year_recharge_user_num
  32. $end_time = date('Y-m-d H:i:s',strtotime($date) + 86400-1);
  33. $data['total_recharge_amount'] = OrderService::getAmount(['distribution_channel_id'=>$distribution_channel_id,'begin_time'=>$date,'end_time'=>$end_time]);//total_recharge_amount
  34. $order_info = OrderService::getChannelDateOrderInfo($distribution_channel_id,$date);
  35. $data['unpaid_num'] = $order_info['total_num'] - $order_info['paid_number'];
  36. $data['paid_num'] = $order_info['paid_number'];
  37. $uv_pv = SendOrderService::getChannelPromotionTotalUvPv($distribution_channel_id,$date);
  38. $data['promotion_total_uv'] = $uv_pv['uv'];
  39. $data['promotion_total_pv'] = $uv_pv['pv'];
  40. $data['force_user_num'] = ForceSubscribeService::forceSubscribeUserCountByChannelIdAndDate($distribution_channel_id,$date,'');
  41. //普通充值数据
  42. $ticket_recharge_data = OrderService::getChannelDayTicketRechargeData($distribution_channel_id, $date);
  43. $data['ticket_recharge_paid_num'] = $ticket_recharge_data['ticket_recharge_paid_num'];
  44. $data['ticket_recharge_unpaid_num'] = $ticket_recharge_data['ticket_recharge_unpaid_num'];
  45. $data['ticket_recharge_amount'] = $ticket_recharge_data['ticket_recharge_amount'];
  46. //包年充值数据
  47. $year_recharge_data = OrderService::getChannelDayYearRechargeData($distribution_channel_id, $date);
  48. $data['year_recharge_paid_num'] = $year_recharge_data['year_recharge_paid_num'];
  49. $data['year_recharge_unpaid_num'] = $year_recharge_data['year_recharge_unpaid_num'];
  50. $data['year_recharge_amount'] = $year_recharge_data['year_recharge_amount'];
  51. //派单数
  52. $data['send_order_num'] = SendOrderService::getChannelPromotionCount($distribution_channel_id, $date, $end_time);
  53. //实际派单数
  54. $data['real_send_order_num'] = SendOrderService::getChannelRealPromotionCount($distribution_channel_id, $date, $end_time);
  55. //注册用户数
  56. $data['register_user_num'] = UserService::getChannelDayTotal($distribution_channel_id,$date);
  57. //实际注册用户数
  58. $data['real_register_user_num'] = $real_reg_num;
  59. //注册用户当日充值数据
  60. $reg_user_data = DB::select("select count(distinct uid) num,sum(price) amount from orders,users where orders.distribution_channel_id = '{$distribution_channel_id}' and orders.created_at > '{$date}' and orders.created_at <= '{$end_time}' and status ='PAID' and users.id = orders.uid and users.created_at > '{$date}' and users.created_at < '{$end_time}'");
  61. $data['reg_user_first_recharge_amount'] = (float)$reg_user_data[0]->amount;
  62. $data['reg_user_first_recharge_user_num'] = (float)$reg_user_data[0]->num;
  63. $old_user_subscribe_num = ForceSubscribeService::getOldUserNewSubsribeNum(array('distribution_channel_id'=>$distribution_channel_id,'start_time'=>date('Y-m-d',strtotime('-1 day')),'end_time'=>date('Y-m-d 23:59:59',strtotime('-1 day'))));
  64. $data['old_user_subscribe_num'] = $old_user_subscribe_num;
  65. $data2 = WapVisitStatService::getReaderUvAndPv($distribution_channel_id, date('Y-m-d',strtotime('-1 day')));
  66. $whole_pv=$whole_uv=0;
  67. if ($data2) {
  68. $whole_uv = $data2['uv'];
  69. $whole_pv = $data2['pv'];
  70. }
  71. try{
  72. $data['whole_site_uv'] =$whole_uv;
  73. $data['whole_site_pv'] =$whole_pv;
  74. $data['auto_force_sub_num']=self::getAutoForceSubUserNum($distribution_channel_id,$date,$end_time);
  75. $data['self_force_sub_num']=self::getSelfForceSubUserNum($distribution_channel_id,$date,$end_time);
  76. }catch (\Exception $e){
  77. \Log::error($e->getMessage());
  78. }
  79. //渠道方通道充值总额
  80. $not_in_pay_merchant_ids = env('not_in_pay_merchant_id');
  81. if($not_in_pay_merchant_ids)
  82. {
  83. $channel_merchant_recharge_amount = DB::select("select sum(price) amount from orders join pay_merchants on pay_merchants.id=orders.pay_merchant_id where orders.distribution_channel_id = '{$distribution_channel_id}' and orders.created_at > '{$date}' and orders.created_at <= '{$end_time}' and orders.status ='PAID' and pay_merchants.is_self_channel <> 1 and orders.pay_merchant_id not in (44,41)");//pay_merchant_id in ({$not_in_pay_merchant_ids})
  84. }
  85. $data['channel_merchant_recharge_amount'] = (float)$channel_merchant_recharge_amount[0]->amount;
  86. //首充数据
  87. $first_recharge_data = OrderService::getChannelDayFirstRechargeData($distribution_channel_id,$date);
  88. $data['first_recharge_user_num'] = $first_recharge_data['count'];
  89. $data['first_recharge_amount'] = $first_recharge_data['amount'];
  90. return OrderDayStat::add($data);
  91. }
  92. static function tempAdd($distribution_channel_id, $channel_user_id, $date, $distribution_channel_name,$real_reg_num)
  93. {
  94. $data = [];
  95. $data['channel_user_id'] = $channel_user_id;
  96. $data['distribution_channel_id'] = $distribution_channel_id;
  97. $data['distribution_channel_name'] = $distribution_channel_name;
  98. $data['date'] = $date;
  99. $data['month'] = date("Y-m",strtotime($date));
  100. $data['pay_success_user_num'] = OrderService::getDayPayUserNumByChannelId($distribution_channel_id, $date);//付费总用户数
  101. $data['ticket_recharge_user_num'] = OrderService::getDayRechargePayUserNumByChannelId($distribution_channel_id, $date);//ticket_recharge_user_num
  102. $data['year_recharge_user_num'] = OrderService::getDayVipPayUserNumByChannelId($distribution_channel_id, $date);//year_recharge_user_num
  103. $end_time = date('Y-m-d H:i:s',strtotime($date) + 86400-1);
  104. $data['total_recharge_amount'] = OrderService::getAmount(['distribution_channel_id'=>$distribution_channel_id,'begin_time'=>$date,'end_time'=>$end_time]);//total_recharge_amount
  105. //$order_info = OrderService::getChannelDateOrderInfo($distribution_channel_id,$date);
  106. $result = Order::where('distribution_channel_id',$distribution_channel_id)->where('created_at','>=',$date)
  107. ->select(DB::raw('status,count(*) as count'))
  108. ->where('created_at','<=',$date.' 23:59:59')->groupBy('status')->get();
  109. $data['unpaid_num'] = 0;
  110. $data['paid_num'] = 0;
  111. foreach ($result as $item){
  112. if($item->status == 'PAID') $data['paid_num'] = $item->count;
  113. if($item->status == 'UNPAID') $data['unpaid_num'] = $item->count;
  114. }
  115. //$data['unpaid_num'] = $order_info['total_num'] - $order_info['paid_number'];
  116. //$data['paid_num'] = $order_info['paid_number'];
  117. $uv_pv = SendOrderService::getChannelPromotionTotalUvPv($distribution_channel_id,$date);
  118. $data['promotion_total_uv'] = $uv_pv['uv'];
  119. $data['promotion_total_pv'] = $uv_pv['pv'];
  120. $data['force_user_num'] = ForceSubscribeService::forceSubscribeUserCountByChannelIdAndDate($distribution_channel_id,$date,'');
  121. //普通充值数据
  122. $ticket_recharge_data = OrderService::getChannelDayTicketRechargeData($distribution_channel_id, $date);
  123. $data['ticket_recharge_paid_num'] = $ticket_recharge_data['ticket_recharge_paid_num'];
  124. $data['ticket_recharge_unpaid_num'] = $ticket_recharge_data['ticket_recharge_unpaid_num'];
  125. $data['ticket_recharge_amount'] = $ticket_recharge_data['ticket_recharge_amount'];
  126. //包年充值数据
  127. $year_recharge_data = OrderService::getChannelDayYearRechargeData($distribution_channel_id, $date);
  128. $data['year_recharge_paid_num'] = $year_recharge_data['year_recharge_paid_num'];
  129. $data['year_recharge_unpaid_num'] = $year_recharge_data['year_recharge_unpaid_num'];
  130. $data['year_recharge_amount'] = $year_recharge_data['year_recharge_amount'];
  131. //派单数
  132. $data['send_order_num'] = SendOrderService::getChannelPromotionCount($distribution_channel_id, $date, $end_time);
  133. //实际派单数
  134. $data['real_send_order_num'] = SendOrderService::getChannelRealPromotionCount($distribution_channel_id, $date, $end_time);
  135. //注册用户数
  136. $data['register_user_num'] = UserService::getChannelDayTotal($distribution_channel_id,$date);
  137. //实际注册用户数
  138. $data['real_register_user_num'] = $real_reg_num;
  139. //注册用户当日充值数据
  140. $reg_user_data = DB::select("select count(distinct uid) num,sum(price) amount from orders,users where orders.distribution_channel_id = '{$distribution_channel_id}' and orders.created_at > '{$date}' and orders.created_at <= '{$end_time}' and status ='PAID' and users.id = orders.uid and users.created_at > '{$date}' and users.created_at < '{$end_time}'");
  141. $data['reg_user_first_recharge_amount'] = (float)$reg_user_data[0]->amount;
  142. $data['reg_user_first_recharge_user_num'] = (float)$reg_user_data[0]->num;
  143. $old_user_subscribe_num = ForceSubscribeService::getOldUserNewSubsribeNum(array('distribution_channel_id'=>$distribution_channel_id,'start_time'=>date('Y-m-d',strtotime('-1 day')),'end_time'=>date('Y-m-d 23:59:59',strtotime('-1 day'))));
  144. $data['old_user_subscribe_num'] = $old_user_subscribe_num;
  145. $data2 = WapVisitStatService::getReaderUvAndPv($distribution_channel_id, date('Y-m-d',strtotime('-1 day')));
  146. $whole_pv=$whole_uv=0;
  147. if ($data2) {
  148. $whole_uv = $data2['uv'];
  149. $whole_pv = $data2['pv'];
  150. }
  151. try{
  152. $data['whole_site_uv'] =$whole_uv;
  153. $data['whole_site_pv'] =$whole_pv;
  154. $data['auto_force_sub_num']=self::getAutoForceSubUserNum($distribution_channel_id,$date,$end_time);
  155. $data['self_force_sub_num']=self::getSelfForceSubUserNum($distribution_channel_id,$date,$end_time);
  156. }catch (\Exception $e){
  157. \Log::error($e->getMessage());
  158. }
  159. //渠道方通道充值总额
  160. $not_in_pay_merchant_ids = env('not_in_pay_merchant_id');
  161. if($not_in_pay_merchant_ids)
  162. {
  163. $channel_merchant_recharge_amount = DB::select("select sum(price) amount from orders join pay_merchants on pay_merchants.id=orders.pay_merchant_id where orders.distribution_channel_id = '{$distribution_channel_id}' and orders.created_at > '{$date}' and orders.created_at <= '{$end_time}' and orders.status ='PAID' and pay_merchants.is_self_channel <> 1 and orders.pay_merchant_id not in (44,41)");//pay_merchant_id in ({$not_in_pay_merchant_ids})
  164. }
  165. $data['channel_merchant_recharge_amount'] = (float)$channel_merchant_recharge_amount[0]->amount;
  166. //首充数据
  167. $first_recharge_data = OrderService::getChannelDayFirstRechargeData($distribution_channel_id,$date);
  168. $data['first_recharge_user_num'] = $first_recharge_data['count'];
  169. $data['first_recharge_amount'] = $first_recharge_data['amount'];
  170. //获取前天用户充值
  171. $dayBeforeYesDay = OrderDayStat::where('distribution_channel_id',$distribution_channel_id)
  172. ->where('date',date('Y-m-d',strtotime($date.' -1 day')))
  173. ->first();
  174. //\Log::info(date('Y-m-d',strtotime($date.' -1 day')));
  175. if($dayBeforeYesDay){
  176. $dayBeforeYesDay = $dayBeforeYesDay->total_recharge_amount;
  177. $dayBeforeYesDayReal = OrderService::getAmount(['distribution_channel_id'=>$distribution_channel_id,'begin_time'=>date('Y-m-d',strtotime($date.' -1 day')),'end_time'=>date('Y-m-d',strtotime($date.' -1 day')).' 23:59:59']);
  178. $diff = $dayBeforeYesDayReal-$dayBeforeYesDay;
  179. if($diff>0){
  180. $data['total_recharge_amount'] +=$diff;
  181. \Log::info('distribution_channel_recharge_regards_as_next_day_for_order_day_stats:distribution:'.$distribution_channel_id.':date:'.$date.':diff:'.$diff);
  182. }
  183. }
  184. /*try{
  185. //男女频充值
  186. $recharge_by_gender_raw = self::getRechargeByGender($distribution_channel_id,date('Y-m-d',strtotime($date.' -1 day')),date('Y-m-d',strtotime($date.' -1 day')).' 23:59:59');
  187. $recharge_amount_left = $data['total_recharge_amount']-$recharge_by_gender_raw['amount'];
  188. $recharge_male_channel = $recharge_female_channel = 0;
  189. if(isset($recharge_by_gender_raw[1])) {
  190. $recharge_male_channel = $recharge_by_gender_raw[1]+($recharge_by_gender_raw[1]/$recharge_by_gender_raw['amount'])*$recharge_amount_left;
  191. }
  192. if(isset($recharge_by_gender_raw[2])) {
  193. $recharge_female_channel = $recharge_by_gender_raw[2]+($recharge_by_gender_raw[2]/$recharge_by_gender_raw['amount'])*$recharge_amount_left;
  194. }
  195. $data['male_channel_recharge']=$recharge_male_channel;
  196. $data['female_channel_recharge']=$recharge_female_channel;
  197. }catch (\Exception $exception){
  198. \Log::error($exception->getMessage());
  199. }*/
  200. return OrderDayStat::add($data);
  201. }
  202. /**
  203. * 查询日统计
  204. * @param $params $distribution_channel_id
  205. */
  206. static function search($params, $is_all=false)
  207. {
  208. return OrderDayStat::search($params, $is_all);
  209. }
  210. /**
  211. * 账户级别查询日统计
  212. * @param $params $distribution_channel_id
  213. */
  214. static function accountSearch($params, $is_all=false)
  215. {
  216. return OrderDayStat::accountSearch($params, $is_all);
  217. }
  218. /**
  219. * 获取渠道汇总统计
  220. */
  221. static function getSumByChannelId($distribution_channel_id)
  222. {
  223. return OrderDayStat::getSumByChannelId($distribution_channel_id);
  224. }
  225. /**
  226. * 获取昨日渠道汇总统计
  227. */
  228. static function getYesterdaySumByChannelId($distribution_channel_id)
  229. {
  230. return OrderDayStat::getYesterdaySumByChannelId($distribution_channel_id);
  231. }
  232. /**
  233. * 获取当月渠道汇总统计
  234. */
  235. static function getCurrentMonthSumByChannelId($distribution_channel_id)
  236. {
  237. return OrderDayStat::getCurrentMonthSumByChannelId($distribution_channel_id);
  238. }
  239. /**
  240. * 获取上月渠道汇总统计
  241. */
  242. static function getLastMonthSumByChannelId($distribution_channel_id)
  243. {
  244. return OrderDayStat::getLastMonthSumByChannelId($distribution_channel_id);
  245. }
  246. /**
  247. * 根据渠道获取合计
  248. */
  249. static function getStatByChannel($params)
  250. {
  251. return OrderDayStat::getStatByChannel($params);
  252. }
  253. /**
  254. *
  255. */
  256. static function getAllGroupbyMonth()
  257. {
  258. return OrderDayStat::getAllGroupbyMonth();
  259. }
  260. static function getAllGroupbyMonthChannel($params,$is_all=false)
  261. {
  262. return OrderDayStat::getAllGroupbyMonthChannel($params,$is_all);
  263. }
  264. /**
  265. * 获取外部渠道充值数据
  266. * @param array $params
  267. * @param bool $isAll
  268. */
  269. static function getOutPayData($params = [], $isAll = false) {
  270. return OrderDayStat::getOutPayData($params, $isAll);
  271. }
  272. static function getRechargeByChannels($distribution_channels,$time_range){
  273. return OrderDayStat::getRechargeByChannels($distribution_channels,$time_range);
  274. }
  275. /**
  276. * 获取自动强关用户数
  277. * @param $channel_id
  278. * @param $start
  279. * @param $end
  280. * @return mixed
  281. */
  282. public static function getAutoForceSubUserNum($channel_id,$start,$end){
  283. $obj= DB::table('force_subscribe_users')
  284. ->whereBetween('created_at',[$start,$end])
  285. ->where('official_account_id','<>',1);
  286. if($channel_id) $obj->where('distribution_channel_id',$channel_id);
  287. return $obj->count();
  288. }
  289. /**
  290. * 获取主动关注用户数
  291. * @param $channel_id
  292. * @param $start
  293. * @param $end
  294. * @return mixed
  295. */
  296. public static function getSelfForceSubUserNum($channel_id,$start,$end){
  297. $obj= DB::table('force_subscribe_users')
  298. ->whereBetween('created_at',[$start,$end])
  299. ->where('official_account_id','=',1);
  300. if($channel_id) $obj->where('distribution_channel_id',$channel_id);
  301. return $obj->count();
  302. }
  303. public static function getRechargeByGender($start,$end) {
  304. $res = DB::table('orders')
  305. ->join('books','books.id','=','orders.from_bid')
  306. ->join('book_categories','book_categories.id','=','books.category_id')
  307. ->groupBy('orders.distribution_channel_id')
  308. ->groupBy('book_categories.pid')
  309. ->where('orders.status','PAID')
  310. //->where('orders.distribution_channel_id',$channel_id)
  311. ->whereBetween('orders.created_at',[$start,$end])
  312. ->select(['book_categories.pid','orders.distribution_channel_id',DB::raw('sum(price) as amount')])
  313. ->get();
  314. //$ret = ['amount'=>0];
  315. $ret = [];
  316. foreach ($res as $key=>$value){
  317. if(array_key_exists($value->distribution_channel_id,$ret)) {
  318. $ret[$value->distribution_channel_id][$value->pid] = $value->amount;
  319. $ret[$value->distribution_channel_id]['amount'] += $value->amount;
  320. }else {
  321. $ret[$value->distribution_channel_id] = array('amount'=>$value->amount);
  322. $ret[$value->distribution_channel_id][$value->pid] = $value->amount;
  323. }
  324. }
  325. return $ret;
  326. }
  327. }