RestaurantTableController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\common\Qrcode;
  4. use app\controller\Curd;
  5. use app\model\SysDept;
  6. use app\model\SysUser;
  7. use support\Db;
  8. use support\exception\BusinessException;
  9. use support\Request;
  10. use support\Response;
  11. use Tinywan\Jwt\JwtToken;
  12. class RestaurantTableController extends Curd
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new SysDept();
  17. }
  18. public function select(Request $request): Response
  19. {
  20. $uid = JwtToken::getCurrentId();
  21. $user = SysUser::where('user_id', $uid)->first();
  22. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  23. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  24. })->first();
  25. // $restaurant = SysDept::where('dept_category','餐厅')->where('dept_super_id',$user->join_user_dept_id)->first();
  26. if (!$restaurant) {
  27. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  28. }
  29. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  30. $where['dept_category'] = '桌台';
  31. $where['dept_super_id'] = $restaurant->dept_id;
  32. $order = $request->get('order', 'desc');
  33. $field = $field ?? 'dept_sort';
  34. $query = $this->doSelect($where, $field, $order);
  35. return $this->doFormat($query, $format, $limit);
  36. }
  37. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  38. {
  39. $model = $this->model->with([
  40. 'premises' => function ($query) {
  41. $query->select('dept_id', 'dept_name');
  42. },
  43. ]);
  44. foreach ($where as $column => $value) {
  45. if (is_array($value)) {
  46. if ($value[0] === 'like' || $value[0] === 'not like') {
  47. $model = $model->where($column, $value[0], "%$value[1]%");
  48. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  49. $model = $model->where($column, $value[0], $value[1]);
  50. } elseif ($value[0] == 'in' && !empty($value[1])) {
  51. $valArr = $value[1];
  52. if (is_string($value[1])) {
  53. $valArr = explode(",", trim($value[1]));
  54. }
  55. $model = $model->whereIn($column, $valArr);
  56. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  57. $valArr = $value[1];
  58. if (is_string($value[1])) {
  59. $valArr = explode(",", trim($value[1]));
  60. }
  61. $model = $model->whereNotIn($column, $valArr);
  62. } elseif ($value[0] == 'null') {
  63. $model = $model->whereNull($column);
  64. } elseif ($value[0] == 'not null') {
  65. $model = $model->whereNotNull($column);
  66. } elseif ($value[0] !== '' || $value[1] !== '') {
  67. $model = $model->whereBetween($column, $value);
  68. }
  69. } else {
  70. $model = $model->where($column, $value);
  71. }
  72. }
  73. if ($field) {
  74. $model = $model->orderBy($field, $order);
  75. }
  76. return $model;
  77. }
  78. /**
  79. * @Desc 桌台管理顶部统计
  80. * @Author Gorden
  81. * @Date 2024/11/1 13:15
  82. *
  83. * @param Request $request
  84. * @return Response
  85. */
  86. public function statistics(Request $request)
  87. {
  88. $uid = JwtToken::getCurrentId();
  89. $user = SysUser::where('user_id', $uid)->first();
  90. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  91. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  92. })->first();
  93. $restaurantId = !empty($restaurant) ? $restaurant->dept_id : '';
  94. $total = SysDept::where('dept_super_id', $restaurantId)->where('dept_category', '桌台')->count('dept_id');
  95. $activedTotal = SysDept::where('dept_super_id', $restaurantId)->where('dept_category', '桌台')->where('dept_status', 'ACTIVED')->count('dept_id');
  96. $pendingTotal = SysDept::where('dept_super_id', $restaurantId)->where('dept_category', '桌台')->where('dept_status', 'PENDING')->count('dept_id');
  97. $usingTotal = SysDept::where('dept_super_id', $restaurantId)->where('dept_category', '桌台')->where('dept_status', 'USING')->count('dept_id');
  98. $disabledTotal = SysDept::where('dept_super_id', $restaurantId)->where('dept_category', '桌台')->where('dept_status', 'DISABLED')->count('dept_id');
  99. return json_success('success', [
  100. 'total' => $total,
  101. 'activedTotal' => $activedTotal,
  102. 'pendingTotal' => $pendingTotal,
  103. 'usingTotal' => $usingTotal,
  104. 'disabledTotal' => $disabledTotal,
  105. ]);
  106. }
  107. public function insert(Request $request): Response
  108. {
  109. $uid = JwtToken::getCurrentId();
  110. $user = SysUser::where('user_id', $uid)->first();
  111. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  112. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  113. })->first();
  114. if (!$restaurant) {
  115. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  116. }
  117. Db::beginTransaction();
  118. try {
  119. $data = $this->insertInput($request);
  120. $data['dept_category'] = '桌台';
  121. $data['dept_super_id'] = $restaurant->dept_id;
  122. $data['dept_city'] = $restaurant->dept_city;
  123. $data['dept_telephone'] = $restaurant->dept_telephone;
  124. $data['dept_position'] = $restaurant->dept_position;
  125. $data['dept_address'] = $restaurant->dept_address;
  126. $data['dept_if_dishes'] = $restaurant->dept_if_dishes;
  127. $deptId = $this->doInsert($data);
  128. if (!$deptId) {
  129. throw new BusinessException('创建桌台失败');
  130. }
  131. // 更新部门path
  132. $path = $restaurant->dept_super_path . $deptId . '/';
  133. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  134. throw new BusinessException('创建桌台失败');
  135. }
  136. Db::commit();
  137. } catch (BusinessException $customException) {
  138. Db::rollBack();
  139. return json_fail($customException->getMessage());
  140. } catch (\Exception $exception) {
  141. dump($exception->getMessage());
  142. Db::rollBack();
  143. return json_fail('创建桌台失败');
  144. }
  145. return json_success('success');
  146. }
  147. public function batchInsert(Request $request)
  148. {
  149. $params = $request->post();
  150. if (intval($params['dept_code_nbr']) < 1) {
  151. return json_fail('数量不足,无法批量生成!');
  152. }
  153. if (intval($params['dept_code']) < 1 || intval($params['dept_code']) + intval($params['dept_code']) > 999) {
  154. return json_fail('请填写数字,且生成后不可大于999!');
  155. }
  156. $uid = JwtToken::getCurrentId();
  157. $user = SysUser::where('user_id', $uid)->first();
  158. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  159. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  160. })->first();
  161. if (!$restaurant) {
  162. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  163. }
  164. Db::beginTransaction();
  165. try {
  166. $data = $this->insertInput($request);
  167. for ($i = 0; $i < $params['dept_code_nbr']; $i++) {
  168. $number = $i + intval($params['dept_code']);
  169. $data['dept_category'] = '桌台';
  170. $data['dept_name'] = $number . '号桌';
  171. $data['dept_code'] = $restaurant->dept_code . '-' . $number;
  172. $data['dept_super_id'] = $restaurant->dept_id;
  173. $data['dept_city'] = $restaurant->dept_city;
  174. $data['dept_telephone'] = $restaurant->dept_telephone;
  175. $data['dept_position'] = $restaurant->dept_position;
  176. $data['dept_address'] = $restaurant->dept_address;
  177. $data['dept_if_dishes'] = $restaurant->dept_if_dishes;
  178. $data['dept_sort'] = $number;
  179. $deptId = $this->doInsert($data);
  180. if (!$deptId) {
  181. throw new BusinessException('批量创建桌台失败');
  182. }
  183. // 更新部门path
  184. $path = $restaurant->dept_super_path . $deptId . '/';
  185. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  186. throw new BusinessException('批量创建桌台失败');
  187. }
  188. }
  189. Db::commit();
  190. } catch (BusinessException $customException) {
  191. Db::rollBack();
  192. return json_fail($customException->getMessage());
  193. } catch (\Exception $exception) {
  194. dump($exception->getMessage());
  195. Db::rollBack();
  196. return json_fail('批量创建桌台失败');
  197. }
  198. return json_success('success');
  199. }
  200. /**
  201. * 更新
  202. * @param Request $request
  203. * @return Response
  204. */
  205. public function update(Request $request): Response
  206. {
  207. try {
  208. [$id, $data] = $this->updateInput($request);
  209. $this->doUpdate($id, $data);
  210. } catch (BusinessException $customException) {
  211. return json_fail($customException->getMessage());
  212. } catch (\Exception $e) {
  213. return json_fail('数据更新失败');
  214. }
  215. return json_success('success');
  216. }
  217. public function exportTable(Request $request)
  218. {
  219. $uid = JwtToken::getCurrentId();
  220. $user = SysUser::where('user_id', $uid)->first();
  221. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  222. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  223. })->first();
  224. if (!$restaurant) {
  225. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  226. }
  227. $premisesId = '';
  228. if ($restaurant->dept_category == '营业场所') {
  229. $premisesId = $restaurant->dept_id;
  230. } else {
  231. $dept = SysDept::where('dept_id', $restaurant->dept_super_id)->first();
  232. if ($dept && $dept->dept_category == '营业场所') {
  233. $premisesId = $dept->dept_id;
  234. }
  235. }
  236. if (empty($premisesId)) {
  237. return json_fail('没有查找到营业场所');
  238. }
  239. $deptId = $request->get('dept_id', []);
  240. $deptName = $request->get('dept_name', '');
  241. $tables = SysDept::where('dept_category', '桌台')
  242. ->where('dept_super_id', $restaurant->dept_id)
  243. ->when(!empty($deptId), function ($query) use ($deptId) {
  244. $query->whereIn('dept_id', $deptId);
  245. })->when(!empty($deptName), function ($query) use ($deptName) {
  246. $query->where('dept_name', 'like', $deptName);
  247. })->get()
  248. ->toArray();
  249. $data = [];
  250. foreach ($tables as $table) {
  251. $data[] = [
  252. 'dept_name' => $table['dept_name'],
  253. 'dept_code' => $table['dept_code'],
  254. 'qrcode_content' => json_encode([
  255. 'action' => 'scan-dishes',
  256. 'data' => [
  257. 'premises' => $premisesId,
  258. 'table' => $table['dept_name'],
  259. 'dept' => $table['dept_id']
  260. ]
  261. ], JSON_UNESCAPED_UNICODE),
  262. ];
  263. }
  264. return json_success('', $data);
  265. }
  266. public function downloadQrcode(Request $request)
  267. {
  268. $tableId = $request->get('table_id', '');
  269. $uid = JwtToken::getCurrentId();
  270. $user = SysUser::where('user_id', $uid)->first();
  271. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  272. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  273. })->first();
  274. if (!$restaurant) {
  275. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  276. }
  277. $fileName = 'table-' . md5($tableId);
  278. // 图片已存在,删除之前的
  279. if (file_exists(public_path() . "/storage/qrcode/" . $fileName . '.png')) {
  280. unlink(public_path() . "/storage/qrcode/" . $fileName . '.png');
  281. }
  282. $table = SysDept::where('dept_id', $tableId)->where('dept_category', '桌台')->first();
  283. $restaurant = SysDept::where('dept_id', $table->dept_super_id)->first();
  284. // $premises = SysDept::where('dept_id',$restaurant->dept_super_id)->first();
  285. // if (!$premises){
  286. // return json_fail('营业场所不存在');
  287. // }
  288. // $qrcodeContent = [
  289. // 'action'=>'scan-dishes',
  290. // 'data'=>[
  291. // 'premises'=>$table->dept_super_id,
  292. // 'table'=>$table->dept_name,
  293. // 'dept'=>$table->dept_id
  294. // ]
  295. // ];
  296. // $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc6274da7198e3eb4&redirect_uri=https%3A%2F%2Fh5.wanyuewellness.com.cn%2F%23%2Fpages%2Findex%2FWeChatGuide?scan="
  297. // . urlencode(json_encode($qrcodeContent)).
  298. // "%7B%22action%22%3A%22scan-dishes%22%2C%22data%22%3A%7B%22premises%22%3A15%2C%22table%22%3A%221%E5%8F%B7%E5%8F%B0%22%2C%22tableid%22%3A16%7D%7D
  299. // "&response_type=code&scope=snsapi_base&connect_redirect=1&state=#wechat_redirect";
  300. $url = getenv('TABLE_QRCODE_DOMAIN') . "/qr?tid=" . $table->dept_id;
  301. $qrcode = new Qrcode();
  302. $qrcode->setLabel($restaurant->dept_name . "(" . $table->dept_name . ")");
  303. $qrcode = $qrcode->create($url, $fileName);
  304. if ($qrcode !== false) {
  305. return json_success('success', ['url' => getenv('STORAGE_DOMAIN') . '/storage/qrcode/' . $fileName . '.png']);
  306. }
  307. }
  308. public function batchDownloadQrcode(Request $request)
  309. {
  310. $uid = JwtToken::getCurrentId();
  311. $user = SysUser::where('user_id', $uid)->first();
  312. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  313. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  314. })->first();
  315. $tables = SysDept::where('dept_super_id', $restaurant->dept_id)
  316. ->where('dept_category', '桌台')
  317. ->where('dept_status', '<>', 'DISABLED')
  318. ->get()
  319. ->toArray();
  320. $zip = new \ZipArchive();
  321. $filename = public_path() . '/storage/qrcode/zip/' . $restaurant->dept_name . '-桌台.zip';
  322. if (file_exists($filename)) {
  323. unlink($filename);
  324. }
  325. if ($zip->open($filename, \ZipArchive::CREATE) !== TRUE) {
  326. return json_fail("创建压缩文件失败");
  327. }
  328. foreach ($tables as $table) {
  329. $fileName = $restaurant->dept_name . '-' . $table['dept_name'];
  330. $url = getenv('TABLE_QRCODE_DOMAIN') . "/qr?tid=" . $table['dept_id'];
  331. $qrcode = new Qrcode();
  332. $qrcode->setLabel($restaurant->dept_name . "(" . $table['dept_name'] . ")");
  333. $qrcode = $qrcode->create($url, $fileName);
  334. if ($qrcode !== false) {
  335. $zip->addFile(public_path() . '/storage/qrcode/' . $fileName . '.png', $fileName . '.png');
  336. // return json_success('success', ['url' => getenv('STORAGE_DOMAIN') . '/storage/qrcode/' . $fileName . '.png']);
  337. }
  338. }
  339. $zip->close();
  340. return json_success('success', ['url' => getenv('STORAGE_DOMAIN') . '/storage/qrcode/zip/' . $restaurant->dept_name . '-桌台.zip']);
  341. }
  342. /**
  343. * 桌台列表
  344. */
  345. public function selectListByPremiseName(Request $request)
  346. {
  347. $premiseName = $request->get('premise', '');
  348. $tableList = [];
  349. try {
  350. $restaurant = SysDept::where('dept_name', $premiseName)->where('dept_category', '餐厅')->where('dept_status', 'ACTIVED')->first();
  351. if (!$restaurant) {
  352. $premise = SysDept::where('dept_name', $premiseName)->where('dept_category', '营业场所')->where('dept_status', 'ACTIVED')->first();
  353. $restaurant = SysDept::where('dept_super_id', $premise->dept_id)->where('dept_category', '餐厅')->where('dept_status', 'ACTIVED')->first();
  354. }
  355. $tables = SysDept::where('dept_super_id', $restaurant->dept_id)->where('dept_category', '桌台')->orderBy('dept_sort', 'ASC')->get()->toArray();
  356. foreach ($tables as $table) {
  357. $tableList[] = [
  358. 'key' => $table['dept_id'],
  359. 'label' => $table['dept_name'],
  360. 'disabled' => $table['dept_status'] == 'ACTIVED' ? false : true,
  361. ];
  362. }
  363. return json_success('', $tableList);
  364. } catch (\Exception $e) {
  365. return json_success('', $tableList);
  366. }
  367. }
  368. }