123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php namespace App\Console\Commands;
- use App\Customer;
- use App\CustomerOrderDay;
- use App\Order;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Input\InputArgument;
- class CustomerOrder extends Command {
- protected $name = 'CustomerOrder';
- protected $description = '统计用户订单';
- protected $page = 1;
- protected $limit = 10;
- public function handle() {
- try{
- $this->getCustomerList();
- } catch (\Exception $e) {
- echo $e->getMessage();
- }
- }
- public function getCustomerList() {
- $page = $this->page;
- $offset = ($page - 1) * $this->limit;
- //分页获取用户列表数据
- $list = Customer::select('id','phone','name','fanTime')->offset($offset)->limit($this->limit)->get();
- $list = json_decode(json_encode($list),true);
- if(count($list) > 0){
- $this->page++;
- sleep(1);
- $this->updateOrInsert($list);
- $this->getCustomerList();
- } else {
- echo 'SUCCESS';die;
- }
- }
- public function updateOrInsert ( $data ) {
- foreach ( $data as $value ) {
- //遍历用户 统计每个用户的订单情况
- $order = Order::select(DB::raw('count(1) as num,
- SUM(receivedAmount) AS totalReceiveAmount,
- SUM(freight_cost) AS totalFreightCost,
- SUM(aftersale_fee) as totalAftersaleFee,
- SUM(refund_price) AS totalRefundPrice,
- SUM(cost) as totalCost'))->where('receiverMobile',$value['phone'])->where('is_del',0)->first();
- $params['totalReceivedAmount'] = $order->totalReceiveAmount;
- $params['totalOrderNumber'] = $order->num;
- $params['totalCost'] = $order->totalCost;
- $params['totalRefundPrice'] = $order->totalRefundPrice;
- $params['totalFreightCost'] = $order->totalFreightCost;
- $params['totalAftersaleFee'] = $order->totalAftersaleFee;
- $params['grossProfit'] = ($params['totalReceivedAmount'] + $params['totalRefundPrice']) - ($params['totalCost'] + $params['totalFreightCost'] + $params['totalAftersaleFee']);
- $params['grossProfitRatio'] = $params['totalReceivedAmount'] == 0 ? 0 : ($params['grossProfit'] / $params['totalReceivedAmount']);
- $res = CustomerOrderDay::where('customer_id',$value['id'])->first();
- if($res){
- CustomerOrderDay::where('customer_id',$value['id'])->update($params);
- } else {
- $params['customer_id'] = $value['id'];
- $params['name'] = $value['name'];
- $params['phone'] = $value['phone'];
- $params['fanTime'] = $value['fanTime'];
- CustomerOrderDay::insert($params);
- }
- }
- }
- }
|