DishesController.php 8.0 KB

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