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

97 lines
2.6 KiB
PHP
Executable File

<?php
namespace App\Console\Commands\Report;
use App\Model\Order;
use App\Model\Report\ReportDaySeller;
use App\Model\Seller\Seller;
use App\Model\Seller\ShopBill;
use Illuminate\Console\Command;
class GenerateDaySeller extends Command
{
/**
* 这个就是命令名称
*/
protected $signature = 'report:generate_day_seller {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'));
}
Seller::chunkById(500, function ($sellers) use ($cdate) {
foreach ($sellers as $seller) {
$this->report($seller, $cdate);
}
});
}
public function report(Seller $seller, $cdate)
{
$chupiaoMoney = Order::usable()
->where('draft_date', $cdate)
->where('draft_user_id', $seller->id)
->sum('money');
$sendMoney = Order::usable()
->where('send_date', $cdate)
->where('send_user_id', $seller->id)
->sum('lottery_send_prize');
$winMoney = Order::usable()
->where('draft_date', $cdate)
->where('draft_user_id', $seller->id)
->sum('lottery_should_send_prize');
$incrMoney = ShopBill::where('created_date', $cdate)
->where('type', ShopBill::TYPE_SELLER_INCR)
->where('seller_id', $seller->id)
->sum('money');
$reduceMoney = ShopBill::where('created_date', $cdate)
->where('type', ShopBill::TYPE_SELLER_REDUCE)
->where('seller_id', $seller->id)
->sum('money');
$report = ReportDaySeller::where('seller_id', $seller->id)
->where('cdate', $cdate)
->first();
if (!$report) {
$report = new ReportDaySeller();
}
$report->seller_id = $seller->id;
$report->shop_id = $seller->shop_id;
$report->cdate = $cdate;
$report->chupiao_money = $chupiaoMoney;
$report->send_prize = $sendMoney;
$report->win_prize = $winMoney;
$report->incr_money = $incrMoney;
$report->reduce_money = $reduceMoney;
$report->save();
return $report;
}
}