优惠券订单及其他脚本

scan_orderv3_otherApp.php 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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://tbk.726p.com/api/v2/MessagePush/rebatepush');//push接口地址
  8. define("ACT_PUSH_URL",'https://tbk.726p.com/api/v2/MessagePush/pullnew_push');//拉新push接口地址
  9. define("ACT_START", '2018-12-25 00:00:00');//
  10. define("ACT_END", '2018-12-31 23:59:59');//
  11. define("ACT_GOODS_IMG", 'http://imgs.726p.com/tbk/upload/advImg2018-12-24/135037/kx-5c2073adbe478.png');//
  12. /**
  13. * 导入及更新 order/order_rebate数据 + 淘宝拉新活动
  14. */
  15. #设置时区
  16. date_default_timezone_set('PRC');
  17. set_time_limit(0);
  18. ini_set('memory_limit','1024M');
  19. function readDataFromDb(){
  20. $scriptStart = date("Y-m-d H:i:s");
  21. # 1.获取8分钟内订单
  22. //$stime = date('Y-m-d H:i:s',strtotime('2018-08-07'));
  23. $stime = date('Y-m-d H:i:s', time()-360);
  24. $sql = "SELECT * from ftxia_taoke_detail2 where update_time > :stime and adv_name not like 'api_%' order by create_time asc,order_sn asc";
  25. $dumpsql = str_replace(':stime', "'".$stime."'", $sql);
  26. echo "\n\n".$dumpsql."\n";
  27. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  28. $stmt = $_PDO->prepare($sql);
  29. $stmt->execute(array(':stime'=>$stime));
  30. $orderResult = $stmt->fetchAll(PDO::FETCH_ASSOC);
  31. # 遍历订单
  32. # 统计
  33. $res_count = array(
  34. 'total_num' => 0,
  35. 'generalinsert_num' => 0,
  36. 'generalupdate_num' => 0,
  37. 'nouserinsert_num' => 0,
  38. 'nouserupdate_num' => 0,
  39. 'fail_insert_num' => 0,
  40. 'fail_insert_ids' => '',
  41. 'fail_insertno_num' => 0,
  42. 'fail_insertno_ids' => '',
  43. 'fail_update_num' => 0,
  44. //'fail_update_ids' => '',
  45. 'fail_updateno_num' => 0,
  46. //'fail_updateno_ids' => '',
  47. );
  48. $orderList = array();
  49. $rewardOrder = array(); //拉新订单记录
  50. $nums = 0;
  51. foreach( $orderResult as $key=>$orderDetail ){
  52. $nums++;
  53. $res_count['total_num']++;
  54. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  55. if( DETAILDEBUG == 1){
  56. echo 'orderDetail:';
  57. }
  58. $order_id=$orderDetail["order_sn"];
  59. $update_time=$orderDetail["update_time"];
  60. $order_create_at=$orderDetail["create_time"];
  61. $order_click_at=$orderDetail["click_time"];
  62. $order_balance_at=$orderDetail["balance_time"];
  63. $goods_id=$orderDetail["goods_id"];
  64. $goods_name=$orderDetail["goods_name"];
  65. $goods_name=str_replace(['"',"'"],'',$goods_name);
  66. $num=$orderDetail["goods_number"];
  67. $price=intval($orderDetail["goods_price"]*100);
  68. $sum_price=intval($orderDetail["order_amount"]*100);
  69. $statusDesc=$orderDetail["order_status"];
  70. $typeDesc=$orderDetail["order_type"];
  71. $orderStatus = 1;
  72. $type=1;
  73. if($statusDesc=="订单付款"){
  74. $orderStatus=1;
  75. }
  76. elseif($statusDesc=="订单失效"){
  77. $orderStatus=0;
  78. }
  79. elseif($statusDesc=="订单结算"){
  80. $orderStatus=2;
  81. }
  82. if($typeDesc=="聚划算"){
  83. $type=0;
  84. }
  85. elseif($typeDesc=="淘宝"){
  86. $type=1;
  87. }
  88. elseif($typeDesc=="天猫"){
  89. $type=2;
  90. }
  91. $adzone_id=$orderDetail["adv_id"];
  92. $ceil_income=round($orderDetail["effect_prediction"], 2);
  93. $predict_income=$ceil_income*100;
  94. $predict_income=ceil($predict_income*conf::$RATE_CONF["globalzk"]);
  95. echo 'order_id:'.$order_id.'|| goods_id:'.$goods_id.'|| orderstatus:'.$orderStatus.'|| order_balance_at:'.$order_balance_at.'|| update_time:'.$update_time;
  96. echo '|| predict_income:'.$predict_income.'|| adzone_id:'.$adzone_id."\n";
  97. $resultone=UserInfo($adzone_id);
  98. if( DETAILDEBUG == 1){
  99. echo 'userinfo:';
  100. var_dump($resultone);
  101. echo "\n";
  102. }
  103. if($sum_price!=0){
  104. if($predict_income<5){
  105. $predict_income=5;
  106. }
  107. }
  108. if( !empty($resultone) ){
  109. echo "User:存在\n";
  110. $orderExists=orderMysql($order_id,$goods_id);
  111. if( empty($orderExists) || in_array($order_id,$orderList)){
  112. echo "action:插入操作\n";
  113. #获取层级规则信息
  114. $userAllInfo=userLevelandPrentInfo($resultone);
  115. echo "<pre>\nuserAllInfo";
  116. print_r($userAllInfo);
  117. if( DETAILDEBUG == 1){
  118. echo 'userallinfo:'."\n";
  119. var_dump($userAllInfo);
  120. }
  121. $userList = $userAllInfo['userList']; //4层 每层userId
  122. $userLevel = $userAllInfo['userLevel']; //4层 每层userlevel
  123. # 从数据库获取规则
  124. $rebateRuleInfo = getRebateRule($userAllInfo['levelRule']);
  125. echo "\nRuleInfo:";
  126. print_r($rebateRuleInfo);
  127. $RuleArr = explode(',',$rebateRuleInfo['rebate_rule']); //4层 每层分佣比例
  128. $TypeArr = explode(',',$rebateRuleInfo['rebate_type']); //4层 每层分佣级别
  129. #获取每一层返利
  130. $moneyInfo=orderMoney($predict_income, $RuleArr); //4层 每层分佣钱数
  131. echo "\nmoneyInfo:";
  132. print_r($moneyInfo);
  133. if( DETAILDEBUG == 1){
  134. echo 'rebateInfo'."\n";
  135. var_dump($moneyInfo);
  136. }
  137. $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
  138. if( !empty($detail) ){
  139. $goods_img=$detail->pict_url;
  140. }
  141. else{
  142. $goods_img="";
  143. }
  144. try{
  145. $_PDO->beginTransaction();
  146. #订单表插入
  147. $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);
  148. if($res){
  149. $orderList[] = $order_id;
  150. $res_count['generalinsert_num']++;
  151. }else{
  152. $res_count['fail_insert_num']++;
  153. $res_count['fail_insert_ids'].=$order_id.'|';
  154. }
  155. echo 'orderinsert:'.$res;
  156. echo "\n";
  157. if($sum_price>=0 and $orderStatus!=0){
  158. foreach($moneyInfo as $k=>$v){
  159. if($v>0){
  160. #order_rebate表插入父级佣金
  161. $rebaseType=$TypeArr[$k];
  162. $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);
  163. # 推送
  164. if($res){
  165. $params = array();
  166. $params['user_id'] = $userList[$k];
  167. $params['type'] = 2;
  168. $params['money'] = round($moneyInfo[$k]/100,2);
  169. $push_res = curl_post( PUSH_URL, $params);
  170. }
  171. echo 'rebateinsert_'.$k.':'.$res;
  172. echo "\n";
  173. }
  174. }
  175. if($order_create_at >= ACT_START && $order_create_at <= ACT_END){
  176. # 判断是否是拉新首单
  177. $isNew = 0;
  178. $isTooNew = 0;
  179. if(array_key_exists($order_id, $rewardOrder)){
  180. $isTooNew = 1;
  181. }else{
  182. $isNew = isNewUser($userList[0]);
  183. }
  184. if($isTooNew || $isNew){
  185. //记录拉新订单分佣情况
  186. if($isNew){
  187. $rewardOrder[$order_id] = $moneyInfo;
  188. }else{
  189. $rewardOrder[$order_id] = arraySums($rewardOrder[$order_id],$moneyInfo);
  190. }
  191. //获取下一个分佣的订单
  192. $nkey = $nums;
  193. //若当前订单和下一笔订单号一样,继续循环,直到本拉新订单分佣rewardOrder[$order_id]统计完毕
  194. if(isset($orderResult[$nkey]['order_sn']) && $order_id == $orderResult[$nkey]['order_sn']){
  195. }else{
  196. // 分奖励佣金 < --------- Start --------- >
  197. $reward_n = 0; // 记录奖励层数 最多两层
  198. $rewardInfo = $rewardOrder[$order_id]; //本订单实际分佣情况
  199. foreach($rewardInfo as $k=>$v){
  200. if($v>0 && $reward_n<2){
  201. # 插入奖励
  202. $rebaseType=6; //奖励佣金
  203. # 计算奖励佣金
  204. $reward = getReward($rewardInfo[$k]);
  205. $goods_name_new = $k==0 ? '恭喜您获得新用户首单奖励' : '恭喜您获得拉新首单奖励';
  206. $goods_img_new = ACT_GOODS_IMG;
  207. $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);
  208. # 奖励推送
  209. if($res){
  210. $params = array();
  211. $params['user_id'] = $userList[$k];
  212. $params['type'] = $k==0 ? 3 : 4; //奖励推送 3新人 4上级
  213. $params['money'] = round($reward/100,2);
  214. $push_res = curl_post( ACT_PUSH_URL, $params);
  215. }
  216. echo 'rewardinsert_'.$reward_n.':'.$res;
  217. echo "\n";
  218. # 记录奖励层数
  219. $reward_n++;
  220. }
  221. }
  222. # 记录新人首单信息
  223. if($res){
  224. //获取运营商
  225. $m = strpos($userAllInfo['levelRule'], '3');
  226. $operate_id = $userList[$m];
  227. $res2 = pullNewInsert($userList[0],$operate_id,$order_id,$order_create_at,$orderStatus);
  228. //获取当前运营商拉新人数
  229. $pullCount = getPullNew($operate_id);
  230. if($pullCount==10 || $pullCount==20 || $pullCount==30){
  231. //符合推送条件
  232. $params = array();
  233. $params['user_id'] = $operate_id;
  234. $params['type'] = 5; //运营商拉新人数推送
  235. $params['count'] = $pullCount;
  236. $push_res = curl_post( ACT_PUSH_URL, $params);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. //津贴单独计算
  244. # 获取顶级运营商-控制中心
  245. if($sum_price>0 and $orderStatus !=0){
  246. $mks = strpos($userAllInfo['levelRule'], '3');
  247. if($mks !== false){
  248. $YysManageInfo = YysManageInfo( $userList[$mks] );
  249. if( !empty($YysManageInfo) ){
  250. $rebaseType=$TypeArr[$mks];
  251. $YysManageRabate = round($predict_income*conf::$RATE_CONF["YysManage"]);
  252. if($YysManageRabate < 1){
  253. $YysManageRabate = 1;
  254. }
  255. $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);
  256. echo 'rebateinsert_YysManage:'.$res;
  257. echo "\n";
  258. }
  259. }
  260. }
  261. $_PDO->commit();
  262. }catch(PDOException $e){
  263. echo 'err_msg'.$e->getMessage()."\n";
  264. $_PDO->rollback();
  265. }
  266. //echo "-----------------insertEnd--------------------\n";
  267. }
  268. else{
  269. try{
  270. $_PDO->beginTransaction();
  271. //更新order/order_rebate表
  272. echo "action:更新操作\n";
  273. $res=generateSqlupdate($orderStatus,$order_balance_at,$order_id,$goods_id);
  274. /*** 增加拉新更新 Start ***/
  275. if($order_create_at >= ACT_START && $order_create_at <= ACT_END){
  276. $pull_up = pullNewUpdate($orderStatus, $order_id);
  277. }
  278. /*** 增加拉新更新 End ***/
  279. if($res){
  280. $res_count['generalupdate_num']++;
  281. }else{
  282. $res_count['fail_update_num']++;
  283. //$res_count['fail_update_ids'].=$order_id.'|';
  284. }
  285. echo "更新数据-> orderstatus:".$orderStatus." order_balance_at:".$order_balance_at;
  286. echo "\n";
  287. echo "order/reabte_update:".$res;
  288. echo "\n";
  289. //echo "-----------------updateEnd-----------------------\n";
  290. $_PDO->commit();
  291. }catch(PDOException $e){
  292. echo 'err_msg'.$e->getMessage()."\n";
  293. $_PDO->rollback();
  294. }
  295. }
  296. //echo "-----------------IssetUserEnd:--------------------\n";
  297. }
  298. else{
  299. $_PDO->beginTransaction();
  300. try{
  301. #user不存在的处理
  302. echo "User:不存在\n";
  303. $ordernoExists=orderMysql($order_id,$goods_id);
  304. if( empty($ordernoExists) ){
  305. $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
  306. if( !empty($detail) ){
  307. $goods_img=$detail->pict_url;
  308. }
  309. else{
  310. $goods_img="";
  311. }
  312. $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);
  313. if($res){
  314. $res_count['nouserinsert_num']++;
  315. }else{
  316. $res_count['fail_insertno_num']++;
  317. $res_count['fail_insertno_ids'].=$order_id.'|';
  318. }
  319. echo "action:插入操作\n";
  320. echo "orderinsert:".$res;
  321. echo "\n";
  322. $_PDO->commit();
  323. }
  324. else{
  325. $res=generateSqlUpdateOther($orderStatus,$order_balance_at,$order_id,$goods_id);
  326. if($res){
  327. $res_count['nouserupdate_num']++;
  328. }else{
  329. $res_count['fail_updateno_num']++;
  330. //$res_count['fail_updateno_ids'].=$order_id.'|';
  331. }
  332. echo "action:更新操作\n";
  333. echo "orderupdate:".$res;
  334. echo "\n";
  335. $_PDO->commit();
  336. }
  337. //echo "-----------------EmptyUserEnd:--------------------\n";
  338. }catch(PDOException $e){
  339. echo 'err_msg'.$e->getMessage()."\n";
  340. $_PDO->rollback();
  341. }
  342. }
  343. echo "/ ** order{$res_count['total_num']}-End ** /";
  344. echo "\n\n\n";
  345. if(LIMIT_NUM >0 && $res_count['total_num'] == LIMIT_NUM) break;
  346. }
  347. print_r($res_count);
  348. echo "\n本次脚本开始时间:". $scriptStart;
  349. echo "\n本次脚本结束时间:". date("Y-m-d H:i:s");
  350. }
  351. function YysManageInfo( $user_id ){
  352. $sql = "SELECT user_id,parent_user_id,level FROM user_level WHERE user_id=:user_id ";
  353. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  354. $stmt = $_PDO->prepare($sql);
  355. $stmt->execute(array(':user_id'=>$user_id));
  356. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  357. if( empty($result) ) return false;
  358. if($result['level'] == 4){
  359. return $result;
  360. }else{
  361. if( empty($result['parent_user_id']) ){
  362. return false;
  363. }
  364. return YysManageInfo( $result['parent_user_id']);
  365. }
  366. return false;
  367. }
  368. function YysInfo( $user_id ){
  369. $sql = "SELECT user_id,parent_user_id,level FROM user_level WHERE user_id=:user_id ";
  370. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  371. $stmt = $_PDO->prepare($sql);
  372. $stmt->execute(array(':user_id'=>$user_id));
  373. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  374. if( empty($result) ) return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  375. if($result['level'] >= 3){
  376. return $result;
  377. }else{
  378. if( empty($result['parent_user_id']) ){
  379. return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  380. }
  381. return YysInfo( $result['parent_user_id']);
  382. }
  383. return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  384. }
  385. function UserInfo($adzone_id){
  386. $sql = "SELECT * FROM user_adzone WHERE adzone_id = :adzone_id ";
  387. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  388. $stmt = $_PDO->prepare($sql);
  389. $stmt->execute(array(':adzone_id'=>$adzone_id));
  390. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  391. if($resultone){
  392. $sql="SELECT * FROM user_level WHERE user_id = :user_id";
  393. $stmt = $_PDO->prepare($sql);
  394. $stmt->execute(array(':user_id'=>$resultone['user_id']));
  395. $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
  396. return $userResult;
  397. }
  398. else{
  399. return false;
  400. }
  401. }
  402. function userLevelInfo($user_id){
  403. $sql="SELECT * FROM user_level WHERE user_id = :user_id ";
  404. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  405. $stmt = $_PDO->prepare($sql);
  406. $stmt->execute(array(':user_id'=>$user_id));
  407. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  408. if($resultone){
  409. $myInfo=["user_id"=>$user_id,"level"=>$resultone['level'],"parent_user_id"=>$resultone['parent_user_id']];
  410. return $myInfo;
  411. }
  412. else{
  413. return false;
  414. }
  415. }
  416. function userMyInfo($user_id){
  417. $sql="SELECT * FROM users WHERE id = :user_id ";
  418. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  419. $stmt = $_PDO->prepare($sql);
  420. $stmt->execute(array(':user_id'=>$user_id));
  421. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  422. if($resultone){
  423. $myInfo['user_id'] = $user_id;
  424. $myInfo['origin'] = $resultone['origin'];
  425. return $myInfo;
  426. }
  427. else{
  428. return false;
  429. }
  430. }
  431. # 获取4层用户级别基础信息
  432. function userLevelandPrentInfo($resultone){
  433. $resultInfo = array();
  434. # self
  435. $userId=$resultone['user_id'];
  436. $userLevel=$resultone['level'];
  437. if($userLevel>=3){
  438. $resultInfo['userList'] = [$userId,0,0,0];
  439. $resultInfo['levelRule'] = 3000;
  440. $resultInfo['userLevel'] = [$userLevel,0,0,0];
  441. return $resultInfo;
  442. }
  443. $userParentId=$resultone['parent_user_id'];
  444. $userGrandId=$resultone['grand_father_id'];
  445. if($userParentId>0){
  446. $resulttwo=userLevelInfo($userParentId);
  447. if( !empty($resulttwo) ){
  448. # parent
  449. $userParentLevel=$resulttwo['level'];
  450. if($userParentLevel>=3){
  451. $resultInfo['userList'] = [$userId,$userParentId,0,0];
  452. $resultInfo['levelRule'] = intval($userLevel.'300');
  453. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,0,0];
  454. return $resultInfo;
  455. }
  456. if($userGrandId>0){
  457. $resultthree=userLevelInfo($userGrandId);
  458. if( !empty($resultthree) ){
  459. # grand
  460. $userGrandLevel=$resultthree['level'];
  461. if($userGrandLevel>=3){
  462. $resultInfo['userList'] = [$userId,$userParentId,$userGrandId,0];
  463. $resultInfo['levelRule'] = intval($userLevel.$userParentLevel.'30');
  464. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,$userGrandLevel,0];
  465. return $resultInfo;
  466. }
  467. }
  468. }
  469. }
  470. }
  471. if( !$userParentId ) $userParentId=0;
  472. if( !$userGrandId ) $userGrandId=0;
  473. if( !isset($userParentLevel) ) $userParentLevel=1;
  474. if( !isset($userGrandLevel) ) $userGrandLevel=1;
  475. #三级里面没有运营商,递归上级运营商
  476. $YysInfo = YysInfo($userGrandId);
  477. $userFourId = $YysInfo['user_id'];
  478. $userFourLevel = $YysInfo['level'];
  479. # 层级规则
  480. $levelRule = $userLevel.$userParentLevel.$userGrandLevel.'3';
  481. $resultInfo['userList']=[$userId,$userParentId,$userGrandId,$userFourId];
  482. $resultInfo['levelRule'] = (int)$levelRule;
  483. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,$userGrandLevel,$userFourLevel];
  484. return $resultInfo;
  485. }
  486. function orderMoney($predict_income, $RuleArr){
  487. $result = array();
  488. foreach($RuleArr as $k=>$v){
  489. if($v>0){
  490. $result[$k] = round($predict_income * $v / 100);
  491. }else{
  492. $result[$k] = 0;
  493. }
  494. }
  495. return $result;
  496. }
  497. function orderMysql($order_id,$goods_id){
  498. $sql="SELECT * from `order` where order_id=:order_id and goods_id=:goods_id";
  499. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  500. $stmt = $_PDO->prepare($sql);
  501. $stmt->execute(array(':order_id'=>$order_id,':goods_id'=>$goods_id));
  502. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  503. return $result;
  504. }
  505. function orderType($adzone_id){
  506. $sql="SELECT * from `adzone` where adzone_id=:adzone_id";
  507. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  508. $stmt = $_PDO->prepare($sql);
  509. $stmt->execute(array(':adzone_id'=>$adzone_id));
  510. $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
  511. return $userResult;
  512. }
  513. 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){
  514. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  515. if( empty($order_balance_at) ){
  516. $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)";
  517. $stmt = $_PDO->prepare($sql);
  518. $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));
  519. return $_PDO->lastinsertid();
  520. }
  521. else{
  522. $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)";
  523. $stmt = $_PDO->prepare($sql);
  524. $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));
  525. return $_PDO->lastinsertid();
  526. }
  527. }
  528. #order表订单插入脚本
  529. 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){
  530. $adzoneDetail=orderType($adzone_id);
  531. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  532. if( empty($order_balance_at) ){
  533. $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)";
  534. $stmt = $_PDO->prepare($sql);
  535. $stmt->execute(array(':company_type'=>$adzoneDetail["type"],':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));
  536. return $_PDO->lastinsertid();
  537. }
  538. else{
  539. $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)";
  540. $stmt = $_PDO->prepare($sql);
  541. $stmt->execute(array(':company_type'=>$adzoneDetail["type"],':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));
  542. return $_PDO->lastinsertid();
  543. }
  544. }
  545. #返利表订单插入脚本
  546. 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){
  547. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  548. if( !empty($order_balance_at) ){
  549. $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)";
  550. $stmt = $_PDO->prepare($sql);
  551. $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));
  552. return $_PDO->lastinsertid();
  553. }
  554. else{
  555. $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)";
  556. $stmt = $_PDO->prepare($sql);
  557. $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));
  558. return $_PDO->lastinsertid();
  559. }
  560. }
  561. #单独处理订单插运营商佣金
  562. 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){
  563. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  564. if( !empty($order_balance_at) ){
  565. $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)";
  566. $stmt = $_PDO->prepare($sql);
  567. $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));
  568. return $_PDO->lastinsertid();
  569. }
  570. else{
  571. $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)";
  572. $stmt = $_PDO->prepare($sql);
  573. $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));
  574. return $_PDO->lastinsertid();
  575. }
  576. }
  577. #返利表订单插入运营商津贴信息
  578. 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){
  579. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  580. if( !empty($order_balance_at) ){
  581. $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) 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)";
  582. $stmt = $_PDO->prepare($sql);
  583. $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));
  584. return $_PDO->lastinsertid();
  585. }
  586. else{
  587. $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) VALUES (:user_level,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income)";
  588. $stmt = $_PDO->prepare($sql);
  589. $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));
  590. return $_PDO->lastinsertid();
  591. }
  592. }
  593. #order订单表返利表同步更新
  594. function generateSqlupdate($status,$order_balance_at,$order_id,$goods_id){
  595. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  596. if( !empty($order_balance_at) ){
  597. $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  598. $stmt = $_PDO->prepare($sql);
  599. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  600. $res = $stmt->rowCount();
  601. //更新order_rebate
  602. $sql="update order_rebate set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  603. $stmt = $_PDO->prepare($sql);
  604. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  605. $res = $stmt->rowCount();
  606. //更新order_rebate_profit
  607. $sql="update order_rebate_profit set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  608. $stmt = $_PDO->prepare($sql);
  609. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  610. $res = $stmt->rowCount();
  611. }
  612. else{
  613. $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  614. $stmt = $_PDO->prepare($sql);
  615. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  616. $res = $stmt->rowCount();
  617. $sql="update order_rebate set status=:status where order_id=:order_id and goods_id=:goods_id ";
  618. $stmt = $_PDO->prepare($sql);
  619. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  620. $res = $stmt->rowCount();
  621. //更新order_rebate_profit
  622. $sql="update order_rebate_profit set status=:status where order_id=:order_id and goods_id=:goods_id ";
  623. $stmt = $_PDO->prepare($sql);
  624. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  625. $res = $stmt->rowCount();
  626. }
  627. return $res;
  628. }
  629. #订单表单独更新
  630. function generateSqlUpdateOther($status,$order_balance_at,$order_id,$goods_id){
  631. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  632. if( !empty($order_balance_at) ){
  633. $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  634. $stmt = $_PDO->prepare($sql);
  635. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  636. $res = $stmt->rowCount();
  637. }
  638. else{
  639. $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  640. $stmt = $_PDO->prepare($sql);
  641. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  642. $res = $stmt->rowCount();
  643. }
  644. return $res;
  645. }
  646. # 获取rebateRule
  647. function getRebateRule($levelRule){
  648. $sql="SELECT * from `rebate_rule` where level_rule = :level_rule";
  649. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  650. $stmt = $_PDO->prepare($sql);
  651. $stmt->execute(array(':level_rule'=>$levelRule));
  652. $ruleResult = $stmt->fetch(PDO::FETCH_ASSOC);
  653. return $ruleResult;
  654. }
  655. # 判断是否是新用户第一单
  656. function isNewUser($user_id){
  657. $start = ACT_START;
  658. $end = ACT_END;
  659. $sql = "SELECT id FROM `user_level` where regist_at>'{$start}' and regist_at<= '{$end}' and user_id = {$user_id} limit 1";
  660. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  661. $stmt = $_PDO->prepare($sql);
  662. $stmt->execute();
  663. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  664. if(!empty($res)){
  665. $sql = "SELECT id FROM `user_pullnew` where user_id={$user_id} and status>0 limit 1";
  666. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  667. $stmt = $_PDO->prepare($sql);
  668. $stmt->execute();
  669. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  670. if(empty($res)){
  671. return true;
  672. }
  673. }
  674. return false;
  675. }
  676. # 计算奖励佣金
  677. function getReward($money){
  678. if($money <= 0){
  679. return 0;
  680. }
  681. if($money > 300){
  682. $reward = 300;
  683. }else{
  684. $reward = $money;
  685. }
  686. return $reward;
  687. }
  688. # 同订单多分佣累加
  689. function arraySums($arr1, $arr2){
  690. $res = array();
  691. foreach($arr1 as $k=>$v){
  692. $res[$k] = $arr1[$k] + $arr2[$k];
  693. }
  694. return $res;
  695. }
  696. #拉新奖励订单插入
  697. 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){
  698. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  699. if( !empty($order_balance_at) ){
  700. $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)";
  701. $stmt = $_PDO->prepare($sql);
  702. $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));
  703. return $_PDO->lastinsertid();
  704. }
  705. else{
  706. $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)";
  707. $stmt = $_PDO->prepare($sql);
  708. $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));
  709. return $_PDO->lastinsertid();
  710. }
  711. }
  712. # 记录拉新首单表
  713. function pullNewInsert($user_id,$operate_id,$first_order_id,$order_create_at,$status){
  714. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  715. $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})";
  716. $stmt = $_PDO->prepare($sql);
  717. $stmt->execute();
  718. return $_PDO->lastinsertid();
  719. }
  720. #更新拉新首单表状态
  721. function pullNewUpdate($status, $order_id){
  722. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  723. $sql="update `user_pullnew` set status=:status where first_order_id=:order_id";
  724. $stmt = $_PDO->prepare($sql);
  725. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id));
  726. $res = $stmt->rowCount();
  727. return $res;
  728. }
  729. # 获取当前运营商拉新状态
  730. function getPullNew($operate_id){
  731. $sql="SELECT count(id) as records from `user_pullnew` where status>0 and operate_id = :operate_id";
  732. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  733. $stmt = $_PDO->prepare($sql);
  734. $stmt->execute(array(':operate_id'=>$operate_id));
  735. $result = $stmt->fetch(PDO::FETCH_OBJ)->records;
  736. return $result;
  737. }
  738. # curl调用接口push
  739. function curl_post($url, $params){
  740. $ch = curl_init();
  741. curl_setopt($ch, CURLOPT_URL, $url);
  742. curl_setopt($ch, CURLOPT_HEADER, 0);
  743. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  744. curl_setopt($ch, CURLOPT_POST, 1);
  745. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  746. $response = curl_exec($ch);
  747. curl_close($ch);
  748. return $response;
  749. }
  750. readDataFromDb();