123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace app\models\store;
- use app\models\user\User;
- use app\models\user\UserBill;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use app\models\store\StoreMgp;
- use think\Exception;
- class StoreGp extends BaseModel
- {
- use ModelTrait;
- /**
- * 创建公排
- * @param $order
- */
- public static function createGp($data)
- {
- $order = $data['data'];
- @file_put_contents("job.txt",json_encode($order));
- try {
- $user = User::where('uid',$order['uid'])->find();
- $spuser = User::where('uid',$user['spread_uid'])->find();
- User::where('uid',$order['uid'])->update(['cycle_time'=>strtotime("+31 day")]);
- if($spuser)
- {
- User::where('uid',$spuser['uid'])->update(['cycle_time'=>strtotime("+31 day")]);
- $sp_uid = $spuser['uid'];
- }
- else
- {
- $sp_uid = 0;
- }
- $no = intval(self::value('max(no)') ?: 0) + 1;
- $rs = self::getpath($no);
- list($p_no, $no_path,$gp_id,$send,$gpinfo) = [$rs['p_no'],$rs['no_path'],$rs['gp_id'],$rs['send'],$rs['info']];
- @file_put_contents("job.txt","\r\n".json_encode($rs),8);
- $order_id = $order['id'];
- $add_time = time();
- $price = $order['total_price'];
- $uid = $order['uid'];
- $status = 0;
- self::create(compact('no', 'uid', 'order_id', 'add_time', 'price', 'p_no', 'no_path','status'));
- if($no>1) {
- self::where('no', $p_no)->inc('cts', 1)->update();
- self::where('no', $gp_id)->inc('team_cts', 1)->update();
- }
-
-
- /*
- if($send==1)
- {
- $p_user = User::find($gpinfo['uid']);
- $sp_brokeragePrice1 = 3000;
- $gp_balance = bcadd($p_user['gp_now_money'], $sp_brokeragePrice1, 2);
- $mark = "公排号:" . $no . "满3个加报单余额3000元,订单号:" . $order['order_id'];
- $res = UserBill::income('满3个报单余额', $p_user['uid'], 'now_money', 'brokerage', $sp_brokeragePrice1, $order['id'], $gp_balance, $mark, 1);
- User::where('uid', $p_user['uid'])->inc('gp_now_money', $sp_brokeragePrice1)->update();
- self::where('no', $p_no)->update(['send'=>1]);
- }*/
- $p_no_cts = self::where('no', $gp_id)->value('team_cts');
- if ($p_no_cts>0 && $p_no_cts % 3 == 0) {
- $info = self::where('no', $gp_id)->find();
- $sp_userInfo = User::find($info['uid']);
- if($sp_userInfo['cycle_time']>time()) {
- switch ($p_no_cts) {
- case 3:
- $sp_brokeragePrice = $order['out_amount'] - 3000;
- $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
- $mark = "公排号:" . $no . "满3个拥金3000元,订单号:" . $order['order_id'];
- $res = UserBill::income('满3个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
- User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
- break;
- case 6:
- case 9:
- $sp_brokeragePrice = $order['out_amount'];
- $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
- $mark = "公排号:" . $no . '满' . $p_no_cts . '个拥金' . $order['out_amount'] . "元,订单号:" . $order['order_id'];
- $res = UserBill::income('满' . $p_no_cts . '个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
- User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
- break;
- case 12:
- $sp_brokeragePrice = $order['out_amount'] - 1000;
- $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
- $mark = "公排号:" . $no . '满' . $p_no_cts . '个拥金' . $order['out_amount'] . "元,订单号:" . $order['order_id'];
- $res = UserBill::income('满' . $p_no_cts . '个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
- User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
- self::where('no', $gp_id)->update(['status' => 1]);
- break;
- }
- }
- }
- $user_path =null;
- if(!StoreMgp::be(['uid'=>$uid]))
- {
- $rs = StoreMgp::getpath($order['uid'],$sp_uid);
- $add_time = time();
- $status = 0;
- list($p_no,$no,$no_path,$user_path,$level) = [$rs['p_no'],$rs['no'],$rs['no_path'],$rs['user_path'],$rs['level']];
- StoreMgp::create(compact('no','p_no','order_id','no_path','user_path','level','price','uid','add_time','status'));
- StoreMgp::where('no',$p_no)->inc('cts',1)->update();
- }
-
- $no_path = explode(",",$user_path);
- if (sizeof($no_path) > 1) {
- $path = array_reverse($no_path);
- $n = 0;
- foreach ($path as $v) {
- $n++;
- if ($n <=1) continue;
- if($n>8) break;
- $sp_brokeragePrice = self::getbrokeragePrice($v);
- if($sp_brokeragePrice==0) continue;
- $user = User::find($v);
- $spuser = User::find($user['spread_uid']);
- if($user['cycle_time']>time()) {
- $balance = bcadd($user['brokerage_price'], $sp_brokeragePrice, 2);
- $mark = "公排号:" . $no . '团队奖' . floatval($sp_brokeragePrice) . "元,订单号:" . $order['order_id'];
- $res1 = UserBill::income('团队奖', $user['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
- User::where('uid', $user['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
- }
- if($spuser && $user['spread_uid']>0 && $spuser['cycle_time']>time())
- {
- $balance = bcadd($spuser['brokerage_price'], $sp_brokeragePrice, 2);
- $mark = "公排号:" . $no . '推荐奖' . $sp_brokeragePrice . "元,订单号:" . $order['order_id'];
- $res = UserBill::income('推荐奖', $spuser['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
- User::where('uid', $spuser['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
- }
- }
- }
- return true;
- }catch (Exception $e)
- {
- @file_put_contents("gp.txt","\r\n". $e->getFile().'--'.$e->getLine(),8);
- return false;
- }
- }
- public static function getbrokeragePrice($uid)
- {
- $num = User::where('spread_uid',$uid)->where('cycle_time','>',0)->count();
- if($uid==105) return sys_config('sp_one_num',100);
- if($num>2) return sys_config('sp_tree_num',200);
- if($num>0) return sys_config('sp_one_num',100);
- return 0;
- }
- /**
- * 获取上级及路径
- * @param $no
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private static function getpath($no)
- {
- $no_path = [];$p_no=0;$send = 0;$info = null;
- if($no==1) {
- $no_path[] =1;
- $gp_id = 1;
- }
- else
- {
- $gp_id = self::where('team_cts','<',12)->value('no');
- $min = self::where('cts','<',3)->order("id asc")->find();
- $p_no = $min['no'];
- $no_path = explode(",",$min['no_path']);
- $no_path[] = $no;
- if($min['cts']==2)
- {
- $send = 1;
- }
- $info = $min;
- }
- $no_path = join(",",$no_path);
- return compact('p_no','no_path','gp_id','send','info');
- }
- }
|