ElasticSearchGoodsNewCrontab2.php 6.6 KB

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