hualuo_tendays_ago(); // echo $tendays_ago; Db::startTrans(); $user = Db::name('user')->where('last_paytime','lt',$tendays_ago)->where('intro_num','gt',0)->lock(true)->find(); if(empty($user)){ echo '没有数据'; Db::rollback(); exit; } dump($user); //我的多个下级,的推荐人,改成我的上级。也就是跳过了我 $my_down = Db::name('user')->where('intro_uid',$user['id'])->select(); if(!empty($my_down)){ foreach($my_down as $key => $down_user){ $rs_down = $this->updateIntro($down_user['id'],$user['intro_uid']); if($rs_down !== true){ echo $rs_down; Db::rollback(); exit; } } } //用不到了 //我滑落了,我的上级保持不变,下级都没了 /*$my_update = [ 'intro_num' => 0, 'intro_num_all' => 0, ]; $rs2 = Db::name('user')->where('id',$user['id'])->update($my_update); if($rs2 === false){ Db::rollback(); }*/ //提交 Db::rollback(); } private function updateIntro($uid,$intro_uid){ $db = Db::name("user"); //验证 if($uid == $intro_uid) $this->error("新邀请人不能是自己!"); $rs_user = $db->where(array('id' => $uid))->find(); if(!$rs_user) $this->error("会员 ".$uid.' 不存在! '); $rs_intro = $db->where(array('id' => $intro_uid))->find(); if(!$rs_intro) $this->error("新邀请人 ".$intro_uid.' 不存在! '); if($rs_user['intro_uid'] == $intro_uid) $this->error("新邀请人不能是原来的邀请人!"); //新推荐人不能是自己下级 if($rs_intro['intro_ids']) { $ary = explode(',', $rs_intro['intro_ids']); if(in_array($rs_user['id'], $ary)) { $this->error("新邀请人不能是自己网体下级会员!"); } } //更新此会员的上级 $data = array(); $data['intro_ids'] = $rs_intro['intro_ids'] ? $rs_intro['intro_ids'].','.$rs_intro['id'] : $rs_intro['id']; //新推荐人id序列 $data['intro_level'] = $rs_intro['intro_level'] + 1; //新层数 $data['intro_uid'] = $rs_intro['id']; //新推荐人id $db->where(['id' => $uid])->update($data); //更新此会员的下级会员 $num_c = $rs_intro['intro_level'] - $rs_user['intro_level'] + 1;//变化的层数差 $user_sub = $db->where("find_in_set('".$rs_user['id']."',intro_ids) > 0")->order("intro_level asc")->select(); //所有下级 foreach($user_sub as $users) { //每个下级(因为是升序查询,所以每个的上级一定先更新完毕) $data_sub = array(); $rs_tjr = $db->where(array('id' => $users['intro_uid']))->field('id,intro_ids')->find(); $data_sub['intro_level'] = $users['intro_level'] + $num_c; //新层数 $data_sub['intro_ids'] = $rs_tjr['intro_ids'].','.$rs_tjr['id']; //新推荐人id序列 $db->where(array('id' => $users['id']))->update($data_sub);//更新移动网体的会员 } //更新直推、团队数 $team = count($user_sub) + 1; if($rs_user['intro_uid']) { $this->addIntroNum($rs_user['intro_uid'], -1, -$team); //有则 更新原上级 } $this->addIntroNum($intro_uid, 1, $team); //更新新上级 $db->commit(); $this->success("更新成功"); } /** * 更新邀请码数和推荐团队人数 * @param string $tjrname 用户id * @param integer $num 直推增减人数 * @param integer $team 团队增减人数 */ private function addIntroNum($tjrname, $num, $team) { //更新直推人数 if($num < 0) { $rs1 = Db::name('user')->where(['id' => $tjrname])->setDec('intro_num', abs($num)); } else { $rs1 = Db::name('user')->where(['id' => $tjrname])->setInc('intro_num', $num); } if($rs1 === false){ return false; } //更新团队 $rstjr = Db::name('user')->where(['id' => $tjrname])->field('id,intro_ids')->find(); if($rstjr) { $tjstr = $rstjr['intro_ids'] ? ($rstjr['intro_ids'] . ',' . $rstjr['id']) : $rstjr['id']; $tjstr = trim($tjstr, ','); $arr_intro = explode(',', $tjstr); if($team < 0) { $rs2 = Db::name('user')->where(['id' => ['in', $arr_intro]])->setDec('intro_num_all', abs($team)); } else { $rs2 = Db::name('user')->where(['id' => ['in', $arr_intro]])->setInc('intro_num_all', $team); } if($rs2 === false){ return false; } } return true; } //滑落要按11天算 private function hualuo_tendays_ago(){ $nowtime = time(); /*if(input('date','')){ $nowtime = strtotime(input('date','')); }*/ $today = strtotime(date('Y-m-d',$nowtime)); $toweek = date('w',$nowtime); $enum = [ 1 => 12,//这一天没人滑落 2 => 12, 3 => 12, 4 => 12, 5 => 11, 6 => 11, 0 => 11, ]; $rs = $today - ($enum[$toweek] * 86400); // dump(date('Y-m-d',$rs)); return $rs; } //订单支付回调之后的任务,每分钟运行 public function auto_order_paid(){ $map = [ 'status' => 1, 'have_paid' => ['gt',0], 'paidtasktime' => 0, ]; $order_list = Db::name('unishop_order')->where($map)->order('id asc')->limit(10)->select(); if(empty($order_list)){ echo '没有数据'; exit; } Db::startTrans(); $walletmodel = new Wallet(); foreach($order_list as $key => $order){ //买家立刻得到积分 if($order['order_shouyi'] > 0){ $rs_wallet = $walletmodel->lockChangeAccountRemain($order['user_id'],'score',$order['order_shouyi'],5,'下单收益','unishop_order',$order['id'],$order['user_id']); if($rs_wallet['status'] === false){ echo $rs_wallet['msg']; Db::rollback(); exit; } } //直推代理商获益 $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid'); if($intro_uid){ $bili = config('site.orderpaid_zhitui_bili') ?: 5; $score = bcdiv(bcmul($order['order_price'],$bili,2),100,2); if($score > 0){ $rs_wallet = $walletmodel->lockChangeAccountRemain($intro_uid,'score',$score,3,'直推代理奖励','unishop_order',$order['id'],$order['user_id']); if($rs_wallet['status'] === false){ echo $rs_wallet['msg']; Db::rollback(); exit; } } } $score = 0; //上上级 $top_uid = Db::name('user')->where('id',$intro_uid)->value('intro_uid'); if($top_uid){ $bili = config('site.orderpaid_jiantui_bili') ?: 1; $score = bcdiv(bcmul($order['order_price'],$bili,2),100,2); if($score > 0){ $rs_wallet = $walletmodel->lockChangeAccountRemain($top_uid,'score',$score,4,'间推代理奖励','unishop_order',$order['id'],$order['user_id']); if($rs_wallet['status'] === false){ echo $rs_wallet['msg']; Db::rollback(); exit; } } } //订单完成 $rs_order = Db::name('unishop_order')->where('id',$order['id'])->update(['paidtasktime'=>time()]); if($rs_order === false){ echo '更新失败'; Db::rollback(); exit; } //循环结束 } Db::commit(); echo '成功'.count($order_list); } //自动结算10日前订单 public function auto_jiesuan_order(){ $nowtime = time(); $toweek = date('w',$nowtime); if($toweek == 0){ //周日不结算(自动到下周一结算) echo '周日不结算'; exit; } $tendays_ago = $this->jiesuan_tendays_ago(); // echo $tendays_ago;exit; // Db::startTrans(); $order_map = [ 'status' => 1, 'have_paid' => ['lt',$tendays_ago], 'jiesuantime' => 0, ]; $order = Db::name('unishop_order')->where($order_map)->where('have_paid','gt',0)->order('id asc')->lock(true)->find(); if(empty($order)){ echo '没有数据'; Db::rollback(); exit; } // dump($order); //为上级做贡献,找到上级 $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid'); if(empty($intro_uid)){ echo '没有上级,结束'; Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>time()]); Db::commit(); exit; } //获取直推人数 $intro_number = Db::name('user')->where('intro_uid',$intro_uid)->count(); // dump($intro_number); //获取业绩 $yeji = $this->jiesuan_yeji($intro_uid); // dump($yeji); //确定代理商等级,拿对应比例 $rule = $this->jiesuan_daili_level($intro_number,$yeji); if($rule['bili'] == 0){ echo '达不到第一级,结束'; Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>time()]); Db::commit(); exit; } // dump($rule); //给直推 $score = bcdiv(bcmul($order['order_shouyi'],$rule['bili'],2),100,2); // dump($score); if($score > 0){ $rs_wallet = model('wallet')->lockChangeAccountRemain($intro_uid,'score',$score,6,$rule['level'].'级代理','unishop_order',$order['id'],$order['user_id']); if($rs_wallet['status'] === false){ echo $rs_wallet['msg']; Db::rollback(); exit; } } //给间推 if($rule['level'] == 1){ //一级代理要有间推 $two_intro_uid = Db::name('user')->where('id',$intro_uid)->value('intro_uid');//上上级id if($two_intro_uid > 0){ $two_intro_count = Db::name('user')->where('intro_uid',$two_intro_uid)->count();//上上级的直推 // $two_yeji = $this->jiesuan_yeji($two_intro_uid);//上上级的业绩 if($two_intro_count >= $rule['intronum']){//也要五个,且业绩不高于15万(极差)。这里很矛盾 $score_2 = $score;//目前是一样的,不再次计算了 if($score_2 > 0){ $rs_wallet = model('wallet')->lockChangeAccountRemain($two_intro_uid,'score',$score_2,7,$rule['level'].'级代理(间推)','unishop_order',$order['id'],$order['user_id']); if($rs_wallet['status'] === false){ echo $rs_wallet['msg']; Db::rollback(); exit; } } } } } //标记为结算 $jiesuan = Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>$nowtime]); if($jiesuan === false){ echo '修改状态失败'; Db::rollback(); exit; } //批发的都卖出 $pifa = Db::name('unishop_order_product')->where('order_id',$order['id'])->update(['pifa_status'=>1]); if($pifa === false){ echo '修改状态失败2'; Db::rollback(); exit; } Db::commit(); echo '完成'.$order['id']; } //获取业绩 private function jiesuan_yeji($user_id){ //找到所有下级 $user_ids = Db::name('user')->where('find_in_set(:intro_ids,intro_ids)', ['intro_ids' => $user_id])->column('id'); // dump($user_ids); if(empty($user_ids)){ return 0; } $map = [ 'status' => 1, 'have_paid' => ['gt',0], 'user_id' => ['IN',$user_ids], ]; $yeji = Db::name('unishop_order')->where($map)->sum('order_price'); return $yeji; } //确认代理等级及规则 private function jiesuan_daili_level($intronum,$yeji){ $data = Db::name('zongdai')->order('id asc')->select(); // dump($data); $return = $data[0]; //默认第0个 foreach($data as $key => $rule){ if($intronum >= $rule['intronum'] && $yeji >= $rule['yeji']){ $return = $rule; } } return $return; } //结算要按10天算 private function jiesuan_tendays_ago(){ $nowtime = time(); /*if(input('date','')){ $nowtime = strtotime(input('date','')); }*/ $today = strtotime(date('Y-m-d',$nowtime)); $toweek = date('w',$nowtime); $enum = [ 1 => 11, 2 => 11, 3 => 11, 4 => 10, 5 => 10, 6 => 10, 0 => 10, //这一天没人结算 ]; $rs = $today - ($enum[$toweek] * 86400); // dump(date('Y-m-d',$rs)); return $rs; } }