vd/app/Domains/Real/Commands/Sync/RefundSync.php
2019-05-13 10:16:26 +08:00

60 lines
2.2 KiB
PHP

<?php
namespace App\Domains\Real\Commands\Sync;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\OrderCardPartitionRepository;
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository as VirtualOrderCardPartitionRepository;
class RefundSync extends Command
{
protected $name = 'real:sync-refund';
protected $description = '同步RD退货数据';
protected $companies;
public function handle()
{
$datetime = $this->getDateTime();
$starttime = $datetime->copy()->startOfMonth()->startOfDay();
$endtime = $datetime->copy()->endOfMonth()->endOfDay();
$refunds = DB::connection('real')->table('jxc_back_card')
->select(['sim', 'create_time'])
->where('status', 2)
->where('create_time', '>=', $starttime)
->where('create_time', '<=', $endtime)
->get();
$simArray = [];
$refunds->map(function ($item) use (&$simArray) {
$item->sim = str_to_array($item->sim, ',');
$simArray = array_merge($simArray, $item->sim);
});
DB::transaction(function () use ($refunds) {
foreach ($refunds as $item) {
DB::table('real_order_cards')
->whereIn('sim', $item->sim)
->where('created_at', '<=', $item->create_time)
->update(['refunded_at' => $item->create_time]);
DB::table('virtual_order_cards_partition')
->leftJoin('cards', 'cards.sim', '=', 'virtual_order_cards_partition.sim')
->whereIn('virtual_order_cards_partition.sim', $item->sim)
->where('virtual_order_cards_partition.created_at', '<=', $item->create_time)
->where(function ($query) use ($item) {
$query->whereNull('cards.virtual_activated_at')->orWhere('cards.virtual_activated_at', '>=', $item->create_time);
})
->update(['virtual_order_cards_partition.refunded_at' => $item->create_time]);
}
});
app(OrderCardPartitionRepository::class)->forgetCached();
app(VirtualOrderCardPartitionRepository::class)->forgetCached();
}
}