AutoUpdateData.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\command;
  4. use app\common\model\BoxModel;
  5. use app\common\model\MoneyLogModel;
  6. use app\common\model\UserModel;
  7. use think\console\Command;
  8. use think\console\Input;
  9. use think\console\Output;
  10. use think\facade\Db;
  11. use utils\RedisCache;
  12. /**
  13. * 自动更新和清除平台基础数据和用户统计 by wes 每天凌晨 2点运行
  14. * Class AutoUpdateData
  15. * @package app\api\command
  16. */
  17. class AutoUpdateData extends Command
  18. {
  19. protected function configure()
  20. {
  21. // 每天刷新用户一些用户信息
  22. $this->setName('auto_update_data')
  23. ->setDescription('the auto_update_data command');
  24. }
  25. /**
  26. * 处理
  27. * @param Input $input
  28. * @param Output $output
  29. * @return int
  30. */
  31. protected function execute(Input $input, Output $output)
  32. {
  33. $cacheKey = "caches:clearData:".date('Ymd');
  34. if(RedisCache::get($cacheKey)){
  35. echo json_encode(['code'=>500,'msg'=>'已经运行过,请不要重复运行','date'=>date('Y-m-d H:i:s')], 256)."\n";
  36. echo json_encode(['code'=>500,'msg'=>'今日已经运行过,请不要重复运行','date'=>date('Y-m-d H:i:s')], 256)."\n";
  37. return false;
  38. }
  39. RedisCache::setnx($cacheKey, date('Y-m-d H:i:s'), 86400);
  40. Db::startTrans();
  41. try {
  42. $this->updateUserData();
  43. Db::commit();
  44. echo json_encode(['code'=>200,'msg'=>'初始化更新数据成功','date'=>date('Y-m-d H:i:s')], 256)."\n";
  45. } catch (\Exception $e) {
  46. Db::rollback();
  47. RedisCache::clear($cacheKey);
  48. echo json_encode(['code'=>500,'msg'=>$e->getMessage(),'date'=>date('Y-m-d H:i:s')], 256)."\n";
  49. }
  50. return true;
  51. }
  52. /**
  53. * 处理数据
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function updateUserData()
  59. {
  60. // 更新用户所有昨日今日数据
  61. $list = MoneyLogModel::where('type', 6)
  62. ->whereDay('create_at', 'yesterday')
  63. ->field(Db::raw('uid,sum(money) as total'))
  64. ->group('uid')
  65. ->select();
  66. $list = $list? $list->toArray() :[];
  67. foreach ($list as $key => $val) {
  68. UserModel::where('id', $val['uid'])->save(['yesterday_money' => $val['total']]);
  69. }
  70. // 盒子数
  71. UserModel::where('today_box', '>', 0)->save(['today_box' => 0]);
  72. UserModel::where('today_team_box', '>', 0)->save(['today_team_box' => 0]);
  73. // 开启下一期 福袋预约
  74. $info = BoxModel::where('status', 1)->order('id desc')->find();
  75. if ($info) {
  76. // 更新旧期数,且只留7天数据
  77. BoxModel::where('status', 2)
  78. ->where('create_time','<',date('Y-m-d H:i:s', time() - 7*86400))
  79. ->delete();;
  80. BoxModel::where('status', 1)->save(['status' => 2]);
  81. $count = $info['buy_most'] * (1 + env('boxsetting.ONCEDAY_ADD_SCALE') / 100);
  82. BoxModel::insert([
  83. 'buy_most' => intval($count / 10) * 10,
  84. 'once_buy' => $info['once_buy'],
  85. 'qi_count' => $info['qi_count'] + 1,
  86. 'time_set' => $info['time_set'],
  87. 'box_img' => $info['box_img'],
  88. 'box_title' => $info['box_title'],
  89. 'create_time' => sr_getcurtime(time()),
  90. 'appoint_day' => sr_getcurtime(time(), 'Y-m-d')
  91. ]);
  92. }
  93. }
  94. }