148 lines
4.9 KiB
PHP
148 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Resources\AreaResource;
|
|
use App\Http\Resources\CategoryResource;
|
|
use App\Http\Resources\PolicyListResource;
|
|
use App\Http\Resources\PolicyResource;
|
|
use App\Http\Resources\TradeResource;
|
|
use App\Models\Category;
|
|
use App\Models\ChinaArea;
|
|
use App\Models\Policy;
|
|
use App\Models\Trade;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PolicyController extends Controller
|
|
{
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$title = $request->title;
|
|
$start = $request->start;
|
|
$end = $request->end;
|
|
$category_id = $request->category_id;
|
|
$trade_id = $request->trade_id;
|
|
$area_id = $request->area_id;
|
|
|
|
if ($start == '开始时间') {
|
|
$start = '';
|
|
}
|
|
|
|
if ($end == '结束时间') {
|
|
$end = '';
|
|
}
|
|
|
|
$policys = Policy::with(['areas'])
|
|
->where('status', 1)
|
|
->where(function ($q) use ($title) {
|
|
$q->when($title, function ($query) use ($title) {
|
|
$query->where('title', 'like', "%{$title}%")->orWhere('no', 'like', "%{$title}%");
|
|
});
|
|
})
|
|
->when($category_id, function ($query) use ($category_id) {
|
|
$query->where('category_id', $category_id);
|
|
})
|
|
|
|
->when($trade_id, function ($query) use ($trade_id) {
|
|
$query->whereHas('trades', function ($query) use ($trade_id) {
|
|
$query->where('trade_id', $trade_id);
|
|
});
|
|
})
|
|
|
|
->when($area_id, function ($query) use ($area_id) {
|
|
$query->whereHas('areas', function ($query) use ($area_id) {
|
|
$area = ChinaArea::find($area_id);
|
|
if ($area->parent_id) {
|
|
$query->whereIn('china_area_id', [$area_id, $area->parent_id]);
|
|
} else {
|
|
$query->where('china_area_id', $area_id);
|
|
}
|
|
});
|
|
})
|
|
->when($start && $end, function ($query) use ($start, $end) {
|
|
return $query->whereBetween('begined_at', [$start, date('Y-m-d', strtotime("+1 days", strtotime($end)))]);
|
|
})
|
|
->when($start && !$end, function ($query) use ($start) {
|
|
return $query->where('begined_at', '>=', $start);
|
|
})
|
|
->when(!$start && $end, function ($query) use ($end) {
|
|
return $query->where('begined_at', '<=', date('Y-m-d', strtotime("+1 days", strtotime($end))));
|
|
})
|
|
->orderBy('created_at', 'desc')->paginate();
|
|
|
|
return PolicyListResource::collection($policys)->additional([
|
|
'status' => 'SUCCESS',
|
|
'status_code' => 200,
|
|
]);
|
|
|
|
// return $this->success(new PolicyCollection($policys));
|
|
}
|
|
|
|
public function show(Policy $policy)
|
|
{
|
|
return $this->success(new PolicyResource($policy));
|
|
}
|
|
|
|
//收藏
|
|
public function favorite(Policy $policy)
|
|
{
|
|
if ($this->user->hasFavorited($policy)) {
|
|
$result = $this->user->unfavorite($policy);
|
|
$text = '取消收藏';
|
|
} else {
|
|
$result = $this->user->favorite($policy);
|
|
$text = '收藏';
|
|
}
|
|
|
|
if ($result) {
|
|
return ['status_code' => 0, 'status' => 'SUCCESS', 'message' => $text . '成功', 'isFavorited' => $this->user->hasFavorited($policy)];
|
|
} else {
|
|
return ['status_code' => 4000, 'status' => 'ERROR', 'message' => $text . '失败', 'isFavorited' => $this->user->hasFavorited($policy)];
|
|
}
|
|
}
|
|
|
|
//部门
|
|
public function categories()
|
|
{
|
|
$lists = Category::where('status', 1)->get();
|
|
return $this->success(CategoryResource::collection($lists));
|
|
}
|
|
|
|
//行业
|
|
public function trades()
|
|
{
|
|
$lists = Trade::where('status', 1)->get();
|
|
return $this->success(TradeResource::collection($lists));
|
|
}
|
|
|
|
//地区
|
|
public function areas()
|
|
{
|
|
$areas = ChinaArea::where('parent_id', 9)->get();
|
|
return $this->success(AreaResource::collection($areas));
|
|
}
|
|
|
|
//搜索条件
|
|
public function parameters()
|
|
{
|
|
$categorys = Category::where('status', 1)->get();
|
|
$trades = Trade::where('status', 1)->get();
|
|
$areas = ChinaArea::where('parent_id', 9)->Orwhere('id', 9)->get();
|
|
|
|
$categorys->prepend(new Category(['id' => '', 'title' => '部门']));
|
|
$trades->prepend(new Trade(['id' => '', 'title' => '行业']));
|
|
$areas->prepend(new ChinaArea(['id' => '', 'name' => '区域']));
|
|
|
|
return [
|
|
'status_code' => 0,
|
|
'status' => 'SUCCESS',
|
|
'categorys' => CategoryResource::collection($categorys),
|
|
'trades' => TradeResource::collection($trades),
|
|
'areas' => AreaResource::collection($areas),
|
|
];
|
|
#Todo..
|
|
}
|
|
|
|
}
|