RestaurantTableController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. public function insert(Request $request): Response
  79. {
  80. $uid = JwtToken::getCurrentId();
  81. $user = SysUser::where('user_id', $uid)->first();
  82. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  83. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  84. })->first();
  85. if (!$restaurant) {
  86. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  87. }
  88. Db::beginTransaction();
  89. try {
  90. $data = $this->insertInput($request);
  91. $data['dept_category'] = '桌台';
  92. $data['dept_super_id'] = $restaurant->dept_id;
  93. $data['dept_city'] = $restaurant->dept_city;
  94. $data['dept_telephone'] = $restaurant->dept_telephone;
  95. $data['dept_position'] = $restaurant->dept_position;
  96. $data['dept_address'] = $restaurant->dept_address;
  97. $data['dept_if_dishes'] = $restaurant->dept_if_dishes;
  98. $deptId = $this->doInsert($data);
  99. if (!$deptId) {
  100. throw new BusinessException('创建桌台失败');
  101. }
  102. // 更新部门path
  103. $path = $restaurant->dept_super_path . $deptId . '/';
  104. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  105. throw new BusinessException('创建桌台失败');
  106. }
  107. Db::commit();
  108. } catch (BusinessException $customException) {
  109. Db::rollBack();
  110. return json_fail($customException->getMessage());
  111. } catch (\Exception $exception) {
  112. dump($exception->getMessage());
  113. Db::rollBack();
  114. return json_fail('创建桌台失败');
  115. }
  116. return json_success('success');
  117. }
  118. public function batchInsert(Request $request)
  119. {
  120. $params = $request->post();
  121. if (intval($params['dept_code_nbr']) < 1) {
  122. return json_fail('数量不足,无法批量生成!');
  123. }
  124. if (intval($params['dept_code']) < 1 || intval($params['dept_code']) + intval($params['dept_code']) > 999) {
  125. return json_fail('请填写数字,且生成后不可大于999!');
  126. }
  127. $uid = JwtToken::getCurrentId();
  128. $user = SysUser::where('user_id', $uid)->first();
  129. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  130. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  131. })->first();
  132. if (!$restaurant) {
  133. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  134. }
  135. Db::beginTransaction();
  136. try {
  137. $data = $this->insertInput($request);
  138. for ($i = 0; $i < $params['dept_code_nbr']; $i++) {
  139. $number = $i + intval($params['dept_code']);
  140. $data['dept_category'] = '桌台';
  141. $data['dept_name'] = $number . '号桌';
  142. $data['dept_code'] = $restaurant->dept_code . '-' . $number;
  143. $data['dept_super_id'] = $restaurant->dept_id;
  144. $data['dept_city'] = $restaurant->dept_city;
  145. $data['dept_telephone'] = $restaurant->dept_telephone;
  146. $data['dept_position'] = $restaurant->dept_position;
  147. $data['dept_address'] = $restaurant->dept_address;
  148. $data['dept_if_dishes'] = $restaurant->dept_if_dishes;
  149. $data['dept_sort'] = $number;
  150. $deptId = $this->doInsert($data);
  151. if (!$deptId) {
  152. throw new BusinessException('批量创建桌台失败');
  153. }
  154. // 更新部门path
  155. $path = $restaurant->dept_super_path . $deptId . '/';
  156. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  157. throw new BusinessException('批量创建桌台失败');
  158. }
  159. }
  160. Db::commit();
  161. } catch (BusinessException $customException) {
  162. Db::rollBack();
  163. return json_fail($customException->getMessage());
  164. } catch (\Exception $exception) {
  165. dump($exception->getMessage());
  166. Db::rollBack();
  167. return json_fail('批量创建桌台失败');
  168. }
  169. return json_success('success');
  170. }
  171. /**
  172. * 更新
  173. * @param Request $request
  174. * @return Response
  175. */
  176. public function update(Request $request): Response
  177. {
  178. try {
  179. [$id, $data] = $this->updateInput($request);
  180. $this->doUpdate($id, $data);
  181. } catch (BusinessException $customException) {
  182. return json_fail($customException->getMessage());
  183. } catch (\Exception $e) {
  184. return json_fail('数据更新失败');
  185. }
  186. return json_success('success');
  187. }
  188. public function exportTable(Request $request)
  189. {
  190. $uid = JwtToken::getCurrentId();
  191. $user = SysUser::where('user_id', $uid)->first();
  192. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  193. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  194. })->first();
  195. if (!$restaurant) {
  196. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  197. }
  198. $premisesId = '';
  199. if ($restaurant->dept_category == '营业场所') {
  200. $premisesId = $restaurant->dept_id;
  201. } else {
  202. $dept = SysDept::where('dept_id', $restaurant->dept_super_id)->first();
  203. if ($dept && $dept->dept_category == '营业场所') {
  204. $premisesId = $dept->dept_id;
  205. }
  206. }
  207. if (empty($premisesId)) {
  208. return json_fail('没有查找到营业场所');
  209. }
  210. $deptId = $request->get('dept_id', []);
  211. $deptName = $request->get('dept_name', '');
  212. $tables = SysDept::where('dept_category', '桌台')
  213. ->where('dept_super_id', $restaurant->dept_id)
  214. ->when(!empty($deptId), function ($query) use ($deptId) {
  215. $query->whereIn('dept_id', $deptId);
  216. })->when(!empty($deptName), function ($query) use ($deptName) {
  217. $query->where('dept_name', 'like', $deptName);
  218. })->get()
  219. ->toArray();
  220. $data = [];
  221. foreach ($tables as $table) {
  222. $data[] = [
  223. 'dept_name' => $table['dept_name'],
  224. 'dept_code' => $table['dept_code'],
  225. 'qrcode_content' => json_encode([
  226. 'action' => 'scan-dishes',
  227. 'data' => [
  228. 'premises' => $premisesId,
  229. 'table' => $table['dept_name'],
  230. 'dept' => $table['dept_id']
  231. ]
  232. ], JSON_UNESCAPED_UNICODE),
  233. ];
  234. }
  235. return json_success('', $data);
  236. }
  237. public function downloadQrcode(Request $request)
  238. {
  239. $tableId = $request->get('table_id', '');
  240. $uid = JwtToken::getCurrentId();
  241. $user = SysUser::where('user_id', $uid)->first();
  242. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  243. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  244. })->first();
  245. if (!$restaurant) {
  246. return json_fail('您所在的营业场所没有餐厅,请联系管理员添加!');
  247. }
  248. $fileName = 'table-' . md5($tableId);
  249. // 图片已存在,删除之前的
  250. if (file_exists(public_path() . "/storage/qrcode/" . $fileName . '.png')) {
  251. unlink(public_path() . "/storage/qrcode/" . $fileName . '.png');
  252. }
  253. $table = SysDept::where('dept_id', $tableId)->where('dept_category', '桌台')->first();
  254. $restaurant = SysDept::where('dept_id', $table->dept_super_id)->first();
  255. // $premises = SysDept::where('dept_id',$restaurant->dept_super_id)->first();
  256. // if (!$premises){
  257. // return json_fail('营业场所不存在');
  258. // }
  259. // $qrcodeContent = [
  260. // 'action'=>'scan-dishes',
  261. // 'data'=>[
  262. // 'premises'=>$table->dept_super_id,
  263. // 'table'=>$table->dept_name,
  264. // 'dept'=>$table->dept_id
  265. // ]
  266. // ];
  267. // $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="
  268. // . urlencode(json_encode($qrcodeContent)).
  269. // "%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
  270. // "&response_type=code&scope=snsapi_base&connect_redirect=1&state=#wechat_redirect";
  271. $url = getenv('TABLE_QRCODE_DOMAIN') . "/qr?tid=" . $table->dept_id;
  272. $qrcode = new Qrcode();
  273. $qrcode->setLabel($restaurant->dept_name . "(" . $table->dept_name . ")");
  274. $qrcode = $qrcode->create($url, $fileName);
  275. if ($qrcode !== false) {
  276. return json_success('success', ['url' => getenv('STORAGE_DOMAIN') . '/storage/qrcode/' . $fileName . '.png']);
  277. }
  278. }
  279. public function batchDownloadQrcode(Request $request)
  280. {
  281. $uid = JwtToken::getCurrentId();
  282. $user = SysUser::where('user_id', $uid)->first();
  283. $restaurant = SysDept::where('dept_category', '餐厅')->where(function ($query) use ($user) {
  284. $query->where('dept_id', $user->join_user_dept_id)->orWhere('dept_super_id', $user->join_user_dept_id);
  285. })->first();
  286. $tables = SysDept::where('dept_super_id',$restaurant->dept_id)
  287. ->where('dept_category','桌台')
  288. ->where('dept_status','<>','DISABLED')
  289. ->get()
  290. ->toArray();
  291. $zip = new \ZipArchive();
  292. $filename = public_path().'/storage/qrcode/zip/'.$restaurant->dept_name.'-桌台.zip';
  293. if (file_exists($filename)){
  294. unlink($filename);
  295. }
  296. if ($zip->open($filename, \ZipArchive::CREATE) !== TRUE) {
  297. return json_fail("创建压缩文件失败");
  298. }
  299. foreach ($tables as $table){
  300. $fileName = $restaurant->dept_name.'-'.$table['dept_name'];
  301. $url = getenv('TABLE_QRCODE_DOMAIN') . "/qr?tid=" . $table['dept_id'];
  302. $qrcode = new Qrcode();
  303. $qrcode->setLabel($restaurant->dept_name . "(" . $table['dept_name'] . ")");
  304. $qrcode = $qrcode->create($url, $fileName);
  305. if ($qrcode !== false) {
  306. $zip->addFile(public_path().'/storage/qrcode/' . $fileName . '.png',$fileName . '.png');
  307. // return json_success('success', ['url' => getenv('STORAGE_DOMAIN') . '/storage/qrcode/' . $fileName . '.png']);
  308. }
  309. }
  310. $zip->close();
  311. return json_success('success', ['url' => getenv('STORAGE_DOMAIN') . '/storage/qrcode/zip/' . $restaurant->dept_name.'-桌台.zip']);
  312. }
  313. /**
  314. * 桌台列表
  315. */
  316. public function selectListByPremiseName(Request $request)
  317. {
  318. $premiseName = $request->get('premise', '');
  319. $tableList = [];
  320. try {
  321. $restaurant = SysDept::where('dept_name', $premiseName)->where('dept_category', '餐厅')->where('dept_status', 'ACTIVED')->first();
  322. if (!$restaurant) {
  323. $premise = SysDept::where('dept_name', $premiseName)->where('dept_category', '营业场所')->where('dept_status', 'ACTIVED')->first();
  324. $restaurant = SysDept::where('dept_super_id', $premise->dept_id)->where('dept_category', '餐厅')->where('dept_status', 'ACTIVED')->first();
  325. }
  326. $tables = SysDept::where('dept_super_id', $restaurant->dept_id)->where('dept_category', '桌台')->orderBy('dept_status','ASC')->get()->toArray();
  327. foreach ($tables as $table) {
  328. $tableList[] = [
  329. 'key' => $table['dept_id'],
  330. 'label' => $table['dept_name'],
  331. 'disabled' => $table['dept_status'] == 'ACTIVED' ? false : true,
  332. ];
  333. }
  334. return json_success('', $tableList);
  335. } catch (\Exception $e) {
  336. return json_success('', $tableList);
  337. }
  338. }
  339. }