GoodsController.php 7.9 KB

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