Plantask.php 4.1 KB

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