From ab347de4f1d2d5f874ffdd6f0aca1cc5683362ab Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Fri, 29 Nov 2024 19:53:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E9=87=8D=E6=9E=84=20IndexCont?= =?UTF-8?q?roller=20=E4=B8=AD=E7=9A=84=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化了数据查询和处理的代码结构 - 提高了代码的可读性和维护性 - 新增 wps_product 方法用于处理采购商品下载 --- app/api/controller/IndexController.php | 74 ++++++++++++++++++-------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/app/api/controller/IndexController.php b/app/api/controller/IndexController.php index 4220e9b9c..c93c80459 100644 --- a/app/api/controller/IndexController.php +++ b/app/api/controller/IndexController.php @@ -58,7 +58,7 @@ class IndexController extends BaseApiController public function index() { d(1); - $arr=Db::name('ceshi_copy')->select(); + $arr = Db::name('ceshi_copy')->select(); foreach ($arr as $k => $v) { $data = [ @@ -74,9 +74,9 @@ class IndexController extends BaseApiController if ($rose_price > 0) { //利润除于零售 $price_div = bcdiv($rose_price, $v['price'], 2); - $rose=bcmul($price_div, 100, 2); + $rose = bcmul($price_div, 100, 2); } - $data['rose']=$rose; + $data['rose'] = $rose; StoreProduct::update($data, ['id' => $v['product_id']]); //修改 StoreBranchProduct::where('product_id', $v['product_id'])->whereNotIn('store_id', [17, 18])->update([ @@ -89,16 +89,16 @@ class IndexController extends BaseApiController ]); } d(11); - $pay_price=StoreOrder::where('store_id',3)->where('id','>=',1867)->where('id','<=',4826)->where('paid',1)->sum('pay_price'); - $refund_price=StoreOrder::where('store_id',3)->where('id','>=',1867)->where('id','<=',4826)->where('paid',1)->sum('refund_price'); - d($pay_price,$refund_price); - $arr=StoreOrder::where('store_id',3)->where('id','>',551)->where('paid',1)->field('id,pay_price,deduction_price,refund_price')->select()->toArray(); - $data=[]; + $pay_price = StoreOrder::where('store_id', 3)->where('id', '>=', 1867)->where('id', '<=', 4826)->where('paid', 1)->sum('pay_price'); + $refund_price = StoreOrder::where('store_id', 3)->where('id', '>=', 1867)->where('id', '<=', 4826)->where('paid', 1)->sum('refund_price'); + d($pay_price, $refund_price); + $arr = StoreOrder::where('store_id', 3)->where('id', '>', 551)->where('paid', 1)->field('id,pay_price,deduction_price,refund_price')->select()->toArray(); + $data = []; foreach ($arr as $k => $v) { - $total_price=StoreOrderCartInfo::where('oid', $v['id'])->sum('total_price'); - if($total_price != $v['pay_price']){ - $s=$v; - $s['total_price']=$total_price; + $total_price = StoreOrderCartInfo::where('oid', $v['id'])->sum('total_price'); + if ($total_price != $v['pay_price']) { + $s = $v; + $s['total_price'] = $total_price; $data[] = $s; } } @@ -259,13 +259,13 @@ class IndexController extends BaseApiController */ public function purchase_product_offer() { - $date=$this->request->get('date'); - if($date){ - $this->request->setGet(['date'=>$date,'export'=>2]); - $lists=new PurchaseProductOfferListsTwo(); + $date = $this->request->get('date'); + if ($date) { + $this->request->setGet(['date' => $date, 'export' => 2]); + $lists = new PurchaseProductOfferListsTwo(); $exportDownloadUrl = $lists->createExcel($lists->setExcelFields(), $lists->lists()); - $fileKey=explode('?file=',$exportDownloadUrl)[1]; - //通过文件缓存的key获取文件储存的路径 + $fileKey = explode('?file=', $exportDownloadUrl)[1]; + //通过文件缓存的key获取文件储存的路径 $exportCache = new ExportCache(); $fileInfo = $exportCache->getFile($fileKey); if (empty($fileInfo)) { @@ -273,10 +273,42 @@ class IndexController extends BaseApiController } //下载前删除缓存 Cache::delete($fileKey); - return response()->download($fileInfo['src'] . $fileInfo['name'],$fileInfo['name']); - }else{ + return response()->download($fileInfo['src'] . $fileInfo['name'], $fileInfo['name']); + } else { + return $this->fail('时间不能为空'); + } + } + + /** + * @notes 采购商品下载 + */ + public function wps_product() + { + $data = $this->request->post(); + if ($data) { + if ($data['product_id'] > 0) { + $find = Db::name('wps_product')->where('product_id', $data['product_id'])->find(); + $arr = []; + if ($data['purchase'] > 0) { + $arr['purchase'] = $data['purchase']; + } + if ($data['cost'] > 0) { + $arr['cost'] = $data['cost']; + } + if ($data['price'] > 0) { + $arr['price'] = $data['price']; + } + if ($find) { + Db::name('wps_product')->where('id', $find['id'])->update($arr); + } else { + $arr['product_id']=$data['product_id']; + $arr['name']=$data['name']; + Db::name('wps_product')->insert($arr); + } + } + return $this->success('ok'); + } else { return $this->fail('时间不能为空'); } } - }