This commit is contained in:
2021-09-28 16:51:09 +08:00
parent bdc7525b65
commit 81b214b135
3 changed files with 158 additions and 206 deletions

View File

@@ -3,10 +3,7 @@
namespace App\Merchant\Controllers\Census;
use App\Merchant\Controllers\Controller;
use App\Merchant\Exporters\CensusExport;
use App\Models\ActivityRule;
use App\Models\Coupon;
use App\Models\UserCode;
use Auth;
use Illuminate\Http\Request;
@@ -16,8 +13,10 @@ class IndexController extends Controller
public function index(Request $request)
{
$user = Auth::guard('merchant')->user();
$month = $request->month ?? now()->format('Y-m');
$action = $request->action ?? 'search';
if ($action == 'search') {
$month = $request->month ?? now()->format('Y-m');
$month = explode('-', $month);
$coupons = Coupon::where('user_id', $user->id)
@@ -47,11 +46,78 @@ class IndexController extends Controller
$coupons = $coupons->sortByDesc('day');
if ($action == 'search') {
return view('Merchant::census.index', compact('coupons', 'all', 'rules'));
} 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();
}
}

View File

@@ -14,7 +14,10 @@ class IndexController extends Controller
public function index(Request $request)
{
$user = Auth::guard('merchant')->user();
$action = $request->action ?? 'search';
if ($action == 'search') {
$rules = ActivityRule::get();
if ($request->start) {
$request->start = $request->start . ' 00:00:00';
}
@@ -29,7 +32,6 @@ class IndexController extends Controller
$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) {
@@ -73,15 +75,10 @@ class IndexController extends Controller
'reject' => $this->getData($request, 'reject', $user),
];
if ($action == 'search') {
$rules = ActivityRule::get();
return view('Merchant::coupon.index', compact('coupons', 'data', 'rules'));
} else {
$this->excel($request, $user);
// return (new CouponExport($request->all(), $user))->download();
}
}
@@ -286,7 +283,7 @@ class IndexController extends Controller
fputcsv($handle, [
$info->id,
$info->outlet ? $info->outlet->nickname : 'Id:' . $info->outletId,
$info->redemptionCode,
$info->redemptionCode . "\t",
' ' . $info->couponName,
$info->price . "\t",
$info->orderid,

View File

@@ -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;
}
}