| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- /**
- * 对接桃牛接口
- * @author: lyh
- * @date: 2019/4/3
- */
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Modes\AccountLog;
- use App\Modes\Advertising;
- use App\Modes\Frozen;
- use App\Modes\Order;
- use App\Modes\Proxy;
- use App\Modes\ProxyInvite;
- use App\Modes\Upgrade;
- use App\Modes\User;
- use App\Modes\UserCoin;
- use App\Modes\UserMsg;
- use App\Service\UpgradeService;
- use Illuminate\Http\Request;
- use Maatwebsite\Excel\Facades\Excel;
- class TestController extends Controller
- {
- public function index(Request $request){
- Upgrade::addUpgradeLog(1,'cesssdfsd',105);exit;
- Order::sendFenyongMsg('测试分佣类型',10,105);exit;
- print_r($this->tuikuan(209));exit;
- }
- function tuikuan($uid){
- $list=Order::whereUid($uid)->get();
- $arr=[];
- $kd=0;
- foreach($list as $k =>$item){
- if($item->order_no!=$item->pay_no){
- if($item->pay_type==2){
- $pay = \App::make('aliPay');
- $res=$pay->getReturnInfo($item->order_no, $item->pay_no,$item->price);
- }else{
- $pay = \App::make('wechat');
- $res=$pay->getReturnInfo($item->order_no, $item->pay_no,$item->price,$item->price);
- }
- $arr[$kd][]=$res;
- $kd++;
- }
- }
- return $arr;
- }
- function getShouYi($ids,$starttime,$endtime){
- $list=AccountLog::where('type','=',5)->whereIn('source_uid',$ids)->where('uid','=',\Auth::id())->whereBetween('created_at',[$starttime,$endtime])->get()->toArray();print_r($list);exit;
- $count=0;
- $sum=0;
- $gguser=[];
- $dluser=[];
- $ggsum=0;
- $dlsum=0;
- if(!empty($list)){
- foreach($list as $key=>$value){
- $count++;
- $sum+=$value->money;
- if(strpos($value->remark,'广告')==true){
- if(!in_array($value->uid,$gguser)){
- $gguser[]=$value->uid;
- }
- $ggsum+=$value->money;
- }else{
- if(!in_array($value->uid,$dluser)){
- $dluser[]=$value->uid;
- }
- $dlsum+=$value->money;
- }
- }
- }
- $arr=[
- 'uv' => $count, // 代理收益次数
- 'earnings' => round($sum,3),// 总收益
- 'inviteNum' => count($ids), // 下级代理数量
- 'proxyNum' => count($dluser), // 代理数量
- 'proxyMoney' => round($dlsum,3), // 代理收益
- 'adverNum' => count($gguser), // 广告数量
- 'adverMoney' => round($ggsum,3), // 广告收益
- ];
- return $arr;
- }
- /*
- * 分佣算法 add by wsl 20190626
- * $fymoney 用户支付的钱
- * $uid 当前支付的用户id
- * $paytype 类型,与order表的type对应
- * */
- public function UserFenYong($fymoney,$uid,$paytype){
- $userinfo=User::whereId($uid)->first()->toArray();
- $proxy_type=$paytype==2?2:1;
- //1.获取直推信息
- if(!empty($userinfo['invitor'])){
- $parentinfo=User::whereId($userinfo['invitor'])->first();
- }
- if(!empty($parentinfo)){
- $parentinfo=$parentinfo->toArray();
- //获取直属上级的配置
- $parentsetting=Proxy::getSettingByAreaAndLevel($parentinfo['level'],$parentinfo['province'],$parentinfo['city'],$parentinfo['district']);
- if(!empty($parentsetting)){
- //算分佣
- if($paytype==2){
- $parentpersent=$parentsetting['adver_invite'];
- $rmarkstr='广告费';
- }else{
- $parentpersent=$parentsetting['proxy_invite'];
- $rmarkstr='代理费';
- }
- $parentmoney=$parentpersent/100*$fymoney;
- $remark='用户【id:'.$uid.'】,用途-支付'.$rmarkstr.','.$fymoney.'元,得到佣金【'.$parentmoney.'】';
- //给直推上级添加余额并生成记录
- $this->updateUserAndinsertLog($parentinfo,$parentmoney,$remark,$uid);
- }
- //间分佣算法 start
- if(!empty($parentinfo['invitor'])){
- $jian1info=User::whereId($parentinfo['invitor'])->first();
- if(!empty($jian1info)){
- $jian1info=$jian1info->toArray();
- //间1分佣
- $jian1money=$this->jianFenYong($jian1info,$fymoney,1,$proxy_type);
- if(!empty($jian1money)){
- $remark='用户【id:'.$uid.'】,用途-支付'.$rmarkstr.','.$fymoney.'元,得到佣金【'.$jian1money.'】';
- //生成记录
- $this->updateUserAndinsertLog($jian1info,$jian1money,$remark,$uid);
- }
- //间2的分佣
- if(!empty($jian1info['invitor'])){
- $jiantwoinfo=User::whereId($jian1info['invitor'])->first();
- if(!empty($jiantwoinfo)){
- $jiantwoinfo=$jiantwoinfo->toArray();
- $jiantwomoney=$this->jianFenYong($jiantwoinfo,$fymoney,2,$proxy_type);
- if(!empty($jiantwomoney)){
- $remark='用户【id:'.$uid.'】,用途-支付'.$rmarkstr.','.$fymoney.'元,得到佣金【'.$jiantwomoney.'】';
- //生成记录
- $this->updateUserAndinsertLog($jiantwoinfo,$jiantwomoney,$remark,$uid);
- }
- }
- }
- }
- }
- //间分佣算法 end
- }
- }
- /*
- * 更新user表的月并且生成记录 add by wsl 20190626
- * $userinfo 要更新的用户信息 array
- * $money 变动金额
- * $remark 备注
- * $source_id 来源id
- * */
- function updateUserAndinsertLog($userinfo,$money,$remark,$source_id){
- \DB::beginTransaction();
- try {
- User::whereId($userinfo['id'])->update(['balance' => $userinfo['balance']+$money]);
- AccountLog::saveDataByPost($userinfo['id'],5,$money,$userinfo['balance'],2,1,$remark,$source_id);
- \DB::commit();
- } catch (Exception $exception) {
- \DB::rollBack();
- }
- }
- /*
- * 计算间1间2分佣得到的金钱 add by wsl 20190626
- * $userinfo 上一级用户信息 array
- * $fymoney 当前用户支付的金钱
- * $jiantype 间1还是间2
- * $proxy_type 广告还是代理
- * */
- function jianFenYong($userinfo,$fymoney,$jiantype=1,$proxy_type){
- //获取间1的配置
- $setting=Proxy::getJian1OrJian2Setting($userinfo['level'],$userinfo['province'],$userinfo['city'],$userinfo['district'],$jiantype,$proxy_type);
- $jianmoney=0;
- if(!empty($setting)&& $userinfo['level']>1) {
- //计算间的直推下级
- $childcount = User::where('invitor', '=', $userinfo['id'])->count();
- //找到判定区间分配佣金//由于第一个设置不要,不需要判定第一个
- if ($childcount <= $setting[1]['max']) {
- $jianmoney = $fymoney * $setting[0]['pct'] / 100;
- } elseif ($childcount > $setting[1]['max'] && $childcount < $setting[2]['max']) {
- $jianmoney = $fymoney * $setting[1]['pct'] / 100;
- } else {
- $jianmoney = $fymoney * $setting[2]['pct'] / 100;
- }
- }
- return $jianmoney;
- }
- public function getSettingByYearAndMonth(Request $request){
- $param = $request->all();
- $uid=$param['userid'];
- //找出自己的所有直属下级
- $childarr=User::where('invitor','=',$uid)->get(['id']);
- $childarr=$childarr->toArray();
- $arr=[];
- if(!empty($childarr)){
- foreach ($childarr as $ke=>$ve){
- $arr[]=$ve['id'];
- }
- }
- $montstat=Order::getMonthOrYearStat($uid,$arr,'month');
- $yearstat=Order::getMonthOrYearStat($uid,$arr,'year');
- //找到对应订单并且统计
- $userinfo=User::where('id','=',$uid)->first()->toArray();
- //根据用户等级找到相关配置
- $setting=Proxy::getSettingByAreaAndLevel($userinfo['level']+1,$userinfo['province'],$userinfo['city'],$userinfo['district']);
- if(!empty($setting)){
- if((!empty($setting['upgrade_business_month'])&&$setting['upgrade_business_month']!='0.000'&&$montstat>=$setting['upgrade_business_month'])
- ||(!empty($setting['upgrade_business_year'])&&$setting['upgrade_business_year']!='0.000'&&$yearstat>=$setting['upgrade_business_year'])
- ){
- //进行升级
- User::whereId($uid)->update(['level' => $userinfo['level']+1]);
- }
- }
- }//testInviteNumUpLevel
- function getSettingByAreaAndLevel2($level,$province,$city,$district){
- $setting=Proxy::where('user_level','=',$level)
- ->where('province','=',$province)
- ->where('city','=',$city)
- ->where('district','=',$district)
- ->first();
- //如果对应区域设置为空的话则找全局配置
- if(empty($setting)){
- $setting=Proxy::where('user_level','=',$level)
- ->where('province','=',0)
- ->where('city','=',0)
- ->where('district','=',0)
- ->first();
- }
- $ss=empty($setting)?array():$setting->toArray();
- return $ss;
- }
- public function testInviteNumUpLevel(Request $request){
- //查询当前的父级
- $param = $request->all();
- $parantInfo=User::whereInviteCode($param['invitor'])->first()->toArray();
- //根据用户等级找到相关配置
- $setting=Proxy::getSettingByAreaAndLevel($parantInfo['level']+1,$parantInfo['province'],$parantInfo['city'],$parantInfo['district']);
- if(!empty($setting) &&!empty($setting['upgrade_invite'])){
- $setting_count=$setting['upgrade_invite'];
- //找出所有推荐的子集个数
- $childcount=User::where('invitor','=',$parantInfo['id'])->count();
- if($childcount>=$setting_count){
- //进行升级
- User::whereId($parantInfo['id'])->update(['level' => $parantInfo['level']+1]);
- }
- }
- }
- public function setAllUserAccount()
- {
- $num = 0;
- $userList = User::all('id');
- foreach ($userList as &$item) {
- // $adver = Advertising::whereUid($item->id)
- // ->whereIn('status', [3, 4, 9])
- // ->select([
- // \DB::raw('count(id) count'),
- // \DB::raw('sum(total_price) allMoney')
- // ])
- // ->first();
- // $num++;
- // $item->count=$adver->count;
- // UpgradeService::incUserAccount($item->id,'ad_num',abs($item->count));
- // $item->allMoney=$adver->allMoney;
- // if ($item->allMoney) {
- // UpgradeService::incUserAccount($item->id,'ad_all_money',abs($item->allMoney));
- // }
- // $item->invite_num = count(User::getAllInvite($item->id, null, null, false)); // 邀请人数
- // UpgradeService::incUserAccount($item->id,'invite_num', abs($item->invite_num));
- // $item->proxyCommission = AccountLog::whereIn('type', [20, 21, 22, 23])->whereStatus(1)->whereUid($item->id)->sum('money'); // 代理分佣
- // UpgradeService::incUserAccount($item->id,'proxy_commission', abs($item->proxyCommission));
- // $item->adverCommission = AccountLog::whereIn('type', [10, 11, 12, 13])->whereStatus(1)->whereUid($item->id)->sum('money');//广告费分佣
- // UpgradeService::incUserAccount($item->id,'adver_commission', abs($item->adverCommission));
- // $item->agentAllmoney = AccountLog::whereIn('type',[7,8])->whereStatus(1)->whereUid($item->id)->sum('money');//总的代理费
- // UpgradeService::incUserAccount($item->id,'agent_all_money', abs($item->agentAllmoney));
- }
- return $num;
- }
- }
|