TestController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * 对接桃牛接口
  4. * @author: lyh
  5. * @date: 2019/4/3
  6. */
  7. namespace App\Http\Controllers\Api;
  8. use App\Http\Controllers\Controller;
  9. use App\Modes\AccountLog;
  10. use App\Modes\Advertising;
  11. use App\Modes\Frozen;
  12. use App\Modes\Order;
  13. use App\Modes\Proxy;
  14. use App\Modes\ProxyInvite;
  15. use App\Modes\Upgrade;
  16. use App\Modes\User;
  17. use App\Modes\UserCoin;
  18. use App\Modes\UserMsg;
  19. use App\Service\UpgradeService;
  20. use Illuminate\Http\Request;
  21. use Maatwebsite\Excel\Facades\Excel;
  22. class TestController extends Controller
  23. {
  24. public function index(Request $request){
  25. Upgrade::addUpgradeLog(1,'cesssdfsd',105);exit;
  26. Order::sendFenyongMsg('测试分佣类型',10,105);exit;
  27. print_r($this->tuikuan(209));exit;
  28. }
  29. function tuikuan($uid){
  30. $list=Order::whereUid($uid)->get();
  31. $arr=[];
  32. $kd=0;
  33. foreach($list as $k =>$item){
  34. if($item->order_no!=$item->pay_no){
  35. if($item->pay_type==2){
  36. $pay = \App::make('aliPay');
  37. $res=$pay->getReturnInfo($item->order_no, $item->pay_no,$item->price);
  38. }else{
  39. $pay = \App::make('wechat');
  40. $res=$pay->getReturnInfo($item->order_no, $item->pay_no,$item->price,$item->price);
  41. }
  42. $arr[$kd][]=$res;
  43. $kd++;
  44. }
  45. }
  46. return $arr;
  47. }
  48. function getShouYi($ids,$starttime,$endtime){
  49. $list=AccountLog::where('type','=',5)->whereIn('source_uid',$ids)->where('uid','=',\Auth::id())->whereBetween('created_at',[$starttime,$endtime])->get()->toArray();print_r($list);exit;
  50. $count=0;
  51. $sum=0;
  52. $gguser=[];
  53. $dluser=[];
  54. $ggsum=0;
  55. $dlsum=0;
  56. if(!empty($list)){
  57. foreach($list as $key=>$value){
  58. $count++;
  59. $sum+=$value->money;
  60. if(strpos($value->remark,'广告')==true){
  61. if(!in_array($value->uid,$gguser)){
  62. $gguser[]=$value->uid;
  63. }
  64. $ggsum+=$value->money;
  65. }else{
  66. if(!in_array($value->uid,$dluser)){
  67. $dluser[]=$value->uid;
  68. }
  69. $dlsum+=$value->money;
  70. }
  71. }
  72. }
  73. $arr=[
  74. 'uv' => $count, // 代理收益次数
  75. 'earnings' => round($sum,3),// 总收益
  76. 'inviteNum' => count($ids), // 下级代理数量
  77. 'proxyNum' => count($dluser), // 代理数量
  78. 'proxyMoney' => round($dlsum,3), // 代理收益
  79. 'adverNum' => count($gguser), // 广告数量
  80. 'adverMoney' => round($ggsum,3), // 广告收益
  81. ];
  82. return $arr;
  83. }
  84. /*
  85. * 分佣算法 add by wsl 20190626
  86. * $fymoney 用户支付的钱
  87. * $uid 当前支付的用户id
  88. * $paytype 类型,与order表的type对应
  89. * */
  90. public function UserFenYong($fymoney,$uid,$paytype){
  91. $userinfo=User::whereId($uid)->first()->toArray();
  92. $proxy_type=$paytype==2?2:1;
  93. //1.获取直推信息
  94. if(!empty($userinfo['invitor'])){
  95. $parentinfo=User::whereId($userinfo['invitor'])->first();
  96. }
  97. if(!empty($parentinfo)){
  98. $parentinfo=$parentinfo->toArray();
  99. //获取直属上级的配置
  100. $parentsetting=Proxy::getSettingByAreaAndLevel($parentinfo['level'],$parentinfo['province'],$parentinfo['city'],$parentinfo['district']);
  101. if(!empty($parentsetting)){
  102. //算分佣
  103. if($paytype==2){
  104. $parentpersent=$parentsetting['adver_invite'];
  105. $rmarkstr='广告费';
  106. }else{
  107. $parentpersent=$parentsetting['proxy_invite'];
  108. $rmarkstr='代理费';
  109. }
  110. $parentmoney=$parentpersent/100*$fymoney;
  111. $remark='用户【id:'.$uid.'】,用途-支付'.$rmarkstr.','.$fymoney.'元,得到佣金【'.$parentmoney.'】';
  112. //给直推上级添加余额并生成记录
  113. $this->updateUserAndinsertLog($parentinfo,$parentmoney,$remark,$uid);
  114. }
  115. //间分佣算法 start
  116. if(!empty($parentinfo['invitor'])){
  117. $jian1info=User::whereId($parentinfo['invitor'])->first();
  118. if(!empty($jian1info)){
  119. $jian1info=$jian1info->toArray();
  120. //间1分佣
  121. $jian1money=$this->jianFenYong($jian1info,$fymoney,1,$proxy_type);
  122. if(!empty($jian1money)){
  123. $remark='用户【id:'.$uid.'】,用途-支付'.$rmarkstr.','.$fymoney.'元,得到佣金【'.$jian1money.'】';
  124. //生成记录
  125. $this->updateUserAndinsertLog($jian1info,$jian1money,$remark,$uid);
  126. }
  127. //间2的分佣
  128. if(!empty($jian1info['invitor'])){
  129. $jiantwoinfo=User::whereId($jian1info['invitor'])->first();
  130. if(!empty($jiantwoinfo)){
  131. $jiantwoinfo=$jiantwoinfo->toArray();
  132. $jiantwomoney=$this->jianFenYong($jiantwoinfo,$fymoney,2,$proxy_type);
  133. if(!empty($jiantwomoney)){
  134. $remark='用户【id:'.$uid.'】,用途-支付'.$rmarkstr.','.$fymoney.'元,得到佣金【'.$jiantwomoney.'】';
  135. //生成记录
  136. $this->updateUserAndinsertLog($jiantwoinfo,$jiantwomoney,$remark,$uid);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. //间分佣算法 end
  143. }
  144. }
  145. /*
  146. * 更新user表的月并且生成记录 add by wsl 20190626
  147. * $userinfo 要更新的用户信息 array
  148. * $money 变动金额
  149. * $remark 备注
  150. * $source_id 来源id
  151. * */
  152. function updateUserAndinsertLog($userinfo,$money,$remark,$source_id){
  153. \DB::beginTransaction();
  154. try {
  155. User::whereId($userinfo['id'])->update(['balance' => $userinfo['balance']+$money]);
  156. AccountLog::saveDataByPost($userinfo['id'],5,$money,$userinfo['balance'],2,1,$remark,$source_id);
  157. \DB::commit();
  158. } catch (Exception $exception) {
  159. \DB::rollBack();
  160. }
  161. }
  162. /*
  163. * 计算间1间2分佣得到的金钱 add by wsl 20190626
  164. * $userinfo 上一级用户信息 array
  165. * $fymoney 当前用户支付的金钱
  166. * $jiantype 间1还是间2
  167. * $proxy_type 广告还是代理
  168. * */
  169. function jianFenYong($userinfo,$fymoney,$jiantype=1,$proxy_type){
  170. //获取间1的配置
  171. $setting=Proxy::getJian1OrJian2Setting($userinfo['level'],$userinfo['province'],$userinfo['city'],$userinfo['district'],$jiantype,$proxy_type);
  172. $jianmoney=0;
  173. if(!empty($setting)&& $userinfo['level']>1) {
  174. //计算间的直推下级
  175. $childcount = User::where('invitor', '=', $userinfo['id'])->count();
  176. //找到判定区间分配佣金//由于第一个设置不要,不需要判定第一个
  177. if ($childcount <= $setting[1]['max']) {
  178. $jianmoney = $fymoney * $setting[0]['pct'] / 100;
  179. } elseif ($childcount > $setting[1]['max'] && $childcount < $setting[2]['max']) {
  180. $jianmoney = $fymoney * $setting[1]['pct'] / 100;
  181. } else {
  182. $jianmoney = $fymoney * $setting[2]['pct'] / 100;
  183. }
  184. }
  185. return $jianmoney;
  186. }
  187. public function getSettingByYearAndMonth(Request $request){
  188. $param = $request->all();
  189. $uid=$param['userid'];
  190. //找出自己的所有直属下级
  191. $childarr=User::where('invitor','=',$uid)->get(['id']);
  192. $childarr=$childarr->toArray();
  193. $arr=[];
  194. if(!empty($childarr)){
  195. foreach ($childarr as $ke=>$ve){
  196. $arr[]=$ve['id'];
  197. }
  198. }
  199. $montstat=Order::getMonthOrYearStat($uid,$arr,'month');
  200. $yearstat=Order::getMonthOrYearStat($uid,$arr,'year');
  201. //找到对应订单并且统计
  202. $userinfo=User::where('id','=',$uid)->first()->toArray();
  203. //根据用户等级找到相关配置
  204. $setting=Proxy::getSettingByAreaAndLevel($userinfo['level']+1,$userinfo['province'],$userinfo['city'],$userinfo['district']);
  205. if(!empty($setting)){
  206. if((!empty($setting['upgrade_business_month'])&&$setting['upgrade_business_month']!='0.000'&&$montstat>=$setting['upgrade_business_month'])
  207. ||(!empty($setting['upgrade_business_year'])&&$setting['upgrade_business_year']!='0.000'&&$yearstat>=$setting['upgrade_business_year'])
  208. ){
  209. //进行升级
  210. User::whereId($uid)->update(['level' => $userinfo['level']+1]);
  211. }
  212. }
  213. }//testInviteNumUpLevel
  214. function getSettingByAreaAndLevel2($level,$province,$city,$district){
  215. $setting=Proxy::where('user_level','=',$level)
  216. ->where('province','=',$province)
  217. ->where('city','=',$city)
  218. ->where('district','=',$district)
  219. ->first();
  220. //如果对应区域设置为空的话则找全局配置
  221. if(empty($setting)){
  222. $setting=Proxy::where('user_level','=',$level)
  223. ->where('province','=',0)
  224. ->where('city','=',0)
  225. ->where('district','=',0)
  226. ->first();
  227. }
  228. $ss=empty($setting)?array():$setting->toArray();
  229. return $ss;
  230. }
  231. public function testInviteNumUpLevel(Request $request){
  232. //查询当前的父级
  233. $param = $request->all();
  234. $parantInfo=User::whereInviteCode($param['invitor'])->first()->toArray();
  235. //根据用户等级找到相关配置
  236. $setting=Proxy::getSettingByAreaAndLevel($parantInfo['level']+1,$parantInfo['province'],$parantInfo['city'],$parantInfo['district']);
  237. if(!empty($setting) &&!empty($setting['upgrade_invite'])){
  238. $setting_count=$setting['upgrade_invite'];
  239. //找出所有推荐的子集个数
  240. $childcount=User::where('invitor','=',$parantInfo['id'])->count();
  241. if($childcount>=$setting_count){
  242. //进行升级
  243. User::whereId($parantInfo['id'])->update(['level' => $parantInfo['level']+1]);
  244. }
  245. }
  246. }
  247. public function setAllUserAccount()
  248. {
  249. $num = 0;
  250. $userList = User::all('id');
  251. foreach ($userList as &$item) {
  252. // $adver = Advertising::whereUid($item->id)
  253. // ->whereIn('status', [3, 4, 9])
  254. // ->select([
  255. // \DB::raw('count(id) count'),
  256. // \DB::raw('sum(total_price) allMoney')
  257. // ])
  258. // ->first();
  259. // $num++;
  260. // $item->count=$adver->count;
  261. // UpgradeService::incUserAccount($item->id,'ad_num',abs($item->count));
  262. // $item->allMoney=$adver->allMoney;
  263. // if ($item->allMoney) {
  264. // UpgradeService::incUserAccount($item->id,'ad_all_money',abs($item->allMoney));
  265. // }
  266. // $item->invite_num = count(User::getAllInvite($item->id, null, null, false)); // 邀请人数
  267. // UpgradeService::incUserAccount($item->id,'invite_num', abs($item->invite_num));
  268. // $item->proxyCommission = AccountLog::whereIn('type', [20, 21, 22, 23])->whereStatus(1)->whereUid($item->id)->sum('money'); // 代理分佣
  269. // UpgradeService::incUserAccount($item->id,'proxy_commission', abs($item->proxyCommission));
  270. // $item->adverCommission = AccountLog::whereIn('type', [10, 11, 12, 13])->whereStatus(1)->whereUid($item->id)->sum('money');//广告费分佣
  271. // UpgradeService::incUserAccount($item->id,'adver_commission', abs($item->adverCommission));
  272. // $item->agentAllmoney = AccountLog::whereIn('type',[7,8])->whereStatus(1)->whereUid($item->id)->sum('money');//总的代理费
  273. // UpgradeService::incUserAccount($item->id,'agent_all_money', abs($item->agentAllmoney));
  274. }
  275. return $num;
  276. }
  277. }