Send.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\data\controller\shop;
  3. use app\data\model\BasePostageCompany;
  4. use app\data\model\DataUser;
  5. use app\data\model\ShopOrder;
  6. use app\data\model\ShopOrderSend;
  7. use app\data\service\ExpressService;
  8. use app\data\service\OrderService;
  9. use think\admin\Controller;
  10. use think\exception\HttpResponseException;
  11. /**
  12. * 订单发货管理
  13. * Class Send
  14. * @package app\data\controller\shop
  15. */
  16. class Send extends Controller
  17. {
  18. /**
  19. * 订单发货管理
  20. * @auth true
  21. * @menu true
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function index()
  27. {
  28. $this->title = '订单发货管理';
  29. // 发货地址数据
  30. $this->address = sysdata('ordersend');
  31. // 状态数据统计
  32. $this->total = ['t0' => 0, 't1' => 0, 't2' => 0, 'ta' => 0];
  33. $db = ShopOrder::mk()->whereIn('status', [4, 5, 6])->where(['truck_type' => 1]);
  34. $query = ShopOrderSend::mk()->whereRaw("order_no in {$db->field('order_no')->buildSql()}");
  35. foreach ($query->fieldRaw('status,count(1) total')->group('status')->cursor() as $vo) {
  36. $this->total["t{$vo['status']}"] = $vo['total'];
  37. $this->total["ta"] += $vo['total'];
  38. }
  39. // 订单列表查询
  40. $query = ShopOrderSend::mQuery();
  41. $query->dateBetween('address_datetime,send_datetime')->equal('status')->like('send_number#truck_number,order_no');
  42. $query->like('address_phone,address_name,address_province|address_city|address_area|address_content#address_content');
  43. // 用户搜索查询
  44. $db = DataUser::mQuery()->like('phone|nickname#user_keys')->db();
  45. if ($db->getOptions('where')) $query->whereRaw("uuid in {$db->field('id')->buildSql()}");
  46. // 订单搜索查询
  47. $db = ShopOrder::mk()->whereIn('status', [4, 5, 6])->where(['truck_type' => 1]);
  48. $query->whereRaw("order_no in {$db->field('order_no')->buildSql()}");
  49. // 列表选项卡状态
  50. if (is_numeric($this->type = trim(input('type', 'ta'), 't'))) {
  51. $query->where(['status' => $this->type]);
  52. }
  53. // 列表排序显示
  54. $query->order('id desc')->page();
  55. }
  56. /**
  57. * 订单列表处理
  58. * @param array $data
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. protected function _index_page_filter(array &$data)
  64. {
  65. OrderService::buildData($data, false);
  66. $orders = array_unique(array_column($data, 'order_no'));
  67. $orderList = ShopOrder::mk()->whereIn('order_no', $orders)->column('*', 'order_no');
  68. foreach ($data as &$vo) $vo['order'] = $orderList[$vo['order_no']] ?? [];
  69. }
  70. /**
  71. * 快递发货地址
  72. * @auth true
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function config()
  78. {
  79. if ($this->request->isGet()) {
  80. $this->vo = sysdata('ordersend');
  81. $this->fetch();
  82. } else {
  83. sysdata('ordersend', $this->request->post());
  84. $this->success('发货地址保存成功');
  85. }
  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 truck()
  95. {
  96. if ($this->request->isGet()) {
  97. $query = BasePostageCompany::mk()->where(['deleted' => 0, 'status' => 1]);
  98. $this->items = $query->order('sort desc,id desc')->select()->toArray();
  99. }
  100. ShopOrderSend::mForm('truck_form', 'order_no');
  101. }
  102. /**
  103. * 快递表单处理
  104. * @param array $vo
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. protected function _truck_form_filter(array &$vo)
  110. {
  111. if ($this->request->isPost()) {
  112. $map = ['order_no' => $vo['order_no']];
  113. $order = ShopOrder::mk()->where($map)->find();
  114. if (empty($order)) $this->error('订单查询异常,请稍候再试!');
  115. // 配送快递公司填写
  116. $map = ['code_1|code_2|code_3' => $vo['company_code']];
  117. $company = BasePostageCompany::mk()->where($map)->find();
  118. if (empty($company)) $this->error('配送快递公司异常,请重新选择快递公司!');
  119. $vo['status'] = 2;
  120. $vo['company_name'] = $company['name'];
  121. $vo['send_datetime'] = $vo['send_datetime'] ?? date('Y-m-d H:i:s');
  122. if ($order['status'] === 4) {
  123. // 更新订单发货状态
  124. $map = ['order_no' => $vo['order_no']];
  125. ShopOrder::mk()->where($map)->update(['status' => 5]);
  126. }
  127. }
  128. }
  129. /**
  130. * 快递追踪查询
  131. * @auth true
  132. */
  133. public function query()
  134. {
  135. try {
  136. $data = $this->_vali(['code.require' => '快递不能为空!', 'number.require' => '单号不能为空!']);
  137. $this->result = ExpressService::query($data['code'], $data['number']);
  138. if (empty($this->result['code'])) $this->error($this->result['info']);
  139. $this->fetch('truck_query');
  140. } catch (HttpResponseException $exception) {
  141. throw $exception;
  142. } catch (\Exception $exception) {
  143. $this->error($exception->getMessage());
  144. }
  145. }
  146. }