98 lines
3.0 KiB
PHP
98 lines
3.0 KiB
PHP
<?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') . '.xlsx';
|
|
$this->user = $user;
|
|
|
|
if (!empty($serchDatas)) {
|
|
foreach ($serchDatas as $key => $data) {
|
|
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,
|
|
' ' . $info->couponName,
|
|
$info->price,
|
|
$info->status_text,
|
|
$info->remark,
|
|
$info->created_at,
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
|
|
}
|