ElasticSearchGoodsNewCrontab2.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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['pcate'], $item['ccate'], $item['tcate'], $item['operators_id']);
  79. $new_goods[$k] = $item;
  80. }
  81. return $new_goods;
  82. }
  83. function getOperatorsCategory($goods_id, $pcate, $ccate, $tcate, $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 == 2) { //自建分类
  97. $sql = "select pcate,ccate,tcate 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. $pcate = $res[0]['pcate'];
  103. $ccate = $res[0]['ccate'];
  104. $tcate = $res[0]['tcate'];
  105. }
  106. }
  107. $result[] = $operators_id.'_'.$category_type.'_'.$pcate.'-'.$ccate.'-'.$tcate;
  108. }
  109. return $result;
  110. }
  111. function getOperatorsIdByGoodsId($goods_id)
  112. {
  113. $sql = "select operators_id from ims_superdesk_shop_operators_goods where goodsid = '{$goods_id}' and `status`=1 AND deleted=0 AND checked=0 ";
  114. $queryBuild = new QueryBuilder();
  115. $queryBuild->raw($sql);
  116. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  117. $result = $res_data['result'];
  118. if (!$result) {
  119. return [];
  120. }
  121. return array_column($result, 'operators_id');
  122. }
  123. /**
  124. * 获取变更的商品ID来自商品主表
  125. */
  126. protected function getGoodsIdFromPool()
  127. {
  128. $time = time() - $this->time;
  129. $where = " `updatetime`> '{$time}' OR createtime > '{$time}' ";
  130. $que_sql = "SELECT id FROM ims_superdesk_shop_goods WHERE {$where} ORDER BY updatetime DESC LIMIT 1000 ";
  131. $queryBuild = new QueryBuilder();
  132. $queryBuild->raw($que_sql);
  133. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  134. return array_column($res_data['result'], 'id');
  135. }
  136. /**
  137. * 获取变更的商品ID来自运营商商品表
  138. */
  139. protected function getGoodsIdFromOperators_goods()
  140. {
  141. $time = time() - $this->time;
  142. $where = " `updatetime`> '{$time}' OR createtime > '{$time}' ";
  143. $que_sql = "SELECT goodsid FROM ims_superdesk_shop_operators_goods WHERE {$where} ORDER BY updatetime DESC LIMIT 1000 ";
  144. $queryBuild = new QueryBuilder();
  145. $queryBuild->raw($que_sql);
  146. $res_data = DbManager::getInstance()->query($queryBuild, true, 'default')->toArray();
  147. return array_column($res_data['result'], 'goodsid');
  148. }
  149. /**
  150. * http请求
  151. * @param $url 请求地址
  152. * @param $data 数组内容
  153. * @return String 请求返回原生数据
  154. */
  155. protected function http_request($url, $data)
  156. {
  157. $opts = array(
  158. 'http' => array(
  159. 'method' => 'POST',
  160. 'header' => "Content-type:application/x-www-form-urlencoded",
  161. 'content' => http_build_query($data),
  162. )
  163. );
  164. $context = stream_context_create($opts);
  165. $response = file_get_contents($url, false, $context);
  166. return $response;
  167. }
  168. public function onException(\Throwable $throwable)
  169. {
  170. // 捕获 run 方法内所抛出的异常
  171. }
  172. }