Plantask.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use app\common\model\Wallet;
  6. class Plantask extends Controller
  7. {
  8. public function index()
  9. {
  10. exit;
  11. }
  12. //10天不买的用户,第10天结算完,第11天自动滑落,上下级自动衔接
  13. //该用户的所有下级,的推荐人,改成此人的上级。
  14. public function auto_hualuo(){
  15. $nowtime = time();
  16. $toweek = date('w',$nowtime);
  17. if($toweek == 1){
  18. //周日不结算(自动到下周一),所以,周一不滑落
  19. exit;
  20. }
  21. //最后买东西是11天前了,intro_num在这里使用,减少查到的数据量
  22. $tendays_ago = $this->hualuo_tendays_ago();
  23. $list = Db::name('user')->where('last_paytime','lt',$tendays_ago)->where('intro_num','gt',0)->select();
  24. if(!empty($list)){
  25. Db::startTrans();
  26. foreach($list as $key => $user){
  27. //我的下级,的推荐人,改成我的上级。也就是跳过了我
  28. $rs = Db::name('user')->where('intro_uid',$user['id'])->update(['intro_uid',$user['intro_uid']]);
  29. if($rs === false){
  30. Db::rollback();
  31. }
  32. //我的推荐人数变0
  33. $rs2 = Db::name('user')->where('id',$user['id'])->update(['intro_num',0]);
  34. if($rs2 === false){
  35. Db::rollback();
  36. }
  37. }
  38. Db::commit();
  39. }
  40. }
  41. //滑落要按11天算
  42. private function hualuo_tendays_ago(){
  43. $nowtime = time();
  44. /*if(input('date','')){
  45. $nowtime = strtotime(input('date',''));
  46. }*/
  47. $today = strtotime(date('Y-m-d',$nowtime));
  48. $toweek = date('w',$nowtime);
  49. $enum = [
  50. 1 => 12,//这一天没人滑落
  51. 2 => 12,
  52. 3 => 12,
  53. 4 => 12,
  54. 5 => 11,
  55. 6 => 11,
  56. 0 => 11,
  57. ];
  58. $rs = $today - ($enum[$toweek] * 86400);
  59. // dump(date('Y-m-d',$rs));
  60. return $rs;
  61. }
  62. //订单支付回调之后的任务,每分钟运行
  63. public function auto_order_paid(){
  64. $map = [
  65. 'status' => 1,
  66. 'have_paid' => ['gt',0],
  67. 'paidtasktime' => 0,
  68. ];
  69. $order_list = Db::name('unishop_order')->where($map)->limit(10)->select();
  70. if(empty($order_list)){
  71. echo '没有数据';
  72. exit;
  73. }
  74. Db::startTrans();
  75. $walletmodel = new Wallet();
  76. foreach($order_list as $key => $order){
  77. //直推代理商获益
  78. $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid');
  79. if($intro_uid){
  80. $bili = config('site.orderpaid_zhitui_bili') ?: 5;
  81. $score = bcdiv(bcmul($order['total_price'],$bili,2),100,2);
  82. if($score > 0){
  83. $rs_wallet = $walletmodel->lockChangeAccountRemain($intro_uid,'score',$score,3,'直推代理奖励','unishop_order',$order['id']);
  84. if($rs_wallet['status'] === false){
  85. echo $rs_wallet['msg'];
  86. Db::rollback();
  87. exit;
  88. }
  89. }
  90. }
  91. $score = 0;
  92. //上上级
  93. $top_uid = Db::name('user')->where('id',$intro_uid)->value('intro_uid');
  94. if($top_uid){
  95. $bili = config('site.orderpaid_jiantui_bili') ?: 1;
  96. $score = bcdiv(bcmul($order['total_price'],$bili,2),100,2);
  97. if($score > 0){
  98. $rs_wallet = $walletmodel->lockChangeAccountRemain($top_uid,'score',$score,4,'间推代理奖励','unishop_order',$order['id']);
  99. if($rs_wallet['status'] === false){
  100. echo $rs_wallet['msg'];
  101. Db::rollback();
  102. exit;
  103. }
  104. }
  105. }
  106. //订单完成
  107. $rs_order = Db::name('unishop_order')->where('id',$order['id'])->update(['paidtasktime'=>time()]);
  108. if($rs_order === false){
  109. echo '更新失败';
  110. Db::rollback();
  111. exit;
  112. }
  113. //循环结束
  114. }
  115. Db::commit();
  116. echo '成功';
  117. }
  118. }