优惠券订单及其他脚本

sync_order.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. date_default_timezone_set('PRC');
  8. $_time = 180;
  9. $now = time();
  10. sync_orders($_time, $now);
  11. sync_balances($_time, $now);
  12. /**
  13. * 同步新订单
  14. */
  15. function sync_orders($_time, $now){
  16. //同步订单脚本
  17. # 1.同步N分钟内所有订单
  18. //$now = strtotime('2019-01-21 16:54:00');
  19. $stime = date("Y-m-d H:i:s", $now-$_time);
  20. $etime = date('Y-m-d H:i:s');
  21. echo "\n\nCreate_time:".$stime;
  22. $orderResult = array();
  23. $time_type = 2;//按付款时间查
  24. $tk_status = 0;//查询所有状态订单
  25. $position_index = '';//查下一页必要参数
  26. //渠道
  27. for($i=1;$i<100;$i++){
  28. $res = TBK::syncOrderNew($i,$stime, $etime, $time_type ,$tk_status,1,$position_index);
  29. if(empty($res)){
  30. break;
  31. }else{
  32. $orderData = $res['data'];
  33. $position_index = $res['position_index'];
  34. }
  35. $orderResult = array_merge($orderResult, $orderData);
  36. }
  37. # 统计
  38. $res_count = array(
  39. 'total_num' => 0,
  40. 'insert_num' => 0,
  41. 'fail_num' => 0,
  42. 'update_num' => 0,
  43. 'nshoud_update_num' =>0 ,
  44. );
  45. #初始化pdo
  46. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  47. foreach($orderResult as $k=>$orderDetail){
  48. $res_count['total_num']++;
  49. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  50. if(!isset($orderDetail['tk_earning_time'])) $orderDetail['tk_earning_time'] = '2000-00-00 00:00:00';
  51. if(!isset($orderDetail['click_time'])) $orderDetail['click_time'] = $orderDetail['tk_create_time'];
  52. if(!isset($orderDetail['special_id'])) $orderDetail['special_id'] = 0;
  53. if(!isset($orderDetail['relation_id'])) $orderDetail['relation_id'] = 0;
  54. if(!isset($orderDetail['total_commission_fee'])) $orderDetail['total_commission_fee'] = 0;
  55. if(!isset($orderDetail['subsidy_fee'])) $orderDetail['subsidy_fee'] = 0;
  56. if(!isset($orderDetail['pay_price'])) $orderDetail['pay_price'] = 0;
  57. if(!isset($orderDetail['pub_share_fee'])) $orderDetail['pub_share_fee'] = 0;
  58. if(!isset($orderDetail['subsidy_type'])) $orderDetail['subsidy_type'] = 0;
  59. if(!isset($orderDetail['flow_source'])) $orderDetail['flow_source'] = '--';
  60. if(!isset($orderDetail['site_name'])) $orderDetail['site_name'] = '';
  61. if(!isset($orderDetail['adzone_name'])) $orderDetail['adzone_name'] = '';
  62. 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'];
  63. if( DETAILDEBUG == 1){
  64. echo 'orderDetail:';
  65. var_dump($orderDetail);
  66. }
  67. $sql = "select id,tk_status from zbds_taoke_orders where trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['item_id']."' limit 1";
  68. $stmt = $_PDO->prepare($sql);
  69. $stmt->execute();
  70. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  71. if(!empty($result)){
  72. echo "\n 该订单已存在,更新操作";
  73. if($orderDetail['tk_status'] != $result['tk_status']){
  74. //更新操作
  75. $sql = "update zbds_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']."'";
  76. $stmt = $_PDO->prepare($sql);
  77. $stmt->execute();
  78. $res = $stmt->rowCount();
  79. if($res){
  80. echo "\n 更新成功 +1";
  81. $res_count['update_num']++;
  82. }else{
  83. echo "\n 更新失败 +1";
  84. $res_count['fail_num']++;
  85. }
  86. }else{
  87. echo "\n 状态未变,不做处理 +1";
  88. $res_count['nshoud_update_num']++;
  89. }
  90. }else{
  91. //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
  92. $res = insertOrder($orderDetail,$res_count);
  93. }
  94. echo "\n/ ** order{$res_count['total_num']}-End ** /";
  95. }
  96. echo "\n同步订单:";
  97. var_dump($res_count);
  98. }
  99. /**
  100. * 同步结算订单
  101. */
  102. function sync_balances($_time,$now){
  103. //同步订单脚本
  104. # 1.同步3分钟内结算订单
  105. $stime = date("Y-m-d H:i:s", $now-$_time);
  106. $etime = date("Y-m-d H:i:s");
  107. $orderResult = array();
  108. $time_type = 3;//按结算时间查
  109. $tk_status = 3;//查询结算状态订单
  110. $position_index = '';//查下一页必要参数
  111. //渠道
  112. for($i=1;$i<100;$i++){
  113. $res = TBK::syncOrderNew($i,$stime, $etime, $time_type ,$tk_status,1,$position_index);
  114. if(empty($res)){
  115. break;
  116. }else{
  117. $orderData = $res['data'];
  118. $position_index = $res['position_index'];
  119. }
  120. $orderResult = array_merge($orderResult, $orderData);
  121. }
  122. # 统计
  123. $res_count = array(
  124. 'total_num' => 0,
  125. 'insert_num' => 0,
  126. 'update_num' => 0,
  127. 'fail_num' => 0,
  128. 'nshoud_update_num' =>0 ,
  129. );
  130. #初始化pdo
  131. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  132. foreach($orderResult as $k=>$orderDetail){
  133. $res_count['total_num']++;
  134. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  135. if(!isset($orderDetail['tk_earning_time'])) $orderDetail['tk_earning_time'] = '2000-00-00 00:00:00';
  136. if(!isset($orderDetail['click_time'])) $orderDetail['click_time'] = $orderDetail['tk_create_time'];
  137. if(!isset($orderDetail['special_id'])) $orderDetail['special_id'] = 0;
  138. if(!isset($orderDetail['relation_id'])) $orderDetail['relation_id'] = 0;
  139. if(!isset($orderDetail['total_commission_fee'])) $orderDetail['total_commission_fee'] = 0;
  140. if(!isset($orderDetail['subsidy_fee'])) $orderDetail['subsidy_fee'] = 0;
  141. if(!isset($orderDetail['pay_price'])) $orderDetail['pay_price'] = 0;
  142. if(!isset($orderDetail['pub_share_fee'])) $orderDetail['pub_share_fee'] = 0;
  143. if(!isset($orderDetail['subsidy_type'])) $orderDetail['subsidy_type'] = 0;
  144. if(!isset($orderDetail['flow_source'])) $orderDetail['flow_source'] = '--';
  145. if(!isset($orderDetail['site_name'])) $orderDetail['site_name'] = '';
  146. if(!isset($orderDetail['adzone_name'])) $orderDetail['adzone_name'] = '';
  147. if(!isset($orderDetail['pub_share_pre_fee'])) $orderDetail['pub_share_pre_fee'] = 0;
  148. if(!isset($orderDetail['alipay_total_price'])) $orderDetail['alipay_total_price'] = 0;
  149. echo 'order_id:'.$orderDetail['trade_id'].'|| goods_id:'.$orderDetail['item_id'].'|| orderstatus:'.$orderDetail['tk_status'].'|| order_balance_at:'.$orderDetail['tk_earning_time'];
  150. if( DETAILDEBUG == 1){
  151. echo 'orderDetail:';
  152. var_dump($orderDetail);
  153. }
  154. $sql = "select id,tk_status from zbds_taoke_orders where trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['item_id']."' limit 1";
  155. $stmt = $_PDO->prepare($sql);
  156. $stmt->execute();
  157. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  158. if(!empty($result)){
  159. if($orderDetail['tk_status'] != $result['tk_status']){
  160. //更新操作
  161. $sql = "update zbds_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']."'";
  162. $stmt = $_PDO->prepare($sql);
  163. $stmt->execute();
  164. $res = $stmt->rowCount();
  165. if($res){
  166. echo "\n 处理成功 +1";
  167. $res_count['update_num']++;
  168. }else{
  169. echo "\n 处理失败 +1";
  170. $res_count['fail_num']++;
  171. }
  172. }else{
  173. echo "\n 状态未变,不做处理 +1";
  174. $res_count['nshoud_update_num']++;
  175. }
  176. }else{
  177. //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
  178. $res = insertOrder($orderDetail, $res_count);
  179. }
  180. echo "\n/ ** order{$res_count['total_num']}-End ** /";
  181. }
  182. echo "\n结算订单:";
  183. var_dump($res_count);
  184. }
  185. function insertOrder($orderDetail, &$res_count){
  186. $orderDetail['item_title'] = addslashes($orderDetail['item_title']);
  187. $orderDetail['seller_nick'] = addslashes($orderDetail['seller_nick']);
  188. $orderDetail['seller_shop_title'] = addslashes($orderDetail['seller_shop_title']);
  189. $sql = "insert into zbds_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'] ."' )";
  190. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  191. $stmt = $_PDO->prepare($sql);
  192. $stmt->execute();
  193. $res = $_PDO->lastinsertid();
  194. if($res){
  195. echo "\n 插入成功 +1";
  196. $res_count['insert_num']++;
  197. }else{
  198. echo "\n 插入失败 +1";
  199. $res_count['fail_num']++;
  200. }
  201. return $res;
  202. }