You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.6 KiB
PHP

<?php
namespace App\Libs;
use App\Models\Partner;
use App\Services\CrossFileService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
trait TraitPartner
{
/**
* 업체목록 가져오기
*
* @param Request $request
* @param int $perPage
* @param bool $status
* @return array
*/
protected function getPartnerList(Request $request, $perPage = 15, $status = true)
{
$list = Partner::query()
->with('user')
->where(function ($query) use ($request, $status) {
if ($request->filled('status')) {
$query->where('status', $request->status);
} else if ($status) {
$query->where('status', 1);
}
if ($request->filled('word')) {
if ($request->filled('field') && $request->field != 'all') {
$query->where($request->field, 'like', '%' . $request->word . '%');
} else {
$query->orWhere('title', 'like', '%'. $request->word .'%');
}
}
})
->latest()
->paginate($perPage);
$list = $this->getListNumber($list);
$search = $request->all();
$list->appends($search);
$searchs = [
'name' => '업체명'
];
$data = [
'mode' => 'list',
'data' => $list,
'search' => $search,
'searchs' => $searchs
];
return $data;
}
/**
* 업체정보 가져오기
*
* @param string $uid
* @return mixed
*/
protected function getPartnerInfo($uid = '')
{
$data = Partner::query()
->with('user')
->where('uid', $uid)
->first();
return $data;
}
/**
* 업체 등록
*
* @param string $uid
* @return array
*/
protected function createPartner($uid = '')
{
if ($uid) {
$data = $this->getPartnerInfo($uid);
$data->mode = 'modify';
} else {
$data = new Partner;
$data->mode = 'add';
$data->uid = '';
$data->category = 1;
$data->status = 1;
}
return $data;
}
/**
* 업체정보 저장
*
* @param Request $request
* @param CrossFileService $fileService
* @return mixed
*/
protected function storePartner(Request $request, CrossFileService $fileService)
{
return DB::transaction(function() use ($request, $fileService) {
$rules = [
'mode' => 'required|string',
'title' => 'required|string'
];
$validator = $this->validation($request, $rules);
if ($validator !== true) {
return $this->sendResult($validator);
}
$mode = $request->get('mode', 'add');
$uid = $request->get('uid', '');
$data = $this->getPartnerInfo($uid);
$postData = $request->only([
'category', 'title', 'link', 'status'
]);
$postData = array_map('setDefault', $postData);
// 업체이미지
if ($request->hasFile('image')) {
// 기존파일 삭제
if ($data && $data->image) {
$fileService->deleteFile($data->image);
}
$asset = $fileService->saveFile(auth()->user(), $request, 'image');
$postData['image'] = $asset->uid;
$postData['width'] = $asset->width;
$postData['height'] = $asset->height;
}
// 수정
if ($mode == 'modify') {
if (!$data) {
return $this->sendResult('업체정보가 존재하지 않습니다.');
}
$data->fill($postData);
$data->save();
} else {
$appends = [
'uid' => Str::uuid()->toString(),
'created_id' => auth()->user()->id,
'created_at' => (string)Carbon::now()
];
$postData = array_merge($postData, $appends);
$data = Partner::query()->create($postData);
if (!$data) {
return $this->sendResult('업체정보 등록에 실패했습니다.');
}
}
if ($mode == 'add') {
$message = '업체정보가 등록되었습니다.';
} else {
$message = '업체정보가 수정되었습니다.';
}
return $this->sendResult($message, 'success');
});
}
/**
* 업체정보 삭제
*
* @param Request $request
* @param CrossFileService $fileService
* @return mixed
*/
public function deletePartner(Request $request, CrossFileService $fileService)
{
return DB::transaction(function() use ($request, $fileService) {
$uid = $request->get('uid', '');
$data = $this->getPartnerInfo($uid);
if (!$data) {
return $this->sendResult('업체정보가 존재하지 않습니다.');
}
// 기존파일 삭제
if ($data->image) {
$fileService->deleteFile($data->image);
}
$data->delete();
return $this->sendResult('업체정보가 삭제되었습니다.', 'success');
});
}
}