优化渠道核销统计

This commit is contained in:
2020-09-02 11:17:12 +08:00
parent 758146a02b
commit 5397d931f7
6 changed files with 234 additions and 220 deletions

View File

@@ -4,12 +4,14 @@ namespace App\Merchant\Controllers\Census;
use App\Merchant\Controllers\Controller;
use App\Merchant\Exporters\CensusExport;
use App\Models\ActivityRule;
use App\Models\Coupon;
use Auth;
use Illuminate\Http\Request;
class IndexController extends Controller
{
public function index(Request $request)
{
$user = Auth::guard('merchant')->user();
@@ -18,33 +20,37 @@ class IndexController extends Controller
$month = explode('-', $month);
$coupons = Coupon::where('user_id', $user->id)
->whereYear('created_at', $month[0])
->whereMonth('created_at', $month[1])
->where('status', 2)
->get(['id', 'thirdPartyGoodsId', 'created_at']);
->whereYear('created_at', $month[0])
->whereMonth('created_at', $month[1])
->where('status', 2)
->get(['id', 'thirdPartyGoodsId', 'created_at']);
$coupons = $coupons->groupBy('create_day')->map(function ($items, $key) {
$rules = ActivityRule::get();
$coupons = $coupons->groupBy('create_day')->map(function ($items, $key) use ($rules) {
$data = [
'day' => $key,
'ysd10' => $items->where('thirdPartyGoodsId', 'YSD-full100-10')->count(),
'ysd25' => $items->where('thirdPartyGoodsId', 'YSD-full100-25')->count(),
'ysd50' => $items->where('thirdPartyGoodsId', 'YSD-full100-50')->count(),
'ysd100' => $items->where('thirdPartyGoodsId', 'YSD-full200-100')->count(),
'day' => $key,
// 'ysd10' => $items->where('thirdPartyGoodsId', 'YSD-full100-10')->count(),
// 'ysd25' => $items->where('thirdPartyGoodsId', 'YSD-full100-25')->count(),
// 'ysd50' => $items->where('thirdPartyGoodsId', 'YSD-full100-50')->count(),
// 'ysd100' => $items->where('thirdPartyGoodsId', 'YSD-full200-100')->count(),
];
foreach ($rules as $rule) {
$data[$rule->code] = $items->where('thirdPartyGoodsId', $rule->code)->count();
}
return collect($data);
});
$all = [
'ysd10' => $coupons->sum('ysd10'),
'ysd25' => $coupons->sum('ysd25'),
'ysd50' => $coupons->sum('ysd50'),
'ysd100' => $coupons->sum('ysd100'),
];
$all = [];
foreach ($rules as $rule) {
$all[$rule->code] = $coupons->sum($rule->code);
}
$coupons = $coupons->sortByDesc('day');
if ($action == 'search') {
return view('Merchant::census.index', compact('coupons', 'all'));
return view('Merchant::census.index', compact('coupons', 'all', 'rules'));
} else {
return (new CensusExport($month, $user))->download();
}