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') ]); } } }