UpdateReferralOrder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lytian
  5. * Date: 2019/5/16
  6. * Time: 15:07
  7. */
  8. namespace app\admin\command;
  9. use app\common\library\Redis;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\Output;
  13. use think\Db;
  14. use think\Log;
  15. use think\Request;
  16. class UpdateReferralOrder extends Command
  17. {
  18. protected $message = '';
  19. protected function configure()
  20. {
  21. $this->setName('UpdateReferralOrder')
  22. ->setDescription('刷新推广链接表订单数');
  23. }
  24. //获取公众号
  25. protected function execute(Input $input, Output $output)
  26. {
  27. //$output->writeln("UpdateReferralOrder---开始");
  28. Log::info("UpdateReferralOrder---开始");
  29. Request::instance()->module('admin');
  30. try {
  31. //拉取订单
  32. $redis = Redis::instance();
  33. $key = 'RFUO';
  34. //$redis->set($key, 0);
  35. while (true) {
  36. $lastId = (int)$redis->get($key);
  37. $count = Db::table("referral")->where('id', '>', $lastId)->where('money', '>', 0)->count();
  38. if ($count < 1) {
  39. break;
  40. } else {
  41. $max = ceil($count/100);
  42. for($i = 0; $i < $max; $i++) {
  43. $lastId = (int)$redis->get($key);
  44. $rows = Db::table('referral')->field('id')->where('id', '>', $lastId)->where('money','>',0)->order('id', 'asc')->limit(100)->select();
  45. $referrids = array_column($rows, 'id');
  46. $referridsStr = implode(',', $referrids);
  47. $sql = "select referral_id, count(id) as orders_num from (select id, referral_id from orders where referral_id in (".$referridsStr.") and state = '1' and deduct = 0 union select id, referral_id_permanent as referral_id from orders where referral_id_permanent in (".$referridsStr.") and state = '1' and deduct = 0) as a group by referral_id";
  48. $datas = Db::query($sql);
  49. $referrid = 0;
  50. foreach ($datas as $data) {
  51. //查询订单数
  52. $referrid = $data['referral_id'];
  53. $orderNums = (int)$data['orders_num'];
  54. $sql = "update referral set orders_num= ".$orderNums." where id =".$referrid;
  55. Db::execute($sql);
  56. }
  57. $redis->set($key, $referrid);
  58. unset($rows);
  59. unset($datas);
  60. }
  61. }
  62. }
  63. Redis::instance()->del($key);
  64. Log::info("UpdateReferralOrder---脚本执行成功 end");
  65. //$output->writeln('UpdateReferralOrder---脚本执行成功 end');
  66. } catch (\Exception $e) {
  67. //$output->writeln('UpdateReferralOrder---脚本执行失败' . $e->getMessage());
  68. Log::error('UpdateReferralOrder---脚本执行成功' . $e->getMessage());
  69. }
  70. }
  71. }