| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- <?php
- namespace App\Modes;
- use App\Service\UpgradeService;
- use Illuminate\Database\Eloquent\Model;
- use PhpParser\Error;
- /**
- * App\Modes\Order
- *
- * @property int $id
- * @property int|null $type 订单类型 1-升级支付 2-购买广告支付 2-点币购买
- * @property int|null $uid 会员ID
- * @property string|null $order_no 订单号
- * @property float|null $price 金额
- * @property int|null $is_pay 订单支付状态(0.未支付,1.已支付)
- * @property string|null $pay_type 支付方式 (wechat微信支付, alipay支付宝支付)
- * @property string|null $pay_no 商户交易号
- * @property string|null $pay_at 支付时间
- * @property string|null $desc 订单描述
- * @property int|null $status 订单状态(0.无效,1.新订单,2.待支付,3.有效)
- * @property int|null $is_deleted 删除状态(1删除,0未删除)
- * @property string|null $create_at 创建时间
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order query()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereCreateAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereDesc($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereIsDeleted($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereIsPay($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereOrderNo($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order wherePayAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order wherePayNo($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order wherePayType($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order wherePrice($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereStatus($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereType($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereUid($value)
- * @mixin \Eloquent
- * @property \Illuminate\Support\Carbon $updated_at
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Order whereUpdatedAt($value)
- */
- class Order extends Model
- {
- protected $table = 'order';
- /**
- * 生成订单号
- * @author lyh
- * @date 2019/3/22
- * @description
- */
- public static function makeOrderNo()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (int)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000) . rand(100, 999);
- return $msectime;
- }
- /**
- * 添加订单
- * @author lyh
- * @date 2019/3/22
- * @param array $orderData
- * @return int
- * @description
- */
- public static function insertData(array $orderData)
- {
- return Order::insertGetId($orderData);
- }
- /*
- * 计算当月或者当年的业绩
- * addby wsl 20190626
- *
- *
- public static function getMonthOrYearStat($uid,$childarr,$stype='month'){
- $startime=$stype=='month'?date('Y-m-01 00:00:00'):date('Y-01-01 00:00:00');
- $curdate=date('Y-m-d H:i:s');
- if(!empty($childarr)){
- $sumprice=Order::where(function ($query) use ($childarr,$uid){
- $query->where('uid','=',$uid)
- ->orWhere(function ($query) use ($childarr){
- $query->whereIn('uid',$childarr)
- ->where('type','=',2);
- });
- })->where('is_pay','=',1)
- ->whereBetween('create_at',[$startime,$curdate])->sum('price');
- }else{
- $sumprice=Order::where('uid','=',$uid)
- ->where('is_pay','=',1)
- ->whereBetween('create_at',[$startime,$curdate])->sum('price');
- }
- return $sumprice;
- }*/
- /*
- * 计算当月或者当年的业绩
- * addby wsl 20190626
- *
- * */
- public static function getMonthOrYearStat($uid,$childarr,$stype='month'){
- $startime=$stype=='month'?date('Y-m-01 00:00:00'):date('Y-01-01 00:00:00');
- $curdate=date('Y-m-d H:i:s');
- $childarr[]=$uid;
-
- //自己和下级升级代理等级的费用
- $upgrade=Upgrade::whereUid($uid)->whereIn('status',[3,9])
- ->whereBetween('created_at',[$startime,$curdate]);
- //自己和下级发布广告的费用
- $adver=Advertising::whereIn('status',[3,4,9])->whereIn('uid',$childarr)
- ->whereBetween('created_at',[$startime,$curdate]);
- //自己申请地区代理的费用
- $proxy=\DB::table('proxy_area as pa')
- ->join('order as o','o.id','=','pa.order_id')
- ->where('pa.uid','=',$uid)->where('pa.status','=',3)
- ->whereBetween('pa.created_at',[$startime,$curdate]);
-
- // 2021/8/2
- $coin = Order::whereUid($uid)->whereIn('status',[3])->whereIn('type',[3])->whereBetween('created_at',[$startime,$curdate]);
-
- //冻结时间内的收益不算
- $frozentimelist=Frozen::getUserFrozentimeList($uid,$startime,$curdate);
- if(!empty($frozentimelist)){
- foreach($frozentimelist as $item){
- $upgrade->whereBetween('created_at',[$item['startime'],$item['endtime']],'and',true);
- $adver->whereBetween('created_at',[$item['startime'],$item['endtime']],'and',true);
- $proxy->whereBetween('pa.created_at',[$item['startime'],$item['endtime']],'and',true);
- $coin->whereBetween('created_at',[$item['startime'],$item['endtime']],'and',true);
- }
- }
- //计算审核并且已支付的订单
- $upgradesum=$upgrade->sum('money');
- $proxyareasum=$proxy->sum('o.price');
- $adversum=$adver->sum('total_price');
- $coinsum=$coin->sum('price');
- $sum=round($adversum+$proxyareasum+$upgradesum+$coinsum,3);
- return $sum;
- }
- /**
- * 验证订单支付结果
- * @author lyh
- * @date 2019/3/22
- * @param $out_trade_no
- * @param $price
- * @param $status
- * @param int $payType 支付方式 0-未支付 1-现金 2-支付宝 3-微信 4-银行转账 5-桃牛支付
- * @description
- * $order->type : 订单类型 1-升级支付 2-购买广告支付 2-点币购买
- */
- public static function verifyOrder($out_trade_no, $price, $payType,$trade_no=null)
- {
- // 更新订单状态
- $order = Order::whereOrderNo($out_trade_no)->first();
- if (empty($order)) {
- ErrorLog::saveMsg('订单号[' . $out_trade_no . ']不存在');
- }
- if($order->is_pay==1){
- exit;
- }
- $updateOrder = Order::whereOrderNo($out_trade_no)->update(['is_pay' => 1, 'status' => 1,'pay_no'=>$trade_no]);
- if (empty($updateOrder)) {
- ErrorLog::saveMsg('更新订单[' . $out_trade_no . ']状态失败');
- }
- // 保存账号流水
- // AccountLog::insert([
- // 'uid' => $order->uid,
- // 'type' => 10,
- // 'status' => 1,
- // 'money_type' => 2,
- // 'money' => $price,
- // 'current_money' => self::getCurentMoney($order->type, $order->uid),
- // 'remark' => self::getRemark($uid = $order->uid, $type = $order->type, $price = $order->price)
- // ]);
- //自动升级 addbywsl 201900626 start
- //找出自己的所有直属下级
- $uid=$order->uid;
- $childarr=User::where('invitor','=',$uid)->get(['id']);
- if(!empty($childarr)){
- $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_month']))
- ||(!empty($setting['upgrade_business_year'])&&$setting['upgrade_business_year']!='0.000'&&$yearstat>=$setting['upgrade_business_year']&&!empty($setting['upgrade_business_year']))
- ){
- //进行升级
- $levelarr=['A','B','C','D','E','F','G','H'];
- $upGradeData = [
- 'uid' => \Auth::id(),
- 'user_level' => \Auth::user()->level,
- 'apply_level' => $userinfo['level']+1,
- 'money' => 0,
- 'remark' => sprintf("用户推荐量达标,由[%s]级升级到等级[%s]级",$levelarr[$userinfo['level']-1], $levelarr[$userinfo['level']]),
- 'status' => 3,
- 'upgrade_way' => 4,
- 'province'=>$userinfo['province'],
- 'city'=>$userinfo['city'],
- 'district'=>$userinfo['district'],
- ];
- Upgrade::insertGetId($upGradeData);
- User::whereId($uid)->update(['level' => $userinfo['level']+1]);
- }
- }
- }
-
- if ($order->type == 2) {
- // todo 广告位
- Advertising::updateAdver($out_trade_no);
- } else if ($order->type == 1) {
- // todo 升级支付
- Upgrade::updateByPay($out_trade_no);
- } else if ($order->type == 3) {
- //点币购买
- UserCoin::updateCoin($order->id);
- // 保存账号流水
- /*AccountLog::insert([
- 'uid' => $order->uid,
- 'type' => 32,
- 'status' => 1,
- 'money_type' => 1,
- 'money' => $order->price,
- 'current_money' => \Auth::user()->coin,
- 'remark' => self::getRemark($uid = $order->uid, 32, $price = $order->price)
- ]);*/
- } else if ($order->type == 4) {
- ProxyArea::updateByVarify($order->id);
- } else {
- ErrorLog::saveMsg('订单类型不存在');
- }
- }
- /**
- * 分佣算法 add by wsl 20190626
- * $fymoney 用户支付的钱
- * $uid 当前支付的用户id
- * $paytype 类型,与order表的type对应
- * */
- public static function UserFenYong($fymoney,$uid,$paytype)
- {
- $fymoney=floatval($fymoney);
- if(empty($fymoney)){
- return;
- }
- $userinfo = User::whereId($uid)->first()->toArray();
- $proxy_type = $paytype == 2 ? 2 : 1;
- //全局分佣,用户冻结的情况下不分佣ok
- Order::getAreaFenyong($userinfo, $fymoney, $proxy_type);
- //1.获取直推信息
- if (!empty($userinfo['invitor'])) {
- $parentinfo = User::whereId($userinfo['invitor'])->first();
- }
- if (!empty($parentinfo->id)) {
- $parentinfo = $parentinfo->toArray();
- //获取直属上级的配置,冻结的不参与分佣ok
- $parentsetting = Proxy::getSettingByAreaAndLevel($parentinfo['level'], $parentinfo['province'], $parentinfo['city'], $parentinfo['district']);
- if (!empty($parentsetting) && $parentinfo['status'] == 1) {
- //算分佣
- if ($paytype == 2) {
- $parentpersent = $parentsetting['adver_invite'];
- $rmarkstr = '广告费';
- $ztlogtype=10;
- $j1logtype=11;
- $j2logtype=12;
- } else {
- $parentpersent = $parentsetting['proxy_invite'];
- $rmarkstr = '代理费';
- $ztlogtype=20;
- $j1logtype=21;
- $j2logtype=22;
- }
- //比例不为0才算分佣
- if (!empty($parentpersent) && $parentpersent > 0) {
- $parentmoney = $parentpersent / 100 * $fymoney;
- $remark = '用户【id:' . $uid . '】,支付' . $rmarkstr . ',' . $fymoney . '元,得到直推佣金【' . $parentmoney . '】';
- //给直推上级添加余额并生成记录
- Order::updateUserAndinsertLog($parentinfo, $parentmoney, $remark, $uid,$ztlogtype);
- // zch 新增累加用户账户统计信息
- if ($rmarkstr=='广告费') {
- UpgradeService::incUserAccount($parentinfo['id'],'adver_commission',$parentmoney);
- }elseif($rmarkstr=='代理费'){
- UpgradeService::incUserAccount($parentinfo['id'],'proxy_commission',$parentmoney);
- }
- Order::sendFenyongMsg('直推'.$rmarkstr.'分佣',$parentmoney,$parentinfo['id']);
- }
- }
- //间分佣算法 start冻结的不参与分佣ok
- if (!empty($parentinfo['invitor'])) {
- $jian1info = User::whereId($parentinfo['invitor'])->first();
- if (!empty($jian1info->id) && $jian1info->status == 1) {
- $jian1info = $jian1info->toArray();
- //间1分佣
- $jian1money = Order::jianFenYong($jian1info, $fymoney, 1, $proxy_type);
- if (!empty($jian1money)) {
- $remark = '用户【id:' . $uid . '】,支付' . $rmarkstr . ',' . $fymoney . '元,得到间1佣金【' . $jian1money . '】';
- //生成记录
- Order::updateUserAndinsertLog($jian1info, $jian1money, $remark, $uid,$j1logtype);
- // zch 新增累加用户账户统计信息
- if ($rmarkstr=='广告费') {
- UpgradeService::incUserAccount($jian1info['id'],'adver_commission',$jian1money);
- }elseif($rmarkstr=='代理费'){
- UpgradeService::incUserAccount($jian1info['id'],'proxy_commission',$jian1money);
- }
- Order::sendFenyongMsg("间1".$rmarkstr.'分佣',$jian1money,$jian1info['id']);
- }
- }
- //间2的分佣 冻结的不参与分佣ok
- if (!empty($jian1info['invitor'])) {
- $jiantwoinfo = User::whereId($jian1info['invitor'])->first();
- if (!empty($jiantwoinfo->id) && $jiantwoinfo->status == 1) {
- $jiantwoinfo = $jiantwoinfo->toArray();
- $jiantwomoney = Order::jianFenYong($jiantwoinfo, $fymoney, 2, $proxy_type);
- if (!empty($jiantwomoney)) {
- $remark = '用户【id:' . $uid . '】,支付' . $rmarkstr . ',' . $fymoney . '元,得到间2佣金【' . $jiantwomoney . '】';
- //生成记录
- Order::updateUserAndinsertLog($jiantwoinfo, $jiantwomoney, $remark, $uid,$j2logtype);
- // zch 新增累加用户账户统计信息
- if ($rmarkstr=='广告费') {
- UpgradeService::incUserAccount($jiantwoinfo['id'],'adver_commission',$jiantwomoney);
- }elseif($rmarkstr=='代理费'){
- UpgradeService::incUserAccount($jiantwoinfo['id'],'proxy_commission',$jiantwomoney);
- }
- Order::sendFenyongMsg('间2'.$rmarkstr.'分佣',$jiantwomoney,$jiantwoinfo['id']);
- }
- }
- }
- }
- //间分佣算法 end
- }
- }
- public static function sendFenyongMsg($profittype,$money,$uid){
- $msg_content=MsgTemplate::getTemplateContent(11,['profittype'=>$profittype,'money'=>$money]);
- $msgarr = ['uid'=>$uid,'title' => '佣金到账通知', 'type' => 5, 'content' => $msg_content];
- UserMsg::SendGetui($msgarr);
- }
- /*
- * wsl
- * 获取区域代理的分佣
- * $userinfo 当前所发生交易的用户
- * */
- public static function getAreaFenyong($userinfo,$fymoney,$proxy_type){
- $money=0;
- //找到该区的所有区域代理
- /*$proxyarea=ProxyArea::whereProvince($userinfo['province'])
- ->whereCity($userinfo['city'])->OrwhereDistrict($userinfo['district'])
- ->whereStatus(3)->distinct()->get();*/
- $proxyarea=ProxyArea::whereStatus(3)
- ->where(function($query) use ($userinfo){
- $query->orWhere(function($q)use($userinfo){
- $q->where('province','=',$userinfo['province'])
- ->where('city','=',$userinfo['city'])
- ->where('district','=',$userinfo['district']);
- })->orWhere(function($q2)use ($userinfo){
- $q2->where('province','=',$userinfo['province'])
- ->where('city','=',$userinfo['city']);
- });
- })->distinct()->get();
- if(!empty($proxyarea)){
- //循环
- foreach($proxyarea as $key=>$item){
- //查找每个用户的配置信息冻结账户不参与分佣
- $areauinfo=User::whereId($item->uid)->whereStatus(1)->first();
- if(!empty($areauinfo->id)){
- $areauinfo=$areauinfo->toArray();
- $setting=Proxy::getSettingByAreaAndLevel($areauinfo['level'],$areauinfo['province'],$areauinfo['city'],$areauinfo['district']);
- //根据配置算出分佣,并添加进记录和余额
- if($proxy_type==2){
- $money=$setting['adver_invite_global']*$fymoney/100;
- $re='广告费';
- $logtype=13;
- }else{
- $money=$setting['proxy_invite_global']*$fymoney/100;
- $re='代理费';
- $logtype=23;
- }
- $remark='用户【id:'.$userinfo['id'].'】,支付'.$re.','.$fymoney.'元,得到全局分佣佣金【'.$money.'】';
- //修改记录
- Order::updateUserAndinsertLog($areauinfo,$money,$remark,$userinfo['id'],$logtype);
- // zch 新增累加用户账户统计信息
- if ($re=='广告费') {
- UpgradeService::incUserAccount($areauinfo['id'],'adver_commission',$money);
- } elseif ($re=='代理费') {
- UpgradeService::incUserAccount($areauinfo['id'],'proxy_commission',$money);
- }
- Order::sendFenyongMsg('全局'.$re.'分佣',$money,$areauinfo['id']);
- }
- }
- }
- return $money;
- }
- /**
- * 更新user表的月并且生成记录 add by wsl 20190626
- * $userinfo 要更新的用户信息 array
- * $money 变动金额
- * $remark 备注
- * $source_id 来源id 13全局广告费分佣 23全局代理费分佣 10直推广告费分佣 20直推代理费分佣 11间1广告分佣 21间1代理费分佣 12间2广告费分佣 22间2代理费分佣
- * */
- public static function updateUserAndinsertLog($userinfo,$money,$remark,$source_id,$type=5){
- \DB::beginTransaction();
- try {
- User::whereId($userinfo['id'])->update(['balance' => $userinfo['balance']+$money]);
- AccountLog::saveDataByPost($userinfo['id'],$type,$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 广告还是代理
- * */
- public static 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'] && $childcount < $setting[2]['max'] && $setting[1]['max']>0 && $setting[2]['max']>0) {
- $jianmoney = $fymoney * $setting[1]['pct'] / 100;
- } elseif($childcount>$setting[2]['max']&&$setting[2]['max']>0) {
- $jianmoney = $fymoney * $setting[2]['pct'] / 100;
- }else{
- $jianmoney = $fymoney * $setting[0]['pct'] / 100;
- }
- }
- return $jianmoney;
- }
- /**
- * @author lyh
- * @date 2019/3/25
- * @param int $pay_type 支付方式 0-未支付 1-现金 2-支付宝 3-微信 4-银行转账 5-桃牛支付
- * @description
- */
- public static function getPayType($pay_type)
- {
- $value = ['未支付', '现金', '支付宝', '微信', '银行转账', '桃牛支付'];
- $value[21] = '支付宝';
- $value[31] = '微信';
- return $value[$pay_type];
- }
- private static function getCurentMoney($type, $uid)
- {
- $user = User::find($uid);
- if ($type = 1) {
- return $user->coin;
- } else {
- return $user->balance;
- }
- }
- private static function getRemark(int $uid, int $type, float $price)
- {
- $typeMsg = AccountLog::getTypeMsg($type);
- return "用户【id:{$uid}】,用途-{$typeMsg},金额【{$price}】";
- }
- }
|