hour.php 10 KB

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