优惠券订单及其他脚本

scan_orderv3_koudai.php 41KB

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