Order.php 7.5 KB

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