CronController.class.php 20 KB

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