RestaurantController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 afterQuery($items)
  67. {
  68. foreach ($items as &$item) {
  69. if (!empty($item->dept_extend_json)) {
  70. $extendJson = json_decode($item->dept_extend_json, true);
  71. $item->dept_week = $extendJson['week'] ?? [];
  72. $item->dept_work = [];
  73. $deptWork = [];
  74. if (!empty($extendJson['times'])) {
  75. $times = explode('~', $extendJson['times']);
  76. $deptWork[] = date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $times[0]));
  77. $deptWork[] = date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $times[1]));
  78. }
  79. $item->dept_work = $deptWork;
  80. }
  81. }
  82. return $items;
  83. }
  84. public function insert(Request $request): Response
  85. {
  86. if ($this->validate && !$this->validateClass->scene('add_premises')->check($request->post())) {
  87. return json_fail($this->validateClass->getError());
  88. }
  89. // 验证编号是否存在
  90. if (!empty($request->post('dept_code')) && SysDept::checkExist('餐厅', 'dept_code', $request->post('dept_code'))) {
  91. return json_fail('编号重复,请重新输入');
  92. }
  93. Db::beginTransaction();
  94. try {
  95. $data = $this->insertInput($request);
  96. $premises = SysDept::where('dept_id',$data['dept_super_id'])->where('dept_category','营业场所')->first();
  97. if (!$premises){
  98. Db::rollBack();
  99. return json_fail('营销场所不存在');
  100. }
  101. $data['dept_category'] = '餐厅';
  102. $deptId = $this->doInsert($data);
  103. if (!$deptId) {
  104. throw new BusinessException('创建餐厅失败');
  105. }
  106. // 更新部门path
  107. $path = $premises-> dept_super_path . $deptId . '/';
  108. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  109. throw new BusinessException('创建餐厅失败');
  110. }
  111. Db::commit();
  112. } catch (BusinessException $customException) {
  113. Db::rollBack();
  114. return json_fail($customException->getMessage());
  115. } catch (\Exception $exception) {
  116. dump($exception->getMessage());
  117. Db::rollBack();
  118. return json_fail('创建餐厅失败');
  119. }
  120. return json_success('success');
  121. }
  122. /**
  123. * 更新
  124. * @param Request $request
  125. * @return Response
  126. */
  127. public function update(Request $request): Response
  128. {
  129. if ($this->validate && !$this->validateClass->scene('update_premises')->check($request->post())) {
  130. return json_fail($this->validateClass->getError());
  131. }
  132. try {
  133. [$id, $data] = $this->updateInput($request);
  134. $premises = SysDept::where('dept_id',$data['dept_super_id'])->where('dept_category','营业场所')->first();
  135. if (!$premises){
  136. Db::rollBack();
  137. return json_fail('营销场所不存在');
  138. }
  139. // 更新部门path
  140. $data['dept_super_path'] = $premises-> dept_super_path . $id . '/';
  141. $this->doUpdate($id, $data);
  142. } catch (BusinessException $customException) {
  143. return json_fail($customException->getMessage());
  144. } catch (\Exception $e) {
  145. return json_fail('数据更新失败');
  146. }
  147. return json_success('success');
  148. }
  149. /**
  150. * 场所名获取餐厅打印机
  151. */
  152. public function getPrinterByPremiseName(Request $request)
  153. {
  154. $premiseName = $request->get('premise','');
  155. $printerList = [];
  156. try{
  157. $resraurant = SysDept::where('dept_name',$premiseName)->where('dept_category','餐厅')->first();
  158. if (!$resraurant){
  159. $premise = SysDept::where('dept_name',$premiseName)->where('dept_category','营业场所')->first();
  160. $resraurant = SysDept::where('dept_super_id',$premise->dept_id)->where('dept_category','餐厅')->first();
  161. }
  162. if (!empty($resraurant->dept_extend_json)){
  163. $deptExtendJson = json_decode($resraurant->dept_extend_json,true);
  164. if (isset($deptExtendJson['printer'])){
  165. $printerList = $deptExtendJson['printer'];
  166. }
  167. }
  168. }catch(\Exception $e){}
  169. return json_success('',$printerList);
  170. }
  171. /**
  172. * 获取用户所在餐厅
  173. */
  174. public function getByUser(Request $request)
  175. {
  176. $userId = $request->get('user_id',JwtToken::getCurrentId());
  177. $user = SysUser::where('user_id',$userId)->first();
  178. $restaurant = SysDept::where('dept_id',$user->join_user_dept_id)->where('dept_category','餐厅')->get()->toArray();
  179. if(empty($restaurant)){
  180. $premise = SysDept::where('dept_id',$user->join_user_dept_id)->where('dept_category','营业场所')->first();
  181. if (!empty($premise)){
  182. $restaurant = SysDept::where('dept_super_id',$premise->dept_id)->where('dept_category','餐厅')->get()->toArray();
  183. }
  184. }
  185. $data = [];
  186. foreach ($restaurant as $item){
  187. $data[] = [
  188. 'key'=>$item['dept_id'],
  189. 'label' => $item['dept_name']
  190. ];
  191. }
  192. return json_success('success',$data);
  193. }
  194. }