下面是我寫的查詢語句,可愁死我了,功能實(shí)現(xiàn)倒是實(shí)現(xiàn)了,寫的太難受了,重復(fù)好多,請問我該如何簡化呢,或者該怎么傳入變量查詢,或者有什么其他方法簡化,請舉例,或演示部分,謝謝
public function postTreat(Request $request)
{
$_page = $request->input("_page"); //頁碼
$_path = $request->input("_path"); //第三級 path
$_sortType = $request->input("_sortType"); //綜合類別
$_sales = $request->input("_sales"); //銷售優(yōu)先
$_priceSmall = $request->input("_priceSmall"); //最低價(jià)
$_priceBig = $request->input("_priceBig"); //最高價(jià)
$page=($_page-1)*4;
// 是否有價(jià)格區(qū)間限制
if(empty($_priceSmall)&&empty($_priceBig)){
// 是否按銷量排序
if(empty($_sales)){
// 是否有綜合排序 判斷綜合類別
if($_sortType=="composite" || $_sortType==""){ //綜合 或 沒有
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_up"){ //價(jià)格最低
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->orderBy('goods_price','asc') // 價(jià)格最低
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_down"){ //價(jià)格最高
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->orderBy('goods_price','desc') // 價(jià)格最高
->skip($page)
->take(4)
->get();
}else if($_sortType=="assess_down"){ // 評價(jià)最多
// 只有在走這個(gè)區(qū)間的時(shí)候,才需要關(guān)聯(lián)查詢 評價(jià)的數(shù)量
$data = DB::table('shop_goods')
->leftJoin('shop_assess', 'shop_goods.goods_id', '=', 'shop_assess.assess_gcode')
->selectRaw('shop_goods.*,COUNT(shop_assess.assess_id) as assess_num')
->where('shop_goods.goods_cid',$_path)
->where('shop_goods.goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('shop_goods.goods_state',0) // 0已上架 1已下架
->where('shop_goods.goods_recycle',0) // 0正常 1回收站
->groupBy('shop_goods.goods_id')
->orderBy('assess_num','desc')
->get();
}else if($_sortType=="publish_new"){ //最新發(fā)布
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->orderBy('goods_time','desc') // 最新發(fā)布
->skip($page)
->take(4)
->get();
}
}else{
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->orderBy('goods_num','desc') // 銷售倒序排列
->skip($page)
->take(4)
->get();
}
}else{
// 是否按銷量排序
if(empty($_sales)){
// 是否有綜合排序 判斷綜合類別
if($_sortType=="composite" || $_sortType==""){
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->whereBetween('goods_price',[$_priceSmall,$_priceBig]) // 價(jià)格區(qū)間
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_up"){
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->whereBetween('goods_price',[$_priceSmall,$_priceBig]) // 價(jià)格區(qū)間
->orderBy('goods_price','asc') // 價(jià)格最低
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_down"){
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->whereBetween('goods_price',[$_priceSmall,$_priceBig]) // 價(jià)格區(qū)間
->orderBy('goods_price','desc') // 價(jià)格最高
->skip($page)
->take(4)
->get();
}else if($_sortType=="assess_down"){
$data = DB::table('shop_goods')
->leftJoin('shop_assess', 'shop_goods.goods_id', '=', 'shop_assess.assess_gcode')
->selectRaw('shop_goods.*,COUNT(shop_assess.assess_id) as assess_num') //統(tǒng)計(jì)評價(jià)的數(shù)量
->where('shop_goods.goods_cid',$_path)
->where('shop_goods.goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('shop_goods.goods_state',0) // 0已上架 1已下架
->where('shop_goods.goods_recycle',0) // 0正常 1回收站
->whereBetween('shop_goods.goods_price',[$_priceSmall,$_priceBig]) // 價(jià)格區(qū)間
->groupBy('shop_goods.goods_id')
->orderBy('assess_num','desc')
->get();
}else if($_sortType=="publish_new"){
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->whereBetween('goods_price',[$_priceSmall,$_priceBig]) // 價(jià)格區(qū)間
->orderBy('goods_time','desc') // 最新發(fā)布
->skip($page)
->take(4)
->get();
}
}else{
$data = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
->whereBetween('goods_price',[$_priceSmall,$_priceBig])
->orderBy('goods_num','desc')
->skip($page)
->take(4)
->get();
}
}
foreach($data as $key => $value){
if($value->goods_num>10000){
$value->goods_num = round(($value->goods_num)/10000,1).'萬'; //將銷量轉(zhuǎn)換
}
}
return $data;
}
代碼重復(fù)的部分多,那么關(guān)鍵就是找出不重復(fù)的地方是什么,然后把重復(fù)的地方先弄成一塊,再按照不同的條件細(xì)分。
比如,你這上面的語句有很多按照$_sortType來判定排序的,自然你可以先用一個(gè)參數(shù)來存儲(chǔ)最基本的查詢語句;
$basic = DB::table('shop_goods')
->where('goods_cid',$_path)
->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('goods_state',0) // 0已上架 1已下架
->where('goods_recycle',0) // 0正常 1回收站
然后再根據(jù)$_sortType進(jìn)行條件判斷:
if($_sortType=="composite" || $_sortType==""){ //綜合 或 沒有
$data = $basic
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_up"){ //價(jià)格最低
$data = $basic
->orderBy('goods_price','asc') // 價(jià)格最低
->skip($page)
->take(4)
->get();
}else if($_sortType=="price_down"){ //價(jià)格最高
$data = $basic
->orderBy('goods_price','desc') // 價(jià)格最高
->skip($page)
->take(4)
->get();
}else if($_sortType=="assess_down"){ // 評價(jià)最多
$data = DB::table('shop_goods')
->leftJoin('shop_assess', 'shop_goods.goods_id', '=', 'shop_assess.assess_gcode')
->selectRaw('shop_goods.*,COUNT(shop_assess.assess_id) as assess_num')
->where('shop_goods.goods_cid',$_path)
->where('shop_goods.goods_status',1) // 0未審核 1審核通過 2審核未通過
->where('shop_goods.goods_state',0) // 0已上架 1已下架
->where('shop_goods.goods_recycle',0) // 0正常 1回收站
->groupBy('shop_goods.goods_id')
->orderBy('assess_num','desc')
->get();
}else if($_sortType=="publish_new"){ //最新發(fā)布
$data = $basic
->orderBy('goods_time','desc') // 最新發(fā)布
->skip($page)
->take(4)
->get();
}
大概的思路就是這樣。
提供一段更優(yōu)雅的代碼
寫一段大概的代碼,具體細(xì)節(jié)你需要再重寫一下
protected $orderByMap = [
'assess_down' => ['assess_num', 'desc'],
'price_up' => ['goods_price', 'asc'],
'price_down' => ['goods_price', 'desc'],
'publish_new' => ['goods_time', 'desc'],
];
/**
* @param Request $request
* @return mixed
*/
public function postTreat(Request $request)
{
// 頁碼
$_page = $request->input("_page");
$page = ($_page-1)*4;
/**
* 構(gòu)建出一個(gè)公共的 Query
*
* @var $shopGoodsQuery \Illuminate\Database\Query\Builder
*/
$shopGoodsQuery = DB::table('shop_goods')
->where('goods_status', 1)// 0未審核 1審核通過 2審核未通過
->where('goods_state', 0)// 0已上架 1已下架
->where('goods_recycle', 0); // 0正常 1回收站;
// 最低價(jià)
$shopGoodsQuery->when($request->input('_priceSmall'), function (Builder $query, $priceSmall) {
$query->where('goods_price', '>', $priceSmall);
});
// 最高價(jià)
$shopGoodsQuery->when($request->input('_priceBig'), function (Builder $query, $priceBig) {
$query->where('goods_price', '<', $priceBig);
});
// 是否按總和排序
$shopGoodsQuery->when($request->input("_sales"), function (Builder $query, $sales) {
$query->where('goods_num', 'desc');
}, function (Builder $query, $sales) {
// 不按總價(jià)排序的, 這個(gè)相當(dāng)于 else
});
// 排序用 map 表映射找出字段排序
$shopGoodsQuery->when($request->input("_sortType"), function (Builder $query, $sortType) {
// 得到要排序的字段, 如果沒有就按默認(rèn)的第一種排序
list($field, $orderBy) = $this->orderByMap[$sortType] ?? array_first($this->orderByMap);
$query->orderBy($field, $orderBy);
});
// 最后進(jìn)行統(tǒng)一分頁
$data = $shopGoodsQuery->skip($page)->take(4)->get();
dd($data);
}
順便發(fā)一下 Laravel 技術(shù)交流群號:584453488
補(bǔ)充, when
和if
是一樣的效果,如果沒有when
就用if
代替
// 頁碼
$_page = $request->input("_page");
$page = ($_page-1)*4;
/**
* 構(gòu)建出一個(gè)公共的 Query
*
* @var $shopGoodsQuery \Illuminate\Database\Query\Builder
*/
$shopGoodsQuery = DB::table('shop_goods')
->where('goods_status', 1)// 0未審核 1審核通過 2審核未通過
->where('goods_state', 0)// 0已上架 1已下架
->where('goods_recycle', 0); // 0正常 1回收站;
// 最低價(jià)
if ($request->filled('_priceSmall')) {
$shopGoodsQuery->where('goods_price', '>', $request->input('_priceSmall'));
}
// 最高價(jià)
if ($request->filled('_priceBig')) {
$shopGoodsQuery->where('goods_price', '>', $request->input('_priceBig'));
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。