argument('cdate'); if (!$cdate) { $cdate = date('Ymd', strtotime('-1 day')); } Shop::chunkById(500, function ($shops) use ($cdate) { foreach ($shops as $shop) { $this->report($shop, $cdate); } }); } public function report(Shop $shop, $cdate) { $chupiaoMoneyRow = Order::select([ DB::raw('sum(IF(type = 3, union_money, money)) as m') ]) ->usable() ->where('draft_date', $cdate) ->where('draft_shop_id', $shop->id) ->first(); $chupiaoMoney = Helps::floatFormat($chupiaoMoneyRow ? $chupiaoMoneyRow->m : 0); $sendMoneyRow = Order::select([ DB::raw('sum(IF(type = 3, union_send_prize, lottery_send_prize)) as m') ]) ->usable() ->where('send_date', $cdate) ->where('draft_shop_id', $shop->id) ->first(); $sendMoney = Helps::floatFormat($sendMoneyRow ? $sendMoneyRow->m : 0); $winMoneyRow = Order::select([ DB::raw('sum(IF(type = 3, union_should_send_prize, lottery_should_send_prize)) as m') ]) ->usable() ->where('draft_date', $cdate) ->where('draft_shop_id', $shop->id) ->first(); $winMoney = Helps::floatFormat($winMoneyRow ? $winMoneyRow->m : 0); $withDraw = CustomerWithdraw::where('shop_id', $shop->id) ->where('created_at', 'like', '%' . date('Y-m-d', strtotime($cdate)) . '%') ->where('state', WithdrawState::SUCCESS) ->sum('money'); $registerNum = Customer::where('shop_id', $shop->id) ->where('created_at', '>=', date('Y-m-d 00:00:00')) ->where('created_at', '<=', date('Y-m-d 23:59:59')) ->count(); $buyerNum = Order::select(['customer_id', DB::raw('count(*)')]) ->usable() ->where('created_date', $cdate) ->where('shop_id', $shop->id) ->where('pay_state', PayState::SUCCESS) ->groupBy('customer_id') ->get() ->count(); $cooperateBrokerage = ShopCooperateBill::where('seller_shop_id', $shop->id) ->where('type', ShopCooperateBill::TYPE_TICKET) ->where('created_date', $cdate) ->sum('cooperate_brokerage'); $customerRecharge = CustomerBill::leftJoin('customer', 'customer.id', 'customer_bill.customer_id') ->where('customer_bill.created_date', $cdate) ->where('customer_bill.type', BillType::RECHARGE) ->where('customer.shop_id', $shop->id) ->sum('customer_bill.money'); $shopBalance = $shop->balance; $customersBalance = Customer::where('shop_id', $shop->id)->sum('balance'); $chupiao_fee_money = ShopBill::where('created_date', $cdate) ->where('shop_id', $shop->id) ->where('type', ShopBill::TYPE_FEE) ->sum('money'); $sellerIds = Seller::where('shop_id', $shop->id)->pluck('id')->toArray(); $incrMoney = 0; $reduceMoney = 0; if ($sellerIds) { $incrMoney = CustomerBill::where('created_date', $cdate) ->where('type', BillType::SELLER_INCR) ->whereIn('seller_id', $sellerIds) ->sum('money'); $reduceMoney = CustomerBill::where('created_date', $cdate) ->where('type', BillType::SELLER_REDUCE) ->whereIn('seller_id', $sellerIds) ->sum('money'); } $report = ReportDayShop::where('shop_id', $shop->id) ->where('cdate', $cdate) ->first(); if (!$report) { $report = new ReportDayShop(); } $report->shop_id = $shop->id; $report->cdate = $cdate; $report->register_num = $registerNum; $report->buyer_num = $buyerNum; $report->shop_banlance = $shopBalance; $report->customers_balance = $customersBalance; $report->chupiao_fee_money = $chupiao_fee_money; $report->chupiao_money = $chupiaoMoney; $report->send_prize = $sendMoney; $report->win_prize = $winMoney; $report->customer_withdraw = $withDraw; $report->incr_money = $incrMoney; $report->reduce_money = $reduceMoney; $report->customer_recharge = $customerRecharge; $report->cooperate_brokerage = $cooperateBrokerage; $report->save(); return $report; } }