优惠券订单及其他脚本

scan_order.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. require_once 'DB_PDO.class.php';
  3. require_once 'TBK.class.php';
  4. require_once 'conf.class.php';
  5. define("DETAILDEBUG", 0);//打印流程详细信息,如有需要,设为1
  6. define("LIMIT_NUM", 0);//限制导入条数,0不限制,方便测试,上线设为0
  7. //define("TBK_PID_STR", 'mm_250470096_196250254_');
  8. define("TBK_PID_STR", 'mm_299550010_328900301_');
  9. //define("PUSH_URL",'https://tbk.726p.com/api/v2/MessagePush/rebatepush');//push接口地址
  10. /**
  11. * 导入及更新 order/order_rebate数据 + 淘宝拉新活动
  12. */
  13. #设置时区
  14. date_default_timezone_set('PRC');
  15. set_time_limit(0);
  16. ini_set('memory_limit','1024M');
  17. function readDataFromDb(){
  18. # 记录脚本开始时间
  19. $scriptStart = date("Y-m-d H:i:s");
  20. # 1.获取6分钟内订单
  21. //$stime = date('Y-m-d H:i:s',strtotime('2018-08-07'));
  22. $stime = date('Y-m-d H:i:s', time()-150);
  23. $sql = "SELECT * from zbds_taoke_orders where update_time > :stime ";
  24. $dumpsql = str_replace(':stime', "'".$stime."'", $sql);
  25. echo "\n\n".$dumpsql."\n";
  26. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  27. $stmt = $_PDO->prepare($sql);
  28. $stmt->execute(array(':stime'=>$stime));
  29. $orderResult = $stmt->fetchAll(PDO::FETCH_ASSOC);
  30. # 遍历订单
  31. # 统计
  32. $res_count = array(
  33. 'total_num' => 0,
  34. 'generalinsert_num' => 0,
  35. 'generalupdate_num' => 0,
  36. 'nouserinsert_num' => 0,
  37. 'nouserupdate_num' => 0,
  38. 'fail_insert_num' => 0,
  39. 'fail_insert_ids' => '',
  40. 'fail_insertno_num' => 0,
  41. 'fail_insertno_ids' => '',
  42. 'fail_update_num' => 0,
  43. //'fail_update_ids' => '',
  44. 'fail_updateno_num' => 0,
  45. //'fail_updateno_ids' => '',
  46. );
  47. foreach( $orderResult as $key=>$orderDetail ){
  48. $res_count['total_num']++;
  49. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  50. if( DETAILDEBUG == 1){
  51. echo 'orderDetail:';
  52. }
  53. //create_time,click_time,item_title,num_iid,seller_nick,seller_shop_title,item_num,price,tk_status,order_type,income_rate,commission_rate,pay_price,pub_share_pre_fee,alipay_total_price,commission,earning_time,total_commission_rate,total_commission_fee,subsidy_rate,subsidy_fee,subsidy_type,terminal_type,tk3rd_type,trade_parent_id,trade_id,auction_category,site_id,site_name,adzone_id,adzone_name,relation_id,special_id
  54. $order_id=$orderDetail["trade_id"];
  55. $update_time=$orderDetail["update_time"];
  56. $order_create_at=$orderDetail["create_time"];
  57. $order_click_at=$orderDetail["click_time"];
  58. $order_balance_at=$orderDetail["earning_time"];
  59. $goods_id=$orderDetail["num_iid"];
  60. $goods_name=$orderDetail["item_title"];
  61. $goods_name=str_replace(['"',"'"],'',$goods_name);
  62. $num=$orderDetail["item_num"];
  63. $price=intval($orderDetail["price"]*100);
  64. $sum_price=intval($orderDetail["alipay_total_price"]*100);
  65. $statusDesc=$orderDetail["tk_status"];
  66. $typeDesc=$orderDetail["order_type"];
  67. $orderStatus = 1;
  68. $type=1;
  69. if($statusDesc=="12"){
  70. $orderStatus=1;
  71. }
  72. elseif($statusDesc=="13"){
  73. $orderStatus=0;
  74. }
  75. elseif($statusDesc=="3"){
  76. $orderStatus=2;
  77. }
  78. if($typeDesc=="聚划算"){
  79. $type=0;
  80. }
  81. elseif($typeDesc=="淘宝"){
  82. $type=1;
  83. }
  84. elseif($typeDesc=="天猫"){
  85. $type=2;
  86. }
  87. $adzone_id=$orderDetail["adzone_id"];
  88. $relation_id = $orderDetail["relation_id"];
  89. $special_id = $orderDetail["special_id"];
  90. $ceil_income=round($orderDetail["pub_share_pre_fee"], 2);
  91. $predict_income=$ceil_income*100;
  92. $predict_income=ceil($predict_income*conf::$RATE_CONF["globalzk"]);
  93. echo 'order_id:'.$order_id.'|| goods_id:'.$goods_id.'|| orderstatus:'.$orderStatus.'|| order_balance_at:'.$order_balance_at;
  94. echo '|| predict_income:'.$predict_income.'|| adzone_id:'.$adzone_id.'|| special_id:'.$special_id.'|| relation_id:'.$relation_id."\n";
  95. $resultone=UserInfo($adzone_id);
  96. if( DETAILDEBUG == 1){
  97. echo 'userinfo:';
  98. var_dump($resultone);
  99. echo "\n";
  100. }
  101. if($sum_price!=0){
  102. if($predict_income<5){
  103. $predict_income=5;
  104. }
  105. }
  106. if( !empty($resultone) ){
  107. echo "User:存在\n";
  108. $user_id = $resultone['id'];
  109. $rate = $resultone['rate'];
  110. $orderExists=orderMysql($order_id,$goods_id);
  111. if( empty($orderExists) ){
  112. echo "action:插入操作\n";
  113. #获取返利
  114. $moneyInfo=orderMoney($predict_income, $rate); //分佣钱数
  115. $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
  116. if( !empty($detail) ){
  117. $goods_img=$detail->pict_url;
  118. }
  119. else{
  120. $goods_img="";
  121. }
  122. try{
  123. $_PDO->beginTransaction();
  124. #订单表插入
  125. $res=generateSqlInsertOrder($goods_img,$goods_name,$user_id,$order_id,$adzone_id,$goods_id,$num,$price,$sum_price,$predict_income,$moneyInfo,$orderStatus,$type,3,$order_create_at,$order_click_at,$order_balance_at);
  126. if($res){
  127. $res_count['generalinsert_num']++;
  128. }else{
  129. $res_count['fail_insert_num']++;
  130. $res_count['fail_insert_ids'].=$order_id.'|';
  131. }
  132. echo 'orderinsert:'.$res;
  133. echo "\n";
  134. #检查sale_goods表是否有该条数据,无则插入
  135. $sale_goods_insert = sale_goods_insert($goods_id);
  136. echo 'sale_goods_insert:'.$sale_goods_insert;
  137. echo "\n";
  138. if($sum_price>0){
  139. #order_rebate表插入父级佣金
  140. $rebaseType=1;
  141. $res=generateSqlInsertRebateSelf(3,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$user_id,$rebaseType,$moneyInfo,$sum_price,$orderStatus,$goods_id,$predict_income,$num);
  142. # 推送
  143. /*$flag_time = time() - strtotime($order_create_at);
  144. if($res && $flag_time<3600){
  145. $params = array();
  146. $params['user_id'] = $user_id;
  147. $params['type'] = 2;
  148. $params['money'] = round($moneyInfo/100,2);
  149. $push_res = curl_post( PUSH_URL, $params);
  150. }*/
  151. echo 'rebateinsert:'.$res;
  152. echo "\n";
  153. }
  154. $_PDO->commit();
  155. }catch(PDOException $e){
  156. echo 'err_msg'.$e->getMessage()."\n";
  157. $_PDO->rollback();
  158. }
  159. //echo "-----------------insertEnd--------------------\n";
  160. }
  161. else{
  162. try{
  163. $_PDO->beginTransaction();
  164. //更新order/order_rebate表
  165. echo "action:更新操作\n";
  166. $res=generateSqlupdate($orderStatus,$order_balance_at,$order_id,$goods_id);
  167. if($res){
  168. $res_count['generalupdate_num']++;
  169. }else{
  170. $res_count['fail_update_num']++;
  171. //$res_count['fail_update_ids'].=$order_id.'|';
  172. }
  173. echo "更新数据-> orderstatus:".$orderStatus." order_balance_at:".$order_balance_at;
  174. echo "\n";
  175. echo "order/reabte_update:".$res;
  176. echo "\n";
  177. //echo "-----------------updateEnd-----------------------\n";
  178. $_PDO->commit();
  179. }catch(PDOException $e){
  180. echo 'err_msg'.$e->getMessage()."\n";
  181. $_PDO->rollback();
  182. }
  183. }
  184. //echo "-----------------IssetUserEnd:--------------------\n";
  185. }
  186. else{
  187. $_PDO->beginTransaction();
  188. try{
  189. #user不存在的处理
  190. echo "User:不存在\n";
  191. $ordernoExists=orderMysql($order_id,$goods_id);
  192. if( empty($ordernoExists) ){
  193. $detail = TBK::tbkItemInfoGet( ['goods_id'=>$goods_id] );
  194. if( !empty($detail) ){
  195. $goods_img=$detail->pict_url;
  196. }
  197. else{
  198. $goods_img="";
  199. }
  200. $res=generateSqlInsertOrderOther($goods_img,$goods_name,$order_id,$adzone_id,$goods_id,$num,$price,$sum_price,$predict_income,0,$orderStatus,$type,$order_create_at,$order_click_at,$order_balance_at);
  201. if($res){
  202. $res_count['nouserinsert_num']++;
  203. }else{
  204. $res_count['fail_insertno_num']++;
  205. $res_count['fail_insertno_ids'].=$order_id.'|';
  206. }
  207. echo "action:插入操作\n";
  208. echo "orderinsert:".$res;
  209. echo "\n";
  210. $_PDO->commit();
  211. }
  212. else{
  213. $res=generateSqlUpdateOther($orderStatus,$order_balance_at,$order_id,$goods_id);
  214. if($res){
  215. $res_count['nouserupdate_num']++;
  216. }else{
  217. $res_count['fail_updateno_num']++;
  218. //$res_count['fail_updateno_ids'].=$order_id.'|';
  219. }
  220. echo "action:更新操作\n";
  221. echo "orderupdate:".$res;
  222. echo "\n";
  223. $_PDO->commit();
  224. }
  225. //echo "-----------------EmptyUserEnd:--------------------\n";
  226. }catch(PDOException $e){
  227. echo 'err_msg'.$e->getMessage()."\n";
  228. $_PDO->rollback();
  229. }
  230. }
  231. echo "/ ** order{$res_count['total_num']}-End ** /";
  232. echo "\n\n\n";
  233. if(LIMIT_NUM >0 && $res_count['total_num'] == LIMIT_NUM) break;
  234. }
  235. print_r($res_count);
  236. echo "\n本次脚本开始时间:". $scriptStart;
  237. echo "\n本次脚本结束时间:". date("Y-m-d H:i:s");
  238. }
  239. function UserInfo($adzone_id){
  240. $sql = "SELECT id,rate FROM users WHERE adzone_id = :adzone_id ";
  241. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  242. $stmt = $_PDO->prepare($sql);
  243. $stmt->execute(array(':adzone_id'=>$adzone_id));
  244. $resultone = $stmt->fetch(PDO::FETCH_ASSOC);
  245. if(!empty($resultone)){
  246. return $resultone;
  247. }
  248. else{
  249. return false;
  250. }
  251. }
  252. function orderMoney($predict_income, $rate){
  253. $result = round($predict_income * $rate / 100);
  254. return $result;
  255. }
  256. function orderMysql($order_id,$goods_id){
  257. $sql="SELECT * from `order` where order_id=:order_id and goods_id=:goods_id";
  258. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  259. $stmt = $_PDO->prepare($sql);
  260. $stmt->execute(array(':order_id'=>$order_id,':goods_id'=>$goods_id));
  261. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  262. return $result;
  263. }
  264. function generateSqlInsertOrderOther($goods_img,$goods_name,$order_id,$adzone_id,$goods_id,$num,$price,$sum_price,$predict_income,$self_income,$orderStatus,$type,$order_create_at,$order_click_at,$order_balance_at){
  265. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  266. if( empty($order_balance_at) ){
  267. $sql="insert into `order` (company_type,goods_img_url,goods_name,order_id,adzone_id,goods_id,num,price,sum_price,predict_income,income,status,type,order_create_at,order_click_at) VALUES (:company_type,:goods_img,:goods_name,:order_id,:adzone_id,:goods_id,:num,:price,:sum_price,:predict_income,:self_income,:orderStatus,:type,:order_create_at,:order_click_at)";
  268. $stmt = $_PDO->prepare($sql);
  269. $stmt->execute(array(':company_type'=>2,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':adzone_id'=>$adzone_id,':goods_id'=>$goods_id,':num'=>$num,':price'=>$price,':sum_price'=>$sum_price,':predict_income'=>$predict_income,':self_income'=>$self_income,':orderStatus'=>$orderStatus,':type'=>$type,':order_create_at'=>$order_create_at,':order_click_at'=>$order_click_at));
  270. return $_PDO->lastinsertid();
  271. }
  272. else{
  273. $sql="insert into `order` (company_type,goods_img_url,goods_name,order_id,adzone_id,goods_id,num,price,sum_price,predict_income,income,status,type,order_create_at,order_click_at,order_balance_at) VALUES (:company_type,:goods_img,:goods_name,:order_id,:adzone_id,:goods_id,:num,:price,:sum_price,:predict_income,:self_income,:orderStatus,:type,:order_create_at,:order_click_at,:order_balance_at)";
  274. $stmt = $_PDO->prepare($sql);
  275. $stmt->execute(array(':company_type'=>2,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':adzone_id'=>$adzone_id,':goods_id'=>$goods_id,':num'=>$num,':price'=>$price,':sum_price'=>$sum_price,':predict_income'=>$predict_income,':self_income'=>$self_income,':orderStatus'=>$orderStatus,':type'=>$type,':order_create_at'=>$order_create_at,':order_click_at'=>$order_click_at,':order_balance_at'=>$order_balance_at));
  276. return $_PDO->lastinsertid();
  277. }
  278. }
  279. #order表订单插入脚本
  280. function generateSqlInsertOrder($goods_img,$goods_name,$userId,$order_id,$adzone_id,$goods_id,$num,$price,$sum_price,$predict_income,$self_income,$orderStatus,$type,$userLevel,$order_create_at,$order_click_at,$order_balance_at){
  281. //$adzoneDetail=orderType($adzone_id);
  282. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  283. if( empty($order_balance_at) ){
  284. $sql="insert into `order` (company_type,goods_img_url,goods_name,user_id,order_id,adzone_id,goods_id,num,price,sum_price,predict_income,income,status,type,user_level,order_create_at,order_click_at) VALUES (:company_type,:goods_img,:goods_name,:userId,:order_id,:adzone_id,:goods_id,:num,:price,:sum_price,:predict_income,:self_income,:orderStatus,:type,:userLevel,:order_create_at,:order_click_at)";
  285. $stmt = $_PDO->prepare($sql);
  286. $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));
  287. return $_PDO->lastinsertid();
  288. }
  289. else{
  290. $sql="insert into `order` (company_type,goods_img_url,goods_name,user_id,order_id,adzone_id,goods_id,num,price,sum_price,predict_income,income,status,type,user_level,order_create_at,order_click_at,order_balance_at) VALUES (:company_type,:goods_img,:goods_name,:userId,:order_id,:adzone_id,:goods_id,:num,:price,:sum_price,:predict_income,:self_income,:orderStatus,:type,:userLevel,:order_create_at,:order_click_at,:order_balance_at)";
  291. $stmt = $_PDO->prepare($sql);
  292. $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));
  293. return $_PDO->lastinsertid();
  294. }
  295. }
  296. #返利表订单插入脚本
  297. function generateSqlInsertRebateSelf($user_level,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$userId,$rebaseType,$last_orderSelfRebaseRebase,$sum_price,$orderStatus,$goods_id,$pre_income,$num){
  298. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  299. if( !empty($order_balance_at) ){
  300. $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,num) 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,:num)";
  301. $stmt = $_PDO->prepare($sql);
  302. $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,':num'=>$num));
  303. return $_PDO->lastinsertid();
  304. }
  305. else{
  306. $sql="insert into order_rebate (user_level,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income,num) VALUES (:user_level,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income,:num)";
  307. $stmt = $_PDO->prepare($sql);
  308. $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,':num'=>$num));
  309. return $_PDO->lastinsertid();
  310. }
  311. }
  312. #order订单表返利表同步更新
  313. function generateSqlupdate($status,$order_balance_at,$order_id,$goods_id){
  314. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  315. if( !empty($order_balance_at) ){
  316. $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  317. $stmt = $_PDO->prepare($sql);
  318. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  319. $res = $stmt->rowCount();
  320. //更新order_rebate
  321. $sql="update order_rebate set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  322. $stmt = $_PDO->prepare($sql);
  323. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  324. $res = $stmt->rowCount();
  325. }
  326. else{
  327. $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  328. $stmt = $_PDO->prepare($sql);
  329. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  330. $res = $stmt->rowCount();
  331. $sql="update order_rebate set status=:status where order_id=:order_id and goods_id=:goods_id ";
  332. $stmt = $_PDO->prepare($sql);
  333. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  334. $res = $stmt->rowCount();
  335. }
  336. return $res;
  337. }
  338. #订单表单独更新
  339. function generateSqlUpdateOther($status,$order_balance_at,$order_id,$goods_id){
  340. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  341. if( !empty($order_balance_at) ){
  342. $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  343. $stmt = $_PDO->prepare($sql);
  344. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  345. $res = $stmt->rowCount();
  346. }
  347. else{
  348. $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  349. $stmt = $_PDO->prepare($sql);
  350. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  351. $res = $stmt->rowCount();
  352. }
  353. return $res;
  354. }
  355. function sale_goods_insert($goods_id){
  356. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  357. $sql = "select id from sale_goods where num_iid = :goods_id limit 1";
  358. $stmt = $_PDO->prepare($sql);
  359. $stmt->execute(array(':goods_id'=>$goods_id));
  360. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  361. $ins = 0;
  362. if(empty($res)){
  363. $keyword = "https://item.taobao.com/item.htm?id=".$goods_id;
  364. $goodsInfo = TBK::search($keyword);
  365. if(!isset($goodsInfo->num_iid)) return false;
  366. if(isset($goodsInfo->coupon_id) && !empty($goodsInfo->coupon_id)){
  367. preg_match_all('/[\d]+/',$goodsInfo->coupon_info,$d);
  368. $coupon_price = isset($d[0][2]) ? $d[0][2] : $d[0][1];
  369. } else {
  370. $coupon_price = "0";
  371. }
  372. $discount_price = changeGoodsBranchToElement(isset($goodsInfo->coupon_id) && !empty($goodsInfo->coupon_id) ? $goodsInfo->zk_final_price - $coupon_price : $goodsInfo->zk_final_price);
  373. $commission_price = changeGoodsBranchToElement($discount_price * $goodsInfo->commission_rate/10000);
  374. $data = array();
  375. $data['num_iid'] = $goodsInfo->num_iid;
  376. $data['coupon_id'] = isset($goodsInfo->coupon_id) ? $goodsInfo->coupon_id : '';
  377. $data['pict_url'] = $goodsInfo->pict_url;
  378. $data['white_image'] = isset($goodsInfo->white_image) ? $goodsInfo->white_image : '';
  379. $data['small_images'] = isset($goodsInfo->small_images->string) ? serialize(json_decode(json_encode($goodsInfo->small_images->string),true)) : '';
  380. $data['seller_id'] = $goodsInfo->seller_id;
  381. $data['shop_title'] = $goodsInfo->shop_title;
  382. $data['short_title'] = isset($goodsInfo->short_title) ? $goodsInfo->short_title : $goodsInfo->title;
  383. $data['coupon_total_count'] = $goodsInfo->coupon_total_count;
  384. $data['user_type'] = $goodsInfo->user_type;
  385. $data['coupon_info'] = $goodsInfo->coupon_info;
  386. $data['coupon_price'] = $coupon_price;
  387. $data['coupon_start_time'] = $goodsInfo->coupon_start_time;
  388. $data['coupon_end_time'] = $goodsInfo->coupon_end_time;
  389. $data['zk_final_price'] = $goodsInfo->zk_final_price;
  390. $data['reserve_price'] = $goodsInfo->reserve_price;
  391. $data['commission_rate'] = $goodsInfo->commission_rate;
  392. $data['commission_price'] = $commission_price;
  393. $data['coupon_remain_count'] = $goodsInfo->coupon_remain_count;
  394. $data['tk_total_sales'] = $goodsInfo->tk_total_sales;
  395. $data['live_price'] = $discount_price;
  396. $data['volume'] = $goodsInfo->volume;
  397. $data['url'] = isset($goodsInfo->coupon_share_url) && !empty($goodsInfo->coupon_share_url) ? $goodsInfo->coupon_share_url : $goodsInfo->url;
  398. $data['status'] = 1;
  399. $sql = getinsertSql($data, 'sale_goods');
  400. $stmt = $_PDO->prepare($sql);
  401. $stmt->execute();
  402. $ins = $_PDO->lastinsertid();
  403. }
  404. return $ins;
  405. }
  406. function changeGoodsBranchToElement($num){
  407. return Number_format($num,2, '.','');
  408. }
  409. function getinsertSql($data,$table){
  410. $columns = '';
  411. $values = '';
  412. foreach($data as $k=>$v){
  413. $columns .= $k.',';
  414. $values .= "'".$v."',";
  415. }
  416. $sql = " insert into {$table}(".trim($columns,',').") values(".trim($values,',').")";
  417. return $sql;
  418. }
  419. # curl调用接口push
  420. function curl_post($url, $params){
  421. $ch = curl_init();
  422. curl_setopt($ch, CURLOPT_URL, $url);
  423. curl_setopt($ch, CURLOPT_HEADER, 0);
  424. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  425. curl_setopt($ch, CURLOPT_POST, 1);
  426. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  427. $response = curl_exec($ch);
  428. curl_close($ch);
  429. return $response;
  430. }
  431. readDataFromDb();