| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- declare (strict_types = 1);
- namespace app\api\command;
- 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 每天晚上21点后运行1次
- * Class SettleUserTodayAward
- * @package app\api\command
- */
- class SettleUserTodayAward extends Command
- {
- protected function configure()
- {
- // 获取用户今日奖金
- $this->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)
- {
- $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;
- }
- }
|