优惠券订单及其他脚本

scan_orderv4_liedou.php 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. <?php
  2. require_once 'DB_PDO.class.php';
  3. require_once 'TBK.class.php';
  4. require_once 'confv2.class.php';
  5. define("DETAILDEBUG", 0);//打印流程详细信息,如有需要,设为1
  6. define("LIMIT_NUM", 0);//限制导入条数,0不限制,方便测试,上线设为0
  7. define("PUSH_URL",'https://liedou.726p.com/api/v2/MessagePush/rebatepush');//push接口地址
  8. define("ACT_PUSH_URL",'https://liedou.726p.com/api/v2/MessagePush/pullnew_push');//拉新push接口地址
  9. define("REFUND_PUSH_URL",'https://liedou.726p.com/api/v2/MessagePush/act_push');//退款push接口地址
  10. define("ACT_START", '2018-12-25 00:00:00');//
  11. define("ACT_END", '2018-12-31 23:59:59');//
  12. define("ACT_GOODS_IMG", 'http://imgs.726p.com/tbk/upload/advImg2018-12-24/135037/kx-5c2073adbe478.png');//
  13. /**
  14. * 导入及更新 order/order_rebate数据 + 淘宝拉新活动
  15. */
  16. #设置时区
  17. date_default_timezone_set('PRC');
  18. set_time_limit(0);
  19. ini_set('memory_limit','1024M');
  20. function readDataFromDb(){
  21. # 记录脚本开始时间
  22. $scriptStart = date("Y-m-d H:i:s");
  23. # 1.获取6分钟内订单
  24. //$stime = date('Y-m-d H:i:s',strtotime('2018-08-07'));
  25. $stime = date('Y-m-d H:i:s', time()-150);
  26. $sql = "SELECT * from liedou_taoke_orders where update_time > :stime ";
  27. $dumpsql = str_replace(':stime', "'".$stime."'", $sql);
  28. echo "\n\n".$dumpsql."\n";
  29. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  30. $stmt = $_PDO->prepare($sql);
  31. $stmt->execute(array(':stime'=>$stime));
  32. $orderResult = $stmt->fetchAll(PDO::FETCH_ASSOC);
  33. # 遍历订单
  34. # 统计
  35. $res_count = array(
  36. 'total_num' => 0,
  37. 'generalinsert_num' => 0,
  38. 'generalupdate_num' => 0,
  39. 'nouserinsert_num' => 0,
  40. 'nouserupdate_num' => 0,
  41. 'fail_insert_num' => 0,
  42. 'fail_insert_ids' => '',
  43. 'fail_insertno_num' => 0,
  44. 'fail_insertno_ids' => '',
  45. 'fail_update_num' => 0,
  46. //'fail_update_ids' => '',
  47. 'fail_updateno_num' => 0,
  48. //'fail_updateno_ids' => '',
  49. );
  50. $rewardOrder = array(); //拉新订单记录
  51. $nums = 0;
  52. foreach( $orderResult as $key=>$orderDetail ){
  53. $nums++;
  54. $res_count['total_num']++;
  55. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  56. if( DETAILDEBUG == 1){
  57. echo 'orderDetail:';
  58. }
  59. //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
  60. $ptype = $orderDetail["ptype"];
  61. $order_id=$orderDetail["trade_id"];
  62. $update_time=$orderDetail["update_time"];
  63. $order_create_at=$orderDetail["create_time"];
  64. $order_click_at=$orderDetail["click_time"];
  65. $order_balance_at=$orderDetail["earning_time"];
  66. $goods_id=$orderDetail["num_iid"];
  67. $goods_name=$orderDetail["item_title"];
  68. $goods_name=str_replace(['"',"'"],'',$goods_name);
  69. $num=$orderDetail["item_num"];
  70. $price=intval($orderDetail["price"]*1000/10);
  71. $sum_price=intval($orderDetail["alipay_total_price"]*1000/10);
  72. $statusDesc=$orderDetail["tk_status"];
  73. $typeDesc=$orderDetail["order_type"];
  74. $orderStatus = 1;
  75. $type=1;
  76. if($statusDesc=="12"){
  77. $orderStatus=1;
  78. }
  79. elseif($statusDesc=="13"){
  80. $orderStatus=0;
  81. }
  82. elseif($statusDesc=="3"){
  83. $orderStatus=2;
  84. } else {
  85. continue; //其他状态:待付款,已付定金,不做处理;
  86. }
  87. if($typeDesc=="聚划算"){
  88. $type=0;
  89. }
  90. elseif($typeDesc=="淘宝"){
  91. $type=1;
  92. }
  93. elseif($typeDesc=="天猫"){
  94. $type=2;
  95. }
  96. elseif($typeDesc=="京东"){
  97. $type=3;
  98. }
  99. elseif($typeDesc=="抖音"){
  100. $type=4;
  101. }
  102. elseif($typeDesc=="拼多多"){
  103. $type = 5;
  104. }
  105. elseif($typeDesc=='唯品会'){
  106. $type = 6;
  107. }
  108. $adzone_id=$orderDetail["adzone_id"];
  109. $relation_id = $orderDetail["relation_id"];
  110. $special_id = $orderDetail["special_id"];
  111. $ceil_income=round($orderDetail["pub_share_pre_fee"], 2);
  112. $predict_income=$ceil_income*100;
  113. $predict_income=ceil($predict_income*conf::$RATE_CONF["globalzk"]);
  114. echo 'order_id:'.$order_id.'|| goods_id:'.$goods_id.'|| orderstatus:'.$orderStatus.'|| order_balance_at:'.$order_balance_at;
  115. echo '|| predict_income:'.$predict_income.'|| adzone_id:'.$adzone_id.'|| special_id:'.$special_id.'|| relation_id:'.$relation_id."\n";
  116. $resultone=UserInfo($special_id, $relation_id, $adzone_id, $ptype);
  117. if( DETAILDEBUG == 1){
  118. echo 'userinfo:';
  119. var_dump($resultone);
  120. echo "\n";
  121. }
  122. if($sum_price!=0){
  123. if($predict_income<5){
  124. $predict_income=5;
  125. }
  126. }
  127. if( !empty($resultone) ){
  128. echo "User:存在\n";
  129. $orderExists=orderMysql($order_id,$goods_id);
  130. if( empty($orderExists) ){
  131. echo "action:插入操作\n";
  132. #获取层级规则信息
  133. $userAllInfo=userLevelandPrentInfo($resultone);
  134. echo "<pre>\nuserAllInfo";
  135. print_r($userAllInfo);
  136. if( DETAILDEBUG == 1){
  137. echo 'userallinfo:'."\n";
  138. var_dump($userAllInfo);
  139. }
  140. $userList = $userAllInfo['userList']; //4层 每层userId
  141. $userLevel = $userAllInfo['userLevel']; //4层 每层userlevel
  142. # 从数据库获取规则
  143. $rebateRuleInfo = getRebateRule($userAllInfo['levelRule']);
  144. echo "\nRuleInfo:";
  145. print_r($rebateRuleInfo);
  146. $RuleArr = explode(',',$rebateRuleInfo['rebate_rule']); //4层 每层分佣比例
  147. $TypeArr = explode(',',$rebateRuleInfo['rebate_type']); //4层 每层分佣级别
  148. #获取每一层返利
  149. $moneyInfo=orderMoney($predict_income, $RuleArr); //4层 每层分佣钱数
  150. echo "\nmoneyInfo:";
  151. print_r($moneyInfo);
  152. if( DETAILDEBUG == 1){
  153. echo 'rebateInfo'."\n";
  154. var_dump($moneyInfo);
  155. }
  156. if($ptype == 2 || $ptype == 3 || $ptype == 4 || $ptype == 5){
  157. $goods_img = $orderDetail["img"];
  158. } else {
  159. $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
  160. if( !empty($detail) ){
  161. $goods_img=$detail->pict_url;
  162. } else {
  163. echo json_encode($detail, 256)."\n";
  164. echo "淘宝客商品详情接口未获取到商品信息,开始调用淘宝客物料搜索接口"."\n";
  165. # 调用淘宝客【推广者-物料搜索】api
  166. $goodsKey = "https://uland.taobao.com/item/edetail?id=".$goods_id;
  167. $goodsList = TBK::search($goodsKey);
  168. if (!empty($goodsList['list'])) {
  169. $goodsDetail = $goodsList['list'][0];
  170. $goods_img = $goodsDetail->pict_url;
  171. echo "通过物料搜索接口获取到商品图"."\n";
  172. } else {
  173. echo "物料搜索接口仍未获取到商品图".json_encode($goodsList, 256)."\n";
  174. $goods_img="";
  175. }
  176. }
  177. }
  178. try{
  179. $_PDO->beginTransaction();
  180. #判断是否是新手第一单
  181. //$newFirstRes = newCustFirstOrder($userList[0]);
  182. #订单表插入
  183. $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);
  184. if($res){
  185. $res_count['generalinsert_num']++;
  186. }else{
  187. $res_count['fail_insert_num']++;
  188. $res_count['fail_insert_ids'].=$order_id.'|';
  189. }
  190. echo 'orderinsert:'.$res;
  191. echo "\n";
  192. if($sum_price>=0 && $res){
  193. foreach($moneyInfo as $k=>$v){
  194. if($v>0){
  195. #order_rebate表插入父级佣金
  196. $rebaseType=$TypeArr[$k];
  197. $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);
  198. # 推送
  199. $flag_time = time() - strtotime($order_create_at);
  200. if($res && $flag_time<3600){
  201. $params = array();
  202. $params['user_id'] = $userList[$k];
  203. $params['type'] = $rebaseType==1 ? 1 : 2;
  204. $params['money'] = round($moneyInfo[$k]/100,2);
  205. $push_res = curl_post( PUSH_URL, $params);
  206. }
  207. echo 'rebateinsert_'.$k.':'.$res;
  208. echo "\n";
  209. }
  210. }
  211. if($order_create_at >= ACT_START && $order_create_at <= ACT_END){
  212. # 判断是否是拉新首单
  213. $isNew = 0;
  214. $isTooNew = 0;
  215. if(array_key_exists($order_id, $rewardOrder)){
  216. $isTooNew = 1;
  217. }else{
  218. $isNew = isNewUser($userList[0]);
  219. }
  220. if($isTooNew || $isNew){
  221. //记录拉新订单分佣情况
  222. if($isNew){
  223. $rewardOrder[$order_id] = $moneyInfo;
  224. }else{
  225. $rewardOrder[$order_id] = arraySums($rewardOrder[$order_id],$moneyInfo);
  226. }
  227. //获取下一个分佣的订单
  228. $nkey = $nums;
  229. //若当前订单和下一笔订单号一样,继续循环,直到本拉新订单分佣rewardOrder[$order_id]统计完毕
  230. if(isset($orderResult[$nkey]['order_sn']) && $order_id == $orderResult[$nkey]['order_sn']){
  231. }else{
  232. // 分奖励佣金 < --------- Start --------- >
  233. $reward_n = 0; // 记录奖励层数 最多两层
  234. $rewardInfo = $rewardOrder[$order_id]; //本订单实际分佣情况
  235. foreach($rewardInfo as $k=>$v){
  236. if($v>0 && $reward_n<2){
  237. # 插入奖励
  238. $rebaseType=6; //奖励佣金
  239. # 计算奖励佣金
  240. $reward = getReward($rewardInfo[$k]);
  241. $goods_name_new = $k==0 ? '恭喜您获得新用户首单奖励' : '恭喜您获得拉新首单奖励';
  242. $goods_img_new = ACT_GOODS_IMG;
  243. $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);
  244. # 奖励推送
  245. if($res){
  246. $params = array();
  247. $params['user_id'] = $userList[$k];
  248. $params['type'] = $k==0 ? 3 : 4; //奖励推送 3新人 4上级
  249. $params['money'] = round($reward/100,2);
  250. $push_res = curl_post( ACT_PUSH_URL, $params);
  251. }
  252. echo 'rewardinsert_'.$reward_n.':'.$res;
  253. echo "\n";
  254. # 记录奖励层数
  255. $reward_n++;
  256. }
  257. }
  258. # 记录新人首单信息
  259. if($res){
  260. //获取运营商
  261. $m = strpos($userAllInfo['levelRule'], '3');
  262. $operate_id = $userList[$m];
  263. $res2 = pullNewInsert($userList[0],$operate_id,$order_id,$order_create_at,$orderStatus);
  264. //获取当前运营商拉新人数
  265. $pullCount = getPullNew($operate_id);
  266. if($pullCount==10 || $pullCount==20 || $pullCount==30){
  267. //符合推送条件
  268. $params = array();
  269. $params['user_id'] = $operate_id;
  270. $params['type'] = 5; //运营商拉新人数推送
  271. $params['count'] = $pullCount;
  272. $push_res = curl_post( ACT_PUSH_URL, $params);
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. //津贴单独计算
  280. # 获取顶级运营商-控制中心 ** ++ 运营商,满足裂变3个运营商以上分团队奖励(新津贴)
  281. if($sum_price>0){
  282. $mks = strpos($userAllInfo['levelRule'], '3');
  283. if($mks !== false){
  284. $YysManageInfo = YysManageInfo( $userList[$mks] );
  285. if( !empty($YysManageInfo) ){
  286. $rebaseType=$TypeArr[$mks];
  287. $YysManageRabate = round($predict_income*$YysManageInfo['rate']);
  288. if($YysManageRabate < 1){
  289. $YysManageRabate = 1;
  290. }
  291. $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);
  292. echo 'rebateinsert_YysManage:'.$res;
  293. echo "\n";
  294. }
  295. #+++新逻辑 **团队奖励(新津贴)
  296. /*
  297. $preYysInfo = preYysInfo( $userList[$mks] );
  298. if( !empty($preYysInfo) ){
  299. $rebaseType=$TypeArr[$mks];
  300. $YysNewRabate = round($predict_income*$preYysInfo['rate']);
  301. if($YysNewRabate < 1){
  302. $YysNewRabate = 1;
  303. }
  304. $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);
  305. echo 'rebateinsert_preYysInfo:'.$res;
  306. echo "\n";
  307. }
  308. */
  309. }
  310. }
  311. $_PDO->commit();
  312. }catch(PDOException $e){
  313. echo 'err_msg'.$e->getMessage()."\n";
  314. $_PDO->rollback();
  315. }
  316. //echo "-----------------insertEnd--------------------\n";
  317. }
  318. else{
  319. try{
  320. $_PDO->beginTransaction();
  321. //更新order/order_rebate表
  322. echo "action:更新操作\n";
  323. $res=generateSqlupdate($orderStatus,$order_balance_at,$order_id,$goods_id);
  324. /*** 增加拉新更新 Start ***/
  325. if($order_create_at >= ACT_START && $order_create_at <= ACT_END){
  326. $isUpOk = pullNewIsUpOk($orderStatus, $order_id, $resultone['user_id']);
  327. echo 'isUpOk:'.$isUpOk."\n";
  328. if($isUpOk) $pull_up = pullNewUpdate($orderStatus, $order_id);
  329. }
  330. /*** 增加拉新更新 End ***/
  331. /*** 增加退款推送 Start***/
  332. if($res && $orderStatus==0){
  333. $refundPush = refundPush($order_id);
  334. }
  335. /*** 增加退款推送 END***/
  336. if($res){
  337. $res_count['generalupdate_num']++;
  338. }else{
  339. $res_count['fail_update_num']++;
  340. //$res_count['fail_update_ids'].=$order_id.'|';
  341. }
  342. echo "更新数据-> orderstatus:".$orderStatus." order_balance_at:".$order_balance_at;
  343. echo "\n";
  344. echo "order/reabte_update:".$res;
  345. echo "\n";
  346. //echo "-----------------updateEnd-----------------------\n";
  347. $_PDO->commit();
  348. }catch(PDOException $e){
  349. echo 'err_msg'.$e->getMessage()."\n";
  350. $_PDO->rollback();
  351. }
  352. }
  353. //echo "-----------------IssetUserEnd:--------------------\n";
  354. }
  355. else{
  356. $_PDO->beginTransaction();
  357. try{
  358. #user不存在的处理
  359. echo "User:不存在\n";
  360. $ordernoExists=orderMysql($order_id,$goods_id);
  361. if( empty($ordernoExists) ){
  362. if($ptype == 2){
  363. $goods_img = $orderDetail["img"];;
  364. } else {
  365. $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
  366. if( !empty($detail) ){
  367. $goods_img=$detail->pict_url;
  368. } else {
  369. echo json_encode($detail, 256)."\n";
  370. echo "淘宝客商品详情接口未获取到商品信息,开始调用淘宝客物料搜索接口"."\n";
  371. # 调用淘宝客【推广者-物料搜索】api
  372. $goodsKey = "https://uland.taobao.com/item/edetail?id=".$goods_id;
  373. $goodsList = TBK::search($goodsKey);
  374. if (!empty($goodsList['list'])) {
  375. $goodsDetail = $goodsList['list'][0];
  376. $goods_img = $goodsDetail->pict_url;
  377. echo "通过物料搜索接口获取到商品图"."\n";
  378. } else {
  379. echo "物料搜索接口仍未获取到商品图".json_encode($goodsList, 256)."\n";
  380. $goods_img="";
  381. }
  382. }
  383. }
  384. $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);
  385. if($res){
  386. $res_count['nouserinsert_num']++;
  387. }else{
  388. $res_count['fail_insertno_num']++;
  389. $res_count['fail_insertno_ids'].=$order_id.'|';
  390. }
  391. echo "action:插入操作\n";
  392. echo "orderinsert:".$res;
  393. echo "\n";
  394. $_PDO->commit();
  395. }
  396. else{
  397. $res=generateSqlUpdateOther($orderStatus,$order_balance_at,$order_id,$goods_id);
  398. if($res){
  399. $res_count['nouserupdate_num']++;
  400. }else{
  401. $res_count['fail_updateno_num']++;
  402. //$res_count['fail_updateno_ids'].=$order_id.'|';
  403. }
  404. echo "action:更新操作\n";
  405. echo "orderupdate:".$res;
  406. echo "\n";
  407. $_PDO->commit();
  408. }
  409. //echo "-----------------EmptyUserEnd:--------------------\n";
  410. }catch(PDOException $e){
  411. echo 'err_msg'.$e->getMessage()."\n";
  412. $_PDO->rollback();
  413. }
  414. }
  415. echo "/ ** order{$res_count['total_num']}-End ** /";
  416. echo "\n\n\n";
  417. if(LIMIT_NUM >0 && $res_count['total_num'] == LIMIT_NUM) break;
  418. }
  419. print_r($res_count);
  420. echo "\n拉新订单:\n";
  421. print_r($rewardOrder);
  422. echo "\n脚本开始时间:". $scriptStart;
  423. echo "\n脚本结束时间:". date("Y-m-d H:i:s");
  424. }
  425. # 查找运营中心规则改版0425
  426. function YysManageInfo( $user_id ){
  427. $sql = "SELECT user_id,parent_user_id,level FROM user_level WHERE user_id=:user_id ";
  428. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  429. $stmt = $_PDO->prepare($sql);
  430. $stmt->execute(array(':user_id'=>$user_id));
  431. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  432. if( empty($result) ) return false;
  433. $data = array();
  434. #如果自己是运营中心
  435. if( $result['level'] == 4 ){
  436. #老运营
  437. //if(!isNewYysManage($user_id)){
  438. $data['user_id'] = $user_id;
  439. $data['rate'] = 0.01;
  440. return $data;
  441. //}else{
  442. //return new_c_rate($user_id);
  443. //}
  444. }else{
  445. #上级信息
  446. $sql = "SELECT id FROM user_level WHERE user_id=:user_id and level=4";
  447. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  448. $stmt = $_PDO->prepare($sql);
  449. $stmt->execute(array(':user_id'=>$result['parent_user_id']));
  450. $result1 = $stmt->fetch(PDO::FETCH_ASSOC);
  451. if(empty($result1)){
  452. return false; //上级若不是运营中心
  453. }
  454. #老运营
  455. //if(!isNewYysManage($result['parent_user_id'])){
  456. $data['user_id'] = $result['parent_user_id'];
  457. $data['rate'] = 0.01;
  458. return $data;
  459. //}else{
  460. //return new_c_rate($result['parent_user_id']);
  461. //}
  462. }
  463. return false;
  464. }
  465. #新运营中心津贴比例
  466. function new_c_rate($user_id){
  467. #新运营中心
  468. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  469. $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' ";
  470. //$sql = "SELECT count(1) as nums from user_level where parent_user_id=:parent_user_id and level>=3 ";
  471. $stmt = $_PDO->prepare($sql);
  472. $stmt->execute(array(':pre_yys_id'=>$user_id));
  473. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  474. $data = array();
  475. if(isset($res['nums']) && $res['nums']>=3){
  476. $data['user_id'] = $user_id;
  477. if($res['nums']>=10){
  478. $data['rate'] = 0.03;
  479. }elseif($res['nums']>=6){
  480. $data['rate'] = 0.02;
  481. }elseif($res['nums']>=3){
  482. $data['rate'] = 0.01;
  483. }
  484. return $data;
  485. }
  486. return false;
  487. }
  488. function YysInfo( $user_id ){
  489. $sql = "SELECT user_id,parent_user_id,level FROM user_level WHERE user_id=:user_id ";
  490. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  491. $stmt = $_PDO->prepare($sql);
  492. $stmt->execute(array(':user_id'=>$user_id));
  493. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  494. if( empty($result) ) return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  495. if($result['level'] >= 3){
  496. return $result;
  497. }else{
  498. if( empty($result['parent_user_id']) ){
  499. return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  500. }
  501. return YysInfo( $result['parent_user_id']);
  502. }
  503. return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  504. }
  505. function UserInfo($special_id, $relation_id, $adzone_id, $ptype=1){
  506. if($ptype == 2 ||$ptype == 3 ||$ptype == 4 ||$ptype == 5){
  507. return JdUserInfo($relation_id);
  508. }
  509. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  510. //新加逻辑 2020-7-8 封relation不分佣
  511. $sql = "SELECT value FROM global_variable WHERE variable_key ='relation_blacklist' ";
  512. $stmt = $_PDO->prepare($sql);
  513. $stmt->execute();
  514. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  515. if( !empty($result) ){
  516. if( strstr($result['value'], $relation_id) ){
  517. return false;
  518. }
  519. }
  520. if(in_array($adzone_id,[123456])){
  521. return false;
  522. }
  523. if($adzone_id=='101804100229'){
  524. if(!empty($relation_id)){
  525. $sql = "SELECT user_id FROM user_special_relation_local WHERE relation_id = '{$relation_id}'";
  526. }elseif(!empty($special_id)){
  527. $sql = "SELECT user_id FROM user_special_relation WHERE special_id = '{$special_id}'";
  528. }
  529. }else{
  530. if(!empty($relation_id)){
  531. $sql = "SELECT user_id FROM user_special_relation WHERE relation_id = '{$relation_id}'";
  532. }elseif(!empty($special_id)){
  533. $sql = "SELECT user_id FROM user_special_relation WHERE special_id = '{$special_id}'";
  534. }else{
  535. return false;
  536. }
  537. }
  538. $stmt = $_PDO->prepare($sql);
  539. $stmt->execute();
  540. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  541. if(!empty($resultone)){
  542. $sql="SELECT * FROM user_level WHERE user_id = :user_id";
  543. $stmt = $_PDO->prepare($sql);
  544. $stmt->execute(array(':user_id'=>$resultone['user_id']));
  545. $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
  546. return $userResult;
  547. }
  548. else{
  549. return false;
  550. }
  551. }
  552. function JdUserInfo($relation_id){
  553. if(!$relation_id){
  554. return false;
  555. }
  556. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  557. $sql = "SELECT id FROM users WHERE invite_code = '{$relation_id}'";
  558. $stmt = $_PDO->prepare($sql);
  559. $stmt->execute();
  560. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  561. if(!empty($resultone)){
  562. $sql="SELECT * FROM user_level WHERE user_id = :user_id";
  563. $stmt = $_PDO->prepare($sql);
  564. $stmt->execute(array(':user_id'=>$resultone['id']));
  565. $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
  566. return $userResult;
  567. }
  568. else{
  569. return false;
  570. }
  571. }
  572. function userLevelInfo($user_id){
  573. $sql="SELECT * FROM user_level WHERE user_id = :user_id ";
  574. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  575. $stmt = $_PDO->prepare($sql);
  576. $stmt->execute(array(':user_id'=>$user_id));
  577. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  578. if($resultone){
  579. $myInfo=["user_id"=>$user_id,"level"=>$resultone['level'],"parent_user_id"=>$resultone['parent_user_id']];
  580. return $myInfo;
  581. }
  582. else{
  583. return false;
  584. }
  585. }
  586. function userMyInfo($user_id){
  587. $sql="SELECT * FROM users WHERE id = :user_id ";
  588. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  589. $stmt = $_PDO->prepare($sql);
  590. $stmt->execute(array(':user_id'=>$user_id));
  591. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  592. if($resultone){
  593. $myInfo['user_id'] = $user_id;
  594. $myInfo['origin'] = $resultone['origin'];
  595. return $myInfo;
  596. }
  597. else{
  598. return false;
  599. }
  600. }
  601. # 获取4层用户级别基础信息
  602. function userLevelandPrentInfo($resultone){
  603. $resultInfo = array();
  604. # self
  605. $userId=$resultone['user_id'];
  606. $userLevel=$resultone['level'];
  607. if($userLevel>=3){
  608. $resultInfo['userList'] = [$userId,0,0,0];
  609. $resultInfo['levelRule'] = 3000;
  610. $resultInfo['userLevel'] = [$userLevel,0,0,0];
  611. return $resultInfo;
  612. }
  613. $userParentId=$resultone['parent_user_id'];
  614. $userGrandId=$resultone['grand_father_id'];
  615. if($userParentId>0){
  616. $resulttwo=userLevelInfo($userParentId);
  617. if( !empty($resulttwo) ){
  618. # parent
  619. $userParentLevel=$resulttwo['level'];
  620. if($userParentLevel>=3){
  621. $resultInfo['userList'] = [$userId,$userParentId,0,0];
  622. $resultInfo['levelRule'] = intval($userLevel.'300');
  623. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,0,0];
  624. return $resultInfo;
  625. }
  626. if($userGrandId>0){
  627. $resultthree=userLevelInfo($userGrandId);
  628. if( !empty($resultthree) ){
  629. # grand
  630. $userGrandLevel=$resultthree['level'];
  631. if($userGrandLevel>=3){
  632. $resultInfo['userList'] = [$userId,$userParentId,$userGrandId,0];
  633. $resultInfo['levelRule'] = intval($userLevel.$userParentLevel.'30');
  634. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,$userGrandLevel,0];
  635. return $resultInfo;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. if( !$userParentId ) $userParentId=0;
  642. if( !$userGrandId ) $userGrandId=0;
  643. if( !isset($userParentLevel) ) $userParentLevel=1;
  644. if( !isset($userGrandLevel) ) $userGrandLevel=1;
  645. #三级里面没有运营商,递归上级运营商
  646. $YysInfo = YysInfo($userGrandId);
  647. $userFourId = $YysInfo['user_id'];
  648. $userFourLevel = $YysInfo['level'];
  649. # 层级规则
  650. $levelRule = $userLevel.$userParentLevel.$userGrandLevel.'3';
  651. $resultInfo['userList']=[$userId,$userParentId,$userGrandId,$userFourId];
  652. $resultInfo['levelRule'] = (int)$levelRule;
  653. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,$userGrandLevel,$userFourLevel];
  654. return $resultInfo;
  655. }
  656. function orderMoney($predict_income, $RuleArr){
  657. $result = array();
  658. foreach($RuleArr as $k=>$v){
  659. if($v>0){
  660. $result[$k] = round($predict_income * $v / 100);
  661. }else{
  662. $result[$k] = 0;
  663. }
  664. }
  665. return $result;
  666. }
  667. function orderMysql($order_id,$goods_id){
  668. $sql="SELECT * from `order` where order_id=:order_id and goods_id=:goods_id";
  669. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  670. $stmt = $_PDO->prepare($sql);
  671. $stmt->execute(array(':order_id'=>$order_id,':goods_id'=>$goods_id));
  672. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  673. return $result;
  674. }
  675. function orderType($adzone_id){
  676. $sql="SELECT * from `adzone` where adzone_id=:adzone_id";
  677. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  678. $stmt = $_PDO->prepare($sql);
  679. $stmt->execute(array(':adzone_id'=>$adzone_id));
  680. $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
  681. return $userResult;
  682. }
  683. 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){
  684. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  685. if( empty($order_balance_at) ){
  686. $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)";
  687. $stmt = $_PDO->prepare($sql);
  688. $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));
  689. return $_PDO->lastinsertid();
  690. }
  691. else{
  692. $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)";
  693. $stmt = $_PDO->prepare($sql);
  694. $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));
  695. return $_PDO->lastinsertid();
  696. }
  697. }
  698. #order表订单插入脚本
  699. 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){
  700. //$adzoneDetail=orderType($adzone_id);
  701. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  702. if( empty($order_balance_at) ){
  703. $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)";
  704. $stmt = $_PDO->prepare($sql);
  705. $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));
  706. return $_PDO->lastinsertid();
  707. }
  708. else{
  709. $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)";
  710. $stmt = $_PDO->prepare($sql);
  711. $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));
  712. return $_PDO->lastinsertid();
  713. }
  714. }
  715. #返利表订单插入脚本
  716. 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){
  717. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  718. if( !empty($order_balance_at) ){
  719. $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)";
  720. $stmt = $_PDO->prepare($sql);
  721. $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));
  722. return $_PDO->lastinsertid();
  723. }
  724. else{
  725. $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)";
  726. $stmt = $_PDO->prepare($sql);
  727. $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));
  728. return $_PDO->lastinsertid();
  729. }
  730. }
  731. #单独处理订单插运营商佣金
  732. 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){
  733. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  734. if( !empty($order_balance_at) ){
  735. $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)";
  736. $stmt = $_PDO->prepare($sql);
  737. $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));
  738. return $_PDO->lastinsertid();
  739. }
  740. else{
  741. $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)";
  742. $stmt = $_PDO->prepare($sql);
  743. $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));
  744. return $_PDO->lastinsertid();
  745. }
  746. }
  747. #返利表订单插入运营商津贴信息
  748. 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){
  749. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  750. if( !empty($order_balance_at) ){
  751. $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)";
  752. $stmt = $_PDO->prepare($sql);
  753. $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));
  754. return $_PDO->lastinsertid();
  755. }
  756. else{
  757. $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)";
  758. $stmt = $_PDO->prepare($sql);
  759. $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));
  760. return $_PDO->lastinsertid();
  761. }
  762. }
  763. #order订单表返利表同步更新
  764. function generateSqlupdate($status,$order_balance_at,$order_id,$goods_id){
  765. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  766. if( !empty($order_balance_at) ){
  767. $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  768. $stmt = $_PDO->prepare($sql);
  769. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  770. $res = $stmt->rowCount();
  771. //更新order_rebate
  772. $sql="update order_rebate set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  773. $stmt = $_PDO->prepare($sql);
  774. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  775. $res = $stmt->rowCount();
  776. //更新order_rebate_profit
  777. $sql="update order_rebate_profit set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  778. $stmt = $_PDO->prepare($sql);
  779. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  780. $res = $stmt->rowCount();
  781. }
  782. else{
  783. $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  784. $stmt = $_PDO->prepare($sql);
  785. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  786. $res = $stmt->rowCount();
  787. $sql="update order_rebate set status=:status where order_id=:order_id and goods_id=:goods_id ";
  788. $stmt = $_PDO->prepare($sql);
  789. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  790. $res = $stmt->rowCount();
  791. //更新order_rebate_profit
  792. $sql="update order_rebate_profit set status=:status where order_id=:order_id and goods_id=:goods_id ";
  793. $stmt = $_PDO->prepare($sql);
  794. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  795. $res = $stmt->rowCount();
  796. }
  797. return $res;
  798. }
  799. #订单表单独更新
  800. function generateSqlUpdateOther($status,$order_balance_at,$order_id,$goods_id){
  801. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  802. if( !empty($order_balance_at) ){
  803. $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  804. $stmt = $_PDO->prepare($sql);
  805. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  806. $res = $stmt->rowCount();
  807. }
  808. else{
  809. $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  810. $stmt = $_PDO->prepare($sql);
  811. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  812. $res = $stmt->rowCount();
  813. }
  814. return $res;
  815. }
  816. # 获取rebateRule
  817. function getRebateRule($levelRule){
  818. $sql="SELECT * from `rebate_rule` where level_rule = :level_rule";
  819. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  820. $stmt = $_PDO->prepare($sql);
  821. $stmt->execute(array(':level_rule'=>$levelRule));
  822. $ruleResult = $stmt->fetch(PDO::FETCH_ASSOC);
  823. return $ruleResult;
  824. }
  825. # 判断是否是新用户第一单
  826. function isNewUser($user_id){
  827. $start = ACT_START;
  828. $end = ACT_END;
  829. $sql = "SELECT id FROM `user_level` where regist_at>'{$start}' and regist_at<= '{$end}' and user_id = {$user_id} limit 1";
  830. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  831. $stmt = $_PDO->prepare($sql);
  832. $stmt->execute();
  833. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  834. if(!empty($res)){
  835. $sql = "SELECT id FROM `user_pullnew` where user_id={$user_id} and status>0 limit 1";
  836. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  837. $stmt = $_PDO->prepare($sql);
  838. $stmt->execute();
  839. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  840. if(empty($res)){
  841. return true;
  842. }
  843. }
  844. return false;
  845. }
  846. # 计算奖励佣金
  847. function getReward($money){
  848. if($money <= 0){
  849. return 0;
  850. }
  851. if($money > 300){
  852. $reward = 300;
  853. }else{
  854. $reward = $money;
  855. }
  856. return $reward;
  857. }
  858. # 同订单多分佣累加
  859. function arraySums($arr1, $arr2){
  860. $res = array();
  861. foreach($arr1 as $k=>$v){
  862. $res[$k] = $arr1[$k] + $arr2[$k];
  863. }
  864. return $res;
  865. }
  866. #拉新奖励订单插入
  867. 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){
  868. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  869. if( !empty($order_balance_at) ){
  870. $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)";
  871. $stmt = $_PDO->prepare($sql);
  872. $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));
  873. return $_PDO->lastinsertid();
  874. }
  875. else{
  876. $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)";
  877. $stmt = $_PDO->prepare($sql);
  878. $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));
  879. return $_PDO->lastinsertid();
  880. }
  881. }
  882. # 记录拉新首单表
  883. function pullNewInsert($user_id,$operate_id,$first_order_id,$order_create_at,$status){
  884. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  885. $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})";
  886. $stmt = $_PDO->prepare($sql);
  887. $stmt->execute();
  888. return $_PDO->lastinsertid();
  889. }
  890. #更新拉新首单表状态
  891. function pullNewUpdate($status, $order_id){
  892. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  893. $sql="update `user_pullnew` set status=:status where first_order_id=:order_id";
  894. $stmt = $_PDO->prepare($sql);
  895. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id));
  896. $res = $stmt->rowCount();
  897. return $res;
  898. }
  899. # 获取当前运营商拉新状态
  900. function getPullNew($operate_id){
  901. $sql="SELECT count(id) as records from `user_pullnew` where status>0 and operate_id = :operate_id";
  902. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  903. $stmt = $_PDO->prepare($sql);
  904. $stmt->execute(array(':operate_id'=>$operate_id));
  905. $result = $stmt->fetch(PDO::FETCH_OBJ)->records;
  906. return $result;
  907. }
  908. # 是否更新拉新表
  909. function pullNewIsUpOk($status, $order_id, $user_id){
  910. $sql = "SELECT id from user_pullnew where first_order_id = '{$order_id}'";
  911. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  912. $stmt = $_PDO->prepare($sql);
  913. $stmt->execute();
  914. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  915. if(empty($res)){
  916. return false;
  917. }
  918. if($status == 0){
  919. $sql="SELECT id from `order` where user_id={$user_id} and status>0 limit 1";
  920. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  921. $stmt = $_PDO->prepare($sql);
  922. $stmt->execute();
  923. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  924. if(!empty($res)){
  925. return false;
  926. }
  927. }
  928. return true;
  929. }
  930. # 判断是否是新手第一单
  931. function newCustFirstOrder($user_id){
  932. $sql = "select id from `order` where user_id={$user_id} limit 1";
  933. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  934. $stmt = $_PDO->prepare($sql);
  935. $stmt->execute();
  936. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  937. //之前没下过订单
  938. if(empty($res)){
  939. # 完成新手首单任务 ->** 逻辑变动,结算才完成任务
  940. /*$sql = " update red_user_mission set status=1 where user_id={$user_id} and type=2 ";
  941. $stmt = $_PDO->prepare($sql);
  942. $stmt->execute();
  943. $update_res = $stmt->rowCount();*/
  944. # 多拆4-5元
  945. $openRes = openRed($user_id);
  946. return true;
  947. }
  948. return false;
  949. }
  950. # 新手第一单多拆出4-5块助力红包
  951. function openRed($user_id){
  952. $time = date("Y-m-d H:i:s", strtotime('-1 day'));
  953. $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
  954. left join user_weixin uw on uw.union_id=rh.union_id
  955. left join user_level ul on ul.user_id = uw.user_id
  956. left join red_user_info ur on ur.id=rh.red_id
  957. 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";
  958. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  959. $stmt = $_PDO->prepare($sql);
  960. $stmt->execute();
  961. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  962. if(!empty($res)){
  963. $last_money = $res['red_money']*100 - $res['red_open_money']*100;
  964. if($last_money<=500){
  965. $_money = $last_money/100;
  966. }else{
  967. $_money = mt_rand(400, 500) / 100;
  968. }
  969. $union_id = $res['union_id'];
  970. $helper_name = $res['helper_name'];
  971. $helper_img = $res['helper_img'];
  972. # 拆红包
  973. $sql = "update red_user_info set red_open_money=red_open_money+{$_money} where id = {$res['id']}";
  974. $stmt = $_PDO->prepare($sql);
  975. $stmt->execute();
  976. $ur_res = $stmt->rowCount();
  977. $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)";
  978. $stmt = $_PDO->prepare($sql);
  979. $stmt->execute();
  980. $rh_res = $_PDO->lastinsertid();
  981. $userAccount = getUserBalace($res['user_id']);
  982. $lastBalance = isset($userAccount['balance']) ? $userAccount['balance'] : 0;
  983. $redAccount = redAccountRecord($res['user_id'], 5, $_money, $rh_res, round($lastBalance+$_money,2));
  984. return true;
  985. }
  986. return false;
  987. }
  988. # 获取用户红包额度
  989. function getUserBalace($user_id){
  990. $table = "red_account_record_".($user_id%10);
  991. $sql = "SELECT balance from {$table} where user_id={$user_id} order by id desc limit 1";
  992. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  993. $stmt = $_PDO->prepare($sql);
  994. $stmt->execute();
  995. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  996. return $res;
  997. }
  998. #更新流水
  999. function redAccountRecord($user_id, $type, $money, $oh_id, $balance){
  1000. $table = "red_account_record_".($user_id%10);
  1001. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  1002. $sql = "insert into `{$table}` (user_id, type, money, oh_id, balance) values ({$user_id},{$type},{$money},'{$oh_id}', {$balance})";
  1003. $stmt = $_PDO->prepare($sql);
  1004. $stmt->execute();
  1005. return $_PDO->lastinsertid();
  1006. }
  1007. #查裂变运营商信息
  1008. function preYysInfo($user_id){
  1009. $sql = "SELECT count(1) as nums,ui.pre_yys_id from user_level_change_info ui
  1010. 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'
  1011. ";
  1012. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  1013. $stmt = $_PDO->prepare($sql);
  1014. $stmt->execute();
  1015. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  1016. $data = array();
  1017. if(isset($res['nums']) && $res['nums']>=3){
  1018. $data['user_id'] = $res['pre_yys_id'];
  1019. if($res['nums']>=10){
  1020. $data['rate'] = 0.03;
  1021. }elseif($res['nums']>=6){
  1022. $data['rate'] = 0.02;
  1023. }elseif($res['nums']>=3){
  1024. $data['rate'] = 0.01;
  1025. }
  1026. }
  1027. return $data;
  1028. }
  1029. #退款推送
  1030. function refundPush($order_id){
  1031. $sql = "SELECT user_id,rebate from order_rebate where order_id='{$order_id}' and status=0 and rebate>0 and type!=7";
  1032. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  1033. $stmt = $_PDO->prepare($sql);
  1034. $stmt->execute();
  1035. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  1036. if(!empty($res)){
  1037. foreach($res as $k=>$v){
  1038. $params = array();
  1039. $params['user_id'] = $v['user_id'];
  1040. $params['type'] = 6; //订单退款
  1041. $params['money'] = round($v['rebate']/100, 2);
  1042. $push_res = curl_post( REFUND_PUSH_URL, $params);
  1043. echo "\n退款推送:用户:".$v['user_id']."|-推送:".$push_res.'|-订单:'.$order_id;
  1044. }
  1045. }
  1046. }
  1047. /**
  1048. * 判断是否是新逻辑运营中心0425
  1049. */
  1050. function isNewYysManage($user_id){
  1051. $Arr = [23512,48941,214262,218326,222044,235824,239555,262168,262285,311074,317849]; //0425-老运营中心
  1052. if(in_array($user_id, $Arr)){
  1053. return false;
  1054. }
  1055. return true;
  1056. }
  1057. # curl调用接口push
  1058. function curl_post($url, $params){
  1059. $ch = curl_init();
  1060. curl_setopt($ch, CURLOPT_URL, $url);
  1061. curl_setopt($ch, CURLOPT_HEADER, 0);
  1062. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1063. curl_setopt($ch, CURLOPT_POST, 1);
  1064. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  1065. $response = curl_exec($ch);
  1066. curl_close($ch);
  1067. return $response;
  1068. }
  1069. readDataFromDb();