This repository has been archived on 2020-11-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
pingan/app/Merchant/Exporters/CouponExport.php
2020-08-06 16:37:53 +08:00

106 lines
3.2 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()
{
if ($this->start) {
$this->start = $this->start . ' 00:00:00';
}
if ($this->end) {
$this->end = $this->end . ' 23:59:59';
}
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->total,
$info->status_text,
$info->remark,
$info->created_at,
];
return $data;
}
}