course/app/common/service/CaseShareService.php

295 lines
9.5 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\CaseShare;
use app\common\model\Photographer;
use app\common\model\ServiceType;
use app\common\model\User;
use app\common\validate\CaseShareValidate;
use app\constant\ResponseCode;
use think\Exception;
class CaseShareService
{
public function insert($request)
{
try {
$user = User::where(['id' => $request->user->id])->findOrEmpty();
if (!$user->is_photographer) {
throw new Exception('您还不是摄影师,不能发布案例');
}
$validate = new CaseShareValidate();
$requestData = $request->post();
if (!$validate->check($requestData)) {
throw new Exception($validate->getError());
}
//查找摄影师
$photographer = Photographer::where(['user_id' => $request->user->id])->findOrEmpty();
if ($photographer->isEmpty()) {
throw new Exception('为找到摄影师信息');
}
$service_type = ServiceType::where(['id'=>$requestData['service_type']])->findOrEmpty();
CaseShare::create([
'user_id' => $request->user->id,
'photographer_id' => $photographer->id,
'service_type' => $requestData['service_type'],
'service_name' => $service_type->service_name,
'case_name' => $requestData['case_name'],
'case_briefing' => $requestData['case_briefing'],
'case_introduce' => $requestData['case_introduce'],
'imgs' => !empty($requestData['imgs']) ? $requestData['imgs'] : '',
// 'imgs' => !empty($requestData['imgs']) ? json_encode($requestData['imgs'], JSON_UNESCAPED_UNICODE) : '',
]);
return [
'code' => ResponseCode::SUCCESS,
'data' => [],
'msg' => '提交成功,审核成功后自动发布'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 更新案例
* @param $request
* @return array
*/
public function update($request)
{
try {
$req_data = $request->post();
$case = CaseShare::where(['id' => $req_data['id']])->findOrEmpty();
if ($case->isEmpty()) {
throw new Exception('未找到案例');
}
if ($case->user_id != $request->user->id) {
throw new Exception('非案例所有者不能操作');
}
//查找摄影师
$photographer = Photographer::where(['user_id' => $request->user->id])->findOrEmpty();
if ($photographer->isEmpty()) {
throw new Exception('为找到摄影师信息');
}
$validate = new CaseShareValidate();
if (!$validate->check($req_data)) {
throw new Exception($validate->getError());
}
$res = $case->save([
'photographer_id' => $photographer->id,
'service_type' => $req_data['service_type'],
'case_name' => $req_data['case_name'],
'case_briefing' => $req_data['case_briefing'],
'case_introduce' => $req_data['case_introduce'],
'imgs' => empty($req_data['imgs']) ? '' : json_encode($req_data['img'], JSON_UNESCAPED_UNICODE),
]);
if (!$res) {
throw new Exception('更新失败');
}
return [
'code' => ResponseCode::SUCCESS,
'msg' => '更新成功'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 删除成功
* @param $request
* @return array
*/
public function del($request)
{
try {
$case = CaseShare::where(['id' => $request['id']])->findOrEmpty();
if ($case->isEmpty()) {
throw new Exception('未找到案例或已被删除');
}
$res = $case->delete();
if (!$res) {
throw new Exception('操作失败');
}
return [
'code' => ResponseCode::SUCCESS,
'msg' => '已删除成功'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 案例列表
* @param $request
* @return array
*/
public function getCaseShareList($request)
{
try {
$case = CaseShare::where(['is_apply' => 1]);
if (isset($request['is_recommend']) && $request['is_recommend']) {
$case->where(['is_recommend' => $request['is_recommend']]);
}
if (isset($request['service_type']) && $request['service_type']) {
$case->where(['service_type' => $request['service_type']]);
}
$page = isset($request['page']) ? $request['page'] : 1;
$limit = isset($request['limit']) ? $request['limit'] : 10;
$total = $case->count();
$list = $case->with(['photographer'])->page($page, $limit)->select();
foreach ($list as &$item){
if($item['imgs']){
$item['imgs'] = explode(',', stripslashes($item['imgs']));
}
}
return [
'code' => ResponseCode::SUCCESS,
'data' => [
'list' => $list,
'total' => $total,
'page' => $page
],
'msg' => 'success'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 用户案例列表
* @param $request
* @return array
*/
public function getUserCaseShareList($request)
{
try {
$requestData = $request->get();
$case = CaseShare::where(['user_id' => $request->user->id]);
if (isset($requestData['is_recommend']) && $requestData['is_recommend']) {
$case->where(['is_recommend' => $requestData['is_recommend']]);
}
if (isset($requestData['service_type']) && $requestData['service_type']) {
$case->where(['service_type' => $requestData['service_type']]);
}
$page = isset($requestData['page']) ? $requestData['page'] : 1;
$limit = isset($requestData['limit']) ? $requestData['limit'] : 10;
$total = $case->count();
$list = $case->page($page, $limit)->select();
foreach ($list as &$item){
if($item['imgs']){
$item['imgs'] = explode(',', $item['imgs']);
}
}
return [
'code' => ResponseCode::SUCCESS,
'data' => [
'list' => $list,
'total' => $total,
'page' => $page
],
'msg' => 'success'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 案例详情
* @param $request
* @return array
*/
public function caseDetail($request)
{
try {
$case = CaseShare::where(['id' => $request['id']])->with(['serviceType', 'photographer'])->findOrEmpty();
if ($case->isEmpty()) {
throw new Exception('未找到案例');
}
if ($case->imgs) {
$case->imgs = explode(',', $case->imgs);
}
return [
'code' => ResponseCode::SUCCESS,
'data' => $case,
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 摄影师案例服务类型
* @param $request
* @return array
*/
public function getPhotographerCaseShareServiceType($request)
{
try {
$photographer = Photographer::where(['id' => $request['photographer_id']])->findOrEmpty();
if ($photographer->isEmpty()) {
throw new Exception('摄影师不存在');
}
$service_type = CaseShare::where(['photographer_id' => $request['photographer_id']])->field('service_type')->select()->toArray();
if($service_type){
$service_type = array_unique(array_column($service_type, 'service_type'));
$service_type = ServiceType::where(['id'=>$service_type])->order('sort desc, id desc')->field('id,service_name')->select();
}else{
$service_type = [];
}
return [
'code' => ResponseCode::SUCCESS,
'data' => $service_type,
'msg' => 'success'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
}