minutes.php 34 KB

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