123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace app\index\controller;
- use think\Controller;
- use think\Db;
- use app\common\model\Wallet;
- class Plantask extends Controller
- {
- public function index()
- {
- exit;
- }
- //10天不买的用户,第10天结算完,第11天自动滑落,上下级自动衔接
- //该用户的所有下级,的推荐人,改成此人的上级。
- public function auto_hualuo(){
- $nowtime = time();
- $toweek = date('w',$nowtime);
- if($toweek == 1){
- //周日不结算(自动到下周一),所以,周一不滑落
- exit;
- }
- //最后买东西是11天前了,intro_num在这里使用,减少查到的数据量
- $tendays_ago = $this->hualuo_tendays_ago();
- $list = Db::name('user')->where('last_paytime','lt',$tendays_ago)->where('intro_num','gt',0)->select();
- if(!empty($list)){
- Db::startTrans();
- foreach($list as $key => $user){
- //我的下级,的推荐人,改成我的上级。也就是跳过了我
- $rs = Db::name('user')->where('intro_uid',$user['id'])->update(['intro_uid',$user['intro_uid']]);
- if($rs === false){
- Db::rollback();
- }
- //我的推荐人数变0
- $rs2 = Db::name('user')->where('id',$user['id'])->update(['intro_num',0]);
- if($rs2 === false){
- Db::rollback();
- }
- }
- Db::commit();
- }
- }
- //滑落要按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)->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']);
- 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']);
- 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']);
- 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 '成功';
- }
- //自动结算10日前订单
- public function auto_jiesuan_order(){
- $nowtime = time();
- $toweek = date('w',$nowtime);
- if($toweek == 0){
- //周日不结算(自动到下周一结算)
- echo '周日不结算';
- exit;
- }
- $tendays_ago = $this->jiesuan_tendays_ago();
- //
- 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();
- //为上级做贡献,找到上级
- $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid');
- if(empty($intro_uid)){
- //没有上级,结束
- 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();
- //获取业绩
- $yeji = $this->jiesuan_yeji($intro_uid);
- //确定代理商等级,拿对应比例
- $rule = $this->jiesuan_daili_level($intro_number,$yeji);
- if($rule['bili'] == 0){
- //达不到第一级
- Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>time()]);
- Db::commit();
- exit;
- }
- //给直推
- $score = bcdiv(bcmul($order['order_shouyi'],$rule['bili'],2),100,2);
- if($score > 0){
- $rs_wallet = model('wallet')->lockChangeAccountRemain($intro_uid,'score',$score,6,$rule['level'].'级代理','unishop_order',$order['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']);
- 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();
- }
- //获取业绩
- 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');
- 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();
- $return = $data[0]; //默认第0个
- foreach($data as $key => $rule){
- if($intronum >= $rule['intronum'] && $yeji >= $rule['bili']){
- $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;
- }
- }
|