ElasticSearchGoodsNewCrontab2.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 ElasticSearchGoodsNewCrontab2 implements JobInterface
  15. {
  16. protected $time = 60 * 30; //xx内更新或新增的商品同步到es
  17. public function jobName(): string
  18. {
  19. // 定时任务的名称
  20. return 'ElasticSearchGoodsNewCrontab2';
  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. [
  49. 'es_index' => 'superdesk_welfare_20230511',
  50. 'es_type' => 'ims_superdesk_shop_goods',
  51. 'goodsid' => $value['id'],
  52. 'goods_body' => json_encode($value),
  53. ]
  54. );
  55. //返回内容写入日志
  56. Logger::getInstance()->notice("(new2)商品ID{$value['id']}导入结果:{$res}\n");
  57. }
  58. }
  59. }
  60. protected function getUpdateGoods($page = 1)
  61. {
  62. $size = 50;
  63. $offset = ($page - 1) * $size;
  64. $ids = array_unique(array_merge($this->getGoodsIdFromPool(), $this->getGoodsIdFromOperators_goods()));
  65. if (!$ids) {
  66. return [];
  67. }
  68. $ids = implode(',', $ids);
  69. $where = " id IN ($ids)";
  70. $que_sql = "SELECT * FROM ims_superdesk_shop_goods WHERE {$where} LIMIT {$offset},{$size}; ";
  71. $queryBuild = new QueryBuilder();
  72. $queryBuild->raw($que_sql);
  73. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  74. $goods = $res_data['result'];
  75. $new_goods = [];
  76. foreach ($goods as $k => $item) {
  77. $item['operators_id'] = $this->getOperatorsIdByGoodsId($item['id']);
  78. $item['category'] = $this->getOperatorsCategory($item['id'], $item['ccate'], $item['operators_id']);
  79. $new_goods[$k] = $item;
  80. }
  81. return $new_goods;
  82. }
  83. function getOperatorsCategory($goods_id, $ccate, $operators_ids)
  84. {
  85. $result = array();
  86. $queryBuild = new QueryBuilder();
  87. $category_type = 0;
  88. foreach ($operators_ids as $operators_id) {
  89. $sql = "select category_type from ims_superdesk_shop_operators_user where id = '{$operators_id}'";
  90. $queryBuild->raw($sql);
  91. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  92. $res = $res_data['result'];
  93. if ($res) {
  94. $category_type = $res[0]['category_type'];
  95. }
  96. if ($category_type) { //自建分类
  97. $sql = "select ccate from ims_superdesk_shop_operators_goods where goodsid = '{$goods_id}' and operators_id = '{$operators_id}' ";
  98. $queryBuild->raw($sql);
  99. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  100. $res = $res_data['result'];
  101. if ($res) {
  102. $ccate = $res[0]['ccate'];
  103. }
  104. }
  105. $result[] = $operators_id.'_'.$category_type.'_'.$ccate;
  106. }
  107. return $result;
  108. }
  109. function getOperatorsIdByGoodsId($goods_id)
  110. {
  111. $sql = "select operators_id from ims_superdesk_shop_operators_goods where goodsid = '{$goods_id}' and `status`=1 AND deleted=0 AND checked=0 ";
  112. $queryBuild = new QueryBuilder();
  113. $queryBuild->raw($sql);
  114. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  115. $result = $res_data['result'];
  116. if (!$result) {
  117. return [];
  118. }
  119. return array_column($result, 'operators_id');
  120. }
  121. /**
  122. * 获取变更的商品ID来自商品主表
  123. */
  124. protected function getGoodsIdFromPool()
  125. {
  126. $time = time() - $this->time;
  127. $where = " `updatetime`> '{$time}' OR createtime > '{$time}' ";
  128. $que_sql = "SELECT id FROM ims_superdesk_shop_goods WHERE {$where} ORDER BY updatetime DESC LIMIT 1000 ";
  129. $queryBuild = new QueryBuilder();
  130. $queryBuild->raw($que_sql);
  131. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  132. return array_column($res_data['result'], 'id');
  133. }
  134. /**
  135. * 获取变更的商品ID来自运营商商品表
  136. */
  137. protected function getGoodsIdFromOperators_goods()
  138. {
  139. $time = time() - $this->time;
  140. $where = " `updatetime`> '{$time}' OR createtime > '{$time}' ";
  141. $que_sql = "SELECT goodsid FROM ims_superdesk_shop_operators_goods WHERE {$where} ORDER BY updatetime DESC LIMIT 1000 ";
  142. $queryBuild = new QueryBuilder();
  143. $queryBuild->raw($que_sql);
  144. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  145. return array_column($res_data['result'], 'goodsid');
  146. }
  147. /**
  148. * http请求
  149. * @param $url 请求地址
  150. * @param $data 数组内容
  151. * @return String 请求返回原生数据
  152. */
  153. protected function http_request($url, $data)
  154. {
  155. $opts = array(
  156. 'http' => array(
  157. 'method' => 'POST',
  158. 'header' => "Content-type:application/x-www-form-urlencoded",
  159. 'content' => http_build_query($data),
  160. )
  161. );
  162. $context = stream_context_create($opts);
  163. $response = file_get_contents($url, false, $context);
  164. return $response;
  165. }
  166. public function onException(\Throwable $throwable)
  167. {
  168. // 捕获 run 方法内所抛出的异常
  169. }
  170. }