Plantask.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Db;
  5. class Plantask extends Controller
  6. {
  7. //每月初自动结算上个月的业绩
  8. public function auto_yeji(){
  9. $date = date('Ym').'01';
  10. //找一个没结算的
  11. $wallet = Db::name('user_wallet')->where('last_jiesuan_date','lt',$date)->order('id asc')->find();
  12. if(empty($wallet)){
  13. echo $date.'没有了';
  14. exit;
  15. }
  16. echo $date.'开始'.$wallet['user_id'];
  17. //直推
  18. $list = $this->get_my_down_list($wallet);
  19. $down_uids = array_column($list,'user_id');
  20. //间推
  21. $list2 = $this->get_my_downdown_list($down_uids,$wallet);
  22. $down_down_uids = array_column($list2,'user_id');
  23. //团队ids
  24. $tuandui_ids = array_merge($down_uids,$down_down_uids);
  25. $tuandui_ids[] = $wallet['user_id'];
  26. //团队总业绩
  27. $tuanduizongyeji = Db::name('pay_order')
  28. ->where('user_id','IN',$tuandui_ids)
  29. ->where('table_name','money_recharge')
  30. ->where('order_status',1)
  31. ->whereTime('createtime','month')
  32. ->sum('order_amount');
  33. //我所在等级的比例
  34. $bili = Db::name('intro_level_config')->where('id',$wallet['intro_level'])->value('intro_bili');
  35. $bili = $bili ?: 0;
  36. $yongjin = bcdiv(bcmul($tuanduizongyeji,$bili,2),100,2);
  37. Db::startTrans();
  38. if($yongjin > 0){
  39. $rs_wallet = model('wallet')->lockChangeAccountRemain($wallet['user_id'],'intromoney',$yongjin,'111',$remark='团队返佣,总业绩'.$tuanduizongyeji);
  40. if($rs_wallet['status'] === false){
  41. Db::rollback();
  42. echo '发放失败'.$wallet['user_id'];
  43. exit;
  44. }
  45. }
  46. //结算完成
  47. $rs1 = Db::name('user_wallet')->where('user_id',$wallet['user_id'])->update(['last_jiesuan_date' => $date]);
  48. if($rs1 === false){
  49. Db::rollback();
  50. echo '更新失败'.$wallet['user_id'];
  51. exit;
  52. }
  53. Db::commit();
  54. echo '结束'.$wallet['user_id'];
  55. }
  56. //获取我的直推
  57. private function get_my_down_list($wallet){
  58. $chuju = config('site.intro_chuju_min_money');
  59. //直推,下级,入金,等级比我低,未出局
  60. $list = Db::name('user_wallet')->alias('uw')
  61. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid')
  62. ->where('uw.intro_uid',$wallet['user_id']) //我邀请的
  63. ->where('uw.intro_level','neq','-1') //入金了
  64. ->where('uw.intro_level','elt',$wallet['intro_level']) //等级比我低
  65. ->where('uw.money','egt',$chuju) //金额小于500,出局
  66. ->select();
  67. return !empty($list) ? $list : [];
  68. }
  69. //获取我的间推
  70. private function get_my_downdown_list($down_uids = false,$wallet){
  71. if($down_uids === false){
  72. $list = $this->get_my_down_list($wallet);
  73. $down_uids = array_column($list,'user_id');
  74. }
  75. if(empty($down_uids)){
  76. return [];
  77. }
  78. $chuju = config('site.intro_chuju_min_money');
  79. //间推,下下级,入金,未出局,等级比我低,等级比自己的上级低
  80. $list2 = Db::name('user_wallet')->alias('uw')
  81. ->field('uw.user_id,uw.money,uw.intro_level,uw.intro_uid')
  82. ->join('user_wallet intro','uw.intro_uid = intro.user_id','LEFT') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  83. ->whereIN('uw.intro_uid',$down_uids) //下下级
  84. ->where('uw.intro_level','neq','-1') //入金了
  85. ->where('uw.money','egt',$chuju) //金额小于500,出局
  86. ->where('uw.intro_level','elt',$wallet['intro_level']) //等级比我低
  87. ->where('uw.intro_level <= intro.intro_level') //加这里,就是防止:下下级当中,虽然都比我低,但是有可能高于自己的上级(也就是我的直推)
  88. ->select();
  89. return !empty($list2) ? $list2 : [];
  90. }
  91. }