getLogisticsInformationByKuaidi.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. ->lock(true)
  27. ->field('oo.id,oo.post_code,oo.updatetime,oa.mobile,oo.post_name')
  28. ->cursor();
  29. if (empty($order_out_list)) $output->writeln('没有符合条件的记录');
  30. else {
  31. foreach ($order_out_list as $order_out) {
  32. $this->_handlePostData($order_out['id'], $order_out['post_code'], $order_out['mobile'], $order_out['post_name']);
  33. }
  34. $output->writeln(date('Y-m-d H:i:s') . '|处理成功');
  35. }
  36. }
  37. //处理物流数据
  38. //@param string order_out_id 发货单ID
  39. //@param string post_data 快递单号
  40. //@param string mobile 收货人手机号,目前只有顺丰需要
  41. //@param string post_name 物流公司
  42. private function _handlePostData(int $order_out_id = 0, string $post_code = '', string $mobile = '', string $post_name = '')
  43. {
  44. $remark = [0 => '物流单号暂无结果', 3 => '在途', 4 => '揽件', 5 => '疑难', 6 => '签收', 7 => '退签', 8 => '派件', 9 => '退回'];
  45. Db::startTrans();
  46. try {
  47. $data = get_logistics_information($post_code, substr($mobile, -4));
  48. if (isset($data['errMsg']) || (isset($data['success']) && $data['success'] != true)) throw new Exception($data['reason']);
  49. $rs = Db::name('express_data')
  50. ->field('id')
  51. ->where(['post_code' => $post_code, 'order_out_id' => $order_out_id])
  52. ->find();
  53. if (empty($rs)) {
  54. Db::name('express_data')
  55. ->insert([
  56. 'order_out_id' => $order_out_id,
  57. 'post_code' => $post_code,
  58. 'post_name' => $data['company'],
  59. 'post_express' => $data['exname'],
  60. 'post_logo' => $data['ico'],
  61. 'post_data' => json_encode($data['data'], JSON_UNESCAPED_UNICODE),
  62. 'status' => $data['status'],
  63. 'remark' => $remark[$data['status']],
  64. 'addtime' => date('Y-m-d H:i:s'),
  65. 'updatetime' => date('Y-m-d H:i:s'),
  66. ]);
  67. } else {
  68. Db::name('express_data')
  69. ->where(['id' => $rs['id'], 'order_out_id' => $order_out_id])
  70. ->update([
  71. 'post_data' => json_encode($data['data'], JSON_UNESCAPED_UNICODE),
  72. 'status' => $data['status'],
  73. 'remark' => $remark[$data['status']],
  74. 'updatetime' => date('Y-m-d H:i:s'),
  75. ]);
  76. }
  77. //确认收货
  78. if ($data['status'] == 6) {
  79. Db::name('order_out')
  80. ->where(['post_code' => $post_code, 'id' => $order_out_id, 'status' => 2])
  81. ->update([
  82. 'status' => 3,//status==3 已收货
  83. 'updatetime' => date('Y-m-d H:i:s'),
  84. ]);
  85. }
  86. //更新物流公司
  87. if ($post_name == '') {
  88. Db::name('order_out')
  89. ->where(['post_code' => $post_code, 'id' => $order_out_id])
  90. ->update([
  91. 'post_name' => $post_name,
  92. 'updatetime' => date('Y-m-d H:i:s'),
  93. ]);
  94. }
  95. Db::commit();
  96. return true;
  97. } catch (Exception $exception) {
  98. Db::rollback();
  99. return false;
  100. }
  101. }
  102. }