1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189 |
- <?php
- require_once 'DB_PDO.class.php';
- require_once 'TBK.class.php';
- require_once 'confv2.class.php';
- define("DETAILDEBUG", 0);//打印流程详细信息,如有需要,设为1
- define("LIMIT_NUM", 0);//限制导入条数,0不限制,方便测试,上线设为0
- define("PUSH_URL",'https://liedou.726p.com/api/v2/MessagePush/rebatepush');//push接口地址
- define("ACT_PUSH_URL",'https://liedou.726p.com/api/v2/MessagePush/pullnew_push');//拉新push接口地址
- define("REFUND_PUSH_URL",'https://liedou.726p.com/api/v2/MessagePush/act_push');//退款push接口地址
- define("ACT_START", '2018-12-25 00:00:00');//
- define("ACT_END", '2018-12-31 23:59:59');//
- define("ACT_GOODS_IMG", 'http://imgs.726p.com/tbk/upload/advImg2018-12-24/135037/kx-5c2073adbe478.png');//
- /**
- * 导入及更新 order/order_rebate数据 + 淘宝拉新活动
- */
- #设置时区
- date_default_timezone_set('PRC');
- set_time_limit(0);
- ini_set('memory_limit','1024M');
- function readDataFromDb(){
- # 记录脚本开始时间
- $scriptStart = date("Y-m-d H:i:s");
- # 1.获取6分钟内订单
- //$stime = date('Y-m-d H:i:s',strtotime('2018-08-07'));
- $stime = date('Y-m-d H:i:s', time()-150);
- $sql = "SELECT * from liedou_taoke_orders where update_time > :stime ";
- $dumpsql = str_replace(':stime', "'".$stime."'", $sql);
- echo "\n\n".$dumpsql."\n";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':stime'=>$stime));
- $orderResult = $stmt->fetchAll(PDO::FETCH_ASSOC);
- # 遍历订单
- # 统计
- $res_count = array(
- 'total_num' => 0,
- 'generalinsert_num' => 0,
- 'generalupdate_num' => 0,
- 'nouserinsert_num' => 0,
- 'nouserupdate_num' => 0,
- 'fail_insert_num' => 0,
- 'fail_insert_ids' => '',
- 'fail_insertno_num' => 0,
- 'fail_insertno_ids' => '',
- 'fail_update_num' => 0,
- //'fail_update_ids' => '',
- 'fail_updateno_num' => 0,
- //'fail_updateno_ids' => '',
- );
- $rewardOrder = array(); //拉新订单记录
- $nums = 0;
- foreach( $orderResult as $key=>$orderDetail ){
- $nums++;
- $res_count['total_num']++;
- echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
- if( DETAILDEBUG == 1){
- echo 'orderDetail:';
- }
- //create_time,click_time,item_title,num_iid,seller_nick,seller_shop_title,item_num,price,tk_status,order_type,income_rate,commission_rate,pay_price,pub_share_pre_fee,alipay_total_price,commission,earning_time,total_commission_rate,total_commission_fee,subsidy_rate,subsidy_fee,subsidy_type,terminal_type,tk3rd_type,trade_parent_id,trade_id,auction_category,site_id,site_name,adzone_id,adzone_name,relation_id,special_id
- $ptype = $orderDetail["ptype"];
- $order_id=$orderDetail["trade_id"];
- $update_time=$orderDetail["update_time"];
- $order_create_at=$orderDetail["create_time"];
- $order_click_at=$orderDetail["click_time"];
- $order_balance_at=$orderDetail["earning_time"];
- $goods_id=$orderDetail["num_iid"];
- $goods_name=$orderDetail["item_title"];
- $goods_name=str_replace(['"',"'"],'',$goods_name);
- $num=$orderDetail["item_num"];
- $price=intval($orderDetail["price"]*1000/10);
- $sum_price=intval($orderDetail["alipay_total_price"]*1000/10);
- $statusDesc=$orderDetail["tk_status"];
- $typeDesc=$orderDetail["order_type"];
- $orderStatus = 1;
- $type=1;
- if($statusDesc=="12"){
- $orderStatus=1;
- }
- elseif($statusDesc=="13"){
- $orderStatus=0;
- }
- elseif($statusDesc=="3"){
- $orderStatus=2;
- } else {
- continue; //其他状态:待付款,已付定金,不做处理;
- }
- if($typeDesc=="聚划算"){
- $type=0;
- }
- elseif($typeDesc=="淘宝"){
- $type=1;
- }
- elseif($typeDesc=="天猫"){
- $type=2;
- }
- elseif($typeDesc=="京东"){
- $type=3;
- }
- elseif($typeDesc=="抖音"){
- $type=4;
- }
- elseif($typeDesc=="拼多多"){
- $type = 5;
- }
- elseif($typeDesc=='唯品会'){
- $type = 6;
- }
- $adzone_id=$orderDetail["adzone_id"];
- $relation_id = $orderDetail["relation_id"];
- $special_id = $orderDetail["special_id"];
- $ceil_income=round($orderDetail["pub_share_pre_fee"], 2);
- $predict_income=$ceil_income*100;
- $predict_income=ceil($predict_income*conf::$RATE_CONF["globalzk"]);
- echo 'order_id:'.$order_id.'|| goods_id:'.$goods_id.'|| orderstatus:'.$orderStatus.'|| order_balance_at:'.$order_balance_at;
- echo '|| predict_income:'.$predict_income.'|| adzone_id:'.$adzone_id.'|| special_id:'.$special_id.'|| relation_id:'.$relation_id."\n";
- $resultone=UserInfo($special_id, $relation_id, $adzone_id, $ptype);
- if( DETAILDEBUG == 1){
- echo 'userinfo:';
- var_dump($resultone);
- echo "\n";
- }
- if($sum_price!=0){
- if($predict_income<5){
- $predict_income=5;
- }
- }
- if( !empty($resultone) ){
- echo "User:存在\n";
- $orderExists=orderMysql($order_id,$goods_id);
- if( empty($orderExists) ){
- echo "action:插入操作\n";
- #获取层级规则信息
- $userAllInfo=userLevelandPrentInfo($resultone);
- echo "<pre>\nuserAllInfo";
- print_r($userAllInfo);
- if( DETAILDEBUG == 1){
- echo 'userallinfo:'."\n";
- var_dump($userAllInfo);
- }
- $userList = $userAllInfo['userList']; //4层 每层userId
- $userLevel = $userAllInfo['userLevel']; //4层 每层userlevel
- # 从数据库获取规则
- $rebateRuleInfo = getRebateRule($userAllInfo['levelRule']);
- echo "\nRuleInfo:";
- print_r($rebateRuleInfo);
- $RuleArr = explode(',',$rebateRuleInfo['rebate_rule']); //4层 每层分佣比例
- $TypeArr = explode(',',$rebateRuleInfo['rebate_type']); //4层 每层分佣级别
- #获取每一层返利
- $moneyInfo=orderMoney($predict_income, $RuleArr); //4层 每层分佣钱数
- echo "\nmoneyInfo:";
- print_r($moneyInfo);
-
- if( DETAILDEBUG == 1){
- echo 'rebateInfo'."\n";
- var_dump($moneyInfo);
- }
- if($ptype == 2 || $ptype == 3 || $ptype == 4 || $ptype == 5){
- $goods_img = $orderDetail["img"];
- } else {
- $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
- if( !empty($detail) ){
- $goods_img=$detail->pict_url;
- } else {
- echo json_encode($detail, 256)."\n";
- echo "淘宝客商品详情接口未获取到商品信息,开始调用淘宝客物料搜索接口"."\n";
- # 调用淘宝客【推广者-物料搜索】api
- $goodsKey = "https://uland.taobao.com/item/edetail?id=".$goods_id;
- $goodsList = TBK::search($goodsKey);
- if (!empty($goodsList['list'])) {
- $goodsDetail = $goodsList['list'][0];
- $goods_img = $goodsDetail->pict_url;
- echo "通过物料搜索接口获取到商品图"."\n";
- } else {
- echo "物料搜索接口仍未获取到商品图".json_encode($goodsList, 256)."\n";
- $goods_img="";
- }
- }
- }
- try{
- $_PDO->beginTransaction();
- #判断是否是新手第一单
- //$newFirstRes = newCustFirstOrder($userList[0]);
- #订单表插入
- $res=generateSqlInsertOrder($goods_img,$goods_name,$userList[0],$order_id,$adzone_id,$goods_id,$num,$price,$sum_price,$predict_income,$moneyInfo[0],$orderStatus,$type,$userLevel[0],$order_create_at,$order_click_at,$order_balance_at);
- if($res){
- $res_count['generalinsert_num']++;
- }else{
- $res_count['fail_insert_num']++;
- $res_count['fail_insert_ids'].=$order_id.'|';
- }
- echo 'orderinsert:'.$res;
- echo "\n";
-
- if($sum_price>=0 && $res){
- foreach($moneyInfo as $k=>$v){
- if($v>0){
- #order_rebate表插入父级佣金
- $rebaseType=$TypeArr[$k];
- $res=generateSqlInsertRebateSelf($userLevel[$k],$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$userList[$k],$rebaseType,$moneyInfo[$k],$sum_price,$orderStatus,$goods_id,$predict_income, $ptype);
- # 推送
- $flag_time = time() - strtotime($order_create_at);
- if($res && $flag_time<3600){
- $params = array();
- $params['user_id'] = $userList[$k];
- $params['type'] = $rebaseType==1 ? 1 : 2;
- $params['money'] = round($moneyInfo[$k]/100,2);
- $push_res = curl_post( PUSH_URL, $params);
- }
- echo 'rebateinsert_'.$k.':'.$res;
- echo "\n";
- }
- }
- if($order_create_at >= ACT_START && $order_create_at <= ACT_END){
- # 判断是否是拉新首单
- $isNew = 0;
- $isTooNew = 0;
- if(array_key_exists($order_id, $rewardOrder)){
- $isTooNew = 1;
- }else{
- $isNew = isNewUser($userList[0]);
- }
- if($isTooNew || $isNew){
-
- //记录拉新订单分佣情况
- if($isNew){
- $rewardOrder[$order_id] = $moneyInfo;
- }else{
- $rewardOrder[$order_id] = arraySums($rewardOrder[$order_id],$moneyInfo);
- }
- //获取下一个分佣的订单
- $nkey = $nums;
- //若当前订单和下一笔订单号一样,继续循环,直到本拉新订单分佣rewardOrder[$order_id]统计完毕
- if(isset($orderResult[$nkey]['order_sn']) && $order_id == $orderResult[$nkey]['order_sn']){
-
- }else{
- // 分奖励佣金 < --------- Start --------- >
- $reward_n = 0; // 记录奖励层数 最多两层
- $rewardInfo = $rewardOrder[$order_id]; //本订单实际分佣情况
- foreach($rewardInfo as $k=>$v){
- if($v>0 && $reward_n<2){
- # 插入奖励
- $rebaseType=6; //奖励佣金
- # 计算奖励佣金
- $reward = getReward($rewardInfo[$k]);
- $goods_name_new = $k==0 ? '恭喜您获得新用户首单奖励' : '恭喜您获得拉新首单奖励';
- $goods_img_new = ACT_GOODS_IMG;
- $res=rewardOrderInsert($userLevel[$k],$order_balance_at,$order_create_at,$goods_img_new,$goods_name_new,$order_id,$userList[$k],$rebaseType,$reward,$sum_price,$orderStatus,$goods_id,$predict_income);
- # 奖励推送
- if($res){
- $params = array();
- $params['user_id'] = $userList[$k];
- $params['type'] = $k==0 ? 3 : 4; //奖励推送 3新人 4上级
- $params['money'] = round($reward/100,2);
- $push_res = curl_post( ACT_PUSH_URL, $params);
- }
- echo 'rewardinsert_'.$reward_n.':'.$res;
- echo "\n";
- # 记录奖励层数
- $reward_n++;
- }
- }
- # 记录新人首单信息
- if($res){
- //获取运营商
- $m = strpos($userAllInfo['levelRule'], '3');
- $operate_id = $userList[$m];
- $res2 = pullNewInsert($userList[0],$operate_id,$order_id,$order_create_at,$orderStatus);
- //获取当前运营商拉新人数
- $pullCount = getPullNew($operate_id);
- if($pullCount==10 || $pullCount==20 || $pullCount==30){
- //符合推送条件
- $params = array();
- $params['user_id'] = $operate_id;
- $params['type'] = 5; //运营商拉新人数推送
- $params['count'] = $pullCount;
- $push_res = curl_post( ACT_PUSH_URL, $params);
- }
- }
- }
- }
- }
- }
-
- //津贴单独计算
- # 获取顶级运营商-控制中心 ** ++ 运营商,满足裂变3个运营商以上分团队奖励(新津贴)
- if($sum_price>0){
- $mks = strpos($userAllInfo['levelRule'], '3');
- if($mks !== false){
- $YysManageInfo = YysManageInfo( $userList[$mks] );
- if( !empty($YysManageInfo) ){
- $rebaseType=$TypeArr[$mks];
- $YysManageRabate = round($predict_income*$YysManageInfo['rate']);
- if($YysManageRabate < 1){
- $YysManageRabate = 1;
- }
- $res = YysManageInsertRebate(4,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$YysManageInfo['user_id'],$rebaseType,$YysManageRabate,$sum_price,$orderStatus,$goods_id,$predict_income,$ptype);
- echo 'rebateinsert_YysManage:'.$res;
- echo "\n";
- }
- #+++新逻辑 **团队奖励(新津贴)
- /*
- $preYysInfo = preYysInfo( $userList[$mks] );
- if( !empty($preYysInfo) ){
- $rebaseType=$TypeArr[$mks];
- $YysNewRabate = round($predict_income*$preYysInfo['rate']);
- if($YysNewRabate < 1){
- $YysNewRabate = 1;
- }
- $res = YysManageInsertRebate(3,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$preYysInfo['user_id'],$rebaseType,$YysNewRabate,$sum_price,$orderStatus,$goods_id,$predict_income);
- echo 'rebateinsert_preYysInfo:'.$res;
- echo "\n";
- }
- */
- }
- }
- $_PDO->commit();
- }catch(PDOException $e){
- echo 'err_msg'.$e->getMessage()."\n";
- $_PDO->rollback();
- }
- //echo "-----------------insertEnd--------------------\n";
- }
- else{
- try{
- $_PDO->beginTransaction();
- //更新order/order_rebate表
- echo "action:更新操作\n";
- $res=generateSqlupdate($orderStatus,$order_balance_at,$order_id,$goods_id);
- /*** 增加拉新更新 Start ***/
- if($order_create_at >= ACT_START && $order_create_at <= ACT_END){
- $isUpOk = pullNewIsUpOk($orderStatus, $order_id, $resultone['user_id']);
- echo 'isUpOk:'.$isUpOk."\n";
- if($isUpOk) $pull_up = pullNewUpdate($orderStatus, $order_id);
- }
- /*** 增加拉新更新 End ***/
- /*** 增加退款推送 Start***/
- if($res && $orderStatus==0){
- $refundPush = refundPush($order_id);
- }
- /*** 增加退款推送 END***/
- if($res){
- $res_count['generalupdate_num']++;
- }else{
- $res_count['fail_update_num']++;
- //$res_count['fail_update_ids'].=$order_id.'|';
- }
- echo "更新数据-> orderstatus:".$orderStatus." order_balance_at:".$order_balance_at;
- echo "\n";
- echo "order/reabte_update:".$res;
- echo "\n";
- //echo "-----------------updateEnd-----------------------\n";
- $_PDO->commit();
- }catch(PDOException $e){
- echo 'err_msg'.$e->getMessage()."\n";
- $_PDO->rollback();
- }
- }
- //echo "-----------------IssetUserEnd:--------------------\n";
- }
- else{
- $_PDO->beginTransaction();
- try{
- #user不存在的处理
- echo "User:不存在\n";
- $ordernoExists=orderMysql($order_id,$goods_id);
-
- if( empty($ordernoExists) ){
- if($ptype == 2){
- $goods_img = $orderDetail["img"];;
- } else {
- $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
- if( !empty($detail) ){
- $goods_img=$detail->pict_url;
- } else {
- echo json_encode($detail, 256)."\n";
- echo "淘宝客商品详情接口未获取到商品信息,开始调用淘宝客物料搜索接口"."\n";
- # 调用淘宝客【推广者-物料搜索】api
- $goodsKey = "https://uland.taobao.com/item/edetail?id=".$goods_id;
- $goodsList = TBK::search($goodsKey);
- if (!empty($goodsList['list'])) {
- $goodsDetail = $goodsList['list'][0];
- $goods_img = $goodsDetail->pict_url;
- echo "通过物料搜索接口获取到商品图"."\n";
- } else {
- echo "物料搜索接口仍未获取到商品图".json_encode($goodsList, 256)."\n";
- $goods_img="";
- }
- }
- }
- $res=generateSqlInsertOrderOther($goods_img,$goods_name,$order_id,$adzone_id,$goods_id,$num,$price,$sum_price,$predict_income,0,$orderStatus,$type,$order_create_at,$order_click_at,$order_balance_at);
- if($res){
- $res_count['nouserinsert_num']++;
- }else{
- $res_count['fail_insertno_num']++;
- $res_count['fail_insertno_ids'].=$order_id.'|';
- }
- echo "action:插入操作\n";
- echo "orderinsert:".$res;
- echo "\n";
-
- $_PDO->commit();
- }
- else{
- $res=generateSqlUpdateOther($orderStatus,$order_balance_at,$order_id,$goods_id);
- if($res){
- $res_count['nouserupdate_num']++;
- }else{
- $res_count['fail_updateno_num']++;
- //$res_count['fail_updateno_ids'].=$order_id.'|';
- }
- echo "action:更新操作\n";
- echo "orderupdate:".$res;
- echo "\n";
- $_PDO->commit();
- }
- //echo "-----------------EmptyUserEnd:--------------------\n";
-
- }catch(PDOException $e){
- echo 'err_msg'.$e->getMessage()."\n";
- $_PDO->rollback();
- }
- }
- echo "/ ** order{$res_count['total_num']}-End ** /";
- echo "\n\n\n";
- if(LIMIT_NUM >0 && $res_count['total_num'] == LIMIT_NUM) break;
- }
- print_r($res_count);
- echo "\n拉新订单:\n";
- print_r($rewardOrder);
- echo "\n脚本开始时间:". $scriptStart;
- echo "\n脚本结束时间:". date("Y-m-d H:i:s");
- }
- # 查找运营中心规则改版0425
- function YysManageInfo( $user_id ){
- $sql = "SELECT user_id,parent_user_id,level FROM user_level WHERE user_id=:user_id ";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$user_id));
- $result = $stmt->fetch(PDO::FETCH_ASSOC);
- if( empty($result) ) return false;
- $data = array();
- #如果自己是运营中心
- if( $result['level'] == 4 ){
- #老运营
- //if(!isNewYysManage($user_id)){
- $data['user_id'] = $user_id;
- $data['rate'] = 0.01;
- return $data;
- //}else{
- //return new_c_rate($user_id);
- //}
- }else{
- #上级信息
- $sql = "SELECT id FROM user_level WHERE user_id=:user_id and level=4";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$result['parent_user_id']));
- $result1 = $stmt->fetch(PDO::FETCH_ASSOC);
- if(empty($result1)){
- return false; //上级若不是运营中心
- }
- #老运营
- //if(!isNewYysManage($result['parent_user_id'])){
- $data['user_id'] = $result['parent_user_id'];
- $data['rate'] = 0.01;
- return $data;
- //}else{
- //return new_c_rate($result['parent_user_id']);
- //}
- }
- return false;
- }
- #新运营中心津贴比例
- function new_c_rate($user_id){
- #新运营中心
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $sql = "SELECT count(1) as nums FROM user_level_change_info where upgrade_to_level=3 and pre_yys_id = :pre_yys_id and upgrade_time>'2019-04-26' ";
- //$sql = "SELECT count(1) as nums from user_level where parent_user_id=:parent_user_id and level>=3 ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':pre_yys_id'=>$user_id));
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- $data = array();
- if(isset($res['nums']) && $res['nums']>=3){
- $data['user_id'] = $user_id;
- if($res['nums']>=10){
- $data['rate'] = 0.03;
- }elseif($res['nums']>=6){
- $data['rate'] = 0.02;
- }elseif($res['nums']>=3){
- $data['rate'] = 0.01;
- }
- return $data;
- }
- return false;
- }
- function YysInfo( $user_id ){
- $sql = "SELECT user_id,parent_user_id,level FROM user_level WHERE user_id=:user_id ";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$user_id));
- $result = $stmt->fetch(PDO::FETCH_ASSOC);
- if( empty($result) ) return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
- if($result['level'] >= 3){
- return $result;
- }else{
- if( empty($result['parent_user_id']) ){
- return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
- }
- return YysInfo( $result['parent_user_id']);
- }
- return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
- }
- function UserInfo($special_id, $relation_id, $adzone_id, $ptype=1){
- if($ptype == 2 ||$ptype == 3 ||$ptype == 4 ||$ptype == 5){
- return JdUserInfo($relation_id);
- }
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- //新加逻辑 2020-7-8 封relation不分佣
- $sql = "SELECT value FROM global_variable WHERE variable_key ='relation_blacklist' ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $result = $stmt->fetch(PDO::FETCH_ASSOC);
- if( !empty($result) ){
- if( strstr($result['value'], $relation_id) ){
- return false;
- }
- }
- if(in_array($adzone_id,[123456])){
- return false;
- }
- if($adzone_id=='101804100229'){
- if(!empty($relation_id)){
- $sql = "SELECT user_id FROM user_special_relation_local WHERE relation_id = '{$relation_id}'";
- }elseif(!empty($special_id)){
- $sql = "SELECT user_id FROM user_special_relation WHERE special_id = '{$special_id}'";
- }
- }else{
- if(!empty($relation_id)){
- $sql = "SELECT user_id FROM user_special_relation WHERE relation_id = '{$relation_id}'";
- }elseif(!empty($special_id)){
- $sql = "SELECT user_id FROM user_special_relation WHERE special_id = '{$special_id}'";
- }else{
- return false;
- }
- }
-
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
- if(!empty($resultone)){
- $sql="SELECT * FROM user_level WHERE user_id = :user_id";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$resultone['user_id']));
- $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
- return $userResult;
- }
- else{
- return false;
- }
- }
- function JdUserInfo($relation_id){
- if(!$relation_id){
- return false;
- }
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $sql = "SELECT id FROM users WHERE invite_code = '{$relation_id}'";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
- if(!empty($resultone)){
- $sql="SELECT * FROM user_level WHERE user_id = :user_id";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$resultone['id']));
- $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
- return $userResult;
- }
- else{
- return false;
- }
- }
- function userLevelInfo($user_id){
- $sql="SELECT * FROM user_level WHERE user_id = :user_id ";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$user_id));
- $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
- if($resultone){
- $myInfo=["user_id"=>$user_id,"level"=>$resultone['level'],"parent_user_id"=>$resultone['parent_user_id']];
- return $myInfo;
- }
- else{
- return false;
- }
- }
- function userMyInfo($user_id){
- $sql="SELECT * FROM users WHERE id = :user_id ";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$user_id));
- $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
- if($resultone){
- $myInfo['user_id'] = $user_id;
- $myInfo['origin'] = $resultone['origin'];
- return $myInfo;
- }
- else{
- return false;
- }
- }
-
- # 获取4层用户级别基础信息
- function userLevelandPrentInfo($resultone){
- $resultInfo = array();
- # self
- $userId=$resultone['user_id'];
- $userLevel=$resultone['level'];
- if($userLevel>=3){
- $resultInfo['userList'] = [$userId,0,0,0];
- $resultInfo['levelRule'] = 3000;
- $resultInfo['userLevel'] = [$userLevel,0,0,0];
- return $resultInfo;
- }
- $userParentId=$resultone['parent_user_id'];
- $userGrandId=$resultone['grand_father_id'];
- if($userParentId>0){
- $resulttwo=userLevelInfo($userParentId);
- if( !empty($resulttwo) ){
- # parent
- $userParentLevel=$resulttwo['level'];
- if($userParentLevel>=3){
- $resultInfo['userList'] = [$userId,$userParentId,0,0];
- $resultInfo['levelRule'] = intval($userLevel.'300');
- $resultInfo['userLevel'] = [$userLevel,$userParentLevel,0,0];
- return $resultInfo;
- }
- if($userGrandId>0){
- $resultthree=userLevelInfo($userGrandId);
- if( !empty($resultthree) ){
- # grand
- $userGrandLevel=$resultthree['level'];
- if($userGrandLevel>=3){
- $resultInfo['userList'] = [$userId,$userParentId,$userGrandId,0];
- $resultInfo['levelRule'] = intval($userLevel.$userParentLevel.'30');
- $resultInfo['userLevel'] = [$userLevel,$userParentLevel,$userGrandLevel,0];
- return $resultInfo;
- }
- }
- }
- }
- }
-
- if( !$userParentId ) $userParentId=0;
- if( !$userGrandId ) $userGrandId=0;
- if( !isset($userParentLevel) ) $userParentLevel=1;
- if( !isset($userGrandLevel) ) $userGrandLevel=1;
-
- #三级里面没有运营商,递归上级运营商
- $YysInfo = YysInfo($userGrandId);
- $userFourId = $YysInfo['user_id'];
- $userFourLevel = $YysInfo['level'];
-
- # 层级规则
- $levelRule = $userLevel.$userParentLevel.$userGrandLevel.'3';
-
- $resultInfo['userList']=[$userId,$userParentId,$userGrandId,$userFourId];
- $resultInfo['levelRule'] = (int)$levelRule;
- $resultInfo['userLevel'] = [$userLevel,$userParentLevel,$userGrandLevel,$userFourLevel];
- return $resultInfo;
- }
- function orderMoney($predict_income, $RuleArr){
- $result = array();
- foreach($RuleArr as $k=>$v){
- if($v>0){
- $result[$k] = round($predict_income * $v / 100);
- }else{
- $result[$k] = 0;
- }
- }
- return $result;
- }
- function orderMysql($order_id,$goods_id){
- $sql="SELECT * from `order` where order_id=:order_id and goods_id=:goods_id";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':order_id'=>$order_id,':goods_id'=>$goods_id));
- $result = $stmt->fetch(PDO::FETCH_ASSOC);
- return $result;
- }
- function orderType($adzone_id){
- $sql="SELECT * from `adzone` where adzone_id=:adzone_id";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':adzone_id'=>$adzone_id));
- $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
- return $userResult;
- }
- function generateSqlInsertOrderOther($goods_img,$goods_name,$order_id,$adzone_id,$goods_id,$num,$price,$sum_price,$predict_income,$self_income,$orderStatus,$type,$order_create_at,$order_click_at,$order_balance_at){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- if( empty($order_balance_at) ){
- $sql="insert into `order` (company_type,goods_img_url,goods_name,order_id,adzone_id,goods_id,num,price,sum_price,predict_income,income,status,type,order_create_at,order_click_at) VALUES (:company_type,:goods_img,:goods_name,:order_id,:adzone_id,:goods_id,:num,:price,:sum_price,:predict_income,:self_income,:orderStatus,:type,:order_create_at,:order_click_at)";
-
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':company_type'=>2,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':adzone_id'=>$adzone_id,':goods_id'=>$goods_id,':num'=>$num,':price'=>$price,':sum_price'=>$sum_price,':predict_income'=>$predict_income,':self_income'=>$self_income,':orderStatus'=>$orderStatus,':type'=>$type,':order_create_at'=>$order_create_at,':order_click_at'=>$order_click_at));
-
- return $_PDO->lastinsertid();
- }
- else{
- $sql="insert into `order` (company_type,goods_img_url,goods_name,order_id,adzone_id,goods_id,num,price,sum_price,predict_income,income,status,type,order_create_at,order_click_at,order_balance_at) VALUES (:company_type,:goods_img,:goods_name,:order_id,:adzone_id,:goods_id,:num,:price,:sum_price,:predict_income,:self_income,:orderStatus,:type,:order_create_at,:order_click_at,:order_balance_at)";
-
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':company_type'=>2,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':adzone_id'=>$adzone_id,':goods_id'=>$goods_id,':num'=>$num,':price'=>$price,':sum_price'=>$sum_price,':predict_income'=>$predict_income,':self_income'=>$self_income,':orderStatus'=>$orderStatus,':type'=>$type,':order_create_at'=>$order_create_at,':order_click_at'=>$order_click_at,':order_balance_at'=>$order_balance_at));
-
- return $_PDO->lastinsertid();
- }
- }
- #order表订单插入脚本
- function generateSqlInsertOrder($goods_img,$goods_name,$userId,$order_id,$adzone_id,$goods_id,$num,$price,$sum_price,$predict_income,$self_income,$orderStatus,$type,$userLevel,$order_create_at,$order_click_at,$order_balance_at){
- //$adzoneDetail=orderType($adzone_id);
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- if( empty($order_balance_at) ){
- $sql="insert into `order` (company_type,goods_img_url,goods_name,user_id,order_id,adzone_id,goods_id,num,price,sum_price,predict_income,income,status,type,user_level,order_create_at,order_click_at) VALUES (:company_type,:goods_img,:goods_name,:userId,:order_id,:adzone_id,:goods_id,:num,:price,:sum_price,:predict_income,:self_income,:orderStatus,:type,:userLevel,:order_create_at,:order_click_at)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':company_type'=>0,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':userId'=>$userId,':order_id'=>$order_id,':adzone_id'=>$adzone_id,':goods_id'=>$goods_id,':num'=>$num,':price'=>$price,':sum_price'=>$sum_price,':predict_income'=>$predict_income,':self_income'=>$self_income,':orderStatus'=>$orderStatus,':type'=>$type,':userLevel'=>$userLevel,':order_create_at'=>$order_create_at,':order_click_at'=>$order_click_at));
- return $_PDO->lastinsertid();
- }
- else{
- $sql="insert into `order` (company_type,goods_img_url,goods_name,user_id,order_id,adzone_id,goods_id,num,price,sum_price,predict_income,income,status,type,user_level,order_create_at,order_click_at,order_balance_at) VALUES (:company_type,:goods_img,:goods_name,:userId,:order_id,:adzone_id,:goods_id,:num,:price,:sum_price,:predict_income,:self_income,:orderStatus,:type,:userLevel,:order_create_at,:order_click_at,:order_balance_at)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':company_type'=>0,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':userId'=>$userId,':order_id'=>$order_id,':adzone_id'=>$adzone_id,':goods_id'=>$goods_id,':num'=>$num,':price'=>$price,':sum_price'=>$sum_price,':predict_income'=>$predict_income,':self_income'=>$self_income,':orderStatus'=>$orderStatus,':type'=>$type,':userLevel'=>$userLevel,':order_create_at'=>$order_create_at,':order_click_at'=>$order_click_at,':order_balance_at'=>$order_balance_at));
- return $_PDO->lastinsertid();
- }
- }
- #返利表订单插入脚本
- function generateSqlInsertRebateSelf($user_level,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$userId,$rebaseType,$last_orderSelfRebaseRebase,$sum_price,$orderStatus,$goods_id,$pre_income,$ptype){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- if( !empty($order_balance_at) ){
- $sql="insert into order_rebate (user_level,order_balance_at,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income,ptype) VALUES (:user_level,:order_balance_at,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income,:ptype)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_level'=>$user_level,':order_balance_at'=>$order_balance_at,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income,':ptype'=>$ptype));
- return $_PDO->lastinsertid();
- }
- else{
- $sql="insert into order_rebate (user_level,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income,ptype) VALUES (:user_level,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income,:ptype)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_level'=>$user_level,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income,':ptype'=>$ptype));
- return $_PDO->lastinsertid();
- }
- }
- #单独处理订单插运营商佣金
- function YysInsertRebate($user_level,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$userId,$rebaseType,$last_orderSelfRebaseRebase,$sum_price,$orderStatus,$goods_id,$pre_income){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- if( !empty($order_balance_at) ){
- $sql="insert into order_rebate (user_level,order_balance_at,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income) VALUES (:user_level,:order_balance_at,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_level'=>$user_level,':order_balance_at'=>$order_balance_at,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income));
- return $_PDO->lastinsertid();
- }
- else{
- $sql="insert into order_rebate (user_level,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income) VALUES (:user_level,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_level'=>$user_level,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income));
- return $_PDO->lastinsertid();
- }
- }
- #返利表订单插入运营商津贴信息
- function YysManageInsertRebate($user_level,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$userId,$rebaseType,$last_orderSelfRebaseRebase,$sum_price,$orderStatus,$goods_id,$pre_income,$ptype=1){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- if( !empty($order_balance_at) ){
- $sql="insert into order_rebate_profit (user_level,order_balance_at,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income,ptype) VALUES (:user_level,:order_balance_at,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income,:ptype)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_level'=>$user_level,':order_balance_at'=>$order_balance_at,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income,':ptype'=>$ptype));
- return $_PDO->lastinsertid();
- }
- else{
- $sql="insert into order_rebate_profit (user_level,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income,ptype) VALUES (:user_level,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income,:ptype)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_level'=>$user_level,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income,':ptype'=>$ptype));
- return $_PDO->lastinsertid();
- }
- }
- #order订单表返利表同步更新
- function generateSqlupdate($status,$order_balance_at,$order_id,$goods_id){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- if( !empty($order_balance_at) ){
- $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
- $res = $stmt->rowCount();
- //更新order_rebate
- $sql="update order_rebate set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
- $res = $stmt->rowCount();
- //更新order_rebate_profit
- $sql="update order_rebate_profit set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
- $res = $stmt->rowCount();
- }
- else{
- $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
- $res = $stmt->rowCount();
- $sql="update order_rebate set status=:status where order_id=:order_id and goods_id=:goods_id ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
- $res = $stmt->rowCount();
- //更新order_rebate_profit
- $sql="update order_rebate_profit set status=:status where order_id=:order_id and goods_id=:goods_id ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
- $res = $stmt->rowCount();
- }
- return $res;
- }
- #订单表单独更新
- function generateSqlUpdateOther($status,$order_balance_at,$order_id,$goods_id){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- if( !empty($order_balance_at) ){
- $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
- $res = $stmt->rowCount();
- }
- else{
- $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
- $res = $stmt->rowCount();
- }
- return $res;
- }
- # 获取rebateRule
- function getRebateRule($levelRule){
- $sql="SELECT * from `rebate_rule` where level_rule = :level_rule";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':level_rule'=>$levelRule));
- $ruleResult = $stmt->fetch(PDO::FETCH_ASSOC);
- return $ruleResult;
- }
- # 判断是否是新用户第一单
- function isNewUser($user_id){
- $start = ACT_START;
- $end = ACT_END;
- $sql = "SELECT id FROM `user_level` where regist_at>'{$start}' and regist_at<= '{$end}' and user_id = {$user_id} limit 1";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- if(!empty($res)){
- $sql = "SELECT id FROM `user_pullnew` where user_id={$user_id} and status>0 limit 1";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- if(empty($res)){
- return true;
- }
- }
- return false;
- }
- # 计算奖励佣金
- function getReward($money){
- if($money <= 0){
- return 0;
- }
- if($money > 300){
- $reward = 300;
- }else{
- $reward = $money;
- }
- return $reward;
- }
- # 同订单多分佣累加
- function arraySums($arr1, $arr2){
- $res = array();
- foreach($arr1 as $k=>$v){
- $res[$k] = $arr1[$k] + $arr2[$k];
- }
- return $res;
- }
- #拉新奖励订单插入
- function rewardOrderInsert($user_level,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$userId,$rebaseType,$last_orderSelfRebaseRebase,$sum_price,$orderStatus,$goods_id,$pre_income){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- if( !empty($order_balance_at) ){
- $sql="insert into order_rebate (user_level,order_balance_at,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income) VALUES (:user_level,:order_balance_at,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_level'=>$user_level,':order_balance_at'=>$order_balance_at,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income));
- return $_PDO->lastinsertid();
- }
- else{
- $sql="insert into order_rebate (user_level,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income) VALUES (:user_level,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_level'=>$user_level,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income));
- return $_PDO->lastinsertid();
- }
- }
- # 记录拉新首单表
- function pullNewInsert($user_id,$operate_id,$first_order_id,$order_create_at,$status){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $sql = "insert into `user_pullnew` (user_id, operate_id, first_order_id, order_create_at, status) values ({$user_id},{$operate_id},'{$first_order_id}','{$order_create_at}', {$status})";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- return $_PDO->lastinsertid();
- }
- #更新拉新首单表状态
- function pullNewUpdate($status, $order_id){
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $sql="update `user_pullnew` set status=:status where first_order_id=:order_id";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':status'=>$status,':order_id'=>$order_id));
- $res = $stmt->rowCount();
- return $res;
- }
- # 获取当前运营商拉新状态
- function getPullNew($operate_id){
- $sql="SELECT count(id) as records from `user_pullnew` where status>0 and operate_id = :operate_id";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':operate_id'=>$operate_id));
- $result = $stmt->fetch(PDO::FETCH_OBJ)->records;
- return $result;
- }
- # 是否更新拉新表
- function pullNewIsUpOk($status, $order_id, $user_id){
- $sql = "SELECT id from user_pullnew where first_order_id = '{$order_id}'";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- if(empty($res)){
- return false;
- }
- if($status == 0){
- $sql="SELECT id from `order` where user_id={$user_id} and status>0 limit 1";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- if(!empty($res)){
- return false;
- }
- }
- return true;
- }
- # 判断是否是新手第一单
- function newCustFirstOrder($user_id){
- $sql = "select id from `order` where user_id={$user_id} limit 1";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- //之前没下过订单
- if(empty($res)){
- # 完成新手首单任务 ->** 逻辑变动,结算才完成任务
- /*$sql = " update red_user_mission set status=1 where user_id={$user_id} and type=2 ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $update_res = $stmt->rowCount();*/
- # 多拆4-5元
- $openRes = openRed($user_id);
- return true;
- }
- return false;
- }
- # 新手第一单多拆出4-5块助力红包
- function openRed($user_id){
- $time = date("Y-m-d H:i:s", strtotime('-1 day'));
- $sql = "select ur.id,ur.red_money,ur.red_open_money,rh.user_id,rh.union_id,rh.helper_name,rh.helper_img from red_help_record rh
- left join user_weixin uw on uw.union_id=rh.union_id
- left join user_level ul on ul.user_id = uw.user_id
- left join red_user_info ur on ur.id=rh.red_id
- where rh.user_id!={$user_id} and uw.user_id={$user_id} and rh.add_time<ul.regist_at and ur.add_time>'{$time}' and ur.red_open_money<ur.red_money order by rh.id desc limit 1";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- if(!empty($res)){
- $last_money = $res['red_money']*100 - $res['red_open_money']*100;
- if($last_money<=500){
- $_money = $last_money/100;
- }else{
- $_money = mt_rand(400, 500) / 100;
- }
- $union_id = $res['union_id'];
- $helper_name = $res['helper_name'];
- $helper_img = $res['helper_img'];
- # 拆红包
- $sql = "update red_user_info set red_open_money=red_open_money+{$_money} where id = {$res['id']}";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $ur_res = $stmt->rowCount();
- $sql = "insert into red_help_record (red_id, user_id, union_id, helper_name, helper_img, helper_money, is_new) values({$res['id']},{$res['user_id']},'{$union_id}','{$helper_name}','{$helper_img}','{$_money}', 2)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $rh_res = $_PDO->lastinsertid();
- $userAccount = getUserBalace($res['user_id']);
- $lastBalance = isset($userAccount['balance']) ? $userAccount['balance'] : 0;
- $redAccount = redAccountRecord($res['user_id'], 5, $_money, $rh_res, round($lastBalance+$_money,2));
- return true;
- }
- return false;
- }
- # 获取用户红包额度
- function getUserBalace($user_id){
- $table = "red_account_record_".($user_id%10);
- $sql = "SELECT balance from {$table} where user_id={$user_id} order by id desc limit 1";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- return $res;
- }
- #更新流水
- function redAccountRecord($user_id, $type, $money, $oh_id, $balance){
- $table = "red_account_record_".($user_id%10);
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $sql = "insert into `{$table}` (user_id, type, money, oh_id, balance) values ({$user_id},{$type},{$money},'{$oh_id}', {$balance})";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- return $_PDO->lastinsertid();
- }
- #查裂变运营商信息
- function preYysInfo($user_id){
- $sql = "SELECT count(1) as nums,ui.pre_yys_id from user_level_change_info ui
- left join user_level_change_info ui2 on ui2.pre_yys_id = ui.pre_yys_id where ui2.user_id = {$user_id} and ui.upgrade_time>'2019-04-25'
- ";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- $data = array();
- if(isset($res['nums']) && $res['nums']>=3){
- $data['user_id'] = $res['pre_yys_id'];
- if($res['nums']>=10){
- $data['rate'] = 0.03;
- }elseif($res['nums']>=6){
- $data['rate'] = 0.02;
- }elseif($res['nums']>=3){
- $data['rate'] = 0.01;
- }
- }
- return $data;
- }
- #退款推送
- function refundPush($order_id){
- $sql = "SELECT user_id,rebate from order_rebate where order_id='{$order_id}' and status=0 and rebate>0 and type!=7";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
- if(!empty($res)){
- foreach($res as $k=>$v){
- $params = array();
- $params['user_id'] = $v['user_id'];
- $params['type'] = 6; //订单退款
- $params['money'] = round($v['rebate']/100, 2);
- $push_res = curl_post( REFUND_PUSH_URL, $params);
- echo "\n退款推送:用户:".$v['user_id']."|-推送:".$push_res.'|-订单:'.$order_id;
- }
- }
- }
- /**
- * 判断是否是新逻辑运营中心0425
- */
- function isNewYysManage($user_id){
- $Arr = [23512,48941,214262,218326,222044,235824,239555,262168,262285,311074,317849]; //0425-老运营中心
- if(in_array($user_id, $Arr)){
- return false;
- }
- return true;
- }
- # curl调用接口push
- function curl_post($url, $params){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- $response = curl_exec($ch);
- curl_close($ch);
- return $response;
- }
- readDataFromDb();
|