优惠券订单及其他脚本

sync_liedou1h_order.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. define( "ROOT_PATH", dirname(dirname(__FILE__)) );
  3. require_once ROOT_PATH.'/DB_PDO.class.php';
  4. require_once ROOT_PATH.'/TBK_v2.class.php';
  5. require_once ROOT_PATH.'/confv2.class.php';
  6. require_once ROOT_PATH.'/RedisClass.php';
  7. define("DETAILDEBUG", 0);//打印流程详细信息,如有需要,设为1
  8. define("LIMIT_NUM", 0);//限制导入条数,0不限制,方便测试,上线设为0
  9. date_default_timezone_set('PRC');
  10. # 同步3小时内的订单
  11. sync_orders();
  12. /**
  13. * 同步新订单
  14. */
  15. function sync_orders(){
  16. //同步订单脚本
  17. # 1.同步N分钟内所有订单
  18. //$now = strtotime('2019-01-21 16:54:00');
  19. $time = date("Y-m-d H:i:s", strtotime('-3 hour'));
  20. echo "\n\nCreate_time:".$time;
  21. $etime = '';
  22. $orderResult = array();
  23. $time_type = 2;//按付款时间查
  24. $tk_status = 0;//查询所有状态订单
  25. for($x=1;$x<10;$x++){
  26. $position_index = '';//查下一页必要参数
  27. $stime = !empty($etime) ? $etime : $time;
  28. $dstr = $time.' '.($x*20).' minutes';
  29. $etime = date("Y-m-d H:i:s",strtotime($dstr));
  30. //渠道
  31. for($i=1;$i<100;$i++){
  32. $res = TBK::syncOrderNew($i,$stime, $etime, $time_type ,$tk_status,2,$position_index);
  33. if(empty($res)){
  34. break;
  35. }else{
  36. $orderData = $res['data'];
  37. $position_index = $res['position_index'];
  38. }
  39. $orderResult = array_merge($orderResult, $orderData);
  40. }
  41. $position_index = '';
  42. //会员
  43. for($i=1;$i<100;$i++){
  44. $res = TBK::syncOrderNew($i,$stime, $etime, $time_type ,$tk_status,3,$position_index);
  45. if(empty($res)){
  46. break;
  47. }else{
  48. $orderData = $res['data'];
  49. $position_index = $res['position_index'];
  50. }
  51. $orderResult = array_merge($orderResult, $orderData);
  52. }
  53. }
  54. # 统计
  55. $res_count = array(
  56. 'total_num' => 0,
  57. 'insert_num' => 0,
  58. 'fail_num' => 0,
  59. 'update_num' => 0,
  60. 'nshoud_update_num' =>0 ,
  61. );
  62. #初始化pdo
  63. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  64. foreach($orderResult as $k=>$orderDetail){
  65. $res_count['total_num']++;
  66. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  67. if(!isset($orderDetail['tk_earning_time'])) $orderDetail['tk_earning_time'] = '2000-00-00 00:00:00';
  68. if(!isset($orderDetail['click_time'])) $orderDetail['click_time'] = $orderDetail['tk_create_time'];
  69. if(!isset($orderDetail['special_id'])) $orderDetail['special_id'] = 0;
  70. if(!isset($orderDetail['relation_id'])) $orderDetail['relation_id'] = 0;
  71. if(!isset($orderDetail['total_commission_fee'])) $orderDetail['total_commission_fee'] = 0;
  72. if(!isset($orderDetail['subsidy_fee'])) $orderDetail['subsidy_fee'] = 0;
  73. if(!isset($orderDetail['pay_price'])) $orderDetail['pay_price'] = 0;
  74. if(!isset($orderDetail['pub_share_fee'])) $orderDetail['pub_share_fee'] = 0;
  75. if(!isset($orderDetail['subsidy_type'])) $orderDetail['subsidy_type'] = 0;
  76. if(!isset($orderDetail['flow_source'])) $orderDetail['flow_source'] = '--';
  77. if(!isset($orderDetail['site_name'])) $orderDetail['site_name'] = '';
  78. if(!isset($orderDetail['adzone_name'])) $orderDetail['adzone_name'] = '';
  79. if(!isset($orderDetail['pub_share_pre_fee'])) $orderDetail['pub_share_pre_fee'] = 0;
  80. if(!isset($orderDetail['alipay_total_price'])) $orderDetail['alipay_total_price'] = 0;
  81. echo 'order_id:'.$orderDetail['trade_id'].'|| goods_id:'.$orderDetail['item_id'].'|| orderstatus:'.$orderDetail['tk_status'].'|| order_balance_at:'.$orderDetail['tk_earning_time'].'|| order_create_time:'.$orderDetail['tk_create_time'];
  82. if( DETAILDEBUG == 1){
  83. echo 'orderDetail:';
  84. var_dump($orderDetail);
  85. }
  86. # 花生联盟活跃relation逻辑
  87. if($orderDetail['adzone_id'] == '102465800487'){
  88. $hs_res = hslmSyncOrder($orderDetail);
  89. continue;
  90. }
  91. $sql = "select id,tk_status from liedou_taoke_orders where trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['item_id']."' limit 1";
  92. $stmt = $_PDO->prepare($sql);
  93. $stmt->execute();
  94. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  95. if(!empty($result)){
  96. echo "\n 该订单已存在,更新操作";
  97. if($orderDetail['tk_status'] != $result['tk_status']){
  98. //更新操作
  99. $sql = "update liedou_taoke_orders set tk_status={$orderDetail['tk_status']},earning_time='".$orderDetail['tk_earning_time']."' WHERE trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['item_id']."'";
  100. $stmt = $_PDO->prepare($sql);
  101. $stmt->execute();
  102. $res = $stmt->rowCount();
  103. if($res){
  104. echo "\n 更新成功 +1";
  105. $res_count['update_num']++;
  106. }else{
  107. echo "\n 更新失败 +1";
  108. $res_count['fail_num']++;
  109. }
  110. }else{
  111. echo "\n 状态未变,不做处理 +1";
  112. $res_count['nshoud_update_num']++;
  113. }
  114. }else{
  115. //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
  116. $res = insertOrder($orderDetail,$res_count);
  117. }
  118. echo "\n/ ** order{$res_count['total_num']}-End ** /";
  119. }
  120. echo "\n同步订单:";
  121. var_dump($res_count);
  122. }
  123. function insertOrder($orderDetail, &$res_count){
  124. $orderDetail['item_title'] = addslashes($orderDetail['item_title']);
  125. $orderDetail['seller_nick'] = addslashes($orderDetail['seller_nick']);
  126. $orderDetail['seller_shop_title'] = addslashes($orderDetail['seller_shop_title']);
  127. $sql = "insert into liedou_taoke_orders(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,tb_paid_time) values('".$orderDetail['tk_create_time']."', '". $orderDetail['click_time']."', '". $orderDetail['item_title']."', '". $orderDetail['item_id']."', '". $orderDetail['seller_nick']."', '". $orderDetail['seller_shop_title']."', '". $orderDetail['item_num']."', '". $orderDetail['item_price']."', '". $orderDetail['tk_status']."', '". $orderDetail['order_type']."', '". $orderDetail['income_rate']."', '". $orderDetail['pub_share_rate']."', '". $orderDetail['pay_price']."', '". $orderDetail['pub_share_pre_fee']."', '". $orderDetail['alipay_total_price']."', '". $orderDetail['pub_share_fee']."', '". $orderDetail['tk_earning_time']."', '". $orderDetail['total_commission_rate']."', '". $orderDetail['total_commission_fee']."', '". $orderDetail['subsidy_rate']."', '". $orderDetail['subsidy_fee']."', '". $orderDetail['subsidy_type']."', '". $orderDetail['terminal_type']."', '". $orderDetail['flow_source']."', '". $orderDetail['trade_parent_id']."', '". $orderDetail['trade_id']."', '". $orderDetail['item_category_name']."', '". $orderDetail['site_id']."', '". $orderDetail['site_name']."', '". $orderDetail['adzone_id']."', '". $orderDetail['adzone_name']."', '". $orderDetail['relation_id']."', '". $orderDetail['special_id']."', '". $orderDetail['tb_paid_time']."')";
  128. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  129. $stmt = $_PDO->prepare($sql);
  130. $stmt->execute();
  131. $res = $_PDO->lastinsertid();
  132. if($res){
  133. echo "\n 插入成功 +1";
  134. $res_count['insert_num']++;
  135. }else{
  136. echo "\n 插入失败 +1";
  137. $res_count['fail_num']++;
  138. }
  139. return $res;
  140. }
  141. function hslmSyncOrder($orderDetail){
  142. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  143. $sql = "select id,tk_status from hslm_taoke_orders where trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['item_id']."' limit 1";
  144. $stmt = $_PDO->prepare($sql);
  145. $stmt->execute();
  146. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  147. if(!empty($result)){
  148. if($orderDetail['tk_status'] != $result['tk_status']){
  149. //更新操作
  150. $sql = "update hslm_taoke_orders set tk_status={$orderDetail['tk_status']},earning_time='".$orderDetail['tk_earning_time']."' WHERE trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['item_id']."'";
  151. $stmt = $_PDO->prepare($sql);
  152. $stmt->execute();
  153. $res = $stmt->rowCount();
  154. if($res){
  155. echo "\n 处理成功 +1";
  156. }else{
  157. echo "\n 处理失败 +1";
  158. }
  159. }else{
  160. echo "\n 状态未变,不做处理 +1";
  161. }
  162. }else{
  163. //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
  164. $res = hslminsertOrder($orderDetail);
  165. }
  166. }
  167. function hslminsertOrder($orderDetail){
  168. $orderDetail['item_title'] = addslashes($orderDetail['item_title']);
  169. $orderDetail['seller_nick'] = addslashes($orderDetail['seller_nick']);
  170. $orderDetail['seller_shop_title'] = addslashes($orderDetail['seller_shop_title']);
  171. $sql = "insert into hslm_taoke_orders(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) values('".$orderDetail['tk_create_time']."', '". $orderDetail['click_time']."', '". $orderDetail['item_title']."', '". $orderDetail['item_id']."', '". $orderDetail['seller_nick']."', '". $orderDetail['seller_shop_title']."', '". $orderDetail['item_num']."', '". $orderDetail['item_price']."', '". $orderDetail['tk_status']."', '". $orderDetail['order_type']."', '". $orderDetail['income_rate']."', '". $orderDetail['pub_share_rate']."', '". $orderDetail['pay_price']."', '". $orderDetail['pub_share_pre_fee']."', '". $orderDetail['alipay_total_price']."', '". $orderDetail['pub_share_fee']."', '". $orderDetail['tk_earning_time']."', '". $orderDetail['total_commission_rate']."', '". $orderDetail['total_commission_fee']."', '". $orderDetail['subsidy_rate']."', '". $orderDetail['subsidy_fee']."', '". $orderDetail['subsidy_type']."', '". $orderDetail['terminal_type']."', '". $orderDetail['flow_source']."', '". $orderDetail['trade_parent_id']."', '". $orderDetail['trade_id']."', '". $orderDetail['item_category_name']."', '". $orderDetail['site_id']."', '". $orderDetail['site_name']."', '". $orderDetail['adzone_id']."', '". $orderDetail['adzone_name']."', '". $orderDetail['relation_id']."', '". $orderDetail['special_id']."')";
  172. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  173. $stmt = $_PDO->prepare($sql);
  174. $stmt->execute();
  175. $res = $_PDO->lastinsertid();
  176. if($res){
  177. //更新redis
  178. $redis = RedisModel::srem( 'PEANUT_LIEDOU_RELATION_ID', $orderDetail['relation_id'] );
  179. echo "\n 插入成功 +1";
  180. }else{
  181. echo "\n 插入失败 +1";
  182. }
  183. return $res;
  184. }