DishesController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\validate\order\OrderValidate;
  4. use app\controller\Curd;
  5. use app\model\Goods;
  6. use app\model\Order;
  7. use app\model\OrderSheet;
  8. use support\Request;
  9. use support\Response;
  10. class DishesController extends Curd
  11. {
  12. public function __construct()
  13. {
  14. $this->model = new Order();
  15. $this->validate = true;
  16. $this->validateClass = new OrderValidate();
  17. }
  18. /**
  19. * @Desc 列表
  20. * @Author Gorden
  21. * @Date 2024/3/28 15:01
  22. *
  23. * @param Request $request
  24. * @return Response
  25. * @throws \support\exception\BusinessException
  26. */
  27. public function select(Request $request): Response
  28. {
  29. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  30. $where['order_classify'] = 'DISHES';
  31. if (!empty($where['order_addtimes'])) {
  32. $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]);
  33. $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]);
  34. }
  35. $order = $request->get('order', 'desc');
  36. $orderId = $request->get('order_id', '');
  37. $field = $field ?? 'order_addtimes';
  38. if (!empty($where['order_status_system']) && in_array($where['order_status_system'], ['PENDING', 'WAITING', 'SENDING', 'RECVING', 'SIGNED', 'CONFIRM'])) {
  39. $where['order_is_complete'] = 'N';
  40. $where['order_category'] = ['<>', 'RETURN'];
  41. }
  42. $orderIds = [];
  43. if (!empty($orderId)) {
  44. $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')
  45. ->where('order_classify', 'DISHES')
  46. ->pluck('order_id')
  47. ->toArray();
  48. }
  49. $goodsName = $request->get('goods_name', '');
  50. if (!empty($goodsName)) {
  51. $goodsIds = Goods::where('goods_classify', 'MEALS')
  52. ->where('goods_name', 'like', '%' . $goodsName . '%')
  53. ->pluck('goods_id')
  54. ->toArray();
  55. $goodsOrderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
  56. if (!empty($where['order_id'])) {
  57. $orderIds = array_intersect($orderIds, $goodsOrderIds);
  58. }else{
  59. $orderIds = $goodsOrderIds;
  60. }
  61. }
  62. if (!empty($orderId) || !empty($goodsName)) {
  63. $where['order_id'] = ['in', implode(',', $orderIds)];
  64. }
  65. $query = $this->doSelect($where, $field, $order);
  66. return $this->doFormat($query, $format, $limit);
  67. }
  68. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  69. {
  70. $model = $this->model->with([
  71. 'sheets' => function ($query) {
  72. $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
  73. },
  74. 'member' => function ($query) {
  75. $query->select('member_id', 'member_mobile');
  76. },
  77. 'cert' => function ($query) {
  78. $query->select('join_cert_member_id', 'member_cert_name');
  79. },
  80. // 'return' => function ($query) use ($where){
  81. // if (isset($where['return'])){
  82. // dump($where['return']);
  83. // $query = $query->where('order_return_status',$where['return']);
  84. // }
  85. // $query->select('orders_return_id', 'join_return_order_id', 'order_return_status');
  86. // },
  87. 'express' => function ($query) {
  88. $query->select('join_express_order_id', 'order_express_type');
  89. }
  90. ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id');
  91. // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
  92. foreach ($where as $column => $value) {
  93. if (is_array($value)) {
  94. if ($value[0] === 'like' || $value[0] === 'not like') {
  95. $model = $model->where($column, $value[0], "%$value[1]%");
  96. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  97. $model = $model->where($column, $value[0], $value[1]);
  98. } elseif ($value[0] == 'in' && !empty($value[1])) {
  99. $valArr = $value[1];
  100. if (is_string($value[1])) {
  101. $valArr = explode(",", trim($value[1]));
  102. }
  103. $model = $model->whereIn($column, $valArr);
  104. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  105. $valArr = $value[1];
  106. if (is_string($value[1])) {
  107. $valArr = explode(",", trim($value[1]));
  108. }
  109. $model = $model->whereNotIn($column, $valArr);
  110. } elseif ($value[0] == 'null') {
  111. $model = $model->whereNull($column);
  112. } elseif ($value[0] == 'not null') {
  113. $model = $model->whereNotNull($column);
  114. } elseif ($value[0] !== '' || $value[1] !== '') {
  115. $model = $model->whereBetween($column, $value);
  116. }
  117. } else {
  118. $model = $model->where($column, $value);
  119. }
  120. }
  121. if ($field) {
  122. $model = $model->orderBy($field, $order);
  123. }
  124. $model = $model->select('order.*', 'order_return.orders_return_id', 'order_return.join_return_order_id', 'order_return.order_return_status', 'order_return.order_return_apply_json', 'order_return.order_return_remark');
  125. return $model;
  126. }
  127. public function afterQuery($items)
  128. {
  129. foreach ($items as &$item) {
  130. $sheetDeng = '';
  131. $item['sheet'] = $item['sheets'][0] ?? [];
  132. if (!empty($item['sheet'])) {
  133. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  134. if (count($item['sheets']) > 1 && $goods && $goods->goods_classify == 'MEALS') {
  135. $sheetDeng = ' 等餐品';
  136. }
  137. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
  138. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  139. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  140. }
  141. unset($item['sheets']);
  142. if (isset($item['orders_return_id'])) {
  143. $item['return'] = [
  144. 'orders_return_id' => $item['orders_return_id'],
  145. 'join_return_order_id' => $item['join_return_order_id'],
  146. 'order_return_status' => $item['order_return_status'],
  147. 'order_return_apply_json' => $item['order_return_apply_json'],
  148. 'order_return_remark' => $item['order_return_remark']
  149. ];
  150. }
  151. }
  152. return $items;
  153. }
  154. }