优惠券订单及其他脚本

red_order_liedou.php 30KB

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