Send.php 5.7 KB

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