GoodsController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 GoodsController 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'] = 'GOODS';
  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. $field = $field ?? 'order_addtimes';
  37. if (!empty($where['order_status_system']) && in_array($where['order_status_system'], ['PENDING', 'WAITING', 'SENDING', 'RECVING', 'SIGNED', 'CONFIRM'])) {
  38. if ($where['order_status_system'] == 'SENDING') {
  39. $where['order_express.order_express_type'] = '配送';
  40. }
  41. $where['order_is_complete'] = 'N';
  42. }
  43. $orderId = $request->get('order_id', '');
  44. $orderIds = [];
  45. if (!empty($orderId)) {
  46. $orderIds = Order::where('order_id', 'like', '%' . $orderId . '%')
  47. ->where('order_classify', 'GOODS')
  48. ->pluck('order_id')
  49. ->toArray();
  50. }
  51. $goodsName = $request->get('goods_name', '');
  52. if (!empty($goodsName)) {
  53. $goodsIds = Goods::where('goods_classify', 'GOODS')
  54. ->where('goods_name', 'like', '%' . $goodsName . '%')
  55. ->pluck('goods_id')
  56. ->toArray();
  57. $goodsOrderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
  58. if (!empty($where['order_id'])) {
  59. $orderIds = array_intersect($orderIds, $goodsOrderIds);
  60. }else{
  61. $orderIds = $goodsOrderIds;
  62. }
  63. }
  64. if (!empty($orderId) || !empty($goodsName)) {
  65. $where['order_id'] = ['in', implode(',', $orderIds)];
  66. }
  67. // 自提订单
  68. if (!empty($where['order_status_system']) && $where['order_status_system'] == 'PICKUP') {
  69. $where['order_is_complete'] = 'N';
  70. $where['order_status_system'] = 'SENDING';
  71. $where['order_express.order_express_type'] = '自提';
  72. }
  73. $query = $this->doSelect($where, $field, $order);
  74. return $this->doFormat($query, $format, $limit);
  75. }
  76. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  77. {
  78. $model = $this->model->with([
  79. 'sheets' => function ($query) {
  80. $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
  81. },
  82. 'member' => function ($query) {
  83. $query->select('member_id', 'member_mobile');
  84. },
  85. 'cert' => function ($query) {
  86. $query->select('join_cert_member_id', 'member_cert_name');
  87. },
  88. // 'return' => function ($query) use ($where){
  89. // if (isset($where['return'])){
  90. // dump($where['return']);
  91. // $query = $query->where('order_return_status',$where['return']);
  92. // }
  93. // $query->select('orders_return_id', 'join_return_order_id', 'order_return_status');
  94. // },
  95. // 'express' => function ($query) {
  96. // $query->select('join_express_order_id', 'order_express_type');
  97. // }
  98. ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id')
  99. ->leftJoin('order_express', 'order_express.join_express_order_id', '=', 'order.order_id');
  100. // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
  101. foreach ($where as $column => $value) {
  102. if (is_array($value)) {
  103. if ($value[0] === 'like' || $value[0] === 'not like') {
  104. $model = $model->where($column, $value[0], "%$value[1]%");
  105. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  106. $model = $model->where($column, $value[0], $value[1]);
  107. } elseif ($value[0] == 'in' && !empty($value[1])) {
  108. $valArr = $value[1];
  109. if (is_string($value[1])) {
  110. $valArr = explode(",", trim($value[1]));
  111. }
  112. $model = $model->whereIn($column, $valArr);
  113. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  114. $valArr = $value[1];
  115. if (is_string($value[1])) {
  116. $valArr = explode(",", trim($value[1]));
  117. }
  118. $model = $model->whereNotIn($column, $valArr);
  119. } elseif ($value[0] == 'null') {
  120. $model = $model->whereNull($column);
  121. } elseif ($value[0] == 'not null') {
  122. $model = $model->whereNotNull($column);
  123. } elseif ($value[0] !== '' || $value[1] !== '') {
  124. $model = $model->whereBetween($column, $value);
  125. }
  126. } else {
  127. $model = $model->where($column, $value);
  128. }
  129. }
  130. if ($field) {
  131. $model = $model->orderBy($field, $order);
  132. }
  133. $model = $model->select('order.*', 'order_express.join_express_order_id', 'order_express.order_express_type', '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');
  134. return $model;
  135. }
  136. public function afterQuery($items)
  137. {
  138. foreach ($items as &$item) {
  139. $sheetDeng = '';
  140. $item['sheet'] = $item['sheets'][0] ?? [];
  141. if (!empty($item['sheet'])) {
  142. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  143. if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') {
  144. $sheetDeng = ' 等餐品';
  145. }
  146. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
  147. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  148. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  149. }
  150. unset($item['sheets']);
  151. if (isset($item['orders_return_id'])) {
  152. $item['return'] = [
  153. 'orders_return_id' => $item['orders_return_id'],
  154. 'join_return_order_id' => $item['join_return_order_id'],
  155. 'order_return_status' => $item['order_return_status'],
  156. 'order_return_apply_json' => $item['order_return_apply_json'],
  157. 'order_return_remark' => $item['order_return_remark']
  158. ];
  159. }
  160. if (isset($item['join_express_order_id'])) {
  161. $item['express'] = [
  162. 'join_express_order_id' => $item['join_express_order_id'],
  163. 'order_express_type' => $item['order_express_type']
  164. ];
  165. unset($item['join_express_order_id'], $item['order_express_type']);
  166. }
  167. }
  168. return $items;
  169. }
  170. }