DishesController.php 8.7 KB

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