|
@@ -0,0 +1,228 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\admin\controller\sys_manage;
|
|
|
+
|
|
|
+use app\controller\Curd;
|
|
|
+use app\model\SysDept;
|
|
|
+use app\model\SysUser;
|
|
|
+use support\Db;
|
|
|
+use support\exception\BusinessException;
|
|
|
+use support\Request;
|
|
|
+use support\Response;
|
|
|
+use Tinywan\Jwt\JwtToken;
|
|
|
+
|
|
|
+class RestaurantTableController extends Curd{
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new SysDept();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function select(Request $request):Response
|
|
|
+ {
|
|
|
+ $uid = JwtToken::getCurrentId();
|
|
|
+ $user = SysUser::where('user_id',$uid)->first();
|
|
|
+ $restaurant = SysDept::where('dept_super_id',$user->join_user_dept_id)->where('dept_category','餐厅')->first();
|
|
|
+ if(!$restaurant){
|
|
|
+ return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
|
|
|
+ }
|
|
|
+
|
|
|
+ [$where, $format, $limit, $field, $order] = $this->selectInput($request);
|
|
|
+ $where['dept_category'] = '桌台';
|
|
|
+ $where['dept_super_id'] = $restaurant->dept_id;
|
|
|
+ $order = $request->get('order', 'desc');
|
|
|
+ $field = $field ?? 'dept_sort';
|
|
|
+ $query = $this->doSelect($where, $field, $order);
|
|
|
+
|
|
|
+ return $this->doFormat($query, $format, $limit);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function doSelect(array $where, string $field = null, string $order = 'desc')
|
|
|
+ {
|
|
|
+ $model = $this->model->with([
|
|
|
+ 'premises' => function ($query) {
|
|
|
+ $query->select('dept_id', 'dept_name');
|
|
|
+ },
|
|
|
+ ]);
|
|
|
+ foreach ($where as $column => $value) {
|
|
|
+ if (is_array($value)) {
|
|
|
+ if ($value[0] === 'like' || $value[0] === 'not like') {
|
|
|
+ $model = $model->where($column, $value[0], "%$value[1]%");
|
|
|
+ } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
|
|
|
+ $model = $model->where($column, $value[0], $value[1]);
|
|
|
+ } elseif ($value[0] == 'in' && !empty($value[1])) {
|
|
|
+ $valArr = $value[1];
|
|
|
+ if (is_string($value[1])) {
|
|
|
+ $valArr = explode(",", trim($value[1]));
|
|
|
+ }
|
|
|
+ $model = $model->whereIn($column, $valArr);
|
|
|
+ } elseif ($value[0] == 'not in' && !empty($value[1])) {
|
|
|
+ $valArr = $value[1];
|
|
|
+ if (is_string($value[1])) {
|
|
|
+ $valArr = explode(",", trim($value[1]));
|
|
|
+ }
|
|
|
+ $model = $model->whereNotIn($column, $valArr);
|
|
|
+ } elseif ($value[0] == 'null') {
|
|
|
+ $model = $model->whereNull($column);
|
|
|
+ } elseif ($value[0] == 'not null') {
|
|
|
+ $model = $model->whereNotNull($column);
|
|
|
+ } elseif ($value[0] !== '' || $value[1] !== '') {
|
|
|
+ $model = $model->whereBetween($column, $value);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $model = $model->where($column, $value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($field) {
|
|
|
+ $model = $model->orderBy($field, $order);
|
|
|
+ }
|
|
|
+ return $model;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function insert(Request $request): Response
|
|
|
+ {
|
|
|
+ $uid = JwtToken::getCurrentId();
|
|
|
+ $user = SysUser::where('user_id',$uid)->first();
|
|
|
+ $restaurant = SysDept::where('dept_super_id',$user->join_user_dept_id)->where('dept_category','餐厅')->first();
|
|
|
+ if(!$restaurant){
|
|
|
+ return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::beginTransaction();
|
|
|
+ try {
|
|
|
+ $data = $this->insertInput($request);
|
|
|
+
|
|
|
+ $data['dept_category'] = '桌台';
|
|
|
+ $data['dept_super_id'] = $restaurant->dept_id;
|
|
|
+ $data['dept_city'] = $restaurant->dept_city;
|
|
|
+ $data['dept_telephone'] = $restaurant->dept_telephone;
|
|
|
+ $data['dept_position'] = $restaurant->dept_position;
|
|
|
+ $data['dept_address'] = $restaurant->dept_address;
|
|
|
+ $data['dept_if_dishes'] = $restaurant->dept_if_dishes;
|
|
|
+ $deptId = $this->doInsert($data);
|
|
|
+ if (!$deptId) {
|
|
|
+ throw new BusinessException('创建桌台失败');
|
|
|
+ }
|
|
|
+ // 更新部门path
|
|
|
+ $path = $restaurant-> dept_super_path . $deptId . '/';
|
|
|
+ if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
|
|
|
+ throw new BusinessException('创建桌台失败');
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ } catch (BusinessException $customException) {
|
|
|
+ Db::rollBack();
|
|
|
+ return json_fail($customException->getMessage());
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ dump($exception->getMessage());
|
|
|
+ Db::rollBack();
|
|
|
+ return json_fail('创建桌台失败');
|
|
|
+ }
|
|
|
+ return json_success('success');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function batchInsert(Request $request)
|
|
|
+ {
|
|
|
+ $params = $request->post();
|
|
|
+ if(intval($params['dept_code_nbr']) < 1){
|
|
|
+ return json_fail('数量不足,无法批量生成!');
|
|
|
+ }
|
|
|
+ if(intval($params['dept_code']) < 1 || intval($params['dept_code'])+intval($params['dept_code']) > 999){
|
|
|
+ return json_fail('请填写数字,且生成后不可大于999!');
|
|
|
+ }
|
|
|
+
|
|
|
+ $uid = JwtToken::getCurrentId();
|
|
|
+ $user = SysUser::where('user_id',$uid)->first();
|
|
|
+ $restaurant = SysDept::where('dept_super_id',$user->join_user_dept_id)->where('dept_category','餐厅')->first();
|
|
|
+ if(!$restaurant){
|
|
|
+ return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::beginTransaction();
|
|
|
+ try {
|
|
|
+ $data = $this->insertInput($request);
|
|
|
+ for($i=0;$i<$params['dept_code_nbr'];$i++){
|
|
|
+ $number = str_pad($i+intval($params['dept_code']),3,0,STR_PAD_LEFT);
|
|
|
+ $data['dept_category'] = '桌台';
|
|
|
+ $data['dept_name'] = $params['dept_name_prefix'].$number.'号桌';
|
|
|
+ $data['dept_code'] = $restaurant->dept_code.'-'.$number;
|
|
|
+ $data['dept_super_id'] = $restaurant->dept_id;
|
|
|
+ $data['dept_city'] = $restaurant->dept_city;
|
|
|
+ $data['dept_telephone'] = $restaurant->dept_telephone;
|
|
|
+ $data['dept_position'] = $restaurant->dept_position;
|
|
|
+ $data['dept_address'] = $restaurant->dept_address;
|
|
|
+ $data['dept_if_dishes'] = $restaurant->dept_if_dishes;
|
|
|
+ $deptId = $this->doInsert($data);
|
|
|
+ if (!$deptId) {
|
|
|
+ throw new BusinessException('批量创建桌台失败');
|
|
|
+ }
|
|
|
+ // 更新部门path
|
|
|
+ $path = $restaurant-> dept_super_path . $deptId . '/';
|
|
|
+ if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
|
|
|
+ throw new BusinessException('批量创建桌台失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ } catch (BusinessException $customException) {
|
|
|
+ Db::rollBack();
|
|
|
+ return json_fail($customException->getMessage());
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ dump($exception->getMessage());
|
|
|
+ Db::rollBack();
|
|
|
+ return json_fail('批量创建桌台失败');
|
|
|
+ }
|
|
|
+ return json_success('success');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function update(Request $request): Response
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ [$id, $data] = $this->updateInput($request);
|
|
|
+
|
|
|
+ $this->doUpdate($id, $data);
|
|
|
+ } catch (BusinessException $customException) {
|
|
|
+ return json_fail($customException->getMessage());
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return json_fail('数据更新失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_success('success');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function exportTable(Request $request)
|
|
|
+ {
|
|
|
+ $uid = JwtToken::getCurrentId();
|
|
|
+ $user = SysUser::where('user_id',$uid)->first();
|
|
|
+ $restaurant = SysDept::where('dept_super_id',$user->join_user_dept_id)->where('dept_category','餐厅')->first();
|
|
|
+ if(!$restaurant){
|
|
|
+ return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
|
|
|
+ }
|
|
|
+
|
|
|
+ $deptId = $request->get('dept_id',[]);
|
|
|
+ $deptName = $request->get('dept_name','');
|
|
|
+
|
|
|
+ $tables = SysDept::where('dept_category','桌台')
|
|
|
+ ->where('dept_super_id',$restaurant->dept_id)
|
|
|
+ ->when(!empty($deptId),function ($query) use ($deptId){
|
|
|
+ $query->whereIn('dept_id',$deptId);
|
|
|
+ })->when(!empty($deptName),function ($query) use ($deptName){
|
|
|
+ $query->where('dept_name','like',$deptName);
|
|
|
+ })->get()
|
|
|
+ ->toArray();
|
|
|
+ $data = [];
|
|
|
+ foreach ($tables as $table){
|
|
|
+ $data[] = [
|
|
|
+ 'dept_name'=> $table['dept_name'],
|
|
|
+ 'dept_code'=> $table['dept_code'],
|
|
|
+ 'qrcode_content'=>json_encode(['code'=>$table['dept_code']]),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_success('',$data);
|
|
|
+
|
|
|
+ }
|
|
|
+}
|