Task.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\PartnerLevel;
  4. use app\common\model\User;
  5. use app\common\model\UserCouponList;
  6. use app\common\model\UserExpLog;
  7. use app\common\model\UserGrowthLog;
  8. use app\common\model\UserIntegralLog;
  9. use app\common\model\UserLevel;
  10. use app\common\model\UserWallet;
  11. use app\common\service\UserIdentity;
  12. /**
  13. * @title 定时任务
  14. * Class Task
  15. * @controller Task
  16. * @group base
  17. */
  18. class Task extends Base
  19. {
  20. // 会员等级
  21. public function checkUserLevel()
  22. {
  23. $user_level = UserLevel::column('id,name,growth','id');
  24. $partner_level = PartnerLevel::column('id,name,growth','id');
  25. $last_year = date('Y-m-d H:i:s',strtotime('-1 year'));
  26. $user_list = User::field('id,level_id,partner_lev,is_partner')
  27. ->with('wallet')
  28. ->where('create_at','> time',$last_year)
  29. ->select()->toArray();
  30. foreach ($user_list as $v)
  31. {
  32. // 一年内获得的经验值
  33. $exp = UserExpLog::where('create_at','> time',$last_year)
  34. ->where('user_id','=',$v['id'])
  35. ->where('is_deleted','=',0)
  36. ->where('exp','>',0)
  37. ->sum('exp');
  38. // 经验过期
  39. if($exp < $v['wallet']['level_exp']) {
  40. UserWallet::userExpChange($v['id'],$v['wallet']['level_exp']-$exp,'经验过期',-1);
  41. $v['wallet']['level_exp'] = $exp;
  42. // 更新会员等级
  43. UserIdentity::userLevel($v,$user_level,$v['id']);
  44. }
  45. //近一年获得的积分
  46. $integral = UserIntegralLog::where('create_at','> time',$last_year)
  47. ->where('user_id','=',$v['id'])
  48. ->where('type','in',[5,6,7])
  49. ->where('is_deleted','=',0)
  50. ->where('integral','>',0)
  51. ->sum('integral');
  52. // 积分过期
  53. if($integral < $v['wallet']['integral']) UserWallet::userIntegralChange($v['id'],$v['wallet']['integral']-$integral,'积分过期',8,-1);
  54. // 不是分销商跳过
  55. if(!$v['is_partner']) continue;
  56. // 成长值
  57. $growth = UserGrowthLog::where('create_at','> time',$last_year)
  58. ->where('user_id','=',$v['id'])
  59. ->where('growth','>',0)
  60. ->sum('growth');
  61. // 成长值 过期
  62. if($growth < $v['wallet']['growth']) {
  63. UserWallet::userGrowthChange($v['id'],$v['wallet']['growth']-$growth,8,'成长值过期',-1);
  64. $v['wallet']['growth'] = $growth;
  65. // 更新会员
  66. UserIdentity::userPartnerLevel($v,$partner_level,$v['id']);
  67. }
  68. }
  69. $this->success('执行'.count($user_list).'条数据');
  70. }
  71. // 会员优惠券过期定时
  72. public function couponOverTime()
  73. {
  74. UserCouponList::where([['type','=',1],['end','< time',date('Y-m-d')],['status','=',0]])->update(['status'=>2]);
  75. }
  76. }