hour.php 9.2 KB

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