Keine Beschreibung

CustomerOrder.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php namespace App\Console\Commands;
  2. use App\Customer;
  3. use App\CustomerOrderDay;
  4. use App\Order;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Symfony\Component\Console\Input\InputOption;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. class CustomerOrder extends Command {
  10. protected $name = 'CustomerOrder';
  11. protected $description = '统计用户订单';
  12. protected $page = 1;
  13. protected $limit = 10;
  14. public function handle() {
  15. try{
  16. $this->getCustomerList();
  17. } catch (\Exception $e) {
  18. echo $e->getMessage();
  19. }
  20. }
  21. public function getCustomerList() {
  22. $page = $this->page;
  23. $offset = ($page - 1) * $this->limit;
  24. //分页获取用户列表数据
  25. $list = Customer::select('id','phone','name','fanTime')->offset($offset)->limit($this->limit)->get();
  26. $list = json_decode(json_encode($list),true);
  27. if(count($list) > 0){
  28. $this->page++;
  29. sleep(1);
  30. $this->updateOrInsert($list);
  31. $this->getCustomerList();
  32. } else {
  33. echo 'SUCCESS';die;
  34. }
  35. }
  36. public function updateOrInsert ( $data ) {
  37. foreach ( $data as $value ) {
  38. //遍历用户 统计每个用户的订单情况
  39. $order = Order::select(DB::raw('count(1) as num,
  40. SUM(receivedAmount) AS totalReceiveAmount,
  41. SUM(freight_cost) AS totalFreightCost,
  42. SUM(aftersale_fee) as totalAftersaleFee,
  43. SUM(refund_price) AS totalRefundPrice,
  44. SUM(cost) as totalCost'))->where('receiverMobile',$value['phone'])->where('is_del',0)->first();
  45. $params['totalReceivedAmount'] = $order->totalReceiveAmount;
  46. $params['totalOrderNumber'] = $order->num;
  47. $params['totalCost'] = $order->totalCost;
  48. $params['totalRefundPrice'] = $order->totalRefundPrice;
  49. $params['totalFreightCost'] = $order->totalFreightCost;
  50. $params['totalAftersaleFee'] = $order->totalAftersaleFee;
  51. $params['grossProfit'] = ($params['totalReceivedAmount'] + $params['totalRefundPrice']) - ($params['totalCost'] + $params['totalFreightCost'] + $params['totalAftersaleFee']);
  52. $params['grossProfitRatio'] = $params['totalReceivedAmount'] == 0 ? 0 : ($params['grossProfit'] / $params['totalReceivedAmount']);
  53. $res = CustomerOrderDay::where('customer_id',$value['id'])->first();
  54. if($res){
  55. CustomerOrderDay::where('customer_id',$value['id'])->update($params);
  56. } else {
  57. $params['customer_id'] = $value['id'];
  58. $params['name'] = $value['name'];
  59. $params['phone'] = $value['phone'];
  60. $params['fanTime'] = $value['fanTime'];
  61. CustomerOrderDay::insert($params);
  62. }
  63. }
  64. }
  65. }