SettleUserTodayAward.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $cacheKey = "caches:settleTodayAward:".date('Ymd');
  33. if(RedisCache::get($cacheKey.'_lock')){
  34. echo json_encode(['code'=>500,'msg'=>'今日奖金已经结算过,请不要重复运行','date'=>date('Y-m-d H:i:s')], 256)."\n";
  35. return false;
  36. }
  37. RedisCache::setnx($cacheKey.'_lock', date('Y-m-d H:i:s'), 86400);
  38. Db::startTrans();
  39. try {
  40. $list = UserModel::where('today_money', '>', 0)->field('id,money,today_money')->select();
  41. $list = $list? $list->toArray() : [];
  42. if(empty($list)){
  43. sr_throw('暂无今日奖金可结算');
  44. }
  45. $totalMoney = 0;
  46. foreach ($list as $key=>$val){
  47. // 结算奖金到账
  48. $uid = isset($val['id'])? intval($val['id']) : 0;
  49. $userMoney = isset($val['money'])? floatval($val['money']) : 0;
  50. $todayMoney = isset($val['today_money'])? floatval($val['today_money']) : 0;
  51. $totalMoney += $todayMoney;
  52. if($uid && !UserModel::where('id',$val['id'])->inc('money', $todayMoney)->inc('todayaward_money', $todayMoney)->update()){
  53. sr_throw("结算用户[{$uid}]今日奖金失败");
  54. }
  55. $data = [
  56. 'uid'=>$uid,
  57. 'type'=> 6,
  58. 'money'=> $todayMoney,
  59. 'create_at'=>sr_getcurtime(time()),
  60. 'state'=> 1,
  61. 'before_money'=> $userMoney,
  62. 'after_money'=> floatval($userMoney + $todayMoney),
  63. 'from_id'=> 0,
  64. 'uid2'=> 0,
  65. 'free_type'=> 0,
  66. 'remark'=> '今日奖金结算'
  67. ];
  68. if(!MoneyLogModel::insertGetId($data)){
  69. sr_throw("用户[{$uid}]今日奖金结算处理失败");
  70. }
  71. }
  72. // 初始化今日奖金
  73. if(!UserModel::where('today_money', '>', 0)->update(['today_money'=>0,'update_time'=>date('Y-m-d H:i:s')])){
  74. sr_throw("今日奖金清除错误");
  75. }
  76. // 初始化今日预约
  77. if(!UserModel::where('total_appoint_count', '>', 0)->update(['total_appoint_count'=>0,'update_time'=>date('Y-m-d H:i:s')])){
  78. sr_throw("今日预约盒子清除错误");
  79. }
  80. Db::commit();
  81. $msg = "今日奖金结算成功,累计用户".count($list)."人,累计金额{$totalMoney}元";
  82. RedisCache::set($cacheKey.'_success',['msg'=> $msg,'date'=>date('Y-m-d H:i:s')], 7200);
  83. echo json_encode(['code'=>500,'msg'=> $msg,'date'=>date('Y-m-d H:i:s')], 256)."\n";
  84. }catch (\Exception $e){
  85. Db::rollback();
  86. RedisCache::clear($cacheKey.'_lock');
  87. RedisCache::set($cacheKey.'_fail',['msg'=> $e->getMessage(),'trace'=>$e->getTrace()], 7200);
  88. echo json_encode(['code'=>500,'msg'=>$e->getMessage(),'date'=>date('Y-m-d H:i:s')], 256)."\n";
  89. }
  90. return true;
  91. }
  92. }