SettleUserTodayAward.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\api\command;
  4. use app\common\model\MoneyLogModel;
  5. use app\common\model\UserModel;
  6. use think\console\Command;
  7. use think\console\Input;
  8. use think\console\Output;
  9. use think\facade\Db;
  10. use utils\RedisCache;
  11. /**
  12. * 结算今日奖金 by wes 每天晚上21点后运行1次
  13. * Class SettleUserTodayAward
  14. * @package app\api\command
  15. */
  16. class SettleUserTodayAward extends Command
  17. {
  18. protected function configure()
  19. {
  20. // 获取用户今日奖金
  21. $this->setName('settle_user_today_award')
  22. ->setDescription('the settle_user_today_award command');
  23. }
  24. /**
  25. * 今日奖金结算
  26. * @param Input $input
  27. * @param Output $output
  28. * @return bool
  29. */
  30. protected function execute(Input $input, Output $output)
  31. {
  32. echo json_encode(['code'=>'error','msg'=>'功能暂未开放~','date'=>date('Y-m-d H:i:s')],256)."\n";
  33. return false;
  34. $cacheKey = "caches:settleTodayAward:".date('Ymd');
  35. if(RedisCache::get($cacheKey.'_lock')){
  36. echo json_encode(['code'=>500,'msg'=>'今日奖金已经结算过,请不要重复运行','date'=>date('Y-m-d H:i:s')], 256)."\n";
  37. return false;
  38. }
  39. RedisCache::setnx($cacheKey.'_lock', date('Y-m-d H:i:s'), 86400);
  40. Db::startTrans();
  41. try {
  42. $list = UserModel::where('today_money', '>', 0)->field('id,money,today_money')->select();
  43. $list = $list? $list->toArray() : [];
  44. if(empty($list)){
  45. sr_throw('暂无今日奖金可结算');
  46. }
  47. $totalMoney = 0;
  48. foreach ($list as $key=>$val){
  49. // 结算奖金到账
  50. $uid = isset($val['id'])? intval($val['id']) : 0;
  51. $userMoney = isset($val['money'])? floatval($val['money']) : 0;
  52. $todayMoney = isset($val['today_money'])? floatval($val['today_money']) : 0;
  53. $totalMoney += $todayMoney;
  54. if($uid && !UserModel::where('id',$val['id'])->inc('money', $todayMoney)->inc('todayaward_money', $todayMoney)->update()){
  55. sr_throw("结算用户[{$uid}]今日奖金失败");
  56. }
  57. $data = [
  58. 'uid'=>$uid,
  59. 'type'=> 6,
  60. 'money'=> $todayMoney,
  61. 'create_at'=>sr_getcurtime(time()),
  62. 'state'=> 1,
  63. 'before_money'=> $userMoney,
  64. 'after_money'=> floatval($userMoney + $todayMoney),
  65. 'from_id'=> 0,
  66. 'uid2'=> 0,
  67. 'free_type'=> 0,
  68. 'remark'=> '今日奖金结算'
  69. ];
  70. if(!MoneyLogModel::insertGetId($data)){
  71. sr_throw("用户[{$uid}]今日奖金结算处理失败");
  72. }
  73. }
  74. // 初始化今日奖金
  75. if(!UserModel::where('today_money', '>', 0)->update(['today_money'=>0,'update_time'=>date('Y-m-d H:i:s')])){
  76. sr_throw("今日奖金清除错误");
  77. }
  78. // 初始化今日预约
  79. if(!UserModel::where('total_appoint_count', '>', 0)->update(['total_appoint_count'=>0,'update_time'=>date('Y-m-d H:i:s')])){
  80. sr_throw("今日预约盒子清除错误");
  81. }
  82. Db::commit();
  83. $msg = "今日奖金结算成功,累计用户".count($list)."人,累计金额{$totalMoney}元";
  84. RedisCache::set($cacheKey.'_success',['msg'=> $msg,'date'=>date('Y-m-d H:i:s')], 7200);
  85. echo json_encode(['code'=>500,'msg'=> $msg,'date'=>date('Y-m-d H:i:s')], 256)."\n";
  86. }catch (\Exception $e){
  87. Db::rollback();
  88. RedisCache::clear($cacheKey.'_lock');
  89. RedisCache::set($cacheKey.'_fail',['msg'=> $e->getMessage(),'trace'=>$e->getTrace()], 7200);
  90. echo json_encode(['code'=>500,'msg'=>$e->getMessage(),'date'=>date('Y-m-d H:i:s')], 256)."\n";
  91. }
  92. return true;
  93. }
  94. }