| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace app\command;
- use app\model\saas\SaasOrder;
- use app\model\saas\SaasOrderExpress;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Webman\RedisQueue\Redis;
- use yzh52521\EasyHttp\Http;
- class ExpressOrder extends Command
- {
- protected static string $defaultName = 'express:order';
- protected static string $defaultDescription = '更新订单物流信息';
- protected function execute(InputInterface $input, OutputInterface $output): int
- {
- try {
- $output->writeln("开始更新快递信息====>" . getDateFull());
- $nextPage = 1;
- while (is_numeric($nextPage)) {
- $resp = (new SaasOrderExpress)->where(["status" => 0])->with(['orders' => function ($query) {
- $query->field("*")->with(['poi']);
- }])->page($nextPage,10)->select();
- if ($resp->isEmpty()) {
- $output->writeln("没有任务啦==".getDateFull());
- $nextPage = null;
- return true;
- }
- foreach ($resp as $item) {
- if (empty($item['orders']['poi'])) {
- echo getDateFull()."=={$item['order_sn']}==订单对应商家不存在===\n";
- continue;
- }
- $startTime = $item['orders']['poi']['order_start']??'09:00';
- $endTime = $item['orders']['poi']['order_end']??'22:30';
- $startTimeStamp = strtotime(date("Y-m-d {$startTime}:00"));
- $endTimeStamp = strtotime(date("Y-m-d {$endTime}:00"));
- echo "开始时间:{$startTimeStamp};结束时间:{$endTimeStamp}\n";
- if (time() > $startTimeStamp && time() < $endTimeStamp) {
- [$state,$expressData] = $this->getExpressData($item['express_sn'],$item['mobile']);
- if ($state && $item['orders']['is_error'] == 0) {
- $isDone = false;
- foreach ($expressData['logisticsTraceDetailList'] as $sk){
- if (in_array($sk['logisticsStatus'],['DELIVERING','AGENT_SIGN','SIGN'])) {
- $isDone = true;
- }
- }
- // if (in_array($expressData['logisticsStatusDesc'],['派件中',"已签收"])) { // 核销订单
- if ($isDone) { // 核销订单
- $output->writeln("订单进入核销=={$item['order_sn']}=={$item['express_sn']}==".getDateFull());
- (new SaasOrderExpress)->where("id",$item['id'])->update(['status' => 1,'last_at' => getDateFull(),'last_msg' => $expressData['logisticsStatusDesc'],'content' => json_encode($expressData['logisticsTraceDetailList'])]);
- Redis::send("order-done",['order' => $item['order_sn']]);
- } else {
- (new SaasOrderExpress)->where("id",$item['id'])->update(['last_msg' => $expressData['logisticsStatusDesc'],'last_at' => getDateFull(),'content' => json_encode($expressData['logisticsTraceDetailList'])]);
- $output->writeln("查询物流成功=={$item['order_sn']}=={$expressData['logisticsStatusDesc']}==".getDateFull());
- }
- } else {
- $output->writeln("查询快递信息失败=={$item['order_sn']}=={$item['express_sn']}==".getDateFull());
- }
- } else {
- echo getDateFull()."==={$item['order_sn']}===不在核销时段==\n";
- }
- }
- $nextPage = $nextPage+1;
- $output->writeln("进入下一页=={$nextPage}==".getDateFull());
- }
- } catch (\Throwable $throwable) {
- echo getDateFull()."===更新快递信息报错\n";
- echo $throwable->getFile()."\n";
- echo $throwable->getLine()."\n";
- echo $throwable->getMessage()."\n";
- }
- return self::SUCCESS;
- }
- protected function getExpressData(string $expressNo = "",string $mobile = ""): array
- {
- $url = "https://kzexpress.market.alicloudapi.com/api-mall/api/express/query";
- $param = [
- "expressNo" => $expressNo,
- "mobile" => substr($mobile,-4)
- ];
- $resp = Http::withHeaders([
- "Authorization" => "APPCODE ".sConf("wechat.kd_code")
- ])->post($url,$param)->array();
- if ($resp['code'] <> 200) {
- return [0,''];
- }
- return [1,$resp['data']];
- }
- }
|