MemberLowerGroupOrderJob.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/10/31
  6. * Time: 14:55
  7. */
  8. namespace app\Jobs;
  9. use app\backend\modules\charts\modules\member\models\MemberLowerGroupOrder;
  10. use app\common\models\MemberShopInfo;
  11. use Illuminate\Bus\Queueable;
  12. use Illuminate\Contracts\Queue\ShouldQueue;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. use Illuminate\Queue\SerializesModels;
  15. use Illuminate\Support\Facades\DB;
  16. use Yunshop\ShareChain\common\model\ShopMemberLevel;
  17. class MemberLowerGroupOrderJob implements ShouldQueue
  18. {
  19. use InteractsWithQueue, Queueable, SerializesModels;
  20. public $uniacid;
  21. public $insert_data;
  22. public function __construct()
  23. {
  24. }
  25. public function handle()
  26. {
  27. $this->memberOrder();
  28. }
  29. public function memberOrder()
  30. {
  31. $orders = [];
  32. $order_data = DB::select('select uid, SUM(price) as amount, SUM(goods_total) as goods_count from '. DB::getTablePrefix() . 'yz_order where status > 0 and uniacid =' . \YunShop::app()->uniacid .' group by uid');
  33. foreach ($order_data as $order) {
  34. $orders[$order['uid']] = $order;
  35. }
  36. unset($order_data);
  37. $members = DB::select('select member_id, parent_id from '. DB::getTablePrefix() . 'yz_member where uniacid =' . \YunShop::app()->uniacid );
  38. $tree = $this->tree($members);
  39. unset($members);
  40. $this->getTreeData($tree,$orders);
  41. unset($tree[0]);
  42. $insert_data = array_chunk($this->insert_data,2000);
  43. foreach ($insert_data as $k=>$v){
  44. MemberLowerGroupOrder::insert($v);
  45. }
  46. }
  47. public function tree($members)
  48. {
  49. $items = [];
  50. foreach ($members as $member) {
  51. $items[$member['member_id']] = $member;
  52. }
  53. foreach ($items as $item) {
  54. $items[$item['parent_id']]['son'][$item['member_id']] = &$items[$item['member_id']];
  55. }
  56. return isset($items[0]['son']) ? $items[0]['son'] : [];
  57. }
  58. function getTreeData($tree, $orders)
  59. {
  60. $data = [];
  61. foreach($tree as $kk => $t){
  62. $this_insert_data = [
  63. 'uid'=>$t['member_id'],
  64. 'uniacid'=>\YunShop::app()->uniacid,
  65. 'amount'=>0,
  66. 'goods_count'=>0,
  67. 'pay_count'=>0,
  68. 'team_count'=>0,
  69. 'created_at'=>time()
  70. ];
  71. $this_order_data = [];
  72. if(isset($t['son'])){
  73. $this_order_data = $this->getTreeData($t['son'], $orders);
  74. $this_insert_data['amount'] = $this_order_data['amount'];
  75. $this_insert_data['goods_count'] = $this_order_data['goods_count'];
  76. $this_insert_data['pay_count'] = $this_order_data['pay_count'];
  77. $this_insert_data['team_count'] = $this_order_data['team_count'];
  78. }
  79. $data['amount'] += $orders[$t['member_id']]['amount'] + $this_order_data['amount'];
  80. $data['goods_count'] += $orders[$t['member_id']]['goods_count'] + $this_order_data['goods_count'];
  81. $pay_count = isset($orders[$t['member_id']]) ? 1 : 0;
  82. $data['pay_count'] += $this_order_data['pay_count'] + $pay_count;
  83. $data['team_count'] += $this_order_data['team_count'] + 1;
  84. $this->insert_data[$kk] = $this_insert_data;
  85. }
  86. return $data;
  87. }
  88. /**
  89. * Execute the job.
  90. *
  91. * @return void
  92. */
  93. public function memberOrder2(){
  94. $time = time();
  95. \Log::debug('团队支付订单排行定时任务开始执行,公众号id为', \YunShop::app()->uniacid);
  96. $t1 = microtime(true);
  97. $insert_data = [];
  98. //查询归类所有订单开始
  99. $order_data = DB::table('yz_order')
  100. ->selectRaw('uid, SUM(price) as amount, COUNT(id) as count, SUM(goods_total) as goods_count')
  101. ->where('status','>',0)
  102. ->where('uniacid', \YunShop::app()->uniacid)
  103. ->groupBy('uid')
  104. ->get();
  105. $members = DB::select('select member_id from '. DB::getTablePrefix() . 'yz_member where uniacid =' . \YunShop::app()->uniacid );
  106. $child = DB::table('yz_member_children')
  107. ->select(['child_id','member_id'])
  108. ->where('uniacid', \YunShop::app()->uniacid)
  109. ->get();
  110. foreach ($members as $member){
  111. $memberID = $member['member_id'];
  112. $this_insert_data = [
  113. 'uid' => $member['member_id'],
  114. 'uniacid'=>\YunShop::app()->uniacid,
  115. 'amount'=>0,
  116. 'goods_count'=>0,
  117. 'pay_count'=>0,
  118. 'team_count'=>$child->where('member_id', $member['member_id'])->count(),
  119. // 'team_count'=>$child->filter(function($user) use ($memberID) {
  120. // return $user['member_id'] === $memberID;
  121. // })->count(),
  122. 'created_at'=>$time
  123. ];
  124. // $total_member = DB::table('yz_member_children')->where('member_id', $member['member_id'])->pluck('child_id');
  125. $total_member = $child->where('member_id', $member['member_id'])->pluck('child_id');
  126. // $total_member = $child->filter(function($user) use ($memberID) {
  127. // return $user['member_id'] === $memberID;
  128. // })->pluck('child_id');
  129. // dd($total_member, $total_member2);
  130. if (!$total_member->isEmpty()) {
  131. $child_member = $order_data->whereIn('uid', $total_member);
  132. // $child_member = $order_data->filter(function($user) use ($total_member) {
  133. // return in_array($user['uid'],$total_member->toArray());
  134. // });
  135. $this_insert_data['pay_count'] = $child_member->sum('count');
  136. $this_insert_data['amount'] = $child_member->sum('amount');
  137. $this_insert_data['goods_count'] = $child_member->sum('goods_count');
  138. }
  139. $insert_data[] = $this_insert_data;
  140. unset($total_member);
  141. unset($child_member);
  142. unset($this_insert_data);
  143. }
  144. $insert_data = array_chunk($insert_data,2000);
  145. foreach ($insert_data as $k=>$v){
  146. MemberLowerGroupOrder::insert($v);
  147. }
  148. \Log::debug('团队支付订单排行定时任务执行结束,公众号id为', \YunShop::app()->uniacid);
  149. }
  150. public function memberOrder1(){
  151. $time = time();
  152. $insert_data = [];
  153. $order_data = DB::select('select uid, SUM(price) as amount, COUNT(id) as count ,SUM(goods_total) as goods_count from '. DB::getTablePrefix() . 'yz_order where status > 0 and uniacid =' . \YunShop::app()->uniacid .' group by uid');
  154. $order_data = array_column($order_data, null, 'uid');
  155. $members = DB::select('select member_id from '. DB::getTablePrefix() . 'yz_member where uniacid =' . \YunShop::app()->uniacid );
  156. foreach ($members as $v){
  157. $child = DB::select('select child_id, level from '. DB::getTablePrefix() . 'yz_member_children where member_id = '.$v['member_id'].' and uniacid =' . \YunShop::app()->uniacid );
  158. $this_insert_data = [
  159. 'uid'=>$v['member_id'],
  160. 'uniacid'=>\YunShop::app()->uniacid,
  161. 'amount'=>0,
  162. 'goods_count'=>0,
  163. 'pay_count'=>0,
  164. 'team_count'=>count($child),
  165. 'created_at'=>$time
  166. ];
  167. if (!empty($child)) {
  168. $this_child = array_column($child, 'level', 'child_id');
  169. foreach ($this_child as $kk=>$vv){
  170. if (!isset($order_data[$kk])) continue;
  171. $this_order_data = $order_data[$kk];
  172. $this_insert_data['pay_count']++;
  173. $this_insert_data['goods_count'] += $this_order_data['goods_count'];
  174. $this_insert_data['amount'] += $this_order_data['amount'];
  175. }
  176. }
  177. $insert_data[] = $this_insert_data;
  178. }
  179. $insert_data = array_chunk($insert_data,2000);
  180. foreach ($insert_data as $k=>$v){
  181. MemberLowerGroupOrder::insert($v);
  182. }
  183. }
  184. }