优惠券订单及其他脚本

red_order.php 31KB

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