RestaurantController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\controller\Curd;
  4. use app\model\SysDept;
  5. use app\model\SysUser;
  6. use support\Db;
  7. use support\exception\BusinessException;
  8. use support\Request;
  9. use support\Response;
  10. use Tinywan\Jwt\JwtToken;
  11. class RestaurantController extends Curd{
  12. public function __construct()
  13. {
  14. $this->model = new SysDept();
  15. }
  16. public function select(Request $request):Response
  17. {
  18. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  19. $where['dept_category'] = '餐厅';
  20. $order = $request->get('order', 'desc');
  21. $field = $field ?? 'dept_sort';
  22. $query = $this->doSelect($where, $field, $order);
  23. return $this->doFormat($query, $format, $limit);
  24. }
  25. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  26. {
  27. $model = $this->model->with([
  28. 'premises' => function ($query) {
  29. $query->select('dept_id', 'dept_name');
  30. },
  31. ]);
  32. foreach ($where as $column => $value) {
  33. if (is_array($value)) {
  34. if ($value[0] === 'like' || $value[0] === 'not like') {
  35. $model = $model->where($column, $value[0], "%$value[1]%");
  36. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  37. $model = $model->where($column, $value[0], $value[1]);
  38. } elseif ($value[0] == 'in' && !empty($value[1])) {
  39. $valArr = $value[1];
  40. if (is_string($value[1])) {
  41. $valArr = explode(",", trim($value[1]));
  42. }
  43. $model = $model->whereIn($column, $valArr);
  44. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  45. $valArr = $value[1];
  46. if (is_string($value[1])) {
  47. $valArr = explode(",", trim($value[1]));
  48. }
  49. $model = $model->whereNotIn($column, $valArr);
  50. } elseif ($value[0] == 'null') {
  51. $model = $model->whereNull($column);
  52. } elseif ($value[0] == 'not null') {
  53. $model = $model->whereNotNull($column);
  54. } elseif ($value[0] !== '' || $value[1] !== '') {
  55. $model = $model->whereBetween($column, $value);
  56. }
  57. } else {
  58. $model = $model->where($column, $value);
  59. }
  60. }
  61. if ($field) {
  62. $model = $model->orderBy($field, $order);
  63. }
  64. return $model;
  65. }
  66. public function insert(Request $request): Response
  67. {
  68. if ($this->validate && !$this->validateClass->scene('add_premises')->check($request->post())) {
  69. return json_fail($this->validateClass->getError());
  70. }
  71. // 验证编号是否存在
  72. if (!empty($request->post('dept_code')) && SysDept::checkExist('餐厅', 'dept_code', $request->post('dept_code'))) {
  73. return json_fail('编号重复,请重新输入');
  74. }
  75. Db::beginTransaction();
  76. try {
  77. $data = $this->insertInput($request);
  78. $premises = SysDept::where('dept_id',$data['dept_super_id'])->where('dept_category','营业场所')->first();
  79. if (!$premises){
  80. Db::rollBack();
  81. return json_fail('营销场所不存在');
  82. }
  83. $data['dept_category'] = '餐厅';
  84. $deptId = $this->doInsert($data);
  85. if (!$deptId) {
  86. throw new BusinessException('创建餐厅失败');
  87. }
  88. // 更新部门path
  89. $path = $premises-> dept_super_path . $deptId . '/';
  90. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  91. throw new BusinessException('创建餐厅失败');
  92. }
  93. Db::commit();
  94. } catch (BusinessException $customException) {
  95. Db::rollBack();
  96. return json_fail($customException->getMessage());
  97. } catch (\Exception $exception) {
  98. dump($exception->getMessage());
  99. Db::rollBack();
  100. return json_fail('创建餐厅失败');
  101. }
  102. return json_success('success');
  103. }
  104. /**
  105. * 更新
  106. * @param Request $request
  107. * @return Response
  108. */
  109. public function update(Request $request): Response
  110. {
  111. if ($this->validate && !$this->validateClass->scene('update_premises')->check($request->post())) {
  112. return json_fail($this->validateClass->getError());
  113. }
  114. try {
  115. [$id, $data] = $this->updateInput($request);
  116. $premises = SysDept::where('dept_id',$data['dept_super_id'])->where('dept_category','营业场所')->first();
  117. if (!$premises){
  118. Db::rollBack();
  119. return json_fail('营销场所不存在');
  120. }
  121. // 更新部门path
  122. $data['dept_super_path'] = $premises-> dept_super_path . $id . '/';
  123. $this->doUpdate($id, $data);
  124. } catch (BusinessException $customException) {
  125. return json_fail($customException->getMessage());
  126. } catch (\Exception $e) {
  127. return json_fail('数据更新失败');
  128. }
  129. return json_success('success');
  130. }
  131. /**
  132. * 场所名获取餐厅打印机
  133. */
  134. public function getPrinterByPremiseName(Request $request)
  135. {
  136. $premiseName = $request->get('premise','');
  137. $printerList = [];
  138. try{
  139. $resraurant = SysDept::where('dept_name',$premiseName)->where('dept_category','餐厅')->first();
  140. if (!$resraurant){
  141. $premise = SysDept::where('dept_name',$premiseName)->where('dept_category','营业场所')->first();
  142. $resraurant = SysDept::where('dept_super_id',$premise->dept_id)->where('dept_category','餐厅')->first();
  143. }
  144. if (!empty($resraurant->dept_extend_json)){
  145. $deptExtendJson = json_decode($resraurant->dept_extend_json,true);
  146. if (isset($deptExtendJson['printer'])){
  147. $printerList = $deptExtendJson['printer'];
  148. }
  149. }
  150. }catch(\Exception $e){}
  151. return json_success('',$printerList);
  152. }
  153. /**
  154. * 获取用户所在餐厅
  155. */
  156. public function getByUser(Request $request)
  157. {
  158. $userId = $request->get('user_id',JwtToken::getCurrentId());
  159. $user = SysUser::where('user_id',$userId)->first();
  160. $restaurant = SysDept::where('dept_id',$user->join_user_dept_id)->where('dept_category','餐厅')->get()->toArray();
  161. if(empty($restaurant)){
  162. $premise = SysDept::where('dept_id',$user->join_user_dept_id)->where('dept_category','营业场所')->first();
  163. if (!empty($premise)){
  164. $restaurant = SysDept::where('dept_super_id',$premise->dept_id)->where('dept_category','餐厅')->get()->toArray();
  165. }
  166. }
  167. $data = [];
  168. foreach ($restaurant as $item){
  169. $data[] = [
  170. 'key'=>$item['dept_id'],
  171. 'label' => $item['dept_name']
  172. ];
  173. }
  174. return json_success('success',$data);
  175. }
  176. }