微调
This commit is contained in:
@@ -3,10 +3,7 @@
|
|||||||
namespace App\Merchant\Controllers\Census;
|
namespace App\Merchant\Controllers\Census;
|
||||||
|
|
||||||
use App\Merchant\Controllers\Controller;
|
use App\Merchant\Controllers\Controller;
|
||||||
use App\Merchant\Exporters\CensusExport;
|
|
||||||
use App\Models\ActivityRule;
|
|
||||||
use App\Models\Coupon;
|
use App\Models\Coupon;
|
||||||
use App\Models\UserCode;
|
|
||||||
use Auth;
|
use Auth;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
@@ -16,42 +13,111 @@ class IndexController extends Controller
|
|||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$user = Auth::guard('merchant')->user();
|
$user = Auth::guard('merchant')->user();
|
||||||
$month = $request->month ?? now()->format('Y-m');
|
|
||||||
$action = $request->action ?? 'search';
|
$action = $request->action ?? 'search';
|
||||||
$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']);
|
|
||||||
|
|
||||||
$rules = $user->code;
|
|
||||||
|
|
||||||
$coupons = $coupons->groupBy('create_day')
|
|
||||||
->map(function ($items, $key) use ($rules) {
|
|
||||||
$data = [
|
|
||||||
'day' => $key,
|
|
||||||
];
|
|
||||||
foreach ($rules as $rule) {
|
|
||||||
$data[$rule->code] = $items->where('thirdPartyGoodsId', $rule->code)->count();
|
|
||||||
}
|
|
||||||
|
|
||||||
return collect($data);
|
|
||||||
});
|
|
||||||
|
|
||||||
$all = [];
|
|
||||||
foreach ($rules as $rule) {
|
|
||||||
$all[$rule->code] = $coupons->sum($rule->code);
|
|
||||||
}
|
|
||||||
|
|
||||||
$coupons = $coupons->sortByDesc('day');
|
|
||||||
|
|
||||||
if ($action == 'search') {
|
if ($action == 'search') {
|
||||||
|
$month = $request->month ?? now()->format('Y-m');
|
||||||
|
$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']);
|
||||||
|
|
||||||
|
$rules = $user->code;
|
||||||
|
|
||||||
|
$coupons = $coupons->groupBy('create_day')
|
||||||
|
->map(function ($items, $key) use ($rules) {
|
||||||
|
$data = [
|
||||||
|
'day' => $key,
|
||||||
|
];
|
||||||
|
foreach ($rules as $rule) {
|
||||||
|
$data[$rule->code] = $items->where('thirdPartyGoodsId', $rule->code)->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($data);
|
||||||
|
});
|
||||||
|
|
||||||
|
$all = [];
|
||||||
|
foreach ($rules as $rule) {
|
||||||
|
$all[$rule->code] = $coupons->sum($rule->code);
|
||||||
|
}
|
||||||
|
|
||||||
|
$coupons = $coupons->sortByDesc('day');
|
||||||
|
|
||||||
return view('Merchant::census.index', compact('coupons', 'all', 'rules'));
|
return view('Merchant::census.index', compact('coupons', 'all', 'rules'));
|
||||||
} else {
|
} else {
|
||||||
return (new CensusExport($month, $user))->download();
|
$this->excel($request, $user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//导出数据
|
||||||
|
public function excel($request, $user)
|
||||||
|
{
|
||||||
|
if (!$request->end) {
|
||||||
|
$request->end = now()->toDateTimeString();
|
||||||
|
}
|
||||||
|
|
||||||
|
set_time_limit(0);
|
||||||
|
ini_set('memory_limit', '1024M');
|
||||||
|
$filename = '核销统计' . date('YmdHis') . '.csv';
|
||||||
|
|
||||||
|
$response = function () use ($user, $request) {
|
||||||
|
$handle = fopen('php://output', 'w');
|
||||||
|
$rules = $user->code;
|
||||||
|
|
||||||
|
$titles = collect(['日期']);
|
||||||
|
|
||||||
|
foreach ($rules as $rule) {
|
||||||
|
$titles->push($rule->rule->title);
|
||||||
|
}
|
||||||
|
|
||||||
|
fputcsv($handle, $titles->toArray());
|
||||||
|
|
||||||
|
$month = $request->month ?? now()->format('Y-m');
|
||||||
|
$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']);
|
||||||
|
|
||||||
|
$coupons = $coupons->groupBy('create_day')
|
||||||
|
->map(function ($items, $key) use ($rules) {
|
||||||
|
$data = [
|
||||||
|
'day' => $key,
|
||||||
|
];
|
||||||
|
foreach ($rules as $rule) {
|
||||||
|
$data[$rule->code] = $items->where('thirdPartyGoodsId', $rule->code)->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
return collect($data);
|
||||||
|
});
|
||||||
|
|
||||||
|
$all = ['全部'];
|
||||||
|
foreach ($rules as $rule) {
|
||||||
|
$all[$rule->code] = $coupons->sum($rule->code);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($coupons as $coupon) {
|
||||||
|
fputcsv($handle, $coupon->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
fputcsv($handle, $all);
|
||||||
|
|
||||||
|
fclose($handle);
|
||||||
|
};
|
||||||
|
|
||||||
|
response()
|
||||||
|
->stream($response, 200, [
|
||||||
|
'Content-Encoding' => 'UTF-8',
|
||||||
|
'Content-Type' => 'text/csv;charset=UTF-8',
|
||||||
|
'Content-Disposition' => "attachment;filename=\"{$filename}\"",
|
||||||
|
])->send();
|
||||||
|
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,75 +13,72 @@ class IndexController extends Controller
|
|||||||
|
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$user = Auth::guard('merchant')->user();
|
$user = Auth::guard('merchant')->user();
|
||||||
|
$action = $request->action ?? 'search';
|
||||||
if ($request->start) {
|
|
||||||
$request->start = $request->start . ' 00:00:00';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($request->end) {
|
|
||||||
$request->end = $request->end . ' 23:59:59';
|
|
||||||
}
|
|
||||||
|
|
||||||
$outlet = $request->outlet;
|
|
||||||
$status = $request->status;
|
|
||||||
$redemptionCode = $request->redemptionCode;
|
|
||||||
$start = $request->start;
|
|
||||||
$end = $request->end;
|
|
||||||
$thirdPartyGoodsId = $request->thirdPartyGoodsId;
|
|
||||||
$action = $request->action ?? 'search';
|
|
||||||
|
|
||||||
$coupons = Coupon::where('user_id', $user->id)
|
|
||||||
->when($outlet, function ($q) use ($outlet) {
|
|
||||||
$q->whereHas('outlet', function ($q) use ($outlet) {
|
|
||||||
$q->whereHas('info', function ($q) use ($outlet) {
|
|
||||||
$q->where('nickname', 'like', "%{$outlet}%");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})
|
|
||||||
->when($redemptionCode, function ($q) use ($redemptionCode) {
|
|
||||||
$q->where('redemptionCode', $redemptionCode);
|
|
||||||
})
|
|
||||||
->when($thirdPartyGoodsId, function ($q) use ($thirdPartyGoodsId) {
|
|
||||||
$q->where('thirdPartyGoodsId', $thirdPartyGoodsId);
|
|
||||||
})
|
|
||||||
->when(is_numeric($status), function ($query) use ($status) {
|
|
||||||
if ($status == 4) {
|
|
||||||
$query->where('is_profit', 1);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
$query->where('status', $status);
|
|
||||||
}
|
|
||||||
|
|
||||||
}, function ($query) {
|
|
||||||
$query->whereIn('status', [2, 3]);
|
|
||||||
})
|
|
||||||
->when($start && $end, function ($query) use ($start, $end) {
|
|
||||||
$query->whereBetween('created_at', [$start, $end]);
|
|
||||||
})
|
|
||||||
->when($start && !$end, function ($query) use ($start) {
|
|
||||||
$query->where('created_at', '>', $start);
|
|
||||||
})
|
|
||||||
->when(!$start && $end, function ($query) use ($end) {
|
|
||||||
$query->where('created_at', '<', $end);
|
|
||||||
})
|
|
||||||
->orderBy('created_at', 'desc')->orderBy('id', 'desc')->paginate();
|
|
||||||
|
|
||||||
$data = [
|
|
||||||
'all' => $this->getData($request, 'all', $user),
|
|
||||||
'pass' => $this->getData($request, 'pass', $user),
|
|
||||||
'reject' => $this->getData($request, 'reject', $user),
|
|
||||||
];
|
|
||||||
|
|
||||||
if ($action == 'search') {
|
if ($action == 'search') {
|
||||||
$rules = ActivityRule::get();
|
$rules = ActivityRule::get();
|
||||||
|
if ($request->start) {
|
||||||
|
$request->start = $request->start . ' 00:00:00';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->end) {
|
||||||
|
$request->end = $request->end . ' 23:59:59';
|
||||||
|
}
|
||||||
|
|
||||||
|
$outlet = $request->outlet;
|
||||||
|
$status = $request->status;
|
||||||
|
$redemptionCode = $request->redemptionCode;
|
||||||
|
$start = $request->start;
|
||||||
|
$end = $request->end;
|
||||||
|
$thirdPartyGoodsId = $request->thirdPartyGoodsId;
|
||||||
|
|
||||||
|
$coupons = Coupon::where('user_id', $user->id)
|
||||||
|
->when($outlet, function ($q) use ($outlet) {
|
||||||
|
$q->whereHas('outlet', function ($q) use ($outlet) {
|
||||||
|
$q->whereHas('info', function ($q) use ($outlet) {
|
||||||
|
$q->where('nickname', 'like', "%{$outlet}%");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
->when($redemptionCode, function ($q) use ($redemptionCode) {
|
||||||
|
$q->where('redemptionCode', $redemptionCode);
|
||||||
|
})
|
||||||
|
->when($thirdPartyGoodsId, function ($q) use ($thirdPartyGoodsId) {
|
||||||
|
$q->where('thirdPartyGoodsId', $thirdPartyGoodsId);
|
||||||
|
})
|
||||||
|
->when(is_numeric($status), function ($query) use ($status) {
|
||||||
|
if ($status == 4) {
|
||||||
|
$query->where('is_profit', 1);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$query->where('status', $status);
|
||||||
|
}
|
||||||
|
|
||||||
|
}, function ($query) {
|
||||||
|
$query->whereIn('status', [2, 3]);
|
||||||
|
})
|
||||||
|
->when($start && $end, function ($query) use ($start, $end) {
|
||||||
|
$query->whereBetween('created_at', [$start, $end]);
|
||||||
|
})
|
||||||
|
->when($start && !$end, function ($query) use ($start) {
|
||||||
|
$query->where('created_at', '>', $start);
|
||||||
|
})
|
||||||
|
->when(!$start && $end, function ($query) use ($end) {
|
||||||
|
$query->where('created_at', '<', $end);
|
||||||
|
})
|
||||||
|
->orderBy('created_at', 'desc')->orderBy('id', 'desc')->paginate();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'all' => $this->getData($request, 'all', $user),
|
||||||
|
'pass' => $this->getData($request, 'pass', $user),
|
||||||
|
'reject' => $this->getData($request, 'reject', $user),
|
||||||
|
];
|
||||||
|
|
||||||
return view('Merchant::coupon.index', compact('coupons', 'data', 'rules'));
|
return view('Merchant::coupon.index', compact('coupons', 'data', 'rules'));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$this->excel($request, $user);
|
$this->excel($request, $user);
|
||||||
|
|
||||||
// return (new CouponExport($request->all(), $user))->download();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +283,7 @@ class IndexController extends Controller
|
|||||||
fputcsv($handle, [
|
fputcsv($handle, [
|
||||||
$info->id,
|
$info->id,
|
||||||
$info->outlet ? $info->outlet->nickname : 'Id:' . $info->outletId,
|
$info->outlet ? $info->outlet->nickname : 'Id:' . $info->outletId,
|
||||||
$info->redemptionCode,
|
$info->redemptionCode . "\t",
|
||||||
' ' . $info->couponName,
|
' ' . $info->couponName,
|
||||||
$info->price . "\t",
|
$info->price . "\t",
|
||||||
$info->orderid,
|
$info->orderid,
|
||||||
|
|||||||
@@ -1,111 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Merchant\Exporters;
|
|
||||||
|
|
||||||
use App\Models\Coupon;
|
|
||||||
use Maatwebsite\Excel\Concerns\Exportable;
|
|
||||||
use Maatwebsite\Excel\Concerns\FromQuery;
|
|
||||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
||||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
||||||
|
|
||||||
class CouponExport implements FromQuery, WithMapping, WithHeadings
|
|
||||||
{
|
|
||||||
|
|
||||||
use Exportable;
|
|
||||||
|
|
||||||
public $outlet = '';
|
|
||||||
|
|
||||||
public $status = '';
|
|
||||||
|
|
||||||
public $redemptionCode = '';
|
|
||||||
|
|
||||||
public $start = '';
|
|
||||||
|
|
||||||
public $end = '';
|
|
||||||
|
|
||||||
public $user = '';
|
|
||||||
|
|
||||||
public $thirdPartyGoodsId = '';
|
|
||||||
|
|
||||||
public function __construct($serchDatas, $user)
|
|
||||||
{
|
|
||||||
$this->fileName = '卡券记录' . date('YmdHis') . '.csv';
|
|
||||||
$this->user = $user;
|
|
||||||
|
|
||||||
if (!empty($serchDatas)) {
|
|
||||||
foreach ($serchDatas as $key => $data) {
|
|
||||||
if ($key == 'start' && !empty($data)) {
|
|
||||||
$data = $data . ' 00:00:01';
|
|
||||||
}
|
|
||||||
if ($key == 'end' && !empty($data)) {
|
|
||||||
$data = $data . ' 23:59:59';
|
|
||||||
}
|
|
||||||
if ($data) {
|
|
||||||
$this->$key = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function headings(): array
|
|
||||||
{
|
|
||||||
return ['ID', '网点名称', '平安券编号', '优惠政策', '核销金额', '状态', '处理结果', '核销时间'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function query()
|
|
||||||
{
|
|
||||||
return Coupon::where('user_id', $this->user->id)
|
|
||||||
->when($this->outlet, function ($q) {
|
|
||||||
$q->whereHas('outlet', function ($q) {
|
|
||||||
$q->whereHas('info', function ($q) {
|
|
||||||
$q->where('nickname', 'like', "%{$this->outlet}%");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})
|
|
||||||
->when($this->redemptionCode, function ($q) {
|
|
||||||
$q->where('redemptionCode', $this->redemptionCode);
|
|
||||||
})
|
|
||||||
->when($this->thirdPartyGoodsId, function ($q) {
|
|
||||||
$q->where('thirdPartyGoodsId', $this->thirdPartyGoodsId);
|
|
||||||
})
|
|
||||||
->when(is_numeric($this->status), function ($query) {
|
|
||||||
if ($this->status == 4) {
|
|
||||||
$query->where('is_profit', 1);
|
|
||||||
} else {
|
|
||||||
$query->where('status', $this->status);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
->when($this->start && $this->end, function ($query) {
|
|
||||||
$query->whereBetween('created_at', [$this->start, $this->end]);
|
|
||||||
})
|
|
||||||
->when($this->start, function ($query) {
|
|
||||||
$query->where('created_at', '>', $this->start);
|
|
||||||
})
|
|
||||||
->when($this->end, function ($query) {
|
|
||||||
$query->where('created_at', '<', $this->end);
|
|
||||||
})
|
|
||||||
->whereIn('status', [2, 3])
|
|
||||||
->orderBy('created_at', 'desc');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function map($info): array
|
|
||||||
{
|
|
||||||
$data = [
|
|
||||||
$info->id,
|
|
||||||
$info->outlet ? $info->outlet->nickname : 'Id:' . $info->outletId,
|
|
||||||
$info->redemptionCode . "\t",
|
|
||||||
' ' . $info->couponName,
|
|
||||||
$info->price,
|
|
||||||
$info->status_text,
|
|
||||||
$info->remark,
|
|
||||||
$info->created_at,
|
|
||||||
];
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user