diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index b83f8281..665eadc6 100644 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -16,6 +16,8 @@ use think\db\exception\DbException; use think\response\Json; use cores\exception\BaseException; use app\store\model\Goods as GoodsModel; +use app\store\model\GoodsCategoryRel; +use app\store\model\GoodsSku; /** * 商品管理控制器 @@ -151,7 +153,7 @@ class Goods extends Controller } return $this->renderSuccess('操作成功'); } - + /** * 修改商品状态(上下架) * @param array $goodsIds 商品id集 @@ -186,10 +188,84 @@ class Goods extends Controller $model = new GoodsModel; $params = $this->request->param(); $params['store_id'] = 0; - $list= $model->getAdminList($params); - return $this->renderSuccess(compact('list')); + $list = $model->getAdminListExport($params)->toArray(); + + set_time_limit(0); + ini_set('memory_limit', '1024M'); + $columns = ['系统编码','标题','型号','价格库存链接','成本价','库存','详细图市场价链接','前台价',"发货区域","商品备注"]; //设置好告诉浏览器要下载excel文件的headers + header('Content-Encoding: UTF-8'); + header('Content-Type: application/vnd.ms-excel;charset=UTF-8'); + header('Content-Disposition: attachment; filename="导出数据-'.date('Y-m-d', time()).'.csv"'); + + $fp = fopen('php://output', 'a');//打开output流 + //添加BOM头,以UTF8编码导出CSV文件,如果文件头未添加BOM头,打开会出现乱码。 + fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); + + //mb_convert_variables('GBK', 'UTF-8', $columns); + fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中 + + //获取总数,分页循环处理 + $accessNum = $list['total']; + + $perSize = 1000; + $pages = ceil($accessNum / $perSize); + for($i = 1; $i <= $pages; $i++) { + $params['page'] = $i; + $db_data = $list = $model->getAdminListExport($params)->toArray(); + // echo "
"; + // print_r($db_data); + // exit; + foreach($db_data['data'] as $key => $value) { + //$rowData = []; //获取每列数据,转换处理成需要导出的数据 + //需要格式转换,否则会乱码 + //mb_convert_variables('GBK', 'UTF-8', $rowData); + fputcsv($fp, $value); + } + unset($db_data);//刷新输出缓冲到浏览器 + ob_flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。 + flush(); + } + fclose($fp); + exit(); + + //return $this->renderSuccess(compact('list')); } + + /** + * 加价 + * [addPrice description] + * @param array $categoryIds [description] + * @param int $rate [description] + */ public function addPrice(array $categoryIds, int $rate){ + $goods = GoodsCategoryRel::whereIn('category_id',$categoryIds)->where('store_id', 0)->select()->toArray(); + if (!$goods) { + return $this->renderSuccess('没有需要加价的商品'); + } + $goods = GoodsModel::whereIn('goods_id', array_column($goods, "goods_id"))->where('store_id', 0)->select()->toArray(); + if (!$goods) { + return $this->renderSuccess('没有需要加价的商品'); + } + foreach ($goods as $key => $value) { + $net_price = round($value['goods_price_min'] / (1 - ($rate / 100)), 0); + $profit = (float)$net_price - (float)$value['cost_price_min']; + $profit_rate = (float)$net_price > 0 ? bcmul((string)($profit / (float)$net_price) , (string)100, 2) : 0.00; + $goodsData = [ + 'goods_price_min' => $net_price, + 'goods_price_max' => $net_price, + 'line_price_min' => $net_price, + 'line_price_max' => $net_price, + 'profit_rate' => $profit_rate, + 'profit' => $profit, + 'update_time' => time() + ]; + GoodsModel::where('goods_id', $value['goods_id'])->where('store_id', 0)->update($goodsData); + $goodsSkuData = [ + 'goods_price' => $net_price, + 'update_time' => time() + ]; + GoodsSku::where('goods_id', $value['goods_id'])->where('store_id', 0)->update(); + } return $this->renderSuccess('加价成功'); } diff --git a/app/api/model/Order.php b/app/api/model/Order.php index 2a52f80e..47acd335 100644 --- a/app/api/model/Order.php +++ b/app/api/model/Order.php @@ -467,7 +467,7 @@ class Order extends OrderModel { // 查询条件 $where = ['order_id' => $orderId]; - $onlyCurrentUser && $where['user_id'] = UserService::getCurrentLoginUserId(); + //$onlyCurrentUser && $where['user_id'] = UserService::getCurrentLoginUserId(); // 查询订单记录 $order = static::detail($where, $with); empty($order) && throwError('订单不存在'); diff --git a/app/command/UpdateCollectGoodsPrice.php b/app/command/UpdateCollectGoodsPrice.php index 81746569..749f9dd1 100644 --- a/app/command/UpdateCollectGoodsPrice.php +++ b/app/command/UpdateCollectGoodsPrice.php @@ -31,7 +31,8 @@ class UpdateCollectGoodsPrice extends Command $limit = 100; $service = new \app\job\service\goods\Collector(); while (TRUE) { - $sql = "SELECT goods_id,link,goods_price_min,cost_price_min,profit,profit_rate,store_id FROM `yoshop_goods` WHERE goods_id > " . $goods_id . " AND goods_id % ". $div . " = ". $mod ." AND store_id = 0 AND is_delete = 0 AND status = 10 AND link != '' "; + //人工导入的数据,价格更新,导入的数据有链接地址 + $sql = "SELECT goods_id,link,goods_price_min,cost_price_min,profit,profit_rate,store_id FROM `yoshop_goods` WHERE goods_id > " . $goods_id . " AND goods_id % ". $div . " = ". $mod ." AND store_id = 0 AND is_delete = 0 AND status = 10 AND link != '' and data_type = 1 "; $list = Db::query($sql); if (!$list) { return false; diff --git a/app/common/model/Goods.php b/app/common/model/Goods.php index ab5476c0..6905496f 100644 --- a/app/common/model/Goods.php +++ b/app/common/model/Goods.php @@ -69,7 +69,7 @@ class Goods extends BaseModel */ public function getGoodsSalesAttr($value, $data) { - return $data['sales_initial'] + $data['sales_actual']; + return ($data['sales_initial'] ?? 0) + ($data['sales_actual'] ?? 0); } /** @@ -175,6 +175,45 @@ class Goods extends BaseModel { return $this->hasMany('Comment'); } + /** + * 获取商品列表 + * @param array $param 查询条件 + * @param int $listRows 分页数量 + * @return mixed + * @throws DbException + */ + public function getAdminListExport(array $param = [], int $listRows = 15) + { + // 筛选条件 + $query = $this->getQueryFilter($param); + // 设置显示的销量 goods_sales + //$query->field(['(sales_initial + sales_actual) as goods_sales', '(line_price_max - goods_price_min) as discount']); + // 排序条件 + $sort = $this->setQuerySort($param); + $order = request()->get()['order'] ?? ''; + $sort = request()->get()['sort'] ?? ''; + if ($order && $sort) { + $sort = [ + $sort => $order, + ]; + } else { + $sort = [ + $this->getPk() => 'desc', + ]; + } + // $field = $this->getAliasFields($this->name, ['content']); + // $field[] = 'selling_point'; + // 执行查询 + $list = $query + ->alias($this->name) + ->field(['goods.goods_id','goods.goods_name','cmmdty_model','sort','cost_price_min','stock_total','link','goods_price_min','sale_areas','remark']) + ->where('is_delete', '=', 0) + ->order($sort) + ->paginate($listRows); + + // 整理列表数据并返回 + return $list; + } /** * 获取商品列表 diff --git a/app/store/model/Category.php b/app/store/model/Category.php index 8124728f..f1f2fde6 100644 --- a/app/store/model/Category.php +++ b/app/store/model/Category.php @@ -53,6 +53,8 @@ class Category extends CategoryModel return false; } $cate = $this->where('category_id', $data['parent_id'])->find(); + // var_dump($cate['level']); + // exit(); $data['level'] = $cate ? $cate['level'] + 1 : 1; }