From 81b214b135cad15201eabc137fe5f35f1b502b04 Mon Sep 17 00:00:00 2001 From: xuanchen <122383162@qq.com> Date: Tue, 28 Sep 2021 16:51:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Census/IndexController.php | 132 +++++++++++++----- .../Controllers/Coupon/IndexController.php | 121 ++++++++-------- app/Merchant/Exporters/CouponExport.php | 111 --------------- 3 files changed, 158 insertions(+), 206 deletions(-) delete mode 100644 app/Merchant/Exporters/CouponExport.php diff --git a/app/Merchant/Controllers/Census/IndexController.php b/app/Merchant/Controllers/Census/IndexController.php index 11814cc..ba1ab93 100644 --- a/app/Merchant/Controllers/Census/IndexController.php +++ b/app/Merchant/Controllers/Census/IndexController.php @@ -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,42 +13,111 @@ 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'; - $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') { + $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')); } 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(); + } + } diff --git a/app/Merchant/Controllers/Coupon/IndexController.php b/app/Merchant/Controllers/Coupon/IndexController.php index 340b11e..cd30d24 100644 --- a/app/Merchant/Controllers/Coupon/IndexController.php +++ b/app/Merchant/Controllers/Coupon/IndexController.php @@ -13,75 +13,72 @@ class IndexController extends Controller public function index(Request $request) { - $user = Auth::guard('merchant')->user(); - - 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), - ]; + $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'; + } + + 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')); } 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, diff --git a/app/Merchant/Exporters/CouponExport.php b/app/Merchant/Exporters/CouponExport.php deleted file mode 100644 index 3019ef7..0000000 --- a/app/Merchant/Exporters/CouponExport.php +++ /dev/null @@ -1,111 +0,0 @@ -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; - } - -}