mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-11-30 22:20:45 +00:00
156 lines
5.4 KiB
PHP
156 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Events\StockUpdateEvent;
|
|
use App\Exports\DiffTodayPriceGoodsExport;
|
|
use App\Models\BusinessGoodsSku;
|
|
use App\Models\BusinessOrder;
|
|
use App\Models\GoodsSku;
|
|
use App\Models\GoodsType;
|
|
use App\Models\Log;
|
|
use App\Models\Shop;
|
|
use App\Models\TodayPrice;
|
|
use App\Services\Business\BusinessFactory;
|
|
use App\Utils\DateTimeUtils;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Jobs\BusinessGoodsSkuIncrQuantity;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class Test extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'test';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '测试';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
$todayPrice = TodayPrice::query()
|
|
->with([
|
|
'goodsSku:id,goods_id,title,external_sku_id',
|
|
'goodsSku.goods:id,title',
|
|
])
|
|
->where('day', date('Y-m-d'))
|
|
->get();
|
|
if ($todayPrice->isEmpty()) {
|
|
exit('今日价格数据未找到,请先上传');
|
|
}
|
|
$todayGoodsPrice = [];
|
|
foreach ($todayPrice as $item) {
|
|
$todayGoodsPrice[$item['external_sku_id']] = [
|
|
'today_price' => $item['price'],
|
|
'goods_name' => $item['goodsSku']['goods']['title'] . $item['goodsSku']['title'],
|
|
'external_sku_id' => $item['external_sku_id'],
|
|
];
|
|
}
|
|
$shops = Shop::query()
|
|
->where('plat_id', Shop::$PLAT_KTT)
|
|
->where('expires_at', '>', time())
|
|
->get();
|
|
$data = [];
|
|
foreach ($shops as $shop) {
|
|
$business = BusinessFactory::init()->make($shop->plat_id);
|
|
$business->setShop($shop);
|
|
$res = $business->queryGroup();
|
|
if (!isset($res['ktt_group_query_list_response'])) {
|
|
continue;
|
|
}
|
|
$activityNos =$activities = [];
|
|
foreach ($res['ktt_group_query_list_response']['activity_list'] as $activity) {
|
|
if (0 === $activity['is_help_sell'] && '补款勿拍' !== $activity['title']) {
|
|
$activityNos[] = $activity['activity_no'];
|
|
$activities[$activity['activity_no']] = $activity['title'];
|
|
}
|
|
}
|
|
$businessGoodsSkus = BusinessGoodsSku::query()
|
|
->where('shop_id', $shop->id)
|
|
->whereIn('activity_no', $activityNos)
|
|
->orderBy('activity_no')
|
|
->get(['shop_id', 'title', 'activity_no', 'goods_name', 'price_in_fen', 'external_sku_id']);
|
|
if ($businessGoodsSkus->isEmpty()) {
|
|
continue;
|
|
}
|
|
$data[$shop->name] = $this->diffTodayPrice($businessGoodsSkus, $todayGoodsPrice,$activities);
|
|
}
|
|
foreach ($data as $shopName => $activity) {
|
|
foreach ($activity as $activityNo => $item) {
|
|
foreach ($item as $k => $v) {
|
|
foreach ($v as $i) {
|
|
if ('diff_price' === $k) {
|
|
$s[] = [$shopName, $i['title'], $i['goods_name'], $i['price_in_fen'], $i['today_price'], $i['external_sku_id']];
|
|
}
|
|
if ('not_in_group' === $k) {
|
|
$s[] = [$shopName, $i['title'], $i['goods_name'], '团购无此商品', $i['today_price'], $i['external_sku_id']];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
dd($s);
|
|
|
|
ob_end_clean();
|
|
return Excel::download(new DiffTodayPriceGoodsExport($data), date('Y-m-d') . '今日差价商品.xlsx');
|
|
}
|
|
|
|
private function diffTodayPrice($businessGoodsSkus, $todayPrice,$activities)
|
|
{
|
|
$data = [];
|
|
foreach ($businessGoodsSkus as $item) {
|
|
// 记录团购下商品id
|
|
$data[$item['activity_no']]['ids'][] = $item['external_sku_id'];
|
|
// 团购中有,表格没有
|
|
// if (!isset($todayPrice[$item['external_sku_id']])) {
|
|
// $data[$item['activity_no']]['in_group'][] = $item->toArray();
|
|
// }
|
|
// 价格不一样
|
|
if (isset($todayPrice[$item['external_sku_id']]) && $item['price_in_fen'] != $todayPrice[$item['external_sku_id']]['today_price']) {
|
|
$item['today_price'] = $todayPrice[$item['external_sku_id']]['today_price'];
|
|
$data[$item['activity_no']]['diff_price'][] = $item->toArray();
|
|
}
|
|
}
|
|
$todayIds = array_keys($todayPrice);
|
|
foreach ($data as $no=> &$arr) {
|
|
foreach ($arr as $key => $value) {
|
|
if ('ids' === $key) {
|
|
$ids = array_unique($value);
|
|
$ids = array_diff($todayIds, $ids);
|
|
foreach ($ids as $id) {
|
|
$todayPrice[$id]['title'] = $activities[$no];
|
|
$arr['not_in_group'][] = $todayPrice[$id];
|
|
}
|
|
}
|
|
}
|
|
unset($arr['ids']);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|