2023-04-04 15:28:56 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Exports;
|
|
|
|
|
|
|
|
|
|
use App\Models\Goods;
|
|
|
|
|
use App\Utils\ArrayUtils;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
|
|
|
|
|
|
class OrderBlankExport implements FromCollection, WithStyles
|
|
|
|
|
{
|
|
|
|
|
private $data;
|
|
|
|
|
private $shopName;
|
|
|
|
|
private $no;
|
|
|
|
|
private $count;
|
|
|
|
|
|
|
|
|
|
public function __construct($shopName, $no, $excelDate)
|
|
|
|
|
{
|
|
|
|
|
$this->shopName = $shopName;
|
|
|
|
|
$this->no = $no;
|
|
|
|
|
$this->count = count($excelDate);
|
|
|
|
|
$this->data = $this->createData($excelDate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
|
*/
|
|
|
|
|
public function collection()
|
|
|
|
|
{
|
|
|
|
|
return new Collection($this->data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function createData($excelDate)
|
|
|
|
|
{
|
|
|
|
|
$noStr = implode(',', $this->no);
|
|
|
|
|
$data = [
|
|
|
|
|
['店铺名称', $this->shopName],
|
2023-04-04 15:36:28 +08:00
|
|
|
['跟团号', $noStr],
|
2023-04-04 21:16:13 +08:00
|
|
|
['种类', '品牌', '商品名称', '数量', '货架', '配货'],
|
2023-04-04 15:28:56 +08:00
|
|
|
];
|
|
|
|
|
$goodsCodes = array_column($excelDate, 'goods_code');
|
|
|
|
|
$goods = Goods::query()
|
|
|
|
|
->with([
|
|
|
|
|
'brand:id,name',
|
|
|
|
|
'type:id,name',
|
2023-04-04 21:16:13 +08:00
|
|
|
'skus:id,goods_id,sku_code,title'
|
2023-04-04 15:28:56 +08:00
|
|
|
])
|
|
|
|
|
->whereIn('goods_code', $goodsCodes)
|
2023-04-04 21:16:13 +08:00
|
|
|
->get(['id', 'title', 'goods_code', 'type_id', 'brand_id'])
|
2023-04-04 15:28:56 +08:00
|
|
|
->toArray();
|
|
|
|
|
$goods = ArrayUtils::index($goods, 'goods_code');
|
|
|
|
|
foreach ($excelDate as $key => $item) {
|
2023-04-04 21:16:13 +08:00
|
|
|
$str = '';
|
|
|
|
|
$num = 0;
|
|
|
|
|
foreach ($item['p'] as $k => $v) {
|
|
|
|
|
$str .= $v . ';';
|
|
|
|
|
$num++;
|
|
|
|
|
if (6 === $num) {
|
|
|
|
|
$str .= PHP_EOL;
|
|
|
|
|
$num = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-04 15:28:56 +08:00
|
|
|
$goodsInfo = $goods[$item['goods_code']];
|
2023-04-04 21:16:13 +08:00
|
|
|
$name = '';
|
|
|
|
|
foreach ($goodsInfo['skus'] as $sku) {
|
|
|
|
|
if ($sku['sku_code'] === $item['sku_code']) {
|
|
|
|
|
$name = $goodsInfo['title'] . $sku['title'];
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-04 15:28:56 +08:00
|
|
|
$arr[0] = $goodsInfo['type']['name'];
|
|
|
|
|
$arr[1] = $goodsInfo['brand']['name'];
|
2023-04-04 21:16:13 +08:00
|
|
|
$arr[2] = $name;
|
|
|
|
|
$arr[3] = $item['num'];
|
|
|
|
|
$arr[4] = $item['local'];
|
|
|
|
|
$arr[5] = $str;
|
2023-04-04 15:28:56 +08:00
|
|
|
$data[] = $arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function styles(Worksheet $sheet)
|
|
|
|
|
{
|
2023-04-04 21:16:13 +08:00
|
|
|
$count = $this->count + 3;
|
2023-04-04 15:28:56 +08:00
|
|
|
$sheet->getStyle('B2')->getAlignment()
|
|
|
|
|
->setWrapText(true);
|
2023-04-04 21:16:13 +08:00
|
|
|
$sheet->mergeCells('B2:F2');
|
|
|
|
|
$sheet->getStyle('F3:F' . $count)->getAlignment()
|
2023-04-04 15:28:56 +08:00
|
|
|
->setWrapText(true);
|
2023-04-04 21:16:13 +08:00
|
|
|
$sheet->getColumnDimension('A')->setWidth(8);
|
2023-04-04 15:28:56 +08:00
|
|
|
$sheet->getColumnDimension('B')->setWidth(10);
|
2023-04-04 21:16:13 +08:00
|
|
|
$sheet->getColumnDimension('C')->setWidth(36);
|
|
|
|
|
$sheet->getColumnDimension('D')->setWidth(5);
|
2023-04-04 15:28:56 +08:00
|
|
|
$sheet->getColumnDimension('E')->setWidth(5);
|
2023-04-04 21:16:13 +08:00
|
|
|
$sheet->getColumnDimension('F')->setWidth(36);
|
|
|
|
|
$sheet->getRowDimension(2)->setRowHeight(90);
|
2023-04-04 15:28:56 +08:00
|
|
|
}
|
|
|
|
|
}
|