Order.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\data\controller\shop;
  3. use app\data\model\DataUser;
  4. use app\data\model\ShopOrder;
  5. use app\data\model\ShopOrderSend;
  6. use app\data\service\OrderService;
  7. use app\data\service\PaymentService;
  8. use app\data\service\UserAdminService;
  9. use think\admin\Controller;
  10. use think\admin\extend\CodeExtend;
  11. use think\exception\HttpResponseException;
  12. /**
  13. * 订单数据管理
  14. * Class Order
  15. * @package app\data\controller\shop
  16. */
  17. class Order extends Controller
  18. {
  19. /**
  20. * 支付方式
  21. * @var array
  22. */
  23. protected $payments = [];
  24. /**
  25. * 控制器初始化
  26. */
  27. protected function initialize()
  28. {
  29. parent::initialize();
  30. $this->payments = PaymentService::getTypeAll();
  31. }
  32. /**
  33. * 订单数据管理
  34. * @auth true
  35. * @menu true
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function index()
  41. {
  42. $this->title = '订单数据管理';
  43. // 状态数据统计
  44. $this->total = ['t0' => 0, 't1' => 0, 't2' => 0, 't3' => 0, 't4' => 0, 't5' => 0, 't6' => 0, 'ta' => 0];
  45. foreach (ShopOrder::mk()->field('status,count(1) total')->group('status')->cursor() as $vo) {
  46. [$this->total["t{$vo['status']}"] = $vo['total'], $this->total["ta"] += $vo['total']];
  47. }
  48. // 订单列表查询
  49. $query = ShopOrder::mQuery();
  50. $query->like('order_no,truck_name,truck_phone,truck_province|truck_area|truck_address#address,truck_send_no,truck_send_name');
  51. $query->equal('status,payment_type,payment_status')->dateBetween('create_at,payment_datetime,cancel_datetime,truck_datetime,truck_send_datetime');
  52. // 发货信息搜索
  53. $db = ShopOrderSend::mQuery()->like('address_name#truck_address_name,address_phone#truck_address_phone,address_province|address_city|address_area|address_content#truck_address_content')->db();
  54. if ($db->getOptions('where')) $query->whereRaw("order_no in {$db->field('order_no')->buildSql()}");
  55. // 用户搜索查询
  56. $db = DataUser::mQuery()->like('phone#user_phone,nickname#user_nickname')->db();
  57. if ($db->getOptions('where')) $query->whereRaw("uuid in {$db->field('id')->buildSql()}");
  58. // 代理搜索查询
  59. $db = DataUser::mQuery()->like('phone#from_phone,nickname#from_nickname')->db();
  60. if ($db->getOptions('where')) $query->whereRaw("puid1 in {$db->field('id')->buildSql()}");
  61. // 列表选项卡
  62. if (is_numeric($this->type = trim(input('type', 'ta'), 't'))) {
  63. $query->where(['status' => $this->type]);
  64. }
  65. // 分页排序处理
  66. $query->where(['deleted_status' => 0])->order('id desc')->page();
  67. }
  68. /**
  69. * 订单列表处理
  70. * @param array $data
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. protected function _index_page_filter(array &$data)
  76. {
  77. UserAdminService::instance()->buildByUid($data);
  78. UserAdminService::instance()->buildByUid($data, 'puid1', 'from');
  79. OrderService::instance()->buildData($data);
  80. foreach ($data as &$vo) $vo['payment_name'] = PaymentService::name($vo['payment_type']);
  81. }
  82. /**
  83. * 单据凭证支付审核
  84. * @auth true
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function audit()
  90. {
  91. if ($this->request->isGet()) {
  92. ShopOrder::mForm('', 'order_no');
  93. } else {
  94. $data = $this->_vali([
  95. 'order_no.require' => '订单单号不能为空!',
  96. 'status.in:0,1' => '审核状态数值异常!',
  97. 'status.require' => '审核状态不能为空!',
  98. 'remark.default' => '',
  99. ]);
  100. if (empty($data['status'])) {
  101. $data['status'] = 0;
  102. $data['cancel_status'] = 1;
  103. $data['cancel_remark'] = $data['remark'] ?: '后台审核驳回并取消订单';
  104. $data['cancel_datetime'] = date('Y-m-d H:i:s');
  105. } else {
  106. $data['status'] = 4;
  107. $data['payment_code'] = CodeExtend::uniqidDate(20, 'T');
  108. $data['payment_status'] = 1;
  109. $data['payment_remark'] = $data['remark'] ?: '后台审核支付凭证通过';
  110. $data['payment_datetime'] = date('Y-m-d H:i:s');
  111. }
  112. $order = ShopOrder::mk()->where(['order_no' => $data['order_no']])->find();
  113. if (empty($order) || $order['status'] !== 3) $this->error('不允许操作审核!');
  114. // 无需发货时的处理
  115. if ($data['status'] === 4 && empty($order['truck_type'])) $data['status'] = 6;
  116. // 更新订单支付状态
  117. $map = ['status' => 3, 'order_no' => $data['order_no']];
  118. if (ShopOrder::mk()->strict(false)->where($map)->update($data) !== false) {
  119. if (in_array($data['status'], [4, 5, 6])) {
  120. $this->app->event->trigger('ShopOrderPayment', $data['order_no']);
  121. $this->success('订单审核通过成功!');
  122. } else {
  123. $this->app->event->trigger('ShopOrderCancel');
  124. OrderService::instance()->stock($data['order_no']);
  125. $this->success('审核驳回并取消成功!');
  126. }
  127. } else {
  128. $this->error('订单审核失败!');
  129. }
  130. }
  131. }
  132. /**
  133. * 清理订单数据
  134. * @auth true
  135. */
  136. public function clean()
  137. {
  138. $this->_queue('定时清理无效订单数据', "xdata:OrderClean", 0, [], 0, 60);
  139. }
  140. /**
  141. * 取消未支付的订单
  142. * @auth true
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\DbException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. */
  147. public function cancel()
  148. {
  149. $map = $this->_vali(['order_no.require' => '订单号不能为空!',]);
  150. $order = ShopOrder::mk()->where($map)->find();
  151. if (empty($order)) $this->error('订单查询异常!');
  152. if (!in_array($order['status'], [1, 2, 3])) $this->error('订单不能取消!');
  153. try {
  154. $result = $order->save([
  155. 'status' => 0,
  156. 'cancel_status' => 1,
  157. 'cancel_remark' => '后台取消未支付的订单',
  158. 'cancel_datetime' => date('Y-m-d H:i:s'),
  159. ]);
  160. if ($result !== false) {
  161. OrderService::instance()->stock($order['order_no']);
  162. $this->app->event->trigger('ShopOrderCancel', $order['order_no']);
  163. $this->success('取消未支付的订单成功!');
  164. } else {
  165. $this->error('取消支付的订单失败!');
  166. }
  167. } catch (HttpResponseException $exception) {
  168. throw $exception;
  169. } catch (\Exception $exception) {
  170. $this->error($exception->getMessage());
  171. }
  172. }
  173. }