Order.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\admin\controller\shopro\trade;
  3. use app\admin\controller\shopro\Common;
  4. use app\admin\model\shopro\trade\Order as TradeOrderModel;
  5. use app\admin\model\shopro\Pay as PayModel;
  6. class Order extends Common
  7. {
  8. protected $noNeedRight = ['getType'];
  9. public function _initialize()
  10. {
  11. parent::_initialize();
  12. $this->model = new TradeOrderModel;
  13. }
  14. /**
  15. * 订单列表
  16. *
  17. * @return \think\Response
  18. */
  19. public function index()
  20. {
  21. if (!$this->request->isAjax()) {
  22. $exportConfig = (new \addons\shopro\library\Export())->getConfig();
  23. $this->assignconfig("save_type", $exportConfig['save_type'] ?? 'download');
  24. return $this->view->fetch();
  25. }
  26. $orders = $this->model->withTrashed()->sheepFilter()->with(['user'])
  27. ->paginate(request()->param('list_rows', 10))->each(function ($order) {
  28. $order->pay_type_text = $order->pay_type_text;
  29. $order->pay_type = $order->pay_type;
  30. })->toArray();
  31. $result = [
  32. 'orders' => $orders,
  33. ];
  34. // 查询各个状态下的订单数量
  35. $searchStatus = $this->model->searchStatusList();
  36. // 所有的数量
  37. $result['all'] = $this->model->withTrashed()->sheepFilter(true, function ($filters) {
  38. unset($filters['status']);
  39. return $filters;
  40. })->count();
  41. foreach ($searchStatus as $status => $text) {
  42. $result[$status] = $this->model->withTrashed()->sheepFilter(true, function ($filters) use ($status) {
  43. $filters['status'] = $status;
  44. return $filters;
  45. })->count();
  46. }
  47. $this->success('获取成功', null, $result);
  48. }
  49. // 获取数据类型
  50. public function getType()
  51. {
  52. $type = $this->model->typeList();
  53. $payType = (new PayModel)->payTypeList();
  54. $platform = $this->model->platformList();
  55. $status = $this->model->searchStatusList();
  56. $result = [
  57. 'type' => $type,
  58. 'pay_type' => $payType,
  59. 'platform' => $platform,
  60. 'status' => $status
  61. ];
  62. $data = [];
  63. foreach ($result as $key => $list) {
  64. $data[$key][] = ['name' => '全部', 'type' => 'all'];
  65. foreach ($list as $k => $v) {
  66. $data[$key][] = [
  67. 'name' => $v,
  68. 'type' => $k
  69. ];
  70. }
  71. }
  72. $this->success('获取成功', null, $data);
  73. }
  74. /**
  75. * 订单详情
  76. *
  77. * @param $id
  78. * @return \think\Response
  79. */
  80. public function detail($id)
  81. {
  82. if (!$this->request->isAjax()) {
  83. return $this->view->fetch();
  84. }
  85. $order = $this->model->withTrashed()->with(['user', 'pays'])->where('id', $id)->find();
  86. if (!$order) {
  87. $this->error(__('No Results were found'));
  88. }
  89. $order->pay_type = $order->pay_type;
  90. $order->pay_type_text = $order->pay_type_text;
  91. $this->success('获取成功', null, $order);
  92. }
  93. public function export()
  94. {
  95. $cellTitles = [
  96. // 订单表字段
  97. 'order_id' => 'Id',
  98. 'order_sn' => '订单号',
  99. 'type_text' => '订单类型',
  100. 'user_nickname' => '下单用户',
  101. 'user_mobile' => '手机号',
  102. 'status_text' => '订单状态',
  103. 'pay_type_text' => '支付类型',
  104. 'remark' => '用户备注',
  105. 'order_amount' => '订单总金额',
  106. 'pay_fee' => '应付总金额',
  107. 'real_pay_fee' => '实付总金额',
  108. 'remain_pay_fee' => '剩余支付金额',
  109. 'paid_time' => '支付完成时间',
  110. 'platform_text' => '交易平台',
  111. 'createtime' => '下单时间',
  112. ];
  113. // 数据总条数
  114. $total = $this->model->withTrashed()->sheepFilter()->count();
  115. if ($total <= 0) {
  116. $this->error('导出数据为空');
  117. }
  118. $export = new \addons\shopro\library\Export();
  119. $params = [
  120. 'file_name' => '交易订单列表',
  121. 'cell_titles' => $cellTitles,
  122. 'total' => $total,
  123. ];
  124. $total_order_amount = 0;
  125. $total_pay_fee = 0;
  126. $total_real_pay_fee = 0;
  127. $result = $export->export($params, function ($pages) use (&$total_order_amount, &$total_pay_fee, &$total_real_pay_fee, $total) {
  128. $datas = $this->model->withTrashed()->sheepFilter()->with(['user'])
  129. ->limit((($pages['page'] - 1) * $pages['list_rows']), $pages['list_rows'])
  130. ->select();
  131. $datas = collection($datas);
  132. $datas = $datas->each(function ($order) {
  133. $order->pay_type_text = $order->pay_type_text;
  134. })->toArray();
  135. $newDatas = [];
  136. foreach ($datas as &$order) {
  137. $data = [
  138. 'order_id' => $order['id'],
  139. 'order_sn' => $order['order_sn'],
  140. 'type_text' => $order['type_text'],
  141. 'user_nickname' => $order['user'] ? $order['user']['nickname'] : '-',
  142. 'user_mobile' => $order['user'] ? $order['user']['mobile'] . ' ' : '-',
  143. 'status_text' => $order['status_text'],
  144. 'pay_type_text' => is_array($order['pay_type_text']) ? join(',', $order['pay_type_text']) : ($order['pay_type_text'] ?: ''),
  145. 'remark' => $order['remark'],
  146. 'order_amount' => $order['order_amount'],
  147. 'pay_fee' => $order['pay_fee'],
  148. 'real_pay_fee' => bcsub($order['pay_fee'], $order['remain_pay_fee'], 2),
  149. 'remain_pay_fee' => $order['remain_pay_fee'],
  150. 'paid_time' => $order['paid_time'],
  151. 'platform_text' => $order['platform_text'],
  152. 'createtime' => $order['createtime'],
  153. ];
  154. $newDatas[] = $data;
  155. }
  156. $total_order_amount += array_sum(array_column($newDatas, 'order_amount'));
  157. $total_pay_fee += array_sum(array_column($newDatas, 'pay_fee'));
  158. $total_real_pay_fee += array_sum(array_column($newDatas, 'real_pay_fee'));
  159. if ($pages['is_last_page']) {
  160. $newDatas[] = ['order_id' => "订单总数:" . $total . ";订单总金额:¥" . $total_order_amount . ";应付总金额:¥" . $total_pay_fee . ";实付总金额:¥" . $total_real_pay_fee];
  161. }
  162. return $newDatas;
  163. });
  164. $this->success('导出成功' . (isset($result['file_path']) && $result['file_path'] ? ',请在服务器: “' . $result['file_path'] . '” 查看' : ''), null, $result);
  165. }
  166. }