diff --git a/app/admin/controller/ActivityZoneController.php b/app/admin/controller/ActivityZoneController.php index 75e3464ec..54f92700b 100644 --- a/app/admin/controller/ActivityZoneController.php +++ b/app/admin/controller/ActivityZoneController.php @@ -91,5 +91,11 @@ class ActivityZoneController extends BaseAdminController return $this->data($result); } + public function export() + { + $params = $this->request->get(); + $file_path = ActivityZoneLogic::export($params); + return $this->success('导出成功', ['url' => $file_path]); + } } \ No newline at end of file diff --git a/app/admin/logic/ActivityZoneLogic.php b/app/admin/logic/ActivityZoneLogic.php index 9cc018d58..a5425ee0a 100644 --- a/app/admin/logic/ActivityZoneLogic.php +++ b/app/admin/logic/ActivityZoneLogic.php @@ -5,6 +5,8 @@ namespace app\admin\logic; use app\common\model\ActivityZone; use app\common\logic\BaseLogic; +use app\common\model\store_product_unit\StoreProductUnit; +use app\common\service\xlsx\ActivityZoneService; use support\exception\BusinessException; use think\facade\Db; @@ -99,4 +101,26 @@ class ActivityZoneLogic extends BaseLogic { return ActivityZone::findOrEmpty($params['id'])->toArray(); } + + public static function export($params) + { + $service = new ActivityZoneService(); + $products = ActivityZone::with('product')->field('id,product_id,type')->where('type', $params['type'])->select()->toArray(); + $unitIds = array_unique(array_column($products, 'unit')); + $unit = StoreProductUnit::whereIn('id', $unitIds)->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'] == $params['type']) { + $typeName = $value['remark']; + break; + } + } + return $service->export($products, $typeName ?? '', $params['remark']); + } + } \ No newline at end of file diff --git a/app/common/service/xlsx/ActivityZoneService.php b/app/common/service/xlsx/ActivityZoneService.php new file mode 100644 index 000000000..29bbab23a --- /dev/null +++ b/app/common/service/xlsx/ActivityZoneService.php @@ -0,0 +1,115 @@ +getActiveSheet(); + $sheet->mergeCells('A1:F1'); + $sheet->mergeCells('A2:B2'); + $sheet->mergeCells('C2:D2'); + $sheet->mergeCells('E2:F2'); + $sheet->mergeCells('A3:B3'); + $sheet->mergeCells('C3:D3'); + $sheet->mergeCells('E3:F3'); + $sheet->mergeCells('A4:B4'); + $sheet->mergeCells('C4:D4'); + $sheet->mergeCells('E4:F4'); + $sheet->mergeCells('A5:F5'); + $sheet->mergeCells('B6:C6'); + + $sheet->setCellValue('A1', "供 投 里 海 农 特 产 品 下 单 清 单({$typeName})"); + $sheet->setCellValue('A2', '姓名:'); + $sheet->setCellValue('C2', '电话:'); + $sheet->setCellValue('E2', '会员角色:'); + $sheet->setCellValue('A3', '下单时间:'); + $sheet->setCellValue('C3', '到货时间:'); + $sheet->setCellValue('E3', '订单编号:'); + $sheet->setCellValue('A4', '办事时间:'); + $sheet->setCellValue('C4', '寿宴当日桌数:'); + $sheet->setCellValue('E4', '厨师:'); + $sheet->setCellValue('A5', '收货地址:'); + $sheet->setCellValue('A6', '编号'); + $sheet->setCellValue('B6', '品名'); + $sheet->setCellValue('D6', '单位'); + $sheet->setCellValue('E6', '下单数量'); + $sheet->setCellValue('F6', '客户备注'); + + // 设置默认的单元格样式 + $defaultStyle = [ + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER, + ], + ]; + + // 应用默认样式到整个工作表 + $spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle); + + // 设置默认的单元格样式 + $leftStyle = [ + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_LEFT, + 'vertical' => Alignment::VERTICAL_CENTER, + ], + ]; + + // 应用默认样式到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']); + } + $count = count($data); + $sheet->mergeCells('A' . ($count + 7) . ':F' . ($count + 7)); + $sheet->setCellValue('A' . ($count + 7), "专区类型:{$typeName}"); + $sheet->mergeCells('A' . ($count + 8) . ':F' . ($count + 8)); + $sheet->setCellValue('A' . ($count + 8), ""); + $sheet->mergeCells('A' . ($count + 9) . ':F' . ($count + 9)); + $sheet->setCellValue('A' . ($count + 9), "备注:{$remark}"); + $sheet->getRowDimension($count + 9)->setRowHeight(50); + $sheet->getStyle('A' . ($count + 9))->getAlignment()->setWrapText(true); + $sheet->getStyle('A' . ($count + 7). ':' . 'F' . ($count + 9))->applyFromArray($leftStyle); + + // 设置单元格的样式 + $styleArray = [ + 'font' => [ + 'bold' => true, + 'size' => 28, + ], + ]; + $sheet->getStyle('A1')->applyFromArray($styleArray); + // 定义线框样式 + $styleArray = [ + 'borders' => [ + 'allBorders' => [ + 'borderStyle' => Border::BORDER_THIN, // 线框样式 + 'color' => ['argb' => '000000'], // 线框颜色 + ], + ], + ]; + $sheet->getStyle('A1:F' . ($count + 9))->applyFromArray($styleArray); + + $writer = new Xlsx($spreadsheet); + $url = '/export/' . date('Y-m') . "/供投里海农特产品下单清单({$typeName})" . date('YmdHi') . '.xlsx'; + $file_path = public_path() . $url; + if (!is_dir(dirname($file_path))) { + mkdir(dirname($file_path), 0777, true); + } + $writer->save($file_path); + return getenv('APP_URL') . $url; + } + +}