AutoUpdateData.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. return false;
  37. }
  38. RedisCache::setnx($cacheKey, date('Y-m-d H:i:s'), 86400);
  39. Db::startTrans();
  40. try {
  41. $this->updateUserData();
  42. Db::commit();
  43. echo json_encode(['code'=>200,'msg'=>'初始化更新数据成功','date'=>date('Y-m-d H:i:s')], 256)."\n";
  44. } catch (\Exception $e) {
  45. Db::rollback();
  46. RedisCache::clear($cacheKey);
  47. echo json_encode(['code'=>500,'msg'=>$e->getMessage(),'date'=>date('Y-m-d H:i:s')], 256)."\n";
  48. }
  49. return true;
  50. }
  51. /**
  52. * 处理数据
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function updateUserData()
  58. {
  59. // 更新用户所有昨日今日数据
  60. $list = MoneyLogModel::where('type', 6)
  61. ->whereDay('create_at', 'yesterday')
  62. ->field(Db::raw('uid,sum(money) as total'))
  63. ->group('uid')
  64. ->select();
  65. $list = $list? $list->toArray() :[];
  66. foreach ($list as $key => $val) {
  67. UserModel::where('id', $val['uid'])->save(['yesterday_money' => $val['total']]);
  68. }
  69. // 盒子数
  70. UserModel::where('today_box', '>', 0)->save(['today_box' => 0]);
  71. UserModel::where('today_team_box', '>', 0)->save(['today_team_box' => 0]);
  72. // 开启下一期 福袋预约
  73. $info = BoxModel::where('status', 1)->order('id desc')->find();
  74. if ($info) {
  75. // 更新旧期数,且只留7天数据
  76. BoxModel::where('status', 2)
  77. ->where('create_time','<',date('Y-m-d H:i:s', time() - 7*86400))
  78. ->delete();;
  79. BoxModel::where('status', 1)->save(['status' => 2]);
  80. $count = $info['buy_most'] * (1 + env('boxsetting.ONCEDAY_ADD_SCALE') / 100);
  81. BoxModel::insert([
  82. 'buy_most' => intval($count / 10) * 10,
  83. 'once_buy' => $info['once_buy'],
  84. 'qi_count' => $info['qi_count'] + 1,
  85. 'time_set' => $info['time_set'],
  86. 'box_img' => $info['box_img'],
  87. 'box_title' => $info['box_title'],
  88. 'create_time' => sr_getcurtime(time()),
  89. 'appoint_day' => sr_getcurtime(time(), 'Y-m-d')
  90. ]);
  91. }
  92. }
  93. }