refactor(api): 重构 IndexController 中的数据处理逻辑

- 优化了数据查询和处理的代码结构
- 提高了代码的可读性和维护性
- 新增 wps_product 方法用于处理采购商品下载
This commit is contained in:
mkm 2024-11-29 19:53:15 +08:00
parent f43f1b0886
commit ab347de4f1

View File

@ -58,7 +58,7 @@ class IndexController extends BaseApiController
public function index() public function index()
{ {
d(1); d(1);
$arr=Db::name('ceshi_copy')->select(); $arr = Db::name('ceshi_copy')->select();
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
$data = [ $data = [
@ -74,9 +74,9 @@ class IndexController extends BaseApiController
if ($rose_price > 0) { if ($rose_price > 0) {
//利润除于零售 //利润除于零售
$price_div = bcdiv($rose_price, $v['price'], 2); $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']]); StoreProduct::update($data, ['id' => $v['product_id']]);
//修改 //修改
StoreBranchProduct::where('product_id', $v['product_id'])->whereNotIn('store_id', [17, 18])->update([ StoreBranchProduct::where('product_id', $v['product_id'])->whereNotIn('store_id', [17, 18])->update([
@ -89,16 +89,16 @@ class IndexController extends BaseApiController
]); ]);
} }
d(11); d(11);
$pay_price=StoreOrder::where('store_id',3)->where('id','>=',1867)->where('id','<=',4826)->where('paid',1)->sum('pay_price'); $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'); $refund_price = StoreOrder::where('store_id', 3)->where('id', '>=', 1867)->where('id', '<=', 4826)->where('paid', 1)->sum('refund_price');
d($pay_price,$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(); $arr = StoreOrder::where('store_id', 3)->where('id', '>', 551)->where('paid', 1)->field('id,pay_price,deduction_price,refund_price')->select()->toArray();
$data=[]; $data = [];
foreach ($arr as $k => $v) { foreach ($arr as $k => $v) {
$total_price=StoreOrderCartInfo::where('oid', $v['id'])->sum('total_price'); $total_price = StoreOrderCartInfo::where('oid', $v['id'])->sum('total_price');
if($total_price != $v['pay_price']){ if ($total_price != $v['pay_price']) {
$s=$v; $s = $v;
$s['total_price']=$total_price; $s['total_price'] = $total_price;
$data[] = $s; $data[] = $s;
} }
} }
@ -259,12 +259,12 @@ class IndexController extends BaseApiController
*/ */
public function purchase_product_offer() public function purchase_product_offer()
{ {
$date=$this->request->get('date'); $date = $this->request->get('date');
if($date){ if ($date) {
$this->request->setGet(['date'=>$date,'export'=>2]); $this->request->setGet(['date' => $date, 'export' => 2]);
$lists=new PurchaseProductOfferListsTwo(); $lists = new PurchaseProductOfferListsTwo();
$exportDownloadUrl = $lists->createExcel($lists->setExcelFields(), $lists->lists()); $exportDownloadUrl = $lists->createExcel($lists->setExcelFields(), $lists->lists());
$fileKey=explode('?file=',$exportDownloadUrl)[1]; $fileKey = explode('?file=', $exportDownloadUrl)[1];
//通过文件缓存的key获取文件储存的路径 //通过文件缓存的key获取文件储存的路径
$exportCache = new ExportCache(); $exportCache = new ExportCache();
$fileInfo = $exportCache->getFile($fileKey); $fileInfo = $exportCache->getFile($fileKey);
@ -273,10 +273,42 @@ class IndexController extends BaseApiController
} }
//下载前删除缓存 //下载前删除缓存
Cache::delete($fileKey); Cache::delete($fileKey);
return response()->download($fileInfo['src'] . $fileInfo['name'],$fileInfo['name']); return response()->download($fileInfo['src'] . $fileInfo['name'], $fileInfo['name']);
}else{ } else {
return $this->fail('时间不能为空'); 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('时间不能为空');
}
}
} }