JuShuiTan.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\admin\command;
  12. use app\common\model\FengsuShip;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\Output;
  16. use GuzzleHttp\Client;
  17. use app\common\model\CrontabLog;
  18. use app\common\model\ImportSku;
  19. use app\common\model\ShopList;
  20. use app\common\model\ProductConfig;
  21. use app\common\model\ShopDelivery;
  22. use app\common\model\CustomerSpec;
  23. use app\admin\service\JuShuiTanService;
  24. use GuzzleHttp\Psr7\Request;
  25. use app\common\model\ImportList;
  26. use think\facade\Cache;
  27. //抖音
  28. class JuShuiTan extends Command
  29. {
  30. //抖音请求地址
  31. protected $url = 'https://innerapi.scm121.com/api/inner/supOrder/getErpNormalOrders';
  32. //时间间隔
  33. protected $start_end_space_time=10*60;
  34. //下一次时间间隔
  35. protected $next_time_space=7*60;
  36. //抖音请求头部
  37. protected $headers = [
  38. //'Host' => 'fsdy2.fengsutb.com',
  39. 'Accept' => 'application/json, text/plain, */*',
  40. 'Authorization' => 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiIyMTk0MzA5MSIsInJvbGVJZHMiOltdLCJ1c2VyX25hbWUiOiIxODE4MjY2OTkzNSIsImNvSWQiOiIxNDc5MjIyMiIsImV4cGlyYXRpb24iOjE3NjYyMTg0NDM1MzQsInVzZXIiOnsiY29JZCI6IjE0NzkyMjIyIiwiY29OYW1lIjoi5LqR5Y2X5LmQ55Wq55Sw5L6b5bqU6ZO-5pyJ6ZmQ5YWs5Y-4IiwibG9naW5OYW1lIjoiMTgxODI2Njk5MzUiLCJsb2dpbldheSI6IlVTRVJOQU1FIiwibmlja05hbWUiOiLmnY4iLCJyb2xlSWRzIjoiMTEsMTIsMTMsMTQsMTUsMTYsMTcsMTgsMjIsMjMsMjcsMjgsMjksMzAsMzEsMzIsMzMsMzQsMzUsMzYsMzksNDAsNDEsNTIsNTMsNTQsNjEsNjIsNjMsNjQsNjUsNjYsNjcsNzAsNzEsNzIsNzMsNzYsOTAsMTAxLDEwMiwxMDMsMTA0LDEwNSwxMDYsMTA3LDEwOCwxMDkiLCJ1aWQiOiIyMTk0MzA5MSJ9LCJhdXRob3JpdGllcyI6WyJKU1QtY2hhbm5lbCIsIm11bHRpTG9naW4iLCJKU1Qtc3VwcGxpZXIiXSwiY2xpZW50X2lkIjoicGMiLCJqdGkiOiIxYjNhZjA4OC04ZDhiLTRhNzMtYjcwNC1lYzhhY2QwNzU1ODUiLCJleHAiOjE3NjYyMTg0NDN9.fXIP-yUlJDIfd49QgxjTQXUlRka2gpw0NrfY7lB50sU',
  41. 'Content-Type' => 'application/json',
  42. // 'platform' => 'dy',
  43. ];
  44. // 打单平台 1:风速 2:聚水潭
  45. protected $type_id = 2;
  46. // 指令配置
  47. protected function configure()
  48. {
  49. $this->setName('ju_shui_tan')->setDescription('the task command');
  50. }
  51. //根据shop_id
  52. protected function execute(Input $input, Output $output)
  53. {
  54. $output->writeln('开始同步聚水潭订单');
  55. $resquet = $this->getOrderList('dy');
  56. if ($resquet['success']) {
  57. $jushuitan = [];
  58. $jushuitan = Cache::get('jushuitan');
  59. if ($resquet['data']!=null&&!empty($resquet['data'])) {
  60. $jushuitan_pageNum = $jushuitan['pageNum'];
  61. $jushuitan_pageNum = $jushuitan_pageNum + 1;
  62. $jushuitan = [
  63. 'startTime' => $jushuitan['startTime'],
  64. 'endTime' => $jushuitan['endTime'],
  65. 'pageNum' => $jushuitan_pageNum,
  66. 'next_time_space' => $jushuitan['next_time_space']
  67. ];
  68. Cache::set('jushuitan', $jushuitan);
  69. $list = $resquet['data'];
  70. // dump($list);
  71. // return;
  72. $this->insertOrder($list);
  73. //dump($resquet);
  74. } else {
  75. $now_time = time();
  76. if ($jushuitan['startTime']>=$now_time||($now_time - $jushuitan['startTime']) < ($this->next_time_space)) {
  77. // dump("超时了");
  78. $pre_time_stamp = $this->next_time_space;
  79. $startTime_stamp = $now_time - $pre_time_stamp;
  80. $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
  81. $jushuitan = [
  82. 'startTime' => $startTime_stamp,
  83. 'endTime' => $endTime_stamp,
  84. 'pageNum' => 1,
  85. 'next_time_space' => $jushuitan['next_time_space']
  86. ];
  87. }else{
  88. $jushuitan = [
  89. 'startTime' => $jushuitan['startTime'] + $jushuitan['next_time_space'],
  90. 'endTime' => $jushuitan['endTime'] + $jushuitan['next_time_space'],
  91. 'pageNum' => 1,
  92. 'next_time_space' => $jushuitan['next_time_space']
  93. ];
  94. }
  95. Cache::set('jushuitan', $jushuitan);
  96. $output->writeln('没有数据');
  97. }
  98. } else {
  99. $output->writeln('接口错误: ' . $resquet['message']);
  100. }
  101. $output->writeln('ok');
  102. }
  103. public function getOrderList($platform = 'dy')
  104. {
  105. $client = new \GuzzleHttp\Client([
  106. 'verify' => false,
  107. // 或指定证书路径
  108. // 'verify' => '/path/to/cacert.pem'
  109. ]);
  110. $headers = [
  111. 'Accept' => ' application/json, text/plain, */*',
  112. 'Authorization' => site_config('addonsd.ju_shui_tan_authorization_toke'),
  113. 'Content-Type' => ' application/json'
  114. ];
  115. //时间间隔
  116. // $this->start_end_space_time = 10 * 60;
  117. //下一次时间间隔
  118. // $this->next_time_space = 7 * 60;
  119. //将查询分为,"2026-01-05 00:00:00~2026-01-05 00:05:00",查询该区间的订单,但是该时间区间单独数量可能超过50条,为了性能又不能一次性查询太多,从而进行分页查询,
  120. //查询不到数据,就往下一个时间区间查询
  121. $todayTime = date("Y-m-d");
  122. $startTime_stamp = strtotime($todayTime . ' 00:00:00');
  123. // $startTime_stamp = strtotime($todayTime . ' 07:20:00');
  124. $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
  125. $pageNum = 1;
  126. $jushuitan = [
  127. 'startTime' => $startTime_stamp,
  128. 'endTime' => $endTime_stamp,
  129. 'pageNum' => $pageNum,
  130. 'next_time_space' => $this->next_time_space
  131. ];
  132. $jushuitan_cache_data = Cache::get('jushuitan');
  133. if (empty($jushuitan_cache_data) || $jushuitan_cache_data == null) {
  134. Cache::set('jushuitan', $jushuitan);
  135. }
  136. $jushuitan = Cache::get('jushuitan');
  137. // 无数据时,获取数据的开始时间会大于现在的时间,这是一个错误的,会导致一直无法获取数据,开始时间一直往后
  138. //当开始时间大于现在时间,将开始时间向前调整3分钟
  139. $now_time = time();
  140. if ($jushuitan['startTime']>=$now_time||($now_time - $jushuitan['startTime']) < ($this->start_end_space_time - $this->next_time_space)) {
  141. // dump("超时了");
  142. $pre_time_stamp = $this->next_time_space;
  143. $startTime_stamp = $now_time - $pre_time_stamp;
  144. $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
  145. $jushuitan = [
  146. 'startTime' => $startTime_stamp,
  147. 'endTime' => $endTime_stamp,
  148. 'pageNum' => 1,
  149. 'next_time_space' => $this->next_time_space
  150. ];
  151. }
  152. Cache::set('jushuitan', $jushuitan);
  153. $startTime = date('Y-m-d H:i:s', $jushuitan['startTime']);
  154. $endTime = date('Y-m-d H:i:s', $jushuitan['endTime']);
  155. $pageNum = (int)$jushuitan['pageNum'];
  156. $body = [
  157. 'startTime' => $startTime, // 动态时间变量
  158. 'endTime' => $endTime,
  159. // 'startTime' => "2026-01-08 00:00:00",
  160. // 'endTime' => "2026-01-08 23:59:59",
  161. 'dateQueryType' => 'DeliveryDate',
  162. 'coId' => '14792222',
  163. 'uid' => '21943091',
  164. 'upSendFailed' => false, // 布尔值保持原始类型
  165. 'isExceptAddress' => 'Except',
  166. 'pageNum' => (int)$pageNum, //当前页数
  167. // 'pageNum' => 1, //当前页数
  168. 'pageSize' => 100, //每页数据多少
  169. 'searchType' => 1,
  170. 'orderStatus' => ['Sent'],
  171. // "trackNo"=>"SF5103166333686",
  172. "excludeOrderLabels"=>["线上发货"]
  173. ];
  174. $body = json_encode($body);
  175. // "orderStatus": [
  176. // "Sent"
  177. // ]
  178. // "orderFrom":"PINDUODUO"
  179. //"trackNo":"@SF3278427215674"
  180. $request = new Request('POST', 'https://innerapi.scm121.com/api/inner/supOrder/getErpNormalOrders', $headers, $body);
  181. $res = $client->sendAsync($request)->wait();
  182. $json = [];
  183. if ($res->getStatusCode() == 200) {
  184. $json = json_decode($res->getBody()->getContents(), true);
  185. } else {
  186. dump($res->getStatusCode(), '接口返回结果');
  187. }
  188. return $json;
  189. }
  190. public function insertOrder($order_list)
  191. {
  192. if (!(count($order_list) > 0)) {
  193. return false;
  194. }
  195. $queue = [];
  196. $shopList = new ShopList();
  197. $importSku = new ImportSku();
  198. $productConfig = new ProductConfig();
  199. $shopDelivery = new ShopDelivery();
  200. $customerSpec = new CustomerSpec();
  201. usort($order_list, function ($a, $b) {
  202. // 将create_time转换为时间戳比较(支持字符串时间戳)
  203. $timeA = strtotime($a['deliveryDate']);
  204. $timeB = strtotime($b['deliveryDate']);
  205. // 降序排序(从新到旧)
  206. // return $timeB - $timeA;
  207. // 升序排序(从旧到新)可改为:
  208. return $timeA - $timeB;
  209. });
  210. $shopDelivery_list = [];
  211. foreach ($order_list as $item) {
  212. if (substr($item['trackNo'], 0, 1) === '@') continue; //同风速的,带@直接跳过
  213. // dump($item['shopId'], '店铺id');
  214. // 防止重复记录,订单号+快递单号
  215. $goods_id = $item['disInnerOrderGoodsViewList'][0]['shopIid'];
  216. $oid = $item['oid'];
  217. $sku_id = $item['disInnerOrderGoodsViewList'][0]['shopSkuId'];
  218. // $waybill_no = ltrim($item['trackNo'], '@');
  219. $waybill_no = $item['trackNo'];
  220. if (empty($waybill_no)) continue;
  221. $add_status = JuShuiTanService::prevent_duplicate_additions($sku_id, $waybill_no);
  222. if ($add_status) continue;
  223. // $status = JuShuiTanService::getAdditionalPrice($shopList, $importSku, $productConfig, $shopDelivery, $customerSpec, $item);
  224. $res = JuShuiTanService::getAdditionalPrice($shopList, $importSku, $productConfig, $shopDelivery, $customerSpec, $item);
  225. $status = $res['status'];
  226. $shopDelivery_list[] = $res['shopDelivery'];
  227. $getPackSpecsFee = JuShuiTanService::getPackSpecsFee($importSku, $item['shopId'], $item['disInnerOrderGoodsViewList'][0]['shopSkuId'], $item['receiverState']);
  228. $queue[] = [
  229. 'type_id' => $this->type_id,
  230. 'shop_id' => $item['shopId'],
  231. 'order_id' => $oid,
  232. 'trade_from' => $item['orderFrom'],
  233. 'province' => $item['receiverState'],
  234. 'city' => $item['receiverCity'],
  235. 'company_name' => $item['expressCompany'],
  236. 'waybill_no' => $waybill_no,
  237. 'consign_time' => $item['deliveryDate'],
  238. 'goods_id' => $goods_id,
  239. 'sku_id' => $item['disInnerOrderGoodsViewList'][0]['shopSkuId'],
  240. 'num' => $item['disInnerOrderGoodsViewList'][0]['itemCount'],
  241. 'goods_title' => $item['disInnerOrderGoodsViewList'][0]['itemName'],
  242. 'goods_info' => $item['disInnerOrderGoodsViewList'][0]['properties'],
  243. 'weight' => $item['weight'],
  244. 'price' => bcdiv((string)$item['disInnerOrderGoodsViewList'][0]['price'], '1', 2),
  245. 'status' => $status,
  246. 'specs_name' => $getPackSpecsFee['data']['title'],
  247. 'pack_specs_id' => $getPackSpecsFee['data']['id'],
  248. 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
  249. 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
  250. 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
  251. 'order_status' => $item['disInnerOrderGoodsViewList'][0]['status'],
  252. ];
  253. }
  254. // 保存到数据库
  255. $shopDelivery->saveAll($shopDelivery_list);
  256. (new ImportList())->saveAll($queue);
  257. // 更新最后执行时间
  258. CrontabLog::create(['type_id' => 2, 'last_time' => date('Y-m-d H:i:s')]);
  259. }
  260. }