setName('settle_user_today_award') ->setDescription('the settle_user_today_award command'); } /** * 今日奖金结算 * @param Input $input * @param Output $output * @return bool */ protected function execute(Input $input, Output $output) { echo json_encode(['code'=>'error','msg'=>'功能暂未开放~','date'=>date('Y-m-d H:i:s')],256)."\n"; return false; $cacheKey = "caches:settleTodayAward:".date('Ymd'); if(RedisCache::get($cacheKey.'_lock')){ echo json_encode(['code'=>500,'msg'=>'今日奖金已经结算过,请不要重复运行','date'=>date('Y-m-d H:i:s')], 256)."\n"; return false; } RedisCache::setnx($cacheKey.'_lock', date('Y-m-d H:i:s'), 86400); Db::startTrans(); try { $list = UserModel::where('today_money', '>', 0)->field('id,money,today_money')->select(); $list = $list? $list->toArray() : []; if(empty($list)){ sr_throw('暂无今日奖金可结算'); } $totalMoney = 0; foreach ($list as $key=>$val){ // 结算奖金到账 $uid = isset($val['id'])? intval($val['id']) : 0; $userMoney = isset($val['money'])? floatval($val['money']) : 0; $todayMoney = isset($val['today_money'])? floatval($val['today_money']) : 0; $totalMoney += $todayMoney; if($uid && !UserModel::where('id',$val['id'])->inc('money', $todayMoney)->inc('todayaward_money', $todayMoney)->update()){ sr_throw("结算用户[{$uid}]今日奖金失败"); } $data = [ 'uid'=>$uid, 'type'=> 6, 'money'=> $todayMoney, 'create_at'=>sr_getcurtime(time()), 'state'=> 1, 'before_money'=> $userMoney, 'after_money'=> floatval($userMoney + $todayMoney), 'from_id'=> 0, 'uid2'=> 0, 'free_type'=> 0, 'remark'=> '今日奖金结算' ]; if(!MoneyLogModel::insertGetId($data)){ sr_throw("用户[{$uid}]今日奖金结算处理失败"); } } // 初始化今日奖金 if(!UserModel::where('today_money', '>', 0)->update(['today_money'=>0,'update_time'=>date('Y-m-d H:i:s')])){ sr_throw("今日奖金清除错误"); } // 初始化今日预约 if(!UserModel::where('total_appoint_count', '>', 0)->update(['total_appoint_count'=>0,'update_time'=>date('Y-m-d H:i:s')])){ sr_throw("今日预约盒子清除错误"); } Db::commit(); $msg = "今日奖金结算成功,累计用户".count($list)."人,累计金额{$totalMoney}元"; RedisCache::set($cacheKey.'_success',['msg'=> $msg,'date'=>date('Y-m-d H:i:s')], 7200); echo json_encode(['code'=>500,'msg'=> $msg,'date'=>date('Y-m-d H:i:s')], 256)."\n"; }catch (\Exception $e){ Db::rollback(); RedisCache::clear($cacheKey.'_lock'); RedisCache::set($cacheKey.'_fail',['msg'=> $e->getMessage(),'trace'=>$e->getTrace()], 7200); echo json_encode(['code'=>500,'msg'=>$e->getMessage(),'date'=>date('Y-m-d H:i:s')], 256)."\n"; } return true; } }