course/app/common/service/StudioService.php

198 lines
5.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\service;
use app\common\model\Photographer;
use app\common\model\ServiceType;
use app\common\model\Studio;
use app\common\model\User;
use app\constant\ResponseCode;
use think\Exception;
class StudioService
{
/**
* @desc 添加工作室
* @param $request
* @return array
*/
public function add($request)
{
try {
$post_data = $request->post();
$user = User::where(['id' => $request->user->id])->findOrEmpty();
if (!$user->is_photographer) {
throw new Exception('您还不是摄影师,请先成为摄影师');
}
$studio = Studio::where(['studio_name' => $post_data['studio_name']])->findOrEmpty();
if (!$studio->isEmpty()) {
throw new Exception('工作室名称已存在,请更换名称');
}
//@todo判断是否一个人只能添加一个工作室
$res = Studio::create([
'studio_name' => $post_data['studio_name'],
'img' => $post_data['img'],
'introductions' => $post_data['introductions'],
'user_id' => $request->user->id
]);
if (!$res) {
throw new Exception('添加失败');
}
User::where(['id' => $request->user->id])->save(['studio_id' => $res->id]);
return [
'code' => ResponseCode::SUCCESS,
'msg' => '添加成功'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 工作室列表
* @param $request
* @return array
*/
public function select($request)
{
try {
$list = Studio::order('id desc')->select();
return [
'code' => ResponseCode::SUCCESS,
'data' => $list,
'msg' => 'success'
];
}catch (Exception $e){
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 详情
* @param $request
* @return array
*/
public function detail($request)
{
try {
$data = $request->get();
$studio = Studio::where(['id' => $data['id']])->findOrEmpty();
if ($studio->isEmpty()) {
throw new Exception('工作室不存在');
}
$studio = $studio->toArray();
$studio['is_me'] = 0;
if((array)$request->user){
if($studio['user_id'] == $request->user->id){
$studio['is_me'] = 1;
}
}
return [
'code' => ResponseCode::SUCCESS,
'data' => $studio,
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 工作室摄影师
* @param $request
* @return array
*/
public function getStudioPhotographer($request)
{
try {
$studio = Studio::where(['id' => $request['studio_id']])->findOrEmpty();
if ($studio->isEmpty()) {
throw new Exception('工作室不存在');
}
$photographer = Photographer::where(['p.studio_id' => $studio->id])->alias('p');
$page = isset($request['page']) ? $request['page'] : 1;
$limit = isset($request['limit']) ? $request['limit'] : 10;
$total = $photographer->count();
$list = $photographer->page($page, $limit)->select();
foreach ($list as &$item) {
$service_type_name = '';
if ($item['service_type']) {
$service_type_name = ServiceType::where([
'id' => explode(',', $item['service_type'])
])
->field('service_name')
->order('sort desc, id desc')
->select();
if ($service_type_name) {
$service_type_name = array_column($service_type_name->toArray(), 'service_name');
}
}
$item['service_type_name'] = $service_type_name;
}
return [
'code' => ResponseCode::SUCCESS,
'data' => [
'list' => $list,
'total' => $total,
'page' => $page
],
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
/**
* @desc 移除摄影师
* @param $request
* @return array
*/
public function removePhotographer($request)
{
try {
$data = $request->post();
$studio = Studio::where(['id' => $data['studio_id']])->findOrEmpty();
$photographer = Photographer::where(['id' => $data['photographer_id']])->findOrEmpty();
if ($studio->user_id != $request->user->id) {
throw new Exception('非工作室管理者不能操作');
}
$photographer->save(['studio_id' => 0]);
User::where(['id' => $photographer->user_id])->save(['studio_id' => 0]);
return [
'code' => ResponseCode::SUCCESS,
'msg' => '操作成功'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
}