CronController.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Controller;
  15. class CronController extends CommonController {
  16. protected function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. public function index()
  21. {
  22. ignore_user_abort();
  23. set_time_limit(0);
  24. //------------------系统未支付订单超时关闭
  25. $lasttime = S('closeorder_lasttime');
  26. $lasttime = strtotime($lasttime);
  27. $interval = 3;//1分钟
  28. $interval *= 60;
  29. $current = time();
  30. //shop_domain get_config_by_name($name)
  31. $url = D('Home/Front')->get_config_by_name('shop_domain');
  32. $url = $url."/index.php?s=/Cron/close";
  33. if (($lasttime + $interval) <= $current) {
  34. S('closeorder_lasttime', date('Y-m-d H:i:s', $current));
  35. $url = $url."/index.php?s=/Cron/close";
  36. ihttp_request($url, NULL, NULL, 1);
  37. //ihttp_request($url, $post = '', $extra = array(), $timeout = 60)
  38. }
  39. $url = D('Home/Front')->get_config_by_name('shop_domain');
  40. $url = $url."/index.php?s=/Cron/statement";
  41. //begin
  42. $today_zero_time = strtotime( date('Y-m-d').' 00:00:00' );
  43. $clear_goodsdaysales = D('Home/Front')->get_config_by_name('clear_goodsdaysales');
  44. //$clear_goodsdaysales = S('clear_goodsdaysales');
  45. if( !isset($today_zero_time) || $clear_goodsdaysales != $today_zero_time )
  46. {
  47. D('Seller/Commonorder')->clear_goods_daysales();
  48. //S('clear_goodsdaysales', $today_zero_time );
  49. $config_data = array();
  50. $config_data['clear_goodsdaysales'] = $today_zero_time;
  51. D('Seller/Config')->update($config_data);
  52. }
  53. //end
  54. $lasttimestatement = S('statementorder');
  55. if( empty($lasttimestatement) )
  56. {
  57. $lasttimestatement = 0;
  58. }
  59. $intervalstatement = 1;//1分钟
  60. $intervalstatement *= 60;
  61. $currentstatement = time();
  62. if (($lasttimestatement + $intervalstatement) <= $currentstatement) {
  63. S('statementorder', $currentstatement);
  64. ihttp_request($url, NULL, NULL, 1);
  65. }
  66. //---
  67. $lasttimeautoreciveorder = $resultstatement = S('autoreciveorder');
  68. $intervalstatement = 1;//1分钟
  69. $intervalstatement *= 60;
  70. $currentstatement = time();
  71. $url = D('Home/Front')->get_config_by_name('shop_domain');
  72. $url = $url."/index.php?s=/Cron/receive";
  73. if (($lasttimeautoreciveorder + $intervalstatement) <= $currentstatement) {
  74. S('autoreciveorder', $currentstatement);
  75. ihttp_request($url, NULL, NULL, 1);
  76. }
  77. echo 3;
  78. }
  79. /**
  80. **/
  81. public function receive()
  82. {
  83. ignore_user_abort();
  84. set_time_limit(0);
  85. $shop_list = M('lionfish_comshop_config')->field('value')->where( array('name' => 'open_auto_recive_order' ) )->select();
  86. foreach($shop_list as $shop)
  87. {
  88. $open_auto_recive_order = $shop['value'];
  89. if($open_auto_recive_order == 1)
  90. {
  91. $receive_day = D('Home/Front')->get_config_by_name('auto_recive_order_time');
  92. $receive_hour_time = time() - 86400 * $receive_day;
  93. $order_list = M('lionfish_comshop_order')->field('order_id,delivery')->where( "express_time <={$receive_hour_time} and order_status_id =4" )->select();
  94. foreach($order_list as $order )
  95. {
  96. //检查是否有部分退款
  97. $order_refund_info = M('lionfish_comshop_order_refund')->field('ref_id')->where( array('order_id' => $order['order_id'], 'state' => 0 ) )->find();
  98. if( !empty($order_refund_info) )
  99. {
  100. continue;
  101. }
  102. if($order['delivery'] != 'hexiao'){
  103. D('Home/Frontorder')->receive_order($order['order_id'], true);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. private function lock_thisfile($tmpFileStr,$locktype=false){
  110. if($locktype == false)
  111. $locktype = LOCK_EX|LOCK_NB;
  112. $can_write = 0;
  113. $lockfp = @fopen($tmpFileStr.".lock","w");
  114. if($lockfp){
  115. $can_write = @flock($lockfp,$locktype);
  116. }
  117. if($can_write){
  118. return $lockfp;
  119. }
  120. else{
  121. if($lockfp){
  122. @fclose($lockfp);
  123. @unlink($tmpFileStr.".lock");
  124. }
  125. return false;
  126. }
  127. }
  128. private function unlock_thisfile($fp,$tmpFileStr){
  129. @flock($fp,LOCK_UN);
  130. @fclose($fp);
  131. @fclose($fp);
  132. @unlink($tmpFileStr.".lock");
  133. }
  134. public function statement()
  135. {
  136. ignore_user_abort();
  137. set_time_limit(0);
  138. //S('closeorder_lasttime');
  139. //文件锁begin
  140. $data_path = dirname( dirname(dirname( dirname(__FILE__) )) ).'/Data/wxpaylogs/'.date('Y-m-d')."/";
  141. RecursiveMkdir($data_path);
  142. $file = $data_path.'lock.txt';
  143. $lockhandle = $this->lock_thisfile($file,true);
  144. if($lockhandle) //锁定当前指针,,,
  145. {
  146. $statementorder_flag = S('statementorder_flag');
  147. if( !empty($statementorder_flag) && $statementorder_flag == 1 )
  148. {
  149. S('statementorder_flag', 0);
  150. die();
  151. }
  152. S('statementorder_flag', 1);
  153. $shop_list = M('lionfish_comshop_config')->field('value')->where( array('name' => 'open_aftersale' ) )->select();
  154. foreach($shop_list as $shop)
  155. {
  156. $open_aftersale = $shop['value'];
  157. if($open_aftersale == 1)
  158. {
  159. $time = time();
  160. $sql = "SELECT o.order_id , og.order_goods_id FROM ".C('DB_PREFIX')."lionfish_comshop_order as o , ".C('DB_PREFIX')."lionfish_comshop_order_goods as og
  161. WHERE o.order_id = og.order_id and o.order_status_id in(6,11) and og.is_statements_state = 0 and og.statements_end_time<{$time} order by o.order_id desc ";
  162. $order_list = M()->query($sql);
  163. foreach($order_list as $order )
  164. {
  165. D('Home/Frontorder')->settlement_order($order['order_id']);
  166. }
  167. }else{
  168. $time = time()-5*60;
  169. $sql = "SELECT o.order_id , og.order_goods_id FROM ".C('DB_PREFIX')."lionfish_comshop_order as o , ".C('DB_PREFIX')."lionfish_comshop_order_goods as og
  170. WHERE o.order_id = og.order_id and o.order_status_id in(6,11) and og.is_statements_state = 0 and o.receive_time < $time order by o.order_id desc ";
  171. $order_list = M()->query($sql);
  172. foreach($order_list as $order )
  173. {
  174. D('Home/Frontorder')->settlement_order($order['order_id']);
  175. }
  176. }
  177. }
  178. S('statementorder_flag', 0);
  179. $runtimelog_flag = S('runtimelog_flag');
  180. $next_time = time()+86400;
  181. if( empty($runtimelog_flag) || $runtimelog_flag > $next_time )
  182. {
  183. $hour = date('H');
  184. $hour = intval($hour);
  185. if( $hour == 2 )
  186. {
  187. $this->clear_runtimelog();
  188. S('statementorder_flag', time() );
  189. }
  190. }
  191. $this->unlock_thisfile($lockhandle,$file);
  192. }
  193. }
  194. private function clear_runtimelog()
  195. {
  196. $logs_path = ROOT_PATH.'Runtime/Logs';
  197. $cache_path = ROOT_PATH.'Runtime/Cache';
  198. $temp_path = ROOT_PATH.'Runtime/Temp';
  199. $this->removeDir($logs_path);
  200. $this->removeDir($cache_path);
  201. $this->removeDir($temp_path);
  202. echo 'ok';
  203. }
  204. /**
  205. * removeDir 删除文件夹下所有文件
  206. * @param $path
  207. */
  208. private function removeDir($path)
  209. {
  210. if(is_dir($path)){
  211. if($handle = opendir($path)){
  212. while($file = readdir($handle)){
  213. if(is_dir($path.'/'.$file.'/') && $file!='.' && $file!='..'){
  214. $this->removeDir($path.'/'.$file.'/');
  215. } else {
  216. if($file!='.' && $file!='..'){
  217. @unlink($path.'/'.$file);
  218. }
  219. }
  220. }
  221. closedir($handle);
  222. }
  223. }
  224. }
  225. public function refund()
  226. {
  227. ignore_user_abort();
  228. set_time_limit(0);
  229. $daytimenow_ev56_s = time();
  230. $condition = " state=0 and end_time < ".$daytimenow_ev56_s;
  231. $pin_list = M()->query("select * from ".C('DB_PREFIX')."lionfish_comshop_pin
  232. where state=0 and end_time <{$daytimenow_ev56_s} order by pin_id asc ");
  233. $weixin_model = D('Home/Weixin');
  234. $pintuan_isvirs_success = D('Home/Front')->get_config_by_name('pintuan_isvirs_success');
  235. if(!empty($pin_list)) {
  236. foreach($pin_list as $pin)
  237. {
  238. //暂时屏蔽
  239. if( isset($pintuan_isvirs_success) && $pintuan_isvirs_success == 1 )
  240. {
  241. $res = D('Home/Pin')->jia_over_order( $pin['pin_id'] );
  242. if( $res )
  243. {
  244. continue;
  245. }
  246. }
  247. M('lionfish_comshop_pin')->where( array('pin_id' => $pin['pin_id'] ) )->save( array('state' => 2) );
  248. $pin_order_list = M('lionfish_comshop_pin_order')->where( array('pin_id' => $pin['pin_id'] ) )->select();
  249. $order_ids = array();
  250. foreach($pin_order_list as $vv)
  251. {
  252. $order_ids[] = $vv['order_id'];
  253. }
  254. $order_ids_str = implode(',', $order_ids);
  255. //ims_
  256. $order_list = M('lionfish_comshop_order')->field('order_id,type')->where(" order_id in ({$order_ids_str}) and order_status_id=2 ")->select();
  257. //https://sqtg.shiziyu888.com/index.php?s=/cron/refund/
  258. $can_cg_state = true;
  259. foreach($order_list as $order)
  260. {
  261. if( $order['type'] != 'ignore' )
  262. {
  263. $res = $weixin_model->refundOrder($order['order_id'], 0);
  264. if( $res['code'] == 0 )
  265. {
  266. //拼团失败,订单退款
  267. $history_data = array();
  268. $history_data['order_id'] = $order['order_id'];
  269. $history_data['order_status_id'] = 7;
  270. $history_data['notify'] = 0;
  271. $history_data['comment'] = '拼团失败,订单退款失败,错误原因: '.$res['msg'];
  272. $history_data['date_added'] = time();
  273. M('lionfish_comshop_order_history')->add( $history_data );
  274. }else{
  275. M('lionfish_comshop_order')->where( array('order_id' => $order['order_id']) )->save( array('order_status_id' => 7) );
  276. //拼团失败,订单退款
  277. $history_data = array();
  278. $history_data['order_id'] = $order['order_id'];
  279. $history_data['order_status_id'] = 7;
  280. $history_data['notify'] = 0;
  281. $history_data['comment'] = '拼团失败,订单退款';
  282. $history_data['date_added'] = time();
  283. M('lionfish_comshop_order_history')->add( $history_data );
  284. D('Seller/Commonorder')->refund_one_order( $order['order_id'] , 1);
  285. }
  286. //
  287. }else{
  288. M('lionfish_comshop_order')->where( array('order_id' => $order['order_id'] ) )->save( array('order_status_id' => 7) );
  289. $res = array('code' => 1);
  290. }
  291. if( $res['code'] == 1)
  292. {
  293. }else{
  294. $can_cg_state = false;
  295. }
  296. }
  297. if( !$can_cg_state )
  298. {
  299. M('lionfish_comshop_pin')->where( array('pin_id' => $pin['pin_id'] ) )->save( array('state' => 0) );
  300. }
  301. }
  302. }
  303. //--
  304. $infos = M('lionfish_comshop_config')->where( array('name' => 'statewaitorder') )->find();
  305. if( empty($infos) )
  306. {
  307. $lasttime = 0;
  308. }else{
  309. $lasttime = $infos['value'];
  310. }
  311. $interval = 3;
  312. $interval *= 60;
  313. $current = time();
  314. if (($lasttime + $interval) <= $current ) {
  315. if( empty($infos) )
  316. {
  317. $ins_data = array();
  318. $ins_data['name'] = 'statewaitorder';
  319. $ins_data['value'] = $current;
  320. M('lionfish_comshop_config')->add( $ins_data );
  321. }else{
  322. M('lionfish_comshop_config')->where( array('id' => $infos['id']) )->save( array('value' => $current) );
  323. }
  324. $sql ="SELECT ho.order_id,o.order_num_alias,ho.order_goods_id FROM ".
  325. C('DB_PREFIX')."lionfish_community_head_commiss_order as ho left join ".C('DB_PREFIX')."lionfish_comshop_order as o on ho.order_id = o.order_id where ho.state = 0 and o.order_status_id IN (6,11)";
  326. $xiufu_list = M()->query($sql);
  327. $need_order = array();
  328. //var_dump($xiufu_list);die();
  329. foreach( $xiufu_list as $vv )
  330. {
  331. $open_aftersale = D('Home/Front')->get_config_by_name('open_aftersale');
  332. if( empty($open_aftersale) )
  333. {
  334. $open_aftersale = 0;
  335. }
  336. if( $open_aftersale == 1 )
  337. {
  338. $n_sql = "select hco.*, og.order_goods_id from ".C('DB_PREFIX')."lionfish_comshop_order_goods as og left join ".
  339. C('DB_PREFIX')."lionfish_community_head_commiss_order as hco on og.order_goods_id = hco.order_goods_id where hco.state = 0 and og.is_statements_state=1 and og.order_id = ".$vv['order_id'];
  340. $info_list = M()->query($n_sql);
  341. if( !empty($info_list) )
  342. {
  343. foreach($info_list as $info)
  344. {
  345. M('lionfish_comshop_order_goods')->where( array('order_goods_id' => $info['order_goods_id'] ) )->save( array('is_statements_state' => 0) );
  346. }
  347. }
  348. }else{
  349. if( empty($need_order) || !in_array($vv['order_id'], $need_order) )
  350. {
  351. if( $vv['order_goods_id'] > 0 )
  352. {
  353. M('lionfish_comshop_order_goods')->where( array('order_goods_id' => $vv['order_goods_id'] ) )->save( array('is_statements_state' => 0) );
  354. }else{
  355. M('lionfish_comshop_order_goods')->where( array('order_id' => $vv['order_id'] ) )->save( array('is_statements_state' => 0) );
  356. }
  357. D('Home/Frontorder')->settlement_order($vv['order_id']);
  358. }
  359. }
  360. }
  361. }
  362. $sql ="SELECT ho.order_id,o.order_num_alias,ho.order_goods_id FROM ".C('DB_PREFIX').
  363. "lionfish_comshop_pintuan_commiss_order as ho left join ".C('DB_PREFIX')."lionfish_comshop_order as o on ho.order_id = o.order_id
  364. where ho.state = 0 and o.order_status_id IN (6,11)";
  365. $pintuan_xiufu_list = M()->query($sql);
  366. $need_order = array();
  367. foreach( $pintuan_xiufu_list as $vv )
  368. {
  369. $open_aftersale = D('Home/Front')->get_config_by_name('open_aftersale');
  370. if( empty($open_aftersale) )
  371. {
  372. $open_aftersale = 0;
  373. }
  374. if( $open_aftersale == 1 )
  375. {
  376. $n_sql = "select hco.*, og.order_goods_id from ".C('DB_PREFIX')."lionfish_comshop_order_goods as og left join ".C('DB_PREFIX')."lionfish_community_head_commiss_order as hco on og.order_goods_id = hco.order_goods_id where hco.state = 0 and og.is_statements_state=1 and og.order_id = ".$vv['order_id'];
  377. $info_list = M()->query($n_sql);
  378. if( !empty($info_list) )
  379. {
  380. foreach($info_list as $info)
  381. {
  382. M('lionfish_comshop_order_goods')->where( array('order_goods_id' => $info['order_goods_id'] ) )->save( array('is_statements_state' => 0) );
  383. }
  384. }
  385. }else{
  386. if( empty($need_order) || !in_array($vv['order_id'], $need_order) )
  387. {
  388. if( $vv['order_goods_id'] > 0 )
  389. {
  390. M('lionfish_comshop_order_goods')->where( array('order_goods_id' => $vv['order_goods_id'] ) )->save( array('is_statements_state' => 0) );
  391. }else{
  392. M('lionfish_comshop_order_goods')->where( array('order_id' => $vv['order_id'] ) )->save( array('is_statements_state' => 0) );
  393. }
  394. D('Home/Frontorder')->settlement_order($vv['order_id']);
  395. }
  396. }
  397. }
  398. //核销订单过期处理
  399. D('Home/Salesroom')->hexiao_expire();
  400. //核销商品过期处理
  401. D('Home/Salesroom')->hexiao_goods_expire();
  402. //预售发送消息处理 + 预售过期处理
  403. D('Home/PresaleGoods')->cronPresaleMsg();
  404. echo 'ok';
  405. die();
  406. }
  407. public function close()
  408. {
  409. ignore_user_abort();
  410. set_time_limit(0);
  411. $redis_new_redis = D('Home/Front')->get_config_by_name('redis_new_redis');
  412. $open_redis_server = D('Home/Front')->get_config_by_name('open_redis_server');
  413. if( empty($redis_new_redis) && !empty($open_redis_server) && $open_redis_server == 1 )
  414. {
  415. D('Seller/Redisorder')->sysnc_allgoods_total();
  416. }
  417. $shop_list = M('lionfish_comshop_config')->field('value')->where( array('name' => 'open_auto_delete') )->select();
  418. foreach($shop_list as $shop)
  419. {
  420. $open_auto_delete = $shop['value'];
  421. if($open_auto_delete == 1)
  422. {
  423. //auto_cancle_order_time
  424. $cancle_hour = D('Home/Front')->get_config_by_name('auto_cancle_order_time');
  425. $cancle_hour_time = time() - 3600 * $cancle_hour;
  426. $sql = "select order_id from ".C('DB_PREFIX')."lionfish_comshop_order
  427. where date_added <={$cancle_hour_time} and order_status_id =3 ";
  428. $order_list = M()->query($sql);
  429. foreach($order_list as $order )
  430. {
  431. D('Home/Frontorder')->cancel_order($order['order_id'], true);
  432. }
  433. }
  434. }
  435. }
  436. public function templatemsg()
  437. {
  438. $template_list = M('lionfish_comshop_templatemsg')->where( array('state' => 0) )->order('id asc')->limit( 100 )->select();
  439. foreach($template_list as $template)
  440. {
  441. if( $template['type'] == 0 )
  442. {
  443. //发给个人
  444. $url = D('Home/Front')->get_config_by_name('shop_domain');
  445. $wx_template_data = array();
  446. $wx_template_data = unserialize($template['template_data']);
  447. $template_id = $template['template_id'];
  448. $pagepath = substr($template['url'],1);
  449. $member_info = M('lionfish_comshop_member')->field('member_id,we_openid')->where( array('we_openid' => $template['open_id'] ) )->find();
  450. $member_formid_info = M('lionfish_comshop_member_formid')->where( " member_id=".$member_info['member_id']." and formid != '' and state = 0 " )->order('id desc')->find();
  451. if( !empty($member_formid_info) )
  452. {
  453. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  454. $weixin_template_pay_order = D('Home/Front')->get_config_by_name('weixin_template_pay_order');
  455. $res = D('Seller/User')->send_wxtemplate_msg($wx_template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'],0,array() );
  456. M('lionfish_comshop_member_formid')->where( array('id' => $member_formid_info['id'] ) )->save( array('state' => 1) );
  457. }
  458. M('lionfish_comshop_templatemsg')->where( array('id' => $template['id'] ) )->save( array('state' => 1) );
  459. }else if( $template['type'] == 1 )
  460. {
  461. //发送给所有人
  462. $offset = $template['send_total_count'];
  463. $limit = 50;
  464. $member_info_list = M('lionfish_comshop_member')->field('member_id ,we_openid')->order('member_id asc')->limit( $offset,$limit )->select();
  465. $url = D('Home/Front')->get_config_by_name('shop_domain');
  466. $wx_template_data = array();
  467. $wx_template_data = unserialize($template['template_data']);
  468. $template_id = $template['template_id'];
  469. $pagepath = substr($template['url'],1);
  470. foreach($member_info_list as $member_info )
  471. {
  472. $member_formid_info = M('lionfish_comshop_member_formid')->where( " member_id=".$member_info['member_id']." and formid != '' and state = 0 " )->order('id desc')->find();
  473. if( !empty($member_formid_info) )
  474. {
  475. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  476. $weixin_template_pay_order = D('Home/Front')->get_config_by_name('weixin_template_pay_order');
  477. $res = D('Seller/User')->send_wxtemplate_msg($wx_template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'],0,array() );
  478. M('lionfish_comshop_member_formid')->where( array('id' => $member_formid_info['id'] ) )->save( array('state' => 1) );
  479. }
  480. }
  481. $new_f = $offset+$limit;
  482. M('lionfish_comshop_templatemsg')->where( array('id' => $template['id'] ) )->save( array('send_total_count' => $new_f ) );
  483. if( $offset+$limit >= $template['total_count'] )
  484. {
  485. M('lionfish_comshop_templatemsg')->where( array('id' => $template['id'] ) )->save( array('state' => 1) );
  486. }
  487. }
  488. /** ---end--- **/
  489. echo 'success <br/>';
  490. }
  491. echo 'ok';
  492. die();
  493. }
  494. }