diff --git a/app/admin/logic/ActivityZoneFormLogic.php b/app/admin/logic/ActivityZoneFormLogic.php index 5f76614a..c8854679 100644 --- a/app/admin/logic/ActivityZoneFormLogic.php +++ b/app/admin/logic/ActivityZoneFormLogic.php @@ -6,6 +6,7 @@ namespace app\admin\logic; use app\common\model\ActivityZone; use app\common\logic\BaseLogic; 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_unit\StoreProductUnit; use app\common\service\xlsx\ActivityZoneService; @@ -46,12 +47,12 @@ class ActivityZoneFormLogic extends BaseLogic $params['cate_ids'] = !empty($cateIds) ? implode(',', $cateIds) : ''; $activityZoneForm = new ActivityZoneForm(); $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 = []; $time = time(); - foreach ($productIds as $productId) { + foreach ($products as $product) { $productInfo[] = [ - 'product_id' => $productId, + 'product_id' => $product['id'], 'form_id' => $activityZoneForm->id, 'create_time' => $time, 'update_time' => $time, @@ -124,22 +125,24 @@ class ActivityZoneFormLogic extends BaseLogic { $service = new ActivityZoneService(); $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')); + $cateIds = array_unique(array_column($products, 'two_cate_id')); $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'); - foreach ($products as &$item) { - $item['unit_name'] = $unit[$item['unit']]['name'] ?? ''; - unset($item['unit'], $item['product_id']); - } - $data = ConfigLogic::getDictByType('activity_zone'); - foreach ($data['activity_zone'] as $value) { - if ($value['value'] == $activityZoneForm['type']) { - $typeName = $value['remark']; - break; + $categories = reset_index($categories, 'id'); + $data = []; + foreach ($products as $item) { + $currentCate = $categories[$item['two_cate_id']]['name'] ?? ''; + if (!empty($currentCate)) { + $item['unit_name'] = $unit[$item['unit']]['name'] ?? ''; + unset($item['unit'], $item['two_cate_id']); + $data[$currentCate][] = $item; } } - return $service->export($products, $activityZoneForm['title'], $typeName ?? '', $activityZoneForm['remark']); + return $service->export($data, $activityZoneForm['title'], $activityZoneForm['remark']); } } \ No newline at end of file diff --git a/app/common/service/xlsx/ActivityZoneService.php b/app/common/service/xlsx/ActivityZoneService.php index c936df08..d3ae3f3b 100644 --- a/app/common/service/xlsx/ActivityZoneService.php +++ b/app/common/service/xlsx/ActivityZoneService.php @@ -9,7 +9,7 @@ use PhpOffice\PhpSpreadsheet\Style\Alignment; class ActivityZoneService { - public function export($data, $title, $typeName, $remark) + public function export($data, $title, $remark) { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); @@ -65,19 +65,30 @@ class ActivityZoneService // 应用默认样式到A2:F5 $sheet->getStyle('A2:F5')->applyFromArray($leftStyle); - foreach ($data as $k => $v) { - $column = $k + 7; - $sheet->mergeCells("B{$column}:C{$column}"); - $sheet->setCellValue('A' . ($k + 7), $v['id']); - $sheet->setCellValue("B{$column}", $v['store_name']); - $sheet->setCellValue("D{$column}", $v['unit_name']); + $column = 7; + foreach ($data as $k => $item) { + $sheet->mergeCells("A{$column}:F{$column}"); + $sheet->setCellValue('A' . $column, $k); + $sheet->getStyle('A' . $column)->applyFromArray([ + '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' . ($count + 7) . ':F' . ($count + 7)); - $sheet->setCellValue('A' . ($count + 7), "备注:{$remark}"); - $sheet->getRowDimension($count + 7)->setRowHeight(50); - $sheet->getStyle('A' . ($count + 7))->getAlignment()->setWrapText(true); - $sheet->getStyle('A' . ($count + 7). ':' . 'F' . ($count + 7))->applyFromArray($leftStyle); + $sheet->mergeCells('A' . $column . ':F' . $column); + $sheet->setCellValue('A' . $column, "备注:{$remark}"); + $sheet->getRowDimension($column)->setRowHeight(50); + $sheet->getStyle('A' . $column)->getAlignment()->setWrapText(true); + $sheet->getStyle('A' . $column. ':' . 'F' . $column)->applyFromArray($leftStyle); // 设置单元格的样式 $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); $url = '/export/' . date('Y-m') . $title . date('YmdHi') . '.xlsx';