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