minutes.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. <?php
  2. /**
  3. * 任务计划 - 分钟执行的任务
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. require_once (BASE_ROOT_PATH . '/helper/message/publisher.php');
  11. require_once(BASE_HELPER_PATH . '/refill/util.php');
  12. require_once(BASE_HELPER_PATH . '/refill/divert_account.php');
  13. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  14. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  15. require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
  16. require_once(BASE_HELPER_PATH . '/task/task_helper.php');
  17. class minutesControl extends BaseCronControl
  18. {
  19. public function indexexOp()
  20. {
  21. $this->_cron_common([1,2,3,4,5,6,7]);
  22. }
  23. public function indexOp()
  24. {
  25. Log::record(__FUNCTION__ . " start",Log::DEBUG);
  26. //未付款订单超期自动关闭
  27. // $this->_order_timeout_cancel();
  28. $this->_cron_common([1,2,3,4,5,6,7]);
  29. $this->_fetch_order_unavaliable();
  30. $this->_check_merchant_alarm_amount();
  31. Log::record(__FUNCTION__ . " end",Log::DEBUG);
  32. // $this->_web_index_update();
  33. // $this->_check_merchant_alarm_amount();
  34. // $this->check_refill_order_limit();
  35. // $this->_cron_mail_send();
  36. // $this->_SendOrderQuery();
  37. }
  38. //任务队列处理
  39. public function admin_taskOp()
  40. {
  41. Log::short_name('task');
  42. Log::record('start task',Log::DEBUG);
  43. $this->exec_task(false);
  44. }
  45. public function update_channel_balanceOp()
  46. {
  47. $balance_getter = function ($provider_id)
  48. {
  49. $provider_model = Model('refill_provider');
  50. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id, 'opened' => 1]);
  51. if (empty($provider_info)) {
  52. return false;
  53. }
  54. $channel_name = $provider_info['name'];
  55. if ($provider_info['type'] == 1) {
  56. $type = 'RefillOil';
  57. } elseif ($provider_info['type'] == 2 || $provider_info['type'] == 3) {
  58. $type = 'RefillPhone';
  59. } else {
  60. $type = 'RefillPhone';
  61. }
  62. Log::record("query balance channel_name:{$channel_name}",Log::DEBUG);
  63. $provider = refill\util::getProvider($channel_name,$type);
  64. if($provider == false) return false;
  65. [$state, $balance] = $provider->balance();
  66. Log::record("query balance channel_name:{$channel_name}, err_msg:{$balance}",Log::DEBUG);
  67. if(!$state) {
  68. return false;
  69. } else {
  70. return round(floatval($balance),4);
  71. }
  72. };
  73. $providers = function ()
  74. {
  75. $pids = [];
  76. $i = 0;
  77. while (true)
  78. {
  79. $start = $i * 1000;
  80. $items = Model()->table('refill_provider')->where(['opened' => 1])->field('*')->order('provider_id desc')->limit("{$start},1000")->select();
  81. if(empty($items)) {
  82. return $pids;
  83. }
  84. $i++;
  85. foreach ($items as $item) {
  86. $pid = intval($item['provider_id']);
  87. $pids[] = $pid;
  88. }
  89. }
  90. };
  91. $mod_provider = Model('refill_provider');
  92. $pids = $providers();
  93. foreach ($pids as $pid)
  94. {
  95. try {
  96. $balance = $balance_getter($pid);
  97. if($balance !== false) {
  98. $mod_provider->editProvider(['balance' => $balance],['provider_id' => $pid]);
  99. }
  100. }
  101. catch (Exception $ex)
  102. {
  103. Log::record($ex->getMessage(),Log::ERR);
  104. }
  105. }
  106. }
  107. private function _fetch_order_unavaliable()
  108. {
  109. try {
  110. $mod_fetch_order = Model('fetch_order');
  111. $trans = new trans_wapper($mod_fetch_order,__METHOD__);
  112. $items = $mod_fetch_order->timeout_orders(900);
  113. foreach ($items as $item) {
  114. $fetch_id = $item['fetch_id'];
  115. $mod_fetch_order->unavaliable($fetch_id);
  116. }
  117. $trans->commit();
  118. foreach ($items as $item) {
  119. $order_sn = $item['order_sn'];
  120. $chname = $item['channel_name'];
  121. refill\util::push_notify($chname,['order_sn' => $order_sn,'state' => 'CANCEL']);
  122. }
  123. }
  124. catch (Exception $ex) {
  125. $trans->rollback();
  126. Log::record(__METHOD__ . " " . $ex->getMessage(),Log::ERR);
  127. }
  128. }
  129. private function _check_merchant_alarm_amount()
  130. {
  131. if(defined('COMPANY_NAME') && COMPANY_NAME === 'XYZ_COMPANY')
  132. {
  133. $mch_cache = rcache("merchant-notify" , 'refill-');
  134. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  135. $new_caches = [];
  136. $merchants = Model('merchant')->getMerchantList(['merchant.merchant_state'=>1],'','',true,'merchant.*,member.available_predeposit' ,"0,1000");
  137. foreach ($merchants as $merchant)
  138. {
  139. $mchid = $merchant['mchid'];
  140. $phones = empty($merchant['warning_phone']) ? [] : unserialize($merchant['warning_phone']);
  141. $available_pd = intval($merchant['available_predeposit']);
  142. $alarm_pd = intval($merchant['alarm_amount']);
  143. if(array_key_exists($mchid,$caches)) {
  144. $mch_cache = $caches[$mchid];
  145. }
  146. else {
  147. $mch_cache = ['last_time' => 0, 'send_count' => 0];
  148. }
  149. if($available_pd < $alarm_pd || $available_pd < 10000)
  150. {
  151. $counts = $mch_cache['send_count'];
  152. if(($mch_cache['last_time'] + 300 < time()) && $counts < 2) {
  153. $mch_cache = ['last_time' => time(), 'send_count' => $counts + 1];
  154. foreach ($phones as $phone) {
  155. if(!empty($phone)){
  156. QueueClient::push('sendSMS', ['mobile'=>$phone,
  157. 'type'=>'balance_warning','datas' => [date("m月d日H时") , $merchant['available_predeposit']]]);
  158. }
  159. }
  160. }
  161. }
  162. else {
  163. $mch_cache = ['last_time' => 0, 'send_count' => 0];
  164. }
  165. $new_caches[$mchid] = $mch_cache;
  166. }
  167. wcache("merchant-notify", ['data' => serialize($new_caches)], 'refill-');
  168. }
  169. }
  170. public function check_refill_order_limit()
  171. {
  172. $mch_cache = rcache("storge_limit" , 'merchant-');
  173. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  174. $reader = function ($mchid,$time)
  175. {
  176. $cond['mchid'] = $mchid;
  177. $cond['inner_status'] = 0;
  178. $cond['order_state'] = ORDER_STATE_SUCCESS;
  179. $cond['refill_order.order_time'] = ['egt', $time];
  180. $items = Model('')->table('refill_order,vr_order')->join('inner')
  181. ->on('refill_order.order_id=vr_order.order_id')
  182. ->field('refill_order.mchid,refill_order.card_type,refill_order.refill_amount,count(*) as num')
  183. ->group('refill_order.card_type,refill_order.refill_amount')
  184. ->where($cond)->select();
  185. return $items;
  186. };
  187. $merger = function ($limits,$reals,$mchid,$card_type)
  188. {
  189. $result = [];
  190. foreach ($limits as $amount => $limit)
  191. {
  192. $limit = intval($limit);
  193. if($limit === -1) {
  194. $allow = true;
  195. }
  196. elseif($limit === 0) {
  197. $allow = false;
  198. }
  199. else
  200. {
  201. $finded = false;
  202. foreach ($reals as $item)
  203. {
  204. $tmp = intval($item['refill_amount'] + 0.005);
  205. if($item['card_type'] == $card_type && $tmp == $amount) {
  206. $num = $item['num'];
  207. $allow = $limit > $num;
  208. $finded = true;
  209. break;
  210. }
  211. }
  212. if(!$finded) {
  213. $allow = true;
  214. }
  215. }
  216. $result["{$mchid}-{$card_type}-{$amount}"] = $allow;
  217. }
  218. return $result;
  219. };
  220. $type_map = ['petrochina' => 1,'sinopec' => 2];
  221. $abilitys = [];
  222. foreach ($caches as $cache)
  223. {
  224. $mchid = intval($cache['mchid']);
  225. $start = $cache['time'];
  226. $petros = $cache['petrochina'];
  227. $sinos = $cache['sinopec'];
  228. $items = $reader($mchid,$start);
  229. $prets = $merger($petros,$items,$mchid,$type_map['petrochina']);
  230. $srets = $merger($sinos,$items,$mchid,$type_map['sinopec']);
  231. foreach ($prets as $key => $val) {
  232. $abilitys[$key] = $val;
  233. }
  234. foreach ($srets as $key => $val) {
  235. $abilitys[$key] = $val;
  236. }
  237. }
  238. $old = rcache("refill_able",'merchant-');
  239. $old = empty($old) ? "" : $old['data'];
  240. ksort($abilitys);
  241. $new = serialize($abilitys);
  242. if($new != $old) {
  243. wcache("refill_able" , ['data' => $new] , 'merchant-');
  244. // $publisher = new message\publisher();
  245. // $publisher->modify_refill_merchant();
  246. }
  247. }
  248. /**
  249. * 未付款订单超期自动关闭
  250. */
  251. private function _order_timeout_cancel()
  252. {
  253. Log::record(__FUNCTION__,Log::DEBUG);
  254. //实物订单超期未支付系统自动关闭
  255. $_break = false;
  256. $model_order = Model('order');
  257. $logic_order = Logic('order');
  258. $condition = [];
  259. $condition['order_state'] = ORDER_STATE_NEW;
  260. $condition['add_time'] = ['lt',time() - ORDER_AUTO_CANCEL_DAY * 86400];
  261. //分批,每批处理100个订单,最多处理5W个订单
  262. for ($i = 0; $i < 500; $i++)
  263. {
  264. if ($_break) {
  265. break;
  266. }
  267. $order_list = $model_order->getOrderList($condition, '', '*', '', 100);
  268. if (empty($order_list)) break;
  269. foreach ($order_list as $order_info)
  270. {
  271. Log::record("1",Log::DEBUG);
  272. $result = $logic_order->changeOrderStateCancel($order_info,'system','系统','超期未支付系统自动关闭订单',true,false);
  273. Log::record("2",Log::DEBUG);
  274. if (!$result['state']) {
  275. $this->log('实物订单超期未支付关闭失败SN:'.$order_info['order_sn']); $_break = true; break;
  276. } else {
  277. Log::record("3",Log::DEBUG);
  278. account_helper::onPredeposit('order_cancel',$order_info['buyer_id'],$order_info['order_sn']);
  279. Log::record("4",Log::DEBUG);
  280. }
  281. }
  282. }
  283. //虚拟订单超期未支付系统自动关闭
  284. $_break = false;
  285. $model_vr_order = Model('vr_order');
  286. $logic_vr_order = Logic('vr_order');
  287. $condition = [];
  288. $condition['order_state'] = ORDER_STATE_NEW;
  289. $condition['add_time'] = ['lt',time() - VRORDER_AUTO_CANCEL_MINUTE * 60];
  290. //分批,每批处理100个订单,最多处理5W个订单
  291. for ($i = 0; $i < 500; $i++)
  292. {
  293. if ($_break) {
  294. break;
  295. }
  296. $order_list = $model_vr_order->getOrderList($condition, '', '*', '',100);
  297. if (empty($order_list)) break;
  298. foreach ($order_list as $order_info) {
  299. $result = $logic_vr_order->changeOrderStateCancel($order_info,'system','超期未支付系统自动关闭订单',false);
  300. }
  301. if (!$result['state']) {
  302. $this->log('虚拟订单超期未支付关闭失败SN:'.$order_info['order_sn']);
  303. $_break = true;
  304. }
  305. }
  306. }
  307. /**
  308. * 更新首页的商品价格信息
  309. */
  310. private function _web_index_update()
  311. {
  312. Model('web_config')->updateWebGoods();
  313. }
  314. /**
  315. * 发送邮件消息
  316. */
  317. private function _cron_mail_send()
  318. {
  319. //每次发送数量
  320. $_num = 50;
  321. $model_storemsgcron = Model('mail_cron');
  322. $cron_array = $model_storemsgcron->getMailCronList([], $_num);
  323. if (!empty($cron_array))
  324. {
  325. $email = new Email();
  326. $mail_array = [];
  327. foreach ($cron_array as $val)
  328. {
  329. $return = $email->send_sys_email($val['mail'],$val['subject'],$val['contnet']);
  330. if ($return) {
  331. // 记录需要删除的id
  332. $mail_array[] = $val['mail_id'];
  333. }
  334. }
  335. // 删除已发送的记录
  336. $model_storemsgcron->delMailCron(['mail_id' => ['in', $mail_array]]);
  337. }
  338. }
  339. public function second_queueOp()
  340. {
  341. while (true)
  342. {
  343. do{
  344. try {
  345. $ret = $this->_cron_common([8]);
  346. perfor_clear();
  347. } catch (Exception $ex) {
  348. Log::record($ex->getMessage(),Log::ERR);
  349. }
  350. }
  351. while($ret === true);
  352. sleep(1);
  353. }
  354. }
  355. public function account_editOp()
  356. {
  357. $stamp = 0;
  358. while (true)
  359. {
  360. try {
  361. $divertor = new refill\divert_account();
  362. $next = time();
  363. $divertor->update_account($stamp);
  364. $stamp = $next;
  365. } catch (Exception $ex) {
  366. Log::record($ex->getMessage(),Log::ERR);
  367. }
  368. sleep(1);
  369. }
  370. }
  371. private function exec_task($merchant)
  372. {
  373. $task_manager = new task\manager();
  374. $task_id = 0;
  375. while (true)
  376. {
  377. try
  378. {
  379. $task = $task_manager->pop_task($task_id,$merchant);
  380. if (!empty($task)) {
  381. $task_manager->handle($task);
  382. $task_id = $task['task_id'];
  383. }
  384. }
  385. catch (Exception $ex) {
  386. Log::record($ex->getMessage(),Log::ERR);
  387. }
  388. sleep(1);
  389. }
  390. }
  391. //任务队列处理
  392. public function merchant_taskOp()
  393. {
  394. Log::short_name('merchant_task');
  395. Log::record('start task',Log::DEBUG);
  396. $this->exec_task(true);
  397. }
  398. //更新统计业务数据
  399. public function stat_utilOp()
  400. {
  401. Log::short_name('stat_util');
  402. while (true)
  403. {
  404. try {
  405. $this->_update_earlist_send();
  406. $this->_calc_arrears();
  407. } catch (Exception $ex) {
  408. Log::record($ex->getMessage(), Log::ERR);
  409. }
  410. sleep(1);
  411. }
  412. }
  413. private function _update_earlist_send()
  414. {
  415. //查找最早的充值中的订单时间
  416. // $update_earliest_ordertime = function () {
  417. // $mod = Model('refill_detail');
  418. // $time = $mod->getEarliestSendTime();
  419. // wcache('stat-info', ['earliest_order_time' => $time], 'refill-');
  420. // };
  421. // $update_earliest_ordertime();
  422. //按各个机构,更新最早的充值中的订单时间
  423. $update_earliest_ordertime_bymerchant = function () {
  424. $mod = Model('refill_detail');
  425. $mtimes = $mod->getEarliestSendTimeByMerchant();
  426. $val = json_encode($mtimes);
  427. wkcachex('stat-earliest-ordertime', $val, 'refill-');
  428. };
  429. $update_earliest_ordertime_bymerchant();
  430. }
  431. private function _calc_arrears()
  432. {
  433. $send_amount_reader = function () {
  434. $val = rkcachex('stat-merchant-sendamount', 'refill-');
  435. $val = json_decode($val, true);
  436. return $val;
  437. };
  438. $del_stop = function ($amounts)
  439. {
  440. $ins = Cache::getInstance('cacheredis');
  441. $items = $ins->hkeys('merchant-debts-detail','refill-');
  442. if(empty($items)) {
  443. return;
  444. }
  445. $keys = [];
  446. foreach ($items as $key) {
  447. $keys[] = intval($key);
  448. }
  449. foreach ($keys as $mchid)
  450. {
  451. if(array_key_exists($mchid,$amounts)) {
  452. continue;
  453. } else {
  454. $ins->hdel('merchant-debts-detail','refill-',$mchid);
  455. }
  456. }
  457. };
  458. $debts_detail_fun = function ($amounts)
  459. {
  460. $debts_detail = [];
  461. $mod = Model();
  462. $i = 0;
  463. while (true)
  464. {
  465. $start = $i * 1000;
  466. $items = $mod->table('merchant,member')->join('inner')->on('member.member_id=merchant.admin_id')
  467. ->field('mchid,credit_bonus,co_id,available_predeposit')->where(['merchant_state' => 1, 'co_id' => ['gt', 0]])->order('mchid asc')->limit("{$start},1000")->select();
  468. if(empty($items)) {
  469. break;
  470. }
  471. $i++;
  472. foreach ($items as $item)
  473. {
  474. $mchid = intval($item['mchid']);
  475. $co_id = intval($item['co_id']);
  476. $credit_bonus = round(floatval($item['credit_bonus']), 2);
  477. $available_predeposit = round(floatval($item['available_predeposit']), 2);
  478. if (array_key_exists($mchid, $amounts)) {
  479. $send_amounts = $amounts[$mchid]['send_amounts'];
  480. $lack_amounts = $amounts[$mchid]['lack_amounts'];
  481. } else {
  482. $send_amounts = 0;
  483. $lack_amounts = 0;
  484. }
  485. Log::record("co_id={$co_id} mchid={$mchid} send_amounts={$send_amounts} lack_amounts={$lack_amounts}",Log::DEBUG);
  486. $available = $available_predeposit - $credit_bonus + $send_amounts;// - $lack_amounts;
  487. $debt = -$available;
  488. $debts_detail[$co_id][$mchid] = $debt;
  489. $val = ['debt' => $debt, 'send_amounts' => $send_amounts, 'lack_amounts' => $lack_amounts];
  490. wcache('merchant-debts-detail', [$mchid => serialize($val)], 'refill-');
  491. }
  492. }
  493. return $debts_detail;
  494. };
  495. $company_debt_fun = function ()
  496. {
  497. $co_debts = [];
  498. $mod = Model('refill_company');
  499. $i = 0;
  500. while (true)
  501. {
  502. $start = $i * 1000;
  503. $items = $mod->field('co_id,max_debt')->where(['opened' => 1, 'co_type' => 'merchant'])->order('co_id asc')->limit("{$start},1000")->select();
  504. if(empty($items)) {
  505. break;
  506. }
  507. $i++;
  508. foreach ($items as $item)
  509. {
  510. $co_id = intval($item['co_id']);
  511. $max_debt = round(floatval($item['max_debt']), 2);
  512. $co_debts[$co_id] = $max_debt;
  513. }
  514. }
  515. return $co_debts;
  516. };
  517. $calc_debts = function ($co_id,$detail)
  518. {
  519. if(!array_key_exists($co_id,$detail)) {
  520. return [[], 0];
  521. }
  522. $mchids=[];
  523. $sum_debts = 0;
  524. $co_details = $detail[$co_id];
  525. foreach ($co_details as $mchid => $debts) {
  526. $mchids[] = $mchid;
  527. $sum_debts += $debts;
  528. }
  529. return [$mchids,$sum_debts];
  530. };
  531. $judge_stop = function ($co_debts, $debts_detail) use ($calc_debts)
  532. {
  533. foreach ($co_debts as $co_id => $max_debts)
  534. {
  535. [$mchids, $sum_debts] = $calc_debts($co_id,$debts_detail);
  536. wcache('company-debts', [$co_id => $sum_debts], 'refill-');
  537. if(empty($mchids)) continue;
  538. if($max_debts < $sum_debts) {
  539. $fStop = 1;
  540. } else {
  541. $fStop = 0;
  542. }
  543. foreach ($mchids as $mchid) {
  544. wcache('merchant-debt-judge', [$mchid => $fStop], 'refill-');
  545. }
  546. }
  547. };
  548. $need_calc = function ($clac_time)
  549. {
  550. static $latest_time = 0;
  551. if($clac_time > $latest_time) {
  552. $latest_time = $clac_time;
  553. return true;
  554. } else {
  555. return false;
  556. }
  557. };
  558. $result = $send_amount_reader();
  559. $del_stop($result);
  560. if (!empty($result) && $need_calc(intval($result['time']))) {
  561. $mch_amounts = $result['send_amounts'];
  562. $debts_detail = $debts_detail_fun($mch_amounts);
  563. $co_debts = $company_debt_fun();
  564. $judge_stop($co_debts, $debts_detail);
  565. }
  566. }
  567. /**
  568. * 执行通用任务
  569. */
  570. private function _cron_common($types)
  571. {
  572. Log::record(__FUNCTION__,Log::DEBUG);
  573. //查找待执行任务
  574. $model_cron = Model('cron');
  575. $cron = $model_cron->getCronList(['exetime'=> ['elt',time()],'type' => ['in',$types]]);
  576. if (!is_array($cron)) return false;
  577. $count = count($cron);
  578. if ($count <= 0) return false;
  579. Log::record("match cron count={$count}",Log::DEBUG);
  580. $cron_array = [];
  581. $cronid = [];
  582. $exeid = 1;
  583. foreach ($cron as $v)
  584. {
  585. $type = intval($v['type']);
  586. if($type == 8) {
  587. $cron_array[$v['type']][$exeid] = $v;
  588. $exeid++;
  589. }
  590. else {
  591. $cron_array[$v['type']][$v['exeid']] = $v;
  592. }
  593. }
  594. foreach ($cron_array as $k=>$v)
  595. {
  596. // 如果方法不存是,直接删除id
  597. if (!method_exists($this,'_cron_'.$k)) {
  598. $tmp = current($v);
  599. $cronid[] = $tmp['id'];
  600. continue;
  601. }
  602. $method = '_cron_'.$k;
  603. Log::record("crontab minutest:{$method}",Log::DEBUG);
  604. $result = call_user_func_array([$this, '_cron_' . $k], [$v]);
  605. if (is_array($result)) {
  606. $cronid = array_merge($cronid, $result);
  607. } else {
  608. $method = '_cron_' . $k;
  609. Log::record("crontab minutest err:{$method}", Log::ERR);
  610. }
  611. }
  612. //删除执行完成的cron信息
  613. if (!empty($cronid) && is_array($cronid)) {
  614. $model_cron->delCron(['id' => ['in', $cronid]]);
  615. }
  616. return true;
  617. }
  618. //'任务类型 1商品上架 2根据商品id更新商品促销价格 3优惠套装过期 4推荐展位过期 5团购开始更新商品促销价格 6团购过期 7限时折扣过期',
  619. //1商品上架
  620. private function _cron_1($cron = array())
  621. {
  622. $condition = ['goods_commonid' => ['in',array_keys($cron)]];
  623. $update = Model('goods')->editProducesOnline($condition);
  624. if ($update)
  625. {
  626. //返回执行成功的cronid
  627. $cronid = [];
  628. foreach ($cron as $v) {
  629. $cronid[] = $v['id'];
  630. }
  631. }
  632. else {
  633. return false;
  634. }
  635. return $cronid;
  636. }
  637. //2根据商品id更新商品促销价格
  638. private function _cron_2($cron = array())
  639. {
  640. $condition = ['goods_id' => ['in',array_keys($cron)]];
  641. $update = Model('goods')->editGoodsPromotionPrice($condition);
  642. if ($update){
  643. //返回执行成功的cronid
  644. $cronid = [];
  645. foreach ($cron as $v) {
  646. $cronid[] = $v['id'];
  647. }
  648. }else{
  649. return false;
  650. }
  651. return $cronid;
  652. }
  653. //3优惠套装过期
  654. private function _cron_3($cron = [])
  655. {
  656. $condition = ['store_id' => ['in', array_keys($cron)]];
  657. $update = Model('p_bundling')->editBundlingQuotaClose($condition);
  658. if ($update) {
  659. //返回执行成功的cronid
  660. $cronid = [];
  661. foreach ($cron as $v) {
  662. $cronid[] = $v['id'];
  663. }
  664. } else {
  665. return false;
  666. }
  667. return $cronid;
  668. }
  669. //4推荐展位过期
  670. private function _cron_4($cron = [])
  671. {
  672. $condition = array('store_id' => array('in', array_keys($cron)));
  673. $update = Model('p_booth')->editBoothClose($condition);
  674. if ($update) {
  675. //返回执行成功的cronid
  676. $cronid = array();
  677. foreach ($cron as $v) {
  678. $cronid[] = $v['id'];
  679. }
  680. } else {
  681. return false;
  682. }
  683. return $cronid;
  684. }
  685. //5团购开始更新商品促销价格
  686. private function _cron_5($cron = array())
  687. {
  688. $condition = [];
  689. $condition['goods_commonid'] = ['in', array_keys($cron)];
  690. $condition['start_time'] = ['lt', time()];
  691. $condition['end_time'] = ['gt', time()];
  692. $groupbuy = Model('groupbuy')->getGroupbuyList($condition);
  693. foreach ($groupbuy as $val) {
  694. Model('goods')->editGoods(['goods_promotion_price' => $val['groupbuy_price'], 'goods_promotion_type' => 1],
  695. ['goods_commonid' => $val['goods_commonid']]);
  696. }
  697. //返回执行成功的cronid
  698. $cronid = array();
  699. foreach ($cron as $v) {
  700. $cronid[] = $v['id'];
  701. }
  702. return $cronid;
  703. }
  704. /**
  705. * 抢购过期
  706. *
  707. * @param array $cron
  708. */
  709. private function _cron_6($cron = array())
  710. {
  711. $condition = ['goods_commonid' => ['in', array_keys($cron)]];
  712. //抢购活动过期
  713. $update = Model('groupbuy')->editExpireGroupbuy($condition);
  714. if ($update){
  715. //返回执行成功的cronid
  716. $cronid = [];
  717. foreach ($cron as $v) {
  718. $cronid[] = $v['id'];
  719. }
  720. }else{
  721. return false;
  722. }
  723. return $cronid;
  724. }
  725. /**
  726. * 限时折扣过期
  727. *
  728. * @param array $cron
  729. */
  730. private function _cron_7($cron = array())
  731. {
  732. $condition = array('xianshi_id' => array('in', array_keys($cron)));
  733. //限时折扣过期
  734. $update = Model('p_xianshi')->editExpireXianshi($condition);
  735. if ($update){
  736. //返回执行成功的cronid
  737. $cronid = array();
  738. foreach ($cron as $v) {
  739. $cronid[] = $v['id'];
  740. }
  741. }
  742. else{
  743. return false;
  744. }
  745. return $cronid;
  746. }
  747. private function _cron_8($cron = array())
  748. {
  749. $cronid = [];
  750. foreach ($cron as $v)
  751. {
  752. $cronid[] = intval($v['id']);
  753. $params = $v['params'];
  754. if(!empty($params))
  755. {
  756. $params = unserialize($params);
  757. if(is_array($params))
  758. {
  759. foreach ($params as $key => $value) {
  760. Log::record("delay queue: {$key}",Log::DEBUG);
  761. QueueClient::push($key,$value);
  762. }
  763. }
  764. }
  765. }
  766. return $cronid;
  767. }
  768. //查询超过5分钟的充值中订单
  769. public function _SendOrderQuery()
  770. {
  771. $model_refill_order = Model('refill_order');
  772. $condition['order_state'] = ORDER_STATE_SEND;
  773. $condition['refill_order.order_time'] = ['lt', (time() - 300)];
  774. $orders = $model_refill_order->getMerchantOrderList($condition, 1000, 0,'refill_order.order_id');
  775. if(!empty($orders)) {
  776. foreach ($orders as $order) {
  777. $order_id = $order['order_id'];
  778. QueueClient::push("QueryRefillState",['order_id' => $order_id]);
  779. }
  780. }
  781. }
  782. //
  783. public function _refill_task(): bool
  784. {
  785. Log::record(__FUNCTION__,Log::DEBUG);
  786. $model_refill_task = Model('refill_task');
  787. $task = $model_refill_task->UnDispose('order_export');
  788. if (empty($task)) return false;
  789. $task_id = $task['task_id'];
  790. $task_condition = unserialize($task['task_condition']);
  791. if (empty($task_condition)) {
  792. Log::record("refill task condition unserialize err, task_id:{$task_id}");
  793. return false;
  794. }
  795. $type = $task['type'];
  796. $cid = $task['cid'];
  797. if ($type === 'merchant') {
  798. $condition['refill_order.mchid'] = $cid;
  799. } elseif ($type === 'provider') {
  800. $condition['vr_order.store_id'] = $cid;
  801. } else {
  802. Log::record("refill task type err, task_id:{$task_id}");
  803. return false;
  804. }
  805. $order_time_type = $task_condition['order_time_type'];
  806. $start_unixtime = $task_condition['start_unixtime'];
  807. $end_unixtime = $task_condition['end_unixtime'];
  808. $order_state = $task_condition['order_state'];
  809. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  810. $condition["refill_order.{$order_time_type}"] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  811. $condition['vr_order.add_time'] = ['egt', $start_unixtime-1];
  812. } elseif ($start_unixtime > 0) {
  813. $condition["refill_order.{$order_time_type}"] = ['egt', $start_unixtime];
  814. $condition['vr_order.add_time'] = ['egt', $start_unixtime-1];
  815. }
  816. if (in_array($order_state, ['0', '10', '20', '30', '40'], true)) {
  817. $condition['order_state'] = $order_state;
  818. }
  819. $model_refill_task->Dispose($task_id);
  820. [$state, $task_result] = $this->order_export($condition, $type, $task_id);
  821. if ($state) {
  822. $model_refill_task->DisposeFinish($task_id, $task_result);
  823. } else {
  824. $model_refill_task->DisposeErr($task_id, $task_result);
  825. }
  826. return true;
  827. }
  828. /**
  829. * @throws PHPExcel_Exception
  830. */
  831. private function order_export($condition, $type, $task_id)
  832. {
  833. $orders = Model('refill_order')->getAllOrders($condition);
  834. if(empty($orders)) {
  835. return [false, '统计数据为空'];
  836. }
  837. $orders = $this->orderFormat($orders);
  838. $objPHPExcel = new PHPExcel();
  839. if ($type === 'merchant') {
  840. $column_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
  841. $column_values = ['商户号', '客户订单号', '平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额'];
  842. $data_keys = ['mchid', 'mch_order', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'mch_amount'];
  843. } elseif ($type === 'provider') {
  844. $column_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'];
  845. $column_values = ['上游订单号', '平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额'];
  846. $data_keys = ['ch_trade_no', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'channel_amount'];
  847. } else {
  848. return [false, '主体类型错误'];
  849. }
  850. $objPHPExcel->setActiveSheetIndex(0);
  851. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  852. foreach ($column_keys as $key => $column_key) {
  853. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(25);
  854. $cell_value = $column_key . 1;
  855. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  856. }
  857. foreach ($orders as $k => $order) {
  858. foreach ($column_keys as $key => $column_key) {
  859. $field = $column_key.($k+2);
  860. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]],
  861. PHPExcel_Cell_DataType::TYPE_STRING);
  862. }
  863. }
  864. try {
  865. $path = BASE_ROOT_PATH . "/data/upload/task/";
  866. if(!is_dir($path)){
  867. mkdir($path, 0755);
  868. }
  869. $filename = date('YmdHis',time())."-任务导出-任务ID:{$task_id}.xlsx";
  870. $file_path = $path . $filename;
  871. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  872. $objWriter->save($file_path);
  873. return [true, $filename];
  874. } catch (Exception $e) {
  875. return [false, $e->getMessage()];
  876. }
  877. }
  878. private function orderFormat($orders): array
  879. {
  880. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  881. $orderState = function ($order_info) {
  882. $is_retrying = $order_info['is_retrying'];
  883. switch ($order_info['order_state']) {
  884. case ORDER_STATE_CANCEL:
  885. if ($is_retrying) {
  886. $order_state = '重试中';
  887. } else {
  888. $order_state = '已取消';
  889. }
  890. break;
  891. case ORDER_STATE_NEW:
  892. $order_state = '待付款';
  893. break;
  894. case ORDER_STATE_PAY:
  895. $order_state = '待发货';
  896. break;
  897. case ORDER_STATE_SEND:
  898. $order_state = '待收货';
  899. break;
  900. case ORDER_STATE_SUCCESS:
  901. $order_state = '交易完成';
  902. break;
  903. default :
  904. $order_state = '未知状态';
  905. }
  906. return $order_state;
  907. };
  908. foreach($orders as $key => $order)
  909. {
  910. $orders[$key]['card_type_text'] = $card_type_texts[$order['card_type']];
  911. $orders[$key]['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  912. $orders[$key]['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  913. $orders[$key]['order_state_text'] = $orderState($order);
  914. }
  915. return $orders;
  916. }
  917. }