修改活动专区商品列表导出

This commit is contained in:
lewis 2024-12-24 17:32:49 +08:00
parent f265a07cd5
commit ed01ef1b9a
2 changed files with 42 additions and 28 deletions

View File

@ -6,6 +6,7 @@ namespace app\admin\logic;
use app\common\model\ActivityZone; use app\common\model\ActivityZone;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\ActivityZoneForm; use app\common\model\ActivityZoneForm;
use app\common\model\store_category\StoreCategory;
use app\common\model\store_product\StoreProduct; use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_unit\StoreProductUnit; use app\common\model\store_product_unit\StoreProductUnit;
use app\common\service\xlsx\ActivityZoneService; use app\common\service\xlsx\ActivityZoneService;
@ -46,12 +47,12 @@ class ActivityZoneFormLogic extends BaseLogic
$params['cate_ids'] = !empty($cateIds) ? implode(',', $cateIds) : ''; $params['cate_ids'] = !empty($cateIds) ? implode(',', $cateIds) : '';
$activityZoneForm = new ActivityZoneForm(); $activityZoneForm = new ActivityZoneForm();
$activityZoneForm->save($params); $activityZoneForm->save($params);
$productIds = StoreProduct::where('two_cate_id', 'in', $secondCateIds)->whereOr('cate_id', 'in', $thirdCateIds)->column('id'); $products = StoreProduct::field('id,two_cate_id,cate_id')->where('two_cate_id', 'in', $secondCateIds)->whereOr('cate_id', 'in', $thirdCateIds)->select()->toArray();
$productInfo = []; $productInfo = [];
$time = time(); $time = time();
foreach ($productIds as $productId) { foreach ($products as $product) {
$productInfo[] = [ $productInfo[] = [
'product_id' => $productId, 'product_id' => $product['id'],
'form_id' => $activityZoneForm->id, 'form_id' => $activityZoneForm->id,
'create_time' => $time, 'create_time' => $time,
'update_time' => $time, 'update_time' => $time,
@ -124,22 +125,24 @@ class ActivityZoneFormLogic extends BaseLogic
{ {
$service = new ActivityZoneService(); $service = new ActivityZoneService();
$activityZoneForm = ActivityZoneForm::findOrEmpty($params['id'])->toArray(); $activityZoneForm = ActivityZoneForm::findOrEmpty($params['id'])->toArray();
$products = ActivityZone::with('product')->field('id,product_id')->where('form_id', $params['id'])->select()->toArray(); $productIds = ActivityZone::where('form_id', $params['id'])->column('product_id');
$products = StoreProduct::field('id,unit,store_name,two_cate_id')->whereIn('id', $productIds)->order('two_cate_id')->select()->toArray();
$unitIds = array_unique(array_column($products, 'unit')); $unitIds = array_unique(array_column($products, 'unit'));
$cateIds = array_unique(array_column($products, 'two_cate_id'));
$unit = StoreProductUnit::whereIn('id', $unitIds)->field('id,name')->withTrashed()->select()->toArray(); $unit = StoreProductUnit::whereIn('id', $unitIds)->field('id,name')->withTrashed()->select()->toArray();
$categories = StoreCategory::whereIn('id', $cateIds)->field('id,name')->withTrashed()->select()->toArray();
$unit = reset_index($unit, 'id'); $unit = reset_index($unit, 'id');
foreach ($products as &$item) { $categories = reset_index($categories, 'id');
$item['unit_name'] = $unit[$item['unit']]['name'] ?? ''; $data = [];
unset($item['unit'], $item['id']); foreach ($products as $item) {
} $currentCate = $categories[$item['two_cate_id']]['name'] ?? '';
$data = ConfigLogic::getDictByType('activity_zone'); if (!empty($currentCate)) {
foreach ($data['activity_zone'] as $value) { $item['unit_name'] = $unit[$item['unit']]['name'] ?? '';
if ($value['value'] == $activityZoneForm['type']) { unset($item['unit'], $item['two_cate_id']);
$typeName = $value['remark']; $data[$currentCate][] = $item;
break;
} }
} }
return $service->export($products, $activityZoneForm['title'], $typeName ?? '', $activityZoneForm['remark']); return $service->export($data, $activityZoneForm['title'], $activityZoneForm['remark']);
} }
} }

View File

@ -9,7 +9,7 @@ use PhpOffice\PhpSpreadsheet\Style\Alignment;
class ActivityZoneService class ActivityZoneService
{ {
public function export($data, $title, $typeName, $remark) public function export($data, $title, $remark)
{ {
$spreadsheet = new Spreadsheet(); $spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); $sheet = $spreadsheet->getActiveSheet();
@ -65,19 +65,30 @@ class ActivityZoneService
// 应用默认样式到A2:F5 // 应用默认样式到A2:F5
$sheet->getStyle('A2:F5')->applyFromArray($leftStyle); $sheet->getStyle('A2:F5')->applyFromArray($leftStyle);
foreach ($data as $k => $v) { $column = 7;
$column = $k + 7; foreach ($data as $k => $item) {
$sheet->mergeCells("B{$column}:C{$column}"); $sheet->mergeCells("A{$column}:F{$column}");
$sheet->setCellValue('A' . ($k + 7), $v['product_id']); $sheet->setCellValue('A' . $column, $k);
$sheet->setCellValue("B{$column}", $v['store_name']); $sheet->getStyle('A' . $column)->applyFromArray([
$sheet->setCellValue("D{$column}", $v['unit_name']); 'font' => [
'bold' => true,
'size' => 16,
],
]);
$column++;
foreach ($item as $value) {
$sheet->mergeCells("B{$column}:C{$column}");
$sheet->setCellValue('A' . $column, $value['id']);
$sheet->setCellValue("B{$column}", $value['store_name']);
$sheet->setCellValue("D{$column}", $value['unit_name']);
$column++;
}
} }
$count = count($data); $sheet->mergeCells('A' . $column . ':F' . $column);
$sheet->mergeCells('A' . ($count + 7) . ':F' . ($count + 7)); $sheet->setCellValue('A' . $column, "备注:{$remark}");
$sheet->setCellValue('A' . ($count + 7), "备注:{$remark}"); $sheet->getRowDimension($column)->setRowHeight(50);
$sheet->getRowDimension($count + 7)->setRowHeight(50); $sheet->getStyle('A' . $column)->getAlignment()->setWrapText(true);
$sheet->getStyle('A' . ($count + 7))->getAlignment()->setWrapText(true); $sheet->getStyle('A' . $column. ':' . 'F' . $column)->applyFromArray($leftStyle);
$sheet->getStyle('A' . ($count + 7). ':' . 'F' . ($count + 7))->applyFromArray($leftStyle);
// 设置单元格的样式 // 设置单元格的样式
$styleArray = [ $styleArray = [
@ -96,7 +107,7 @@ class ActivityZoneService
], ],
], ],
]; ];
$sheet->getStyle('A1:F' . ($count + 7))->applyFromArray($styleArray); $sheet->getStyle('A1:F' . $column)->applyFromArray($styleArray);
$writer = new Xlsx($spreadsheet); $writer = new Xlsx($spreadsheet);
$url = '/export/' . date('Y-m') . $title . date('YmdHi') . '.xlsx'; $url = '/export/' . date('Y-m') . $title . date('YmdHi') . '.xlsx';