hour.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * 任务计划 - 小时执行的任务
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class hourControl extends BaseCronControl
  11. {
  12. /**
  13. * 执行频率常量 1小时
  14. * @var int
  15. */
  16. const EXE_TIMES = 3600;
  17. private $_doc;
  18. private $_xs;
  19. private $_index;
  20. private $_search;
  21. /**
  22. * 默认方法
  23. */
  24. public function indexOp()
  25. {
  26. $this->repush_oms();
  27. $this->release_bonus();
  28. $this->expired_bonus();
  29. //$this->_xs_update();
  30. }
  31. /**
  32. * 初始化对象
  33. */
  34. private function _ini_xs(){
  35. require(BASE_DATA_PATH.'/api/xs/lib/XS.php');
  36. $this->_doc = new XSDocument();
  37. $this->_xs = new XS(C('fullindexer.appname'));
  38. $this->_index = $this->_xs->index;
  39. $this->_search = $this->_xs->search;
  40. $this->_search->setCharset(CHARSET);
  41. }
  42. /**
  43. * 全量创建索引
  44. */
  45. public function xs_createOp() {
  46. if (!C('fullindexer.open')) return;
  47. $this->_ini_xs();
  48. try {
  49. //每次批量更新商品数
  50. $step_num = 200;
  51. $model_goods = Model('goods');
  52. $count = $model_goods->getGoodsOnlineCount(array(),"distinct CONCAT(goods_commonid,',',color_id)");
  53. echo 'Total:'.$count."\n";
  54. $fields = "*,CONCAT(goods_commonid,',',color_id) as nc_distinct";
  55. for ($i = 0; $i <= $count; $i = $i + $step_num){
  56. $goods_list = $model_goods->getGoodsOnlineList(array(), $fields, 0, '', "{$i},{$step_num}", 'nc_distinct');
  57. $this->_build_goods($goods_list);
  58. echo $i." ok\n";
  59. flush();
  60. ob_flush();
  61. }
  62. if ($count > 0) {
  63. sleep(2);
  64. $this->_index->flushIndex();
  65. sleep(2);
  66. $this->_index->flushLogging();
  67. }
  68. } catch (XSException $e) {
  69. $this->log($e->getMessage());
  70. }
  71. }
  72. /**
  73. * 更新增量索引
  74. */
  75. public function _xs_update() {
  76. if (!C('fullindexer.open')) return;
  77. $this->_ini_xs();
  78. try {
  79. //更新多长时间内的新增(编辑)商品信息,该时间一般与定时任务触发间隔时间一致,单位是秒,默认3600
  80. $step_time = self::EXE_TIMES + 60;
  81. //每次批量更新商品数
  82. $step_num = 100;
  83. $model_goods = Model('goods');
  84. $condition = array();
  85. $condition['goods_edittime'] = array('egt',time()-$step_time);
  86. $count = $model_goods->getGoodsOnlineCount($condition,"distinct CONCAT(goods_commonid,',',color_id)");
  87. $fields = "*,CONCAT(goods_commonid,',',color_id) as nc_distinct";
  88. for ($i = 0; $i <= $count; $i = $i + $step_num){
  89. $goods_list = $model_goods->getGoodsOnlineList($condition, $fields, 0, '', "{$i},{$step_num}", 'nc_distinct');
  90. $this->_build_goods($goods_list);
  91. }
  92. if ($count > 0) {
  93. sleep(2);
  94. $this->_index->flushIndex();
  95. sleep(2);
  96. $this->_index->flushLogging();
  97. }
  98. } catch (XSException $e) {
  99. $this->log($e->getMessage());
  100. }
  101. }
  102. /**
  103. * 索引商品数据
  104. * @param array $goods_list
  105. */
  106. private function _build_goods($goods_list = array()) {
  107. if (empty($goods_list) || !is_array($goods_list)) return;
  108. $goods_class = Model('goods_class')->getGoodsClassForCacheModel();
  109. $goods_commonid_array = array();
  110. $goods_id_array = array();
  111. $store_id_array = array();
  112. foreach ($goods_list as $k => $v) {
  113. $goods_commonid_array[] = $v['goods_commonid'];
  114. $goods_id_array[] = $v['goods_id'];
  115. $store_id_array[] = $v['store_id'];
  116. }
  117. //取common表内容
  118. $model_goods = Model('goods');
  119. $condition_common = array();
  120. $condition_common['goods_commonid'] = array('in',$goods_commonid_array);
  121. $goods_common_list = $model_goods->getGoodsCommonOnlineList($condition_common,'*',0);
  122. $goods_common_new_list = array();
  123. foreach($goods_common_list as $k => $v) {
  124. $goods_common_new_list[$v['goods_commonid']] = $v;
  125. }
  126. //取属性表值
  127. $model_type = Model('type');
  128. $attr_list = $model_type->getGoodsAttrIndexList(array('goods_id'=>array('in',$goods_id_array)),0,'goods_id,attr_value_id');
  129. if (is_array($attr_list) && !empty($attr_list)) {
  130. $attr_value_list = array();
  131. foreach ($attr_list as $val) {
  132. $attr_value_list[$val['goods_id']][] = $val['attr_value_id'];
  133. }
  134. }
  135. //整理需要索引的数据
  136. foreach ($goods_list as $k => $v) {
  137. $gc_id = $v['gc_id'];
  138. $depth = $goods_class[$gc_id]['depth'];
  139. if ($depth == 3) {
  140. $cate_3 = $gc_id; $gc_id = $goods_class[$gc_id]['gc_parent_id']; $depth--;
  141. }
  142. if ($depth == 2) {
  143. $cate_2 = $gc_id; $gc_id = $goods_class[$gc_id]['gc_parent_id']; $depth--;
  144. }
  145. if ($depth == 1) {
  146. $cate_1 = $gc_id; $gc_id = $goods_class[$gc_id]['gc_parent_id'];
  147. }
  148. $index_data = array();
  149. $index_data['pk'] = $v['goods_id'];
  150. $index_data['goods_id'] = $v['goods_id'];
  151. $index_data['goods_name'] = $v['goods_name'].$v['goods_jingle'];
  152. $index_data['brand_id'] = $v['brand_id'];
  153. $index_data['goods_price'] = $v['goods_promotion_price'];
  154. $index_data['goods_click'] = $v['goods_click'];
  155. $index_data['goods_salenum'] = $v['goods_salenum'];
  156. // 判断店铺是否为自营店铺
  157. $index_data['store_id'] = $v['is_own_shop'];
  158. $index_data['area_id'] = $v['areaid_1'];
  159. $index_data['gc_id'] = $v['gc_id'];
  160. $index_data['gc_name'] = str_replace('&gt;','',$goods_common_new_list[$v['goods_commonid']]['gc_name']);
  161. $index_data['brand_name'] = $goods_common_new_list[$v['goods_commonid']]['brand_name'];
  162. $index_data['have_gift'] = $v['have_gift'];
  163. if (!empty($attr_value_list[$v['goods_id']])) {
  164. $index_data['attr_id'] = implode('_',$attr_value_list[$v['goods_id']]);
  165. }
  166. if (!empty($cate_1)) {
  167. $index_data['cate_1'] = $cate_1;
  168. }
  169. if (!empty($cate_2)) {
  170. $index_data['cate_2'] = $cate_2;
  171. }
  172. if (!empty($cate_3)) {
  173. $index_data['cate_3'] = $cate_3;
  174. }
  175. //添加到索引库
  176. $this->_doc->setFields($index_data);
  177. $this->_index->update($this->_doc);
  178. }
  179. }
  180. public function xs_clearOp(){
  181. if (!C('fullindexer.open')) return;
  182. $this->_ini_xs();
  183. try {
  184. $this->_index->clean();
  185. } catch (XSException $e) {
  186. $this->log($e->getMessage());
  187. }
  188. }
  189. public function xs_flushLoggingOp(){
  190. if (!C('fullindexer.open')) return;
  191. $this->_ini_xs();
  192. try {
  193. $this->_index->flushLogging();
  194. } catch (XSException $e) {
  195. $this->log($e->getMessage());
  196. }
  197. }
  198. public function xs_flushIndexOp(){
  199. if (!C('fullindexer.open')) return;
  200. $this->_ini_xs();
  201. try {
  202. $this->_index->flushIndex();
  203. } catch (XSException $e) {
  204. $this->log($e->getMessage());
  205. }
  206. }
  207. /**
  208. * 重新推送失败订单
  209. */
  210. private function repush_oms()
  211. {
  212. $order_data = Model('omsfail')->field('*')->where(array('order_status' => 0))->limit(false)->select();
  213. foreach ($order_data as $value)
  214. {
  215. $fail_id = $value['fail_id'];
  216. $logic_delivery = Logic('delivery');
  217. $ret = $logic_delivery->putOrder($value['pay_sn'], $value['pay_sn'],false);
  218. if ($ret) {
  219. Model('omsfail')->where(array('fail_id' => $fail_id))->update(array('order_status' => 1));
  220. }
  221. }
  222. }
  223. /**
  224. * 定时释放红包
  225. */
  226. private function release_bonus()
  227. {
  228. notify_helper::release_bonus();
  229. }
  230. /**
  231. * 定时清理已领取但未使用的红包
  232. */
  233. private function expired_bonus()
  234. {
  235. notify_helper::bonus_expired();
  236. }
  237. }