jingcai-php/app/Console/Commands/Report/GenerateDayShop.php

169 lines
5.6 KiB
PHP
Executable File

<?php
namespace App\Console\Commands\Report;
use App\Enums\BillType;
use App\Enums\PayState;
use App\Enums\WithdrawState;
use App\Model\Customer\Customer;
use App\Model\Customer\CustomerBill;
use App\Model\Customer\CustomerWithdraw;
use App\Model\Order;
use App\Model\Report\ReportDayShop;
use App\Model\Seller\Seller;
use App\Model\Seller\Shop;
use App\Model\Seller\ShopBill;
use App\Model\Seller\ShopCooperateBill;
use App\Utils\Helps;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class GenerateDayShop extends Command
{
/**
* 这个就是命令名称
*/
protected $signature = 'report:generate_day_shop {cdate?}';
/**
* 命令的说明描述
* @var string
*/
protected $description = '';
/**
* 创建命令的构造方法。
* @param string $words 传入的字符参数
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* 命令的具体执行触发方法
* @return mixed
*/
public function handle()
{
$cdate = $this->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;
}
}