RestaurantController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\controller\Curd;
  4. use app\model\SysDept;
  5. use support\Db;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use support\Response;
  9. class RestaurantController extends Curd{
  10. public function __construct()
  11. {
  12. $this->model = new SysDept();
  13. }
  14. public function select(Request $request):Response
  15. {
  16. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  17. $where['dept_category'] = '餐厅';
  18. $order = $request->get('order', 'desc');
  19. $field = $field ?? 'dept_sort';
  20. $query = $this->doSelect($where, $field, $order);
  21. return $this->doFormat($query, $format, $limit);
  22. }
  23. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  24. {
  25. $model = $this->model->with([
  26. 'premises' => function ($query) {
  27. $query->select('dept_id', 'dept_name');
  28. },
  29. ]);
  30. foreach ($where as $column => $value) {
  31. if (is_array($value)) {
  32. if ($value[0] === 'like' || $value[0] === 'not like') {
  33. $model = $model->where($column, $value[0], "%$value[1]%");
  34. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  35. $model = $model->where($column, $value[0], $value[1]);
  36. } elseif ($value[0] == 'in' && !empty($value[1])) {
  37. $valArr = $value[1];
  38. if (is_string($value[1])) {
  39. $valArr = explode(",", trim($value[1]));
  40. }
  41. $model = $model->whereIn($column, $valArr);
  42. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  43. $valArr = $value[1];
  44. if (is_string($value[1])) {
  45. $valArr = explode(",", trim($value[1]));
  46. }
  47. $model = $model->whereNotIn($column, $valArr);
  48. } elseif ($value[0] == 'null') {
  49. $model = $model->whereNull($column);
  50. } elseif ($value[0] == 'not null') {
  51. $model = $model->whereNotNull($column);
  52. } elseif ($value[0] !== '' || $value[1] !== '') {
  53. $model = $model->whereBetween($column, $value);
  54. }
  55. } else {
  56. $model = $model->where($column, $value);
  57. }
  58. }
  59. if ($field) {
  60. $model = $model->orderBy($field, $order);
  61. }
  62. return $model;
  63. }
  64. public function insert(Request $request): Response
  65. {
  66. if ($this->validate && !$this->validateClass->scene('add_premises')->check($request->post())) {
  67. return json_fail($this->validateClass->getError());
  68. }
  69. // 验证编号是否存在
  70. if (!empty($request->post('dept_code')) && SysDept::checkExist('餐厅', 'dept_code', $request->post('dept_code'))) {
  71. return json_fail('编号重复,请重新输入');
  72. }
  73. Db::beginTransaction();
  74. try {
  75. $data = $this->insertInput($request);
  76. $premises = SysDept::where('dept_id',$data['dept_super_id'])->where('dept_category','营业场所')->first();
  77. if (!$premises){
  78. Db::rollBack();
  79. return json_fail('营销场所不存在');
  80. }
  81. $data['dept_category'] = '餐厅';
  82. $deptId = $this->doInsert($data);
  83. if (!$deptId) {
  84. throw new BusinessException('创建餐厅失败');
  85. }
  86. // 更新部门path
  87. $path = $premises-> dept_super_path . $deptId . '/';
  88. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  89. throw new BusinessException('创建餐厅失败');
  90. }
  91. Db::commit();
  92. } catch (BusinessException $customException) {
  93. Db::rollBack();
  94. return json_fail($customException->getMessage());
  95. } catch (\Exception $exception) {
  96. dump($exception->getMessage());
  97. Db::rollBack();
  98. return json_fail('创建餐厅失败');
  99. }
  100. return json_success('success');
  101. }
  102. /**
  103. * 更新
  104. * @param Request $request
  105. * @return Response
  106. */
  107. public function update(Request $request): Response
  108. {
  109. if ($this->validate && !$this->validateClass->scene('update_premises')->check($request->post())) {
  110. return json_fail($this->validateClass->getError());
  111. }
  112. try {
  113. [$id, $data] = $this->updateInput($request);
  114. $premises = SysDept::where('dept_id',$data['dept_super_id'])->where('dept_category','营业场所')->first();
  115. if (!$premises){
  116. Db::rollBack();
  117. return json_fail('营销场所不存在');
  118. }
  119. // 更新部门path
  120. $data['dept_super_path'] = $premises-> dept_super_path . $id . '/';
  121. $this->doUpdate($id, $data);
  122. } catch (BusinessException $customException) {
  123. return json_fail($customException->getMessage());
  124. } catch (\Exception $e) {
  125. return json_fail('数据更新失败');
  126. }
  127. return json_success('success');
  128. }
  129. }