Order.php 7.5 KB

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