ElasticSearchGoodsNewCrontab.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Crontab;
  3. use EasySwoole\Crontab\JobInterface;
  4. use App\Com\SmtpSend;
  5. use EasySwoole\EasySwoole\Config;
  6. use App\Models\WarningBody;
  7. use EasySwoole\Mysqli\QueryBuilder;
  8. use EasySwoole\ORM\DbManager;
  9. use EasySwoole\EasySwoole\Logger;
  10. /**
  11. * 任务说明:每10分钟抓取30分钟内更新的商品内容,
  12. * 使用福利api提供的接口推送到Es搜索引擎中
  13. */
  14. class ElasticSearchGoodsNewCrontab implements JobInterface
  15. {
  16. protected $time = 60 * 30; //xx内更新或新增的商品同步到es
  17. public function jobName(): string
  18. {
  19. // 定时任务的名称
  20. return 'ElasticSearchGoodsNewCrontab';
  21. }
  22. public function crontabRule(): string
  23. {
  24. // 定义执行规则 根据 Crontab 来定义
  25. // 这里是每10分钟执行 1 次
  26. return '*/10 * * * *';
  27. // return '* * * * *';
  28. }
  29. public function run()
  30. {
  31. //定时任务的执行逻辑
  32. // $welfare_api_set = Config::getInstance()->getConf('WELFARE_API');
  33. $welfare_api_set = [
  34. 'url' => 'https://open.api.superdesk.cn/api/base/dict_new/pushGoods',
  35. // 'url' => '',
  36. ];
  37. $page = 1;
  38. while (true) {
  39. $goods = $this->getUpdateGoods($page);
  40. if (!$goods) {
  41. break;
  42. } else {
  43. $page++;
  44. }
  45. foreach ($goods as $key => $value) {
  46. $res = $this->http_request(
  47. $welfare_api_set['url'],
  48. ['goodsid' => $value['id'], 'goods_body' => json_encode($value)]
  49. );
  50. //返回内容写入日志
  51. Logger::getInstance()->notice("(new)商品ID{$value['id']}导入结果:{$res}\n");
  52. }
  53. }
  54. }
  55. protected function getUpdateGoods($page = 1)
  56. {
  57. $size = 50;
  58. $offset = ($page - 1) * $size;
  59. $ids = array_unique(array_merge($this->getGoodsIdFromPool(), $this->getGoodsIdFromOperators_goods()));
  60. if (!$ids) {
  61. return [];
  62. }
  63. $ids = implode(',', $ids);
  64. $where = " id IN ($ids)";
  65. $que_sql = "SELECT * FROM ims_superdesk_shop_goods WHERE {$where} LIMIT {$offset},{$size}; ";
  66. $queryBuild = new QueryBuilder();
  67. $queryBuild->raw($que_sql);
  68. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  69. $goods = $res_data['result'];
  70. $new_goods = [];
  71. foreach ($goods as $k => $item) {
  72. $item['operators_id'] = $this->getOperatorsIdByGoodsId($item['id']);
  73. $new_goods[$k] = $item;
  74. }
  75. return $new_goods;
  76. }
  77. function getOperatorsIdByGoodsId($goods_id)
  78. {
  79. $sql = "select operators_id from ims_superdesk_shop_operators_goods where goodsid = '{$goods_id}' and `status`=1 AND deleted=0 AND checked=0 ";
  80. $queryBuild = new QueryBuilder();
  81. $queryBuild->raw($sql);
  82. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  83. $result = $res_data['result'];
  84. if (!$result) {
  85. return [];
  86. }
  87. return array_column($result, 'operators_id');
  88. }
  89. /**
  90. * 获取变更的商品ID来自商品主表
  91. */
  92. protected function getGoodsIdFromPool()
  93. {
  94. $time = time() - $this->time;
  95. $where = " `updatetime`> '{$time}' OR createtime > '{$time}' ";
  96. $que_sql = "SELECT id FROM ims_superdesk_shop_goods WHERE {$where} ORDER BY updatetime DESC LIMIT 1000 ";
  97. $queryBuild = new QueryBuilder();
  98. $queryBuild->raw($que_sql);
  99. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  100. return array_column($res_data['result'], 'id');
  101. }
  102. /**
  103. * 获取变更的商品ID来自运营商商品表
  104. */
  105. protected function getGoodsIdFromOperators_goods()
  106. {
  107. $time = time() - $this->time;
  108. $where = " `updatetime`> '{$time}' OR createtime > '{$time}' ";
  109. $que_sql = "SELECT goodsid FROM ims_superdesk_shop_operators_goods WHERE {$where} ORDER BY updatetime DESC LIMIT 1000 ";
  110. $queryBuild = new QueryBuilder();
  111. $queryBuild->raw($que_sql);
  112. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  113. return array_column($res_data['result'], 'goodsid');
  114. }
  115. /**
  116. * http请求
  117. * @param $url 请求地址
  118. * @param $data 数组内容
  119. * @return String 请求返回原生数据
  120. */
  121. protected function http_request($url, $data)
  122. {
  123. $opts = array(
  124. 'http' => array(
  125. 'method' => 'POST',
  126. 'header' => "Content-type:application/x-www-form-urlencoded",
  127. 'content' => http_build_query($data),
  128. )
  129. );
  130. $context = stream_context_create($opts);
  131. $response = file_get_contents($url, false, $context);
  132. return $response;
  133. }
  134. public function onException(\Throwable $throwable)
  135. {
  136. // 捕获 run 方法内所抛出的异常
  137. }
  138. }