Send.php 6.1 KB

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