getLogisticsInformationByKuaidi.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use app\admin\model\Test1;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\Output;
  8. use think\Exception;
  9. use think\facade\Db;
  10. //获取实时快递信息
  11. class getLogisticsInformationByKuaidi extends Command
  12. {
  13. protected function configure()
  14. {
  15. // 指令配置
  16. $this->setName('getLogisticsInformationByKuaidi')
  17. ->setDescription('获取实时快递信息');
  18. }
  19. //获取实时快递信息
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $order_out_list = Db::name('order_out')
  23. ->alias('oo')
  24. ->where('oo.status', 2)//status==2 已发货待收货
  25. ->leftJoin('order_addr oa', 'oa.id=oo.addrid AND oa.is_del=0')
  26. ->field('oo.id,oo.post_code,oo.updatetime,oa.mobile,oo.post_name')
  27. ->cursor();
  28. if (empty($order_out_list)) $output->writeln('没有符合条件的记录');
  29. else {
  30. foreach ($order_out_list as $order_out) {
  31. $this->_handlePostData($order_out['id'], $order_out['post_code'], $order_out['mobile'], $order_out['post_name']);
  32. }
  33. $output->writeln(date('Y-m-d H:i:s') . '|处理成功');
  34. }
  35. }
  36. //处理物流数据
  37. //@param string order_out_id 发货单ID
  38. //@param string post_data 快递单号
  39. //@param string mobile 收货人手机号,目前只有顺丰需要
  40. //@param string post_name 物流公司
  41. private function _handlePostData(int $order_out_id = 0, string $post_code = '', string $mobile = '', string $post_name = '')
  42. {
  43. $remark = [0 => '物流单号暂无结果', 3 => '在途', 4 => '揽件', 5 => '疑难', 6 => '签收', 7 => '退签', 8 => '派件', 9 => '退回'];
  44. Db::startTrans();
  45. try {
  46. $data = get_logistics_information($post_code, substr($mobile, -4));
  47. if (isset($data['errMsg']) || (isset($data['success']) && $data['success'] != true)) throw new Exception($data['reason']);
  48. $rs = Db::name('express_data')
  49. ->field('id')
  50. ->where(['post_code' => $post_code, 'order_out_id' => $order_out_id])
  51. ->find();
  52. if (empty($rs)) {
  53. Db::name('express_data')
  54. ->insert([
  55. 'order_out_id' => $order_out_id,
  56. 'post_code' => $post_code,
  57. 'post_name' => $data['company'],
  58. 'post_express' => $data['exname'],
  59. 'post_logo' => $data['ico'],
  60. 'post_data' => json_encode($data['data'], JSON_UNESCAPED_UNICODE),
  61. 'status' => $data['status'],
  62. 'remark' => $remark[$data['status']],
  63. 'addtime' => date('Y-m-d H:i:s'),
  64. 'updatetime' => date('Y-m-d H:i:s'),
  65. ]);
  66. } else {
  67. Db::name('express_data')
  68. ->where(['id' => $rs['id'], 'order_out_id' => $order_out_id])
  69. ->update([
  70. 'post_data' => json_encode($data['data'], JSON_UNESCAPED_UNICODE),
  71. 'status' => $data['status'],
  72. 'remark' => $remark[$data['status']],
  73. 'updatetime' => date('Y-m-d H:i:s'),
  74. ]);
  75. }
  76. //确认收货
  77. if ($data['status'] == 6) {
  78. Db::name('order_out')
  79. ->where(['post_code' => $post_code, 'id' => $order_out_id, 'status' => 2])
  80. ->update([
  81. 'status' => 3,//status==3 已收货
  82. 'updatetime' => date('Y-m-d H:i:s'),
  83. ]);
  84. }
  85. //更新物流公司
  86. if ($post_name == '') {
  87. Db::name('order_out')
  88. ->where(['post_code' => $post_code, 'id' => $order_out_id])
  89. ->update([
  90. 'post_name' => $post_name,
  91. 'updatetime' => date('Y-m-d H:i:s'),
  92. ]);
  93. }
  94. Db::commit();
  95. return true;
  96. } catch (Exception $exception) {
  97. Db::rollback();
  98. return false;
  99. }
  100. }
  101. }