JuShuiTan.php 12 KB

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