GoodsController.php 8.4 KB

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