ServicesController.php 6.9 KB

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