hour.php 8.7 KB

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