优惠券订单及其他脚本

red_order_tmp.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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("ACT_PUSH_URL",'http://apitbk.726p.com/api/v2/MessagePush/act_push');//push接口地址
  8. define("ACT_GOODS_IMG", 'https://kx-youhuiquan.oss-cn-beijing.aliyuncs.com/v2/hongbaojiangli.png');
  9. $redConfig = getConfig();
  10. if($redConfig['red_active_status'] == 0){
  11. //exit('活动未开启');
  12. }
  13. define("EXTRATE", $redConfig['red_rebate_rate']); //红包提现匹配的佣金比例
  14. define('RED_ACT_OPEN', $redConfig['red_active_status']);//活动开启状态
  15. /**
  16. * 导入及更新 order/order_rebate数据 + 淘宝拉新活动
  17. */
  18. #设置时区
  19. date_default_timezone_set('PRC');
  20. set_time_limit(0);
  21. ini_set('memory_limit','1024M');
  22. function readDataFromDb(){
  23. # 记录脚本开始时间
  24. $scriptStart = date("Y-m-d H:i:s");
  25. # 1.获取8分钟内订单
  26. //$stime = date('Y-m-d H:i:s',strtotime('2018-08-07'));
  27. $stime = date('Y-m-d H:i:s', time()-360);
  28. $sql = "SELECT * from ftxia_taoke_detail2 where update_time > :stime and adv_name like 'api_%' and create_time>'2019-01-24' order by create_time asc,order_sn asc";
  29. $dumpsql = str_replace(':stime', "'".$stime."'", $sql);
  30. echo "\n\n".$dumpsql."\n";
  31. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  32. $stmt = $_PDO->prepare($sql);
  33. $stmt->execute(array(':stime'=>$stime));
  34. $orderResult = $stmt->fetchAll(PDO::FETCH_ASSOC);
  35. # 遍历订单
  36. # 统计
  37. $res_count = array(
  38. 'total_num' => 0,
  39. 'generalinsert_num' => 0,
  40. 'generalupdate_num' => 0,
  41. 'nouserinsert_num' => 0,
  42. 'nouserupdate_num' => 0,
  43. 'fail_insert_num' => 0,
  44. 'fail_insert_ids' => '',
  45. 'fail_insertno_num' => 0,
  46. 'fail_insertno_ids' => '',
  47. 'fail_update_num' => 0,
  48. 'fail_updateno_num' => 0,
  49. );
  50. $orderList = array();
  51. $rewardOrder = array(); //同订单多商品记录
  52. $orderRedArr = array(); // 红包订单记录
  53. $nums = 0;
  54. foreach( $orderResult as $key=>$orderDetail ){
  55. $nums++;
  56. $res_count['total_num']++;
  57. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  58. if( DETAILDEBUG == 1){
  59. echo 'orderDetail:';
  60. }
  61. $order_id=$orderDetail["order_sn"];
  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["balance_time"];
  66. $goods_id=$orderDetail["goods_id"];
  67. $goods_name=$orderDetail["goods_name"];
  68. $goods_name=str_replace(['"',"'"],'',$goods_name);
  69. $num=$orderDetail["goods_number"];
  70. $price=intval($orderDetail["goods_price"]*100);
  71. $sum_price=intval($orderDetail["order_amount"]*100);
  72. $statusDesc=$orderDetail["order_status"];
  73. $typeDesc=$orderDetail["order_type"];
  74. $orderStatus = 1;
  75. $type=1;
  76. if($statusDesc=="订单付款"){
  77. $orderStatus=1;
  78. }
  79. elseif($statusDesc=="订单失效"){
  80. $orderStatus=0;
  81. }
  82. elseif($statusDesc=="订单结算"){
  83. $orderStatus=2;
  84. }
  85. if($typeDesc=="聚划算"){
  86. $type=0;
  87. }
  88. elseif($typeDesc=="淘宝"){
  89. $type=1;
  90. }
  91. elseif($typeDesc=="天猫"){
  92. $type=2;
  93. }
  94. $adzone_id=$orderDetail["adv_id"];
  95. $ceil_income=round($orderDetail["effect_prediction"], 2);
  96. $predict_income=$ceil_income*100;
  97. $predict_income=ceil($predict_income*conf::$RATE_CONF["globalzk"]);
  98. echo 'order_id:'.$order_id.'|| goods_id:'.$goods_id.'|| orderstatus:'.$orderStatus.'|| order_balance_at:'.$order_balance_at.'|| update_time:'.$update_time;
  99. echo '|| predict_income:'.$predict_income.'|| adzone_id:'.$adzone_id."\n";
  100. $resultone=UserInfo($adzone_id);
  101. if( DETAILDEBUG == 1){
  102. echo 'userinfo:';
  103. var_dump($resultone);
  104. echo "\n";
  105. }
  106. if($sum_price!=0){
  107. if($predict_income<5){
  108. $predict_income=5;
  109. }
  110. }
  111. if( !empty($resultone) ){
  112. echo "User:存在\n";
  113. $orderExists=orderMysql($order_id,$goods_id);
  114. if( empty($orderExists) || in_array($order_id,$orderList)){
  115. if(RED_ACT_OPEN == 0){
  116. //活动未开启时,跳过插入
  117. continue;
  118. }
  119. echo "action:插入操作\n";
  120. #获取层级规则信息
  121. $userAllInfo=userLevelandPrentInfo($resultone);
  122. //echo "<pre>\nuserAllInfo";
  123. //print_r($userAllInfo);
  124. if( DETAILDEBUG == 1){
  125. echo 'userallinfo:'."\n";
  126. var_dump($userAllInfo);
  127. }
  128. $userList = $userAllInfo['userList']; //4层 每层userId
  129. $userLevel = $userAllInfo['userLevel']; //4层 每层userlevel
  130. # 从数据库获取规则
  131. $rebateRuleInfo = getRebateRule($userAllInfo['levelRule']);
  132. //echo "\nRuleInfo:";
  133. //print_r($rebateRuleInfo);
  134. $RuleArr = explode(',',$rebateRuleInfo['rebate_rule']); //4层 每层分佣比例
  135. $TypeArr = explode(',',$rebateRuleInfo['rebate_type']); //4层 每层分佣级别
  136. #获取每一层返利
  137. $moneyInfo=orderMoney($predict_income, $RuleArr); //4层 每层分佣钱数
  138. echo "\nmoneyInfo:";
  139. print_r($moneyInfo);
  140. $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
  141. if( !empty($detail) ){
  142. $goods_img=$detail->pict_url;
  143. }
  144. else{
  145. $goods_img="";
  146. }
  147. try{
  148. $_PDO->beginTransaction();
  149. /*** 判断是否是红包商品 ***/
  150. if(!in_array($order_id, $orderRedArr)){
  151. $is_red_good = isRedGoods($goods_id);
  152. if(!empty($is_red_good)){
  153. //是红包商品,给下单人分红包
  154. $addred = addRed($resultone['user_id'], $order_id, $goods_id, $is_red_good['discount_price']);
  155. # 奖励推送
  156. if($addred){
  157. $params = array();
  158. $params['user_id'] = $resultone['user_id'];
  159. $params['type'] = 1; //1 收到新红包
  160. $params['money'] = $addred;
  161. $push_res = curl_post( ACT_PUSH_URL, $params);
  162. }
  163. $orderRedArr[] = $order_id;
  164. }
  165. }
  166. #订单表插入
  167. $res=InsertRedOrder($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);
  168. if($res){
  169. $orderList[] = $order_id;
  170. $res_count['generalinsert_num']++;
  171. }else{
  172. $res_count['fail_insert_num']++;
  173. $res_count['fail_insert_ids'].=$order_id.'|';
  174. }
  175. echo 'orderinsert:'.$res;
  176. echo "\n";
  177. $isTooNew = 0;
  178. $isNew = 0;
  179. if($sum_price>=0 and $orderStatus!=0){
  180. if(array_key_exists($order_id, $rewardOrder)){
  181. $isTooNew = 1; //一单多商品
  182. }else{
  183. $isNew = 1; //一单第一个商品
  184. }
  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. $rewardInfo = $rewardOrder[$order_id]; //本订单实际分佣情况
  198. #获取每一层balance
  199. $balanceArr = getBalance($userList, $rewardInfo);
  200. echo 'balanceArr'."\n";
  201. print_r($balanceArr);
  202. foreach($balanceArr as $k=>$v){
  203. if($v>0 ){
  204. # 插入奖励
  205. $rebaseType=7;//$TypeArr[$k];
  206. # 计算奖励佣金
  207. $reward = getReward($rewardInfo[$k], $v, EXTRATE);
  208. //$goods_name_new = $k==0 ? '恭喜您获得红包提现奖励' : '恭喜您获得红包提现奖励';
  209. $goods_img_new = ACT_GOODS_IMG;
  210. //插入red_order_reabte
  211. $res=rewardOrderInsert($userLevel[$k],$order_balance_at,$order_create_at,$goods_img_new,$goods_name,$order_id,$userList[$k],$rebaseType,$reward,$sum_price,$orderStatus,$goods_id,$predict_income);
  212. echo 'order_rebate_insert_'.$k.':'.$res."\n";
  213. //记入流水
  214. $redAccount = redAccountRecord($userList[$k], 2, round($reward/100, 2), $order_id, round(($balanceArr[$k]-$reward)/100,2));
  215. echo '记入流水表:'.$redAccount."\n";
  216. # 奖励推送
  217. if($res){
  218. $params = array();
  219. $params['user_id'] = $userList[$k];
  220. $params['type'] = $k==0 ? 2 : 3; //2自购 3下级购买
  221. $params['money'] = round($reward/100,2);
  222. $push_res = curl_post( ACT_PUSH_URL, $params);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. $_PDO->commit();
  229. }catch(PDOException $e){
  230. echo 'err_msg'.$e->getMessage()."\n";
  231. $_PDO->rollback();
  232. }
  233. //echo "-----------------insertEnd--------------------\n";
  234. }
  235. else{
  236. try{
  237. $_PDO->beginTransaction();
  238. //更新order/order_rebate表
  239. echo "action:更新操作\n";
  240. $res=generateSqlupdate($orderStatus,$order_balance_at,$order_id,$goods_id);
  241. $redUpRes = updateRed( $order_id, $orderStatus );
  242. /*** 失效订单单独更新 Start ***/
  243. if($orderStatus == 0){
  244. //首先判断该订单是否已经失效
  245. $order_old_status = $orderExists['status'];
  246. if($order_old_status != 0){
  247. if(!empty($redUpRes) && $redUpRes['red_open_money'] > 0){ //存在红包并且有拆除金额
  248. //1. 退红包的处理
  249. //更新流水表
  250. //获取用户最新balance
  251. echo "红包订单失效\n";
  252. echo "失效红包:".$redUpRes['id']."\n";
  253. $userAccount = getUserBalace($resultone['user_id']);
  254. $upAccount = redAccountRecord( $resultone['user_id'], 3, $redUpRes['red_open_money'], $redUpRes['id'], round(($userAccount['balance']*100 - $redUpRes['red_open_money']*100)/100, 2));
  255. echo "更新流水:".$upAccount."\n";
  256. }
  257. //判断是否该订单存在红包提现分单
  258. $redAccountInfo = insertLoseAccount($order_id);
  259. }
  260. }
  261. /*** 增加拉新更新 End ***/
  262. if($res){
  263. $res_count['generalupdate_num']++;
  264. }else{
  265. $res_count['fail_update_num']++;
  266. //$res_count['fail_update_ids'].=$order_id.'|';
  267. }
  268. echo "更新数据-> orderstatus:".$orderStatus." order_balance_at:".$order_balance_at;
  269. echo "\n";
  270. echo "order/reabte_update:".$res;
  271. echo "\n";
  272. //echo "-----------------updateEnd-----------------------\n";
  273. $_PDO->commit();
  274. }catch(PDOException $e){
  275. echo 'err_msg'.$e->getMessage()."\n";
  276. $_PDO->rollback();
  277. }
  278. }
  279. //echo "-----------------IssetUserEnd:--------------------\n";
  280. }
  281. echo "/ ** order{$res_count['total_num']}-End ** /";
  282. echo "\n\n\n";
  283. if(LIMIT_NUM >0 && $res_count['total_num'] == LIMIT_NUM) break;
  284. }
  285. print_r($res_count);
  286. echo "\n本次脚本开始时间:". $scriptStart;
  287. echo "\n本次脚本结束时间:". date("Y-m-d H:i:s");
  288. }
  289. function YysInfo( $user_id ){
  290. $sql = "SELECT user_id,parent_user_id,level FROM user_level WHERE user_id=:user_id ";
  291. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  292. $stmt = $_PDO->prepare($sql);
  293. $stmt->execute(array(':user_id'=>$user_id));
  294. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  295. if( empty($result) ) return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  296. if($result['level'] >= 3){
  297. return $result;
  298. }else{
  299. if( empty($result['parent_user_id']) ){
  300. return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  301. }
  302. return YysInfo( $result['parent_user_id']);
  303. }
  304. return ['user_id'=>438999,'level'=>4,'parent_user_id'=>'-1'];
  305. }
  306. function UserInfo($adzone_id){
  307. $sql = "SELECT * FROM user_adzone WHERE adzone_id = :adzone_id ";
  308. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  309. $stmt = $_PDO->prepare($sql);
  310. $stmt->execute(array(':adzone_id'=>$adzone_id));
  311. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  312. if($resultone){
  313. $sql="SELECT * FROM user_level WHERE user_id = :user_id";
  314. $stmt = $_PDO->prepare($sql);
  315. $stmt->execute(array(':user_id'=>$resultone['user_id']));
  316. $userResult = $stmt->fetch(PDO::FETCH_ASSOC);
  317. return $userResult;
  318. }
  319. else{
  320. return false;
  321. }
  322. }
  323. function userLevelInfo($user_id){
  324. $sql="SELECT * FROM user_level WHERE user_id = :user_id ";
  325. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  326. $stmt = $_PDO->prepare($sql);
  327. $stmt->execute(array(':user_id'=>$user_id));
  328. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  329. if($resultone){
  330. $myInfo=["user_id"=>$user_id,"level"=>$resultone['level'],"parent_user_id"=>$resultone['parent_user_id']];
  331. return $myInfo;
  332. }
  333. else{
  334. return false;
  335. }
  336. }
  337. # 获取4层用户级别基础信息
  338. function userLevelandPrentInfo($resultone){
  339. $resultInfo = array();
  340. # self
  341. $userId=$resultone['user_id'];
  342. $userLevel=$resultone['level'];
  343. if($userLevel>=3){
  344. $resultInfo['userList'] = [$userId,0,0,0];
  345. $resultInfo['levelRule'] = 3000;
  346. $resultInfo['userLevel'] = [$userLevel,0,0,0];
  347. return $resultInfo;
  348. }
  349. $userParentId=$resultone['parent_user_id'];
  350. $userGrandId=$resultone['grand_father_id'];
  351. if($userParentId>0){
  352. $resulttwo=userLevelInfo($userParentId);
  353. if( !empty($resulttwo) ){
  354. # parent
  355. $userParentLevel=$resulttwo['level'];
  356. if($userParentLevel>=3){
  357. $resultInfo['userList'] = [$userId,$userParentId,0,0];
  358. $resultInfo['levelRule'] = intval($userLevel.'300');
  359. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,0,0];
  360. return $resultInfo;
  361. }
  362. if($userGrandId>0){
  363. $resultthree=userLevelInfo($userGrandId);
  364. if( !empty($resultthree) ){
  365. # grand
  366. $userGrandLevel=$resultthree['level'];
  367. if($userGrandLevel>=3){
  368. $resultInfo['userList'] = [$userId,$userParentId,$userGrandId,0];
  369. $resultInfo['levelRule'] = intval($userLevel.$userParentLevel.'30');
  370. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,$userGrandLevel,0];
  371. return $resultInfo;
  372. }
  373. }
  374. }
  375. }
  376. }
  377. if( !$userParentId ) $userParentId=0;
  378. if( !$userGrandId ) $userGrandId=0;
  379. if( !isset($userParentLevel) ) $userParentLevel=1;
  380. if( !isset($userGrandLevel) ) $userGrandLevel=1;
  381. #三级里面没有运营商,递归上级运营商
  382. $YysInfo = YysInfo($userGrandId);
  383. $userFourId = $YysInfo['user_id'];
  384. $userFourLevel = $YysInfo['level'];
  385. # 层级规则
  386. $levelRule = $userLevel.$userParentLevel.$userGrandLevel.'3';
  387. $resultInfo['userList']=[$userId,$userParentId,$userGrandId,$userFourId];
  388. $resultInfo['levelRule'] = (int)$levelRule;
  389. $resultInfo['userLevel'] = [$userLevel,$userParentLevel,$userGrandLevel,$userFourLevel];
  390. return $resultInfo;
  391. }
  392. function orderMoney($predict_income, $RuleArr){
  393. $result = array();
  394. foreach($RuleArr as $k=>$v){
  395. if($v>0){
  396. $result[$k] = round($predict_income * $v / 100);
  397. }else{
  398. $result[$k] = 0;
  399. }
  400. }
  401. return $result;
  402. }
  403. function orderMysql($order_id,$goods_id){
  404. $sql="SELECT * from `red_order` where order_id=:order_id and goods_id=:goods_id";
  405. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  406. $stmt = $_PDO->prepare($sql);
  407. $stmt->execute(array(':order_id'=>$order_id,':goods_id'=>$goods_id));
  408. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  409. return $result;
  410. }
  411. #order表订单插入脚本
  412. function InsertRedOrder($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){
  413. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  414. if( empty($order_balance_at) ){
  415. $sql="insert into `red_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)";
  416. $stmt = $_PDO->prepare($sql);
  417. $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));
  418. return $_PDO->lastinsertid();
  419. }
  420. else{
  421. $sql="insert into `red_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)";
  422. $stmt = $_PDO->prepare($sql);
  423. $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));
  424. return $_PDO->lastinsertid();
  425. }
  426. }
  427. #order订单表返利表同步更新
  428. function generateSqlupdate($status,$order_balance_at,$order_id,$goods_id){
  429. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  430. if( !empty($order_balance_at) ){
  431. $sql="update `red_order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  432. $stmt = $_PDO->prepare($sql);
  433. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  434. $res = $stmt->rowCount();
  435. //更新order_rebate
  436. $sql="update order_rebate set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  437. $stmt = $_PDO->prepare($sql);
  438. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  439. $res = $stmt->rowCount();
  440. }
  441. else{
  442. $sql="update `red_order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  443. $stmt = $_PDO->prepare($sql);
  444. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  445. $res = $stmt->rowCount();
  446. $sql="update order_rebate set status=:status where order_id=:order_id and goods_id=:goods_id ";
  447. $stmt = $_PDO->prepare($sql);
  448. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  449. $res = $stmt->rowCount();
  450. }
  451. return $res;
  452. }
  453. # 获取rebateRule
  454. function getRebateRule($levelRule){
  455. $sql="SELECT * from `rebate_rule` where level_rule = :level_rule";
  456. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  457. $stmt = $_PDO->prepare($sql);
  458. $stmt->execute(array(':level_rule'=>$levelRule));
  459. $ruleResult = $stmt->fetch(PDO::FETCH_ASSOC);
  460. return $ruleResult;
  461. }
  462. # 计算提现红包
  463. function getReward($money, $balance, $rate){
  464. $money = $money * $rate;
  465. if($money > $balance){
  466. return $balance;
  467. }
  468. return round($money);
  469. }
  470. # 同订单多分佣累加
  471. function arraySums($arr1, $arr2){
  472. $res = array();
  473. foreach($arr1 as $k=>$v){
  474. $res[$k] = $arr1[$k] + $arr2[$k];
  475. }
  476. return $res;
  477. }
  478. #提现红包订单插入
  479. 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){
  480. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  481. if( !empty($order_balance_at) ){
  482. $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)";
  483. $stmt = $_PDO->prepare($sql);
  484. $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));
  485. return $_PDO->lastinsertid();
  486. }
  487. else{
  488. $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)";
  489. $stmt = $_PDO->prepare($sql);
  490. $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));
  491. return $_PDO->lastinsertid();
  492. }
  493. }
  494. function getBalance($userList, $moneyInfo){
  495. $result = array();
  496. foreach($userList as $k=>$v){
  497. if($moneyInfo[$k]>0){
  498. $res = getUserBalace($v);
  499. if(isset($res['balance'])){
  500. $result[$k] = $res['balance']*100;
  501. }else{
  502. $result[$k] = 0;
  503. }
  504. }else{
  505. $result[$k] = 0;
  506. }
  507. }
  508. return $result;
  509. }
  510. function getUserBalace($user_id){
  511. $table = "red_account_record_".($user_id%10);
  512. $sql = "SELECT balance from {$table} where user_id={$user_id} order by id desc limit 1";
  513. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  514. $stmt = $_PDO->prepare($sql);
  515. $stmt->execute();
  516. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  517. return $res;
  518. }
  519. function redAccountRecord($user_id, $type, $money, $oh_id, $balance){
  520. $table = "red_account_record_".($user_id%10);
  521. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  522. $sql = "insert into `{$table}` (user_id, type, money, oh_id, balance) values ({$user_id},{$type},{$money},'{$oh_id}', {$balance})";
  523. $stmt = $_PDO->prepare($sql);
  524. $stmt->execute();
  525. return $_PDO->lastinsertid();
  526. }
  527. function updateRed($order_id, $status ){
  528. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  529. $sql="update `red_user_info` set order_status=:status where order_id=:order_id";
  530. $stmt = $_PDO->prepare($sql);
  531. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id));
  532. $res = $stmt->rowCount();
  533. if($res){
  534. $sql = "select * from `red_user_info` where order_id = '{$order_id}'";
  535. $stmt = $_PDO->prepare($sql);
  536. $stmt->execute();
  537. $redinfo = $stmt->fetch(PDO::FETCH_ASSOC);
  538. return $redinfo;
  539. }
  540. return $res;
  541. }
  542. function insertLoseAccount($order_id){
  543. // 查出哪些人分到了红包提现
  544. $sql = "SELECT user_id from `order_rebate` where order_id='{$order_id}' and type=7 ";
  545. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  546. $stmt = $_PDO->prepare($sql);
  547. $stmt->execute();
  548. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  549. if(empty($res)){
  550. return false;
  551. }
  552. echo "退红包提现,返还流水balance:\n";
  553. foreach($res as $k=>$v){
  554. $table = "red_account_record_".($v['user_id']%10);
  555. $sql = "SELECT * from {$table} where user_id={$v['user_id']} and oh_id='{$order_id}' and type=2";
  556. $stmt = $_PDO->prepare($sql);
  557. $stmt->execute();
  558. $record = $stmt->fetch(PDO::FETCH_ASSOC);
  559. $account = getUserBalace($v['user_id']);
  560. $insertLose = redAccountRecord($v['user_id'], 4, $record['money'], $order_id, round(($account['balance']*100 + $record['money']*100)/100, 2));
  561. echo "用户".$v['user_id'].":返还".$record['money']." | 处理结果:".$insertLose."\n";
  562. }
  563. return $insertLose;
  564. }
  565. function addRed($user_id, $order_id, $goods_id, $price){
  566. $red_config = getARedConfig($price);
  567. $red_money = $red_config['red_money'];
  568. $open_red_limit = $red_config['open_red_limit'];
  569. $red_money_open_limit = $red_config['red_money_open_limit'];
  570. $red_money_open_limit_new = $red_config['red_money_open_limit_new'];
  571. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  572. $sql = "insert into red_user_info (user_id, order_id, goods_id, red_money, type, red_open_money, open_red_limit, red_money_open_limit, red_money_open_limit_new) values({$user_id}, '{$order_id}', '{$goods_id}', {$red_money}, 1, 0, {$open_red_limit}, '{$red_money_open_limit}', '{$red_money_open_limit_new}') ";
  573. $stmt = $_PDO->prepare($sql);
  574. $stmt->execute();
  575. if($_PDO->lastinsertid()){
  576. return $red_money;
  577. }else{
  578. return 0;
  579. }
  580. }
  581. function isRedGoods($goods_id){
  582. $sql="SELECT id,discount_price from `red_goods` where goods_id='{$goods_id}' limit 1";
  583. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  584. $stmt = $_PDO->prepare($sql);
  585. $stmt->execute();
  586. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  587. return $res;
  588. }
  589. function getConfig(){
  590. $result = array(
  591. 'red_active_status' => 0
  592. );
  593. # 活动开启
  594. $sql="SELECT value from `global_variable` where variable_key='red_active_status' and is_deleted=0 order by id desc limit 1";
  595. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  596. $stmt = $_PDO->prepare($sql);
  597. $stmt->execute();
  598. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  599. if(empty($res) || $res['value'] == 0){
  600. return $result;
  601. }
  602. $result['red_active_status'] = 1;
  603. $sql="SELECT value from `global_variable` where variable_key='red_rebate_rate' and is_deleted=0 ";
  604. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  605. $stmt = $_PDO->prepare($sql);
  606. $stmt->execute();
  607. $rate = $stmt->fetch(PDO::FETCH_ASSOC);
  608. $result['red_rebate_rate'] = $rate['value'];
  609. return $result;
  610. }
  611. function getARedConfig($price){
  612. # 获取相关配置
  613. $sql="SELECT value from `global_variable` where variable_key='red_conf_json' and is_deleted=0 ";
  614. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  615. $stmt = $_PDO->prepare($sql);
  616. $stmt->execute();
  617. $configs = $stmt->fetch(PDO::FETCH_ASSOC);
  618. $configs = json_decode($configs['value'], true);
  619. $result = array();
  620. foreach($configs as $k=>$v){
  621. if($price>$v['min'] && $price<$v['max']){
  622. $result['red_money'] = $v['redmoney'];
  623. $result['open_red_limit'] = $v['op'];
  624. $result['red_money_open_limit'] = $v['open_limit'];
  625. $result['red_money_open_limit_new'] = $v['open_limit'];
  626. break;
  627. }
  628. }
  629. if(empty($result)){
  630. # 获取其他相关配置
  631. $sql="SELECT variable_key,value from `global_variable` where variable_key in('red_money','open_red_limit','red_money_open_limit','red_money_open_limit_new') and is_deleted=0 ";
  632. $stmt = $_PDO->prepare($sql);
  633. $stmt->execute();
  634. $configs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  635. foreach($configs as $k=>$v){
  636. $result[$v['variable_key']] = $v['value'];
  637. }
  638. }
  639. return $result;
  640. }
  641. # curl调用接口push
  642. function curl_post($url, $params){
  643. $ch = curl_init();
  644. curl_setopt($ch, CURLOPT_URL, $url);
  645. curl_setopt($ch, CURLOPT_HEADER, 0);
  646. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  647. curl_setopt($ch, CURLOPT_POST, 1);
  648. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  649. $response = curl_exec($ch);
  650. curl_close($ch);
  651. return $response;
  652. }
  653. readDataFromDb();