DishesController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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','DISHES']);
  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->where(function ($query) use ($value){
  120. $query->whereJsonContains($value[0] . '->' . $value[1], intval($value[2]))
  121. ->orWhereJsonContains($value[0] . '->' . $value[1], strval($value[2]));
  122. });
  123. // $model = $model->whereJsonContains($value[0] . '->' . $value[1], intval($value[2]));
  124. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  125. $model = $model->where($column, $value[0], $value[1]);
  126. } elseif ($value[0] == 'in' && !empty($value[1])) {
  127. $valArr = $value[1];
  128. if (is_string($value[1])) {
  129. $valArr = explode(",", trim($value[1]));
  130. }
  131. $model = $model->whereIn($column, $valArr);
  132. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  133. $valArr = $value[1];
  134. if (is_string($value[1])) {
  135. $valArr = explode(",", trim($value[1]));
  136. }
  137. $model = $model->whereNotIn($column, $valArr);
  138. } elseif ($value[0] == 'null') {
  139. $model = $model->whereNull($column);
  140. } elseif ($value[0] == 'not null') {
  141. $model = $model->whereNotNull($column);
  142. } elseif ($value[0] !== '' || $value[1] !== '') {
  143. $model = $model->whereBetween($column, $value);
  144. }
  145. } else {
  146. $model = $model->where($column, $value);
  147. }
  148. }
  149. if ($field) {
  150. $model = $model->orderBy($field, $order);
  151. }
  152. $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');
  153. return $model;
  154. }
  155. public function afterQuery($items)
  156. {
  157. foreach ($items as &$item) {
  158. $sheetDeng = '';
  159. $item['sheet'] = $item['sheets'][0] ?? [];
  160. if (!empty($item['sheet'])) {
  161. $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
  162. if (count($item['sheets']) > 1 && $goods && $goods->goods_classify == 'MEALS') {
  163. $sheetDeng = ' 等餐品';
  164. }
  165. $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
  166. $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
  167. $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
  168. }
  169. unset($item['sheets']);
  170. $item['order_return_amount'] = 0;
  171. if (isset($item['orders_return_id'])) {
  172. $item['return'] = [
  173. 'orders_return_id' => $item['orders_return_id'],
  174. 'join_return_order_id' => $item['join_return_order_id'],
  175. 'order_return_status' => $item['order_return_status'],
  176. 'order_return_apply_json' => $item['order_return_apply_json'],
  177. 'order_return_remark' => $item['order_return_remark']
  178. ];
  179. if (!empty($item['order_return_refund_json'])) {
  180. $refundJson = json_decode($item['order_return_refund_json'], true);
  181. if (isset($refundJson['amount'])) {
  182. $item['order_return_amount'] = $refundJson['amount'];
  183. }
  184. }
  185. }
  186. $item['payDetail'] = PayDetailService::getPayWay($item['order_groupby'], $item['order_id']);
  187. }
  188. return $items;
  189. }
  190. }