OrderController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace Yunshop\FightGroups\admin\controllers;
  3. use app\common\components\BaseController;
  4. use app\common\exceptions\AppException;
  5. use Yunshop\FightGroups\common\models\Order;
  6. use app\frontend\modules\order\services\OrderService;
  7. class OrderController extends BaseController
  8. {
  9. const PAGE_SIZE = 10;
  10. public function index()
  11. {
  12. $list['allStatus'] = (new Order())->allStatus;
  13. return view('Yunshop\FightGroups::admin.order.index', ['data'=>json_encode($list)]);
  14. }
  15. // 列表字段
  16. public function searchListFields()
  17. {
  18. return Order::uniacid()->select(['yz_order.*','yz_fight_groups_team_member.group_id',
  19. 'yz_fight_groups_team_member.level_id','yz_fight_groups_team_member.team_id',
  20. 'yz_fight_groups.title as group_title','mc_members.nickname as leader_name','mc_members.avatar','yz_fight_groups_level.member_num',])
  21. ->with(['hasOnePayType', 'belongsToMember', 'hasManyOrderGoods' => function ($query) {
  22. $query->select('total', 'price', 'goods_price', 'title', 'thumb', 'order_id', 'goods_option_title');
  23. }]);
  24. }
  25. // 价格统计
  26. public function searchTotalPrice()
  27. {
  28. return Order::uniacid();
  29. }
  30. public function searchWhere($search,$fight_groups_id,$fight_groups_title,$team_status,$leader_name,$order_sn,$status,$create_time)
  31. {
  32. $search = $search->leftJoin('yz_fight_groups_team_member','yz_order.id','=','yz_fight_groups_team_member.order_id')
  33. ->leftJoin('yz_fight_groups_level','yz_fight_groups_team_member.level_id','=','yz_fight_groups_level.id')
  34. ->leftJoin('yz_fight_groups','yz_fight_groups_team_member.group_id','=','yz_fight_groups.id')
  35. ->leftJoin('yz_fight_groups_team','yz_fight_groups_team_member.team_id','=','yz_fight_groups_team.id')
  36. ->leftJoin('mc_members','yz_fight_groups_team.leader_id','=','mc_members.uid')
  37. ->isPlugin()
  38. //->where('yz_order.is_member_deleted',0)
  39. ->where(app('OrderManager')->make('Order')->getTable() . '.is_member_deleted', 0)
  40. ->pluginId();
  41. if (!empty($fight_groups_id)) {
  42. $search = $search->where('yz_fight_groups_team_member.group_id', $fight_groups_id);
  43. }
  44. if (!empty($fight_groups_title)) {
  45. $search = $search->where('yz_fight_groups.title', 'like', $fight_groups_title.'%');
  46. }
  47. if (!empty($team_status) || $team_status === 0 || $team_status === "0") {
  48. $search = $search->where('yz_fight_groups_team.status', $team_status );
  49. }
  50. if (!empty($leader_name)) {
  51. $search = $search->where('mc_members.nickname', 'like', $leader_name.'%' );
  52. }
  53. if (!empty($order_sn)) {
  54. $search = $search->where('yz_order.order_sn', $order_sn );
  55. }
  56. if (!empty($status) || $status === 0 || $status === "0") {
  57. $search = $search->where('yz_order.status', $status );
  58. }
  59. if (!empty($create_time)) {
  60. $search = $search->whereBetween('yz_order.create_time', $create_time);
  61. }
  62. return $search;
  63. }
  64. // fight_groups_id,fight_groups_title,leader_name,order_sn,status,create_time
  65. public function search()
  66. {
  67. $page = (int)request()->page ? (int)request()->page : 1;//分页
  68. $fight_groups_id = request()->fight_groups_id;// 活动编号
  69. $fight_groups_title = request()->fight_groups_title;// 活动名称
  70. $team_status = request()->team_status;// 团队状态
  71. $leader_name = request()->leader_name;// 团长名称
  72. $order_sn = request()->order_sn;// 订单号
  73. $status = request()->status;// 订单状态
  74. $create_time = request()->create_time;// 订单开始和结束时间
  75. // 获取订单列表
  76. $queryList = $this->searchListFields();
  77. $queryList = $this->searchWhere($queryList,$fight_groups_id,$fight_groups_title,$team_status,$leader_name,$order_sn,$status,$create_time);
  78. $list = $queryList->orderBy('yz_order.created_at', 'desc')->paginate(static::PAGE_SIZE,['*'], 'page', $page)->toArray();
  79. // 统计订单金额
  80. $queryTotalPrice = $this->searchTotalPrice();
  81. $queryTotalPrice = $this->searchWhere($queryTotalPrice,$fight_groups_id,$fight_groups_title,$team_status,$leader_name,$order_sn,$status,$create_time);
  82. $totalPrice = $queryTotalPrice->sum('yz_order.price');
  83. $list['sum_price'] = !empty($totalPrice) ? $totalPrice : 0;
  84. /*
  85. $search = Order::uniacid()->select(['yz_order.*','yz_fight_groups_team_member.group_id',
  86. 'yz_fight_groups_team_member.level_id','yz_fight_groups_team_member.team_id',
  87. 'yz_fight_groups.title as group_title','mc_members.nickname as leader_name','mc_members.avatar','yz_fight_groups_level.member_num',
  88. ])
  89. ->with(['hasOnePayType', 'belongsToMember', 'hasManyOrderGoods' => function ($query) {
  90. $query->select('total', 'price', 'goods_price', 'title', 'thumb', 'order_id', 'goods_option_title');
  91. }])
  92. ->leftJoin('yz_fight_groups_team_member','yz_order.id','=','yz_fight_groups_team_member.order_id')
  93. ->leftJoin('yz_fight_groups_level','yz_fight_groups_team_member.level_id','=','yz_fight_groups_level.id')
  94. ->leftJoin('yz_fight_groups','yz_fight_groups_team_member.group_id','=','yz_fight_groups.id')
  95. ->leftJoin('yz_fight_groups_team','yz_fight_groups_team_member.team_id','=','yz_fight_groups_team.id')
  96. ->leftJoin('mc_members','yz_fight_groups_team.leader_id','=','mc_members.uid')
  97. ->isPlugin()
  98. //->where('yz_order.is_member_deleted',0)
  99. ->where(app('OrderManager')->make('Order')->getTable() . '.is_member_deleted', 0)
  100. ->pluginId();
  101. if (!empty($fight_groups_id)) {
  102. $search = $search->where('yz_fight_groups_team_member.group_id', $fight_groups_id);
  103. }
  104. if (!empty($fight_groups_title)) {
  105. $search = $search->where('yz_fight_groups.title', 'like', $fight_groups_title.'%');
  106. }
  107. if (!empty($leader_name)) {
  108. $search = $search->where('mc_members.nickname', 'like', $leader_name.'%' );
  109. }
  110. if (!empty($order_sn)) {
  111. $search = $search->where('yz_order.order_sn', $order_sn );
  112. }
  113. if (!empty($status) || $status === 0 || $status === "0") {
  114. $search = $search->where('yz_order.status', $status );
  115. }
  116. if (!empty($create_time)) {
  117. $search = $search->whereBetween('yz_order.create_time', $create_time);
  118. }
  119. $list = $search->orderBy('yz_order.created_at', 'desc')->paginate(static::PAGE_SIZE,['*'], 'page', $page)->toArray();
  120. // 统计订单总金额
  121. $list['sum'] = 0;
  122. foreach ($list['data'] as $order) {
  123. $list['sum'] += $order['price'];
  124. }
  125. */
  126. return $this->successJson('成功',$list);
  127. }
  128. /**
  129. * @return string
  130. * @throws AppException
  131. * @throws \Throwable
  132. */
  133. public function detail(){
  134. $order = Order::orders()->with(['deductions', 'coupons', 'discounts', 'hasOnePayType', 'orderPays' => function ($query) {
  135. $query->with('payType');
  136. }, 'hasManyOrderGoods'=> function ($query){
  137. $query->with(['goods'=> function ($query){
  138. $query->select(['id','goods_sn','product_sn','market_price','price','cost_price','status']);
  139. }]);
  140. }]);
  141. if (request()->has('order_id')) {
  142. $order = $order->find(request('order_id'));
  143. }
  144. if (request()->has('order_sn')) {
  145. $order = $order->where('order_sn', request('order_sn'))->first();
  146. }
  147. if (!$order) {
  148. throw new AppException('未找到订单');
  149. }
  150. if (!empty($order->express)) {
  151. $express = $order->express->getExpress($order->express->express_code, $order->express->express_sn);
  152. $dispatch['express_sn'] = $order->express->express_sn;
  153. $dispatch['company_name'] = $order->express->express_company_name;
  154. $dispatch['data'] = $express['data'];
  155. $dispatch['thumb'] = $order->hasManyOrderGoods[0]->thumb;
  156. $dispatch['tel'] = '95533';
  157. $dispatch['status_name'] = $express['status_name'];
  158. }
  159. return view('Yunshop\FightGroups::admin.order.basicDetail', ['data' => json_encode($order)]);
  160. }
  161. /**
  162. * @return \Illuminate\Http\JsonResponse
  163. * @throws \app\common\exceptions\AppException
  164. */
  165. public function pay()
  166. {
  167. $order = Order::find(request()->input('order_id'));
  168. $order->backendPay();
  169. return $this->successJson();
  170. }
  171. /**
  172. * @return \Illuminate\Http\JsonResponse
  173. * @throws \app\common\exceptions\AppException
  174. */
  175. public function receive()
  176. {
  177. OrderService::orderReceive(['order_id'=>request()->input('order_id')]);
  178. return $this->successJson();
  179. }
  180. /**
  181. * @return mixed
  182. * @throws AppException
  183. */
  184. public function send()
  185. {
  186. /**
  187. * @var Order $order
  188. */
  189. OrderService::orderSend(request()->only(['order_id','express_code','express_sn']));
  190. return $this->successJson('成功');
  191. }
  192. /**
  193. * @return \Illuminate\Http\JsonResponse
  194. * @throws \app\common\exceptions\AppException
  195. */
  196. public function close()
  197. {
  198. /**
  199. * @var Order $order
  200. */
  201. OrderService::orderClose(['order_id'=>request()->input('order_id')]);
  202. return $this->successJson('成功');
  203. }
  204. }