| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- declare (strict_types=1);
- namespace app\api\command;
- use app\common\model\BoxModel;
- use app\common\model\MoneyLogModel;
- use app\common\model\UserModel;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Db;
- use utils\RedisCache;
- /**
- * 自动更新和清除平台基础数据和用户统计 by wes 每天凌晨 2点运行
- * Class AutoUpdateData
- * @package app\api\command
- */
- class AutoUpdateData extends Command
- {
- protected function configure()
- {
- // 每天刷新用户一些用户信息
- $this->setName('auto_update_data')
- ->setDescription('the auto_update_data command');
- }
- /**
- * 处理
- * @param Input $input
- * @param Output $output
- * @return int
- */
- protected function execute(Input $input, Output $output)
- {
- $cacheKey = "caches:clearData:".date('Ymd');
- if(RedisCache::get($cacheKey)){
- echo json_encode(['code'=>500,'msg'=>'已经运行过,请不要重复运行','date'=>date('Y-m-d H:i:s')], 256)."\n";
- echo json_encode(['code'=>500,'msg'=>'今日已经运行过,请不要重复运行','date'=>date('Y-m-d H:i:s')], 256)."\n";
- return false;
- }
- RedisCache::setnx($cacheKey, date('Y-m-d H:i:s'), 86400);
- Db::startTrans();
- try {
- $this->updateUserData();
- Db::commit();
- echo json_encode(['code'=>200,'msg'=>'初始化更新数据成功','date'=>date('Y-m-d H:i:s')], 256)."\n";
- } catch (\Exception $e) {
- Db::rollback();
- RedisCache::clear($cacheKey);
- echo json_encode(['code'=>500,'msg'=>$e->getMessage(),'date'=>date('Y-m-d H:i:s')], 256)."\n";
- }
- return true;
- }
- /**
- * 处理数据
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function updateUserData()
- {
- // 更新用户所有昨日今日数据
- $list = MoneyLogModel::where('type', 6)
- ->whereDay('create_at', 'yesterday')
- ->field(Db::raw('uid,sum(money) as total'))
- ->group('uid')
- ->select();
- $list = $list? $list->toArray() :[];
- foreach ($list as $key => $val) {
- UserModel::where('id', $val['uid'])->save(['yesterday_money' => $val['total']]);
- }
- // 盒子数
- UserModel::where('today_box', '>', 0)->save(['today_box' => 0]);
- UserModel::where('today_team_box', '>', 0)->save(['today_team_box' => 0]);
- // 开启下一期 福袋预约
- $info = BoxModel::where('status', 1)->order('id desc')->find();
- if ($info) {
- // 更新旧期数,且只留7天数据
- BoxModel::where('status', 2)
- ->where('create_time','<',date('Y-m-d H:i:s', time() - 7*86400))
- ->delete();;
- BoxModel::where('status', 1)->save(['status' => 2]);
- $count = $info['buy_most'] * (1 + env('boxsetting.ONCEDAY_ADD_SCALE') / 100);
- BoxModel::insert([
- 'buy_most' => intval($count / 10) * 10,
- 'once_buy' => $info['once_buy'],
- 'qi_count' => $info['qi_count'] + 1,
- 'time_set' => $info['time_set'],
- 'box_img' => $info['box_img'],
- 'box_title' => $info['box_title'],
- 'create_time' => sr_getcurtime(time()),
- 'appoint_day' => sr_getcurtime(time(), 'Y-m-d')
- ]);
- }
- }
- }
|