diff --git a/app/common/server/ExportGoodsExcelServer.php b/app/common/server/ExportGoodsExcelServer.php new file mode 100644 index 00000000..11e3d2a5 --- /dev/null +++ b/app/common/server/ExportGoodsExcelServer.php @@ -0,0 +1,57 @@ +getActiveSheet(); + // 表头单元格内容 第一行 + $titCol = 'A'; + foreach ($title as $value) { + // 单元格内容写入 + $sheet->setCellValue($titCol . '1', $value); + $titCol++; + } + //单元格内容居中 + $sheet->getDefaultColumnDimension()->setAutoSize(true);//列宽自适应 + $sheet->getStyle('A:' . $titCol)->getAlignment()->setWrapText(true);//内容容垂居中 + $sheet->getStyle('A:' . $titCol)->getAlignment()->setVertical('center');//内容容垂居中 + $sheet->getStyle('A:' . $titCol)->getAlignment()->setHorizontal('center');//内水平直居中 + // 从第二行开始写入数据 + $row = 2; + foreach ($data as &$item) { + foreach ($item as &$value) { + $dataCol = 'A'; + foreach($value as &$v) { + $sheet->setCellValue($dataCol . $row, $v); + $len = strlen($v); + if ($len > 1 ){ //空数据不做处理避免影响到有数据的列宽度 + $sheet->getColumnDimension($dataCol)->setAutoSize(true); + } + $dataCol++; + } + $row++; + } + } + + $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); + if (is_file($path . $file_name)) { + file_put_contents($path . $file_name,'');; + } + $writer->save($path . $file_name); + return array('url' => trim($file_name,'.')); + }catch (\Exception $e){ + } + } +} \ No newline at end of file diff --git a/app/shop/controller/goods/Goods.php b/app/shop/controller/goods/Goods.php index d6131344..71ecc26d 100644 --- a/app/shop/controller/goods/Goods.php +++ b/app/shop/controller/goods/Goods.php @@ -382,5 +382,17 @@ class Goods extends ShopBase return JsonServer::error(GoodsLogic::getError()); } } - + /** + * @notes 导出Excel + */ + public function export() + { + $result = GoodsLogic::exportData($this->shop_id); + if(false === $result) { + return JsonServer::error(OrderLogic::getError() ?: '导出失败'); + } + $src = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME'); + $result['url'] = $src . '/uploads/excel/' . $result['url']; + return JsonServer::success('', ['url' => $result['url']]); + } } diff --git a/app/shop/logic/goods/GoodsLogic.php b/app/shop/logic/goods/GoodsLogic.php index 0ddf230e..2cb41bc6 100644 --- a/app/shop/logic/goods/GoodsLogic.php +++ b/app/shop/logic/goods/GoodsLogic.php @@ -35,7 +35,7 @@ use app\common\model\goods\Goodscontact; use app\common\server\UrlServer; use think\facade\Db; use app\common\model\shop\Shop; - +use app\common\server\ExportGoodsExcelServer; /** * 商品管理-逻辑 @@ -1232,5 +1232,55 @@ class GoodsLogic extends Logic return $data; } + /** + * @notes 导出Excel + */ + public static function exportData($shop_id) { + try { + $where = [ + ['shop_id', '=', $shop_id], + ['status', '=', GoodsEnum::STATUS_SHELVES], + ['del', '=', GoodsEnum::DEL_NORMAL], + ['audit_status', '=', GoodsEnum::AUDIT_STATUS_OK], + ['stock','exp', Db::raw('>=stock_warn')] + ]; + $order = [ + 'sort' => 'asc', + 'id' => 'desc' + ]; + + $lists = Goods::where($where) + ->with('goods_item') + ->order($order) + ->select() + ->toArray(); + + $title = ['商品名称', '颜色', '市场价', '价格', '成本价', '批发价', '集采价', '代发价']; + $data = array(); + foreach ($lists as $key => $item) { + if ($item['goods_item']) { + $count_goods_item = count($item['goods_item']); + foreach ($item['goods_item'] as $key2 => $items) { + $data[$key][] = array( + 'name' => ($count_goods_item > 1 && $key2 > 0) ? '-' : $item['name'], + 'spec_value_str' => $items['spec_value_str'], + 'market_price' => $items['market_price'], + 'price' => $items['price'], + 'chengben_price' => $items['chengben_price'], + 'pifa_price' => $items['wholesale_pce'], + 'purchase_price' => $items['purchase_price'], + 'issuing_price' => $items['issuing_price'], + ); + } + } + } + + $export = ExportGoodsExcelServer::export($title, $data, ROOT_PATH .'/uploads/excel/', '商品-' . time() . '.xlsx'); + return $export; + } catch (\Exception $e) { + self::$error = $e->getMessage(); + return false; + } + } } diff --git a/app/shop/view/goods/goods/lists.html b/app/shop/view/goods/goods/lists.html index 1bf91456..a5a3b119 100644 --- a/app/shop/view/goods/goods/lists.html +++ b/app/shop/view/goods/goods/lists.html @@ -65,6 +65,7 @@