OrderDayStatService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. if(isset($channel_merchant_recharge_amount[0]->amount)){
  86. $data['channel_merchant_recharge_amount'] = (float)$channel_merchant_recharge_amount[0]->amount;
  87. }else{
  88. $data['channel_merchant_recharge_amount'] = 0;
  89. }
  90. //首充数据
  91. $first_recharge_data = OrderService::getChannelDayFirstRechargeData($distribution_channel_id,$date);
  92. $data['first_recharge_user_num'] = $first_recharge_data['count'];
  93. $data['first_recharge_amount'] = $first_recharge_data['amount'];
  94. return OrderDayStat::add($data);
  95. }
  96. static function tempAdd($distribution_channel_id, $channel_user_id, $date, $distribution_channel_name,$real_reg_num)
  97. {
  98. $data = [];
  99. $data['channel_user_id'] = $channel_user_id;
  100. $data['distribution_channel_id'] = $distribution_channel_id;
  101. $data['distribution_channel_name'] = $distribution_channel_name;
  102. $data['date'] = $date;
  103. $data['month'] = date("Y-m",strtotime($date));
  104. $data['pay_success_user_num'] = OrderService::getDayPayUserNumByChannelId($distribution_channel_id, $date);//付费总用户数
  105. $data['ticket_recharge_user_num'] = OrderService::getDayRechargePayUserNumByChannelId($distribution_channel_id, $date);//ticket_recharge_user_num
  106. $data['year_recharge_user_num'] = OrderService::getDayVipPayUserNumByChannelId($distribution_channel_id, $date);//year_recharge_user_num
  107. $end_time = date('Y-m-d H:i:s',strtotime($date) + 86400-1);
  108. $data['total_recharge_amount'] = OrderService::getAmount(['distribution_channel_id'=>$distribution_channel_id,'begin_time'=>$date,'end_time'=>$end_time]);//total_recharge_amount
  109. //$order_info = OrderService::getChannelDateOrderInfo($distribution_channel_id,$date);
  110. $result = Order::where('distribution_channel_id',$distribution_channel_id)->where('created_at','>=',$date)
  111. ->select(DB::raw('status,count(*) as count'))
  112. ->where('created_at','<=',$date.' 23:59:59')->groupBy('status')->get();
  113. $data['unpaid_num'] = 0;
  114. $data['paid_num'] = 0;
  115. foreach ($result as $item){
  116. if($item->status == 'PAID') $data['paid_num'] = $item->count;
  117. if($item->status == 'UNPAID') $data['unpaid_num'] = $item->count;
  118. }
  119. //$data['unpaid_num'] = $order_info['total_num'] - $order_info['paid_number'];
  120. //$data['paid_num'] = $order_info['paid_number'];
  121. $uv_pv = SendOrderService::getChannelPromotionTotalUvPv($distribution_channel_id,$date);
  122. $data['promotion_total_uv'] = $uv_pv['uv'];
  123. $data['promotion_total_pv'] = $uv_pv['pv'];
  124. $data['force_user_num'] = ForceSubscribeService::forceSubscribeUserCountByChannelIdAndDate($distribution_channel_id,$date,'');
  125. //普通充值数据
  126. $ticket_recharge_data = OrderService::getChannelDayTicketRechargeData($distribution_channel_id, $date);
  127. $data['ticket_recharge_paid_num'] = $ticket_recharge_data['ticket_recharge_paid_num'];
  128. $data['ticket_recharge_unpaid_num'] = $ticket_recharge_data['ticket_recharge_unpaid_num'];
  129. $data['ticket_recharge_amount'] = $ticket_recharge_data['ticket_recharge_amount'];
  130. //包年充值数据
  131. $year_recharge_data = OrderService::getChannelDayYearRechargeData($distribution_channel_id, $date);
  132. $data['year_recharge_paid_num'] = $year_recharge_data['year_recharge_paid_num'];
  133. $data['year_recharge_unpaid_num'] = $year_recharge_data['year_recharge_unpaid_num'];
  134. $data['year_recharge_amount'] = $year_recharge_data['year_recharge_amount'];
  135. //派单数
  136. $data['send_order_num'] = SendOrderService::getChannelPromotionCount($distribution_channel_id, $date, $end_time);
  137. //实际派单数
  138. $data['real_send_order_num'] = SendOrderService::getChannelRealPromotionCount($distribution_channel_id, $date, $end_time);
  139. //注册用户数
  140. $data['register_user_num'] = UserService::getChannelDayTotal($distribution_channel_id,$date);
  141. //实际注册用户数
  142. $data['real_register_user_num'] = $real_reg_num;
  143. //注册用户当日充值数据
  144. $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}'");
  145. $data['reg_user_first_recharge_amount'] = (float)$reg_user_data[0]->amount;
  146. $data['reg_user_first_recharge_user_num'] = (float)$reg_user_data[0]->num;
  147. $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'))));
  148. $data['old_user_subscribe_num'] = $old_user_subscribe_num;
  149. $data2 = WapVisitStatService::getReaderUvAndPv($distribution_channel_id, date('Y-m-d',strtotime('-1 day')));
  150. $whole_pv=$whole_uv=0;
  151. if ($data2) {
  152. $whole_uv = $data2['uv'];
  153. $whole_pv = $data2['pv'];
  154. }
  155. try{
  156. $data['whole_site_uv'] =$whole_uv;
  157. $data['whole_site_pv'] =$whole_pv;
  158. $data['auto_force_sub_num']=self::getAutoForceSubUserNum($distribution_channel_id,$date,$end_time);
  159. $data['self_force_sub_num']=self::getSelfForceSubUserNum($distribution_channel_id,$date,$end_time);
  160. }catch (\Exception $e){
  161. \Log::error($e->getMessage());
  162. }
  163. //渠道方通道充值总额
  164. $not_in_pay_merchant_ids = env('not_in_pay_merchant_id');
  165. if($not_in_pay_merchant_ids)
  166. {
  167. $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})
  168. }
  169. $data['channel_merchant_recharge_amount'] = (float)$channel_merchant_recharge_amount[0]->amount;
  170. //首充数据
  171. $first_recharge_data = OrderService::getChannelDayFirstRechargeData($distribution_channel_id,$date);
  172. $data['first_recharge_user_num'] = $first_recharge_data['count'];
  173. $data['first_recharge_amount'] = $first_recharge_data['amount'];
  174. //获取前天用户充值
  175. $dayBeforeYesDay = OrderDayStat::where('distribution_channel_id',$distribution_channel_id)
  176. ->where('date',date('Y-m-d',strtotime($date.' -1 day')))
  177. ->first();
  178. //\Log::info(date('Y-m-d',strtotime($date.' -1 day')));
  179. if($dayBeforeYesDay){
  180. $dayBeforeYesDay = $dayBeforeYesDay->total_recharge_amount;
  181. $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']);
  182. $diff = $dayBeforeYesDayReal-$dayBeforeYesDay;
  183. if($diff>0){
  184. $data['total_recharge_amount'] +=$diff;
  185. \Log::info('distribution_channel_recharge_regards_as_next_day_for_order_day_stats:distribution:'.$distribution_channel_id.':date:'.$date.':diff:'.$diff);
  186. }
  187. }
  188. /*try{
  189. //男女频充值
  190. $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');
  191. $recharge_amount_left = $data['total_recharge_amount']-$recharge_by_gender_raw['amount'];
  192. $recharge_male_channel = $recharge_female_channel = 0;
  193. if(isset($recharge_by_gender_raw[1])) {
  194. $recharge_male_channel = $recharge_by_gender_raw[1]+($recharge_by_gender_raw[1]/$recharge_by_gender_raw['amount'])*$recharge_amount_left;
  195. }
  196. if(isset($recharge_by_gender_raw[2])) {
  197. $recharge_female_channel = $recharge_by_gender_raw[2]+($recharge_by_gender_raw[2]/$recharge_by_gender_raw['amount'])*$recharge_amount_left;
  198. }
  199. $data['male_channel_recharge']=$recharge_male_channel;
  200. $data['female_channel_recharge']=$recharge_female_channel;
  201. }catch (\Exception $exception){
  202. \Log::error($exception->getMessage());
  203. }*/
  204. return OrderDayStat::add($data);
  205. }
  206. /**
  207. * 查询日统计
  208. * @param $params $distribution_channel_id
  209. */
  210. static function search($params, $is_all=false)
  211. {
  212. return OrderDayStat::search($params, $is_all);
  213. }
  214. /**
  215. * 账户级别查询日统计
  216. * @param $params $distribution_channel_id
  217. */
  218. static function accountSearch($params, $is_all=false)
  219. {
  220. return OrderDayStat::accountSearch($params, $is_all);
  221. }
  222. /**
  223. * 获取渠道汇总统计
  224. */
  225. static function getSumByChannelId($distribution_channel_id)
  226. {
  227. return OrderDayStat::getSumByChannelId($distribution_channel_id);
  228. }
  229. /**
  230. * 获取昨日渠道汇总统计
  231. */
  232. static function getYesterdaySumByChannelId($distribution_channel_id)
  233. {
  234. return OrderDayStat::getYesterdaySumByChannelId($distribution_channel_id);
  235. }
  236. /**
  237. * 获取当月渠道汇总统计
  238. */
  239. static function getCurrentMonthSumByChannelId($distribution_channel_id)
  240. {
  241. return OrderDayStat::getCurrentMonthSumByChannelId($distribution_channel_id);
  242. }
  243. /**
  244. * 获取上月渠道汇总统计
  245. */
  246. static function getLastMonthSumByChannelId($distribution_channel_id)
  247. {
  248. return OrderDayStat::getLastMonthSumByChannelId($distribution_channel_id);
  249. }
  250. /**
  251. * 根据渠道获取合计
  252. */
  253. static function getStatByChannel($params)
  254. {
  255. return OrderDayStat::getStatByChannel($params);
  256. }
  257. /**
  258. *
  259. */
  260. static function getAllGroupbyMonth()
  261. {
  262. return OrderDayStat::getAllGroupbyMonth();
  263. }
  264. static function getAllGroupbyMonthChannel($params,$is_all=false)
  265. {
  266. return OrderDayStat::getAllGroupbyMonthChannel($params,$is_all);
  267. }
  268. /**
  269. * 获取外部渠道充值数据
  270. * @param array $params
  271. * @param bool $isAll
  272. */
  273. static function getOutPayData($params = [], $isAll = false) {
  274. return OrderDayStat::getOutPayData($params, $isAll);
  275. }
  276. static function getRechargeByChannels($distribution_channels,$time_range){
  277. return OrderDayStat::getRechargeByChannels($distribution_channels,$time_range);
  278. }
  279. /**
  280. * 获取自动强关用户数
  281. * @param $channel_id
  282. * @param $start
  283. * @param $end
  284. * @return mixed
  285. */
  286. public static function getAutoForceSubUserNum($channel_id,$start,$end){
  287. $obj= DB::table('force_subscribe_users')
  288. ->whereBetween('created_at',[$start,$end])
  289. ->where('official_account_id','<>',1);
  290. if($channel_id) $obj->where('distribution_channel_id',$channel_id);
  291. return $obj->count();
  292. }
  293. /**
  294. * 获取主动关注用户数
  295. * @param $channel_id
  296. * @param $start
  297. * @param $end
  298. * @return mixed
  299. */
  300. public static function getSelfForceSubUserNum($channel_id,$start,$end){
  301. $obj= DB::table('force_subscribe_users')
  302. ->whereBetween('created_at',[$start,$end])
  303. ->where('official_account_id','=',1);
  304. if($channel_id) $obj->where('distribution_channel_id',$channel_id);
  305. return $obj->count();
  306. }
  307. public static function getRechargeByGender($start,$end) {
  308. $res = DB::table('orders')
  309. ->join('books','books.id','=','orders.from_bid')
  310. ->join('book_categories','book_categories.id','=','books.category_id')
  311. ->groupBy('orders.distribution_channel_id')
  312. ->groupBy('book_categories.pid')
  313. ->where('orders.status','PAID')
  314. //->where('orders.distribution_channel_id',$channel_id)
  315. ->whereBetween('orders.created_at',[$start,$end])
  316. ->select(['book_categories.pid','orders.distribution_channel_id',DB::raw('sum(price) as amount')])
  317. ->get();
  318. //$ret = ['amount'=>0];
  319. $ret = [];
  320. foreach ($res as $key=>$value){
  321. if(array_key_exists($value->distribution_channel_id,$ret)) {
  322. $ret[$value->distribution_channel_id][$value->pid] = $value->amount;
  323. $ret[$value->distribution_channel_id]['amount'] += $value->amount;
  324. }else {
  325. $ret[$value->distribution_channel_id] = array('amount'=>$value->amount);
  326. $ret[$value->distribution_channel_id][$value->pid] = $value->amount;
  327. }
  328. }
  329. return $ret;
  330. }
  331. }