优惠券订单及其他脚本

sync_liedou1h_order.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. require_once 'DB_PDO.class.php';
  3. require_once 'TBK_v2.class.php';
  4. require_once 'confv2.class.php';
  5. require_once 'RedisClass.php';
  6. define("DETAILDEBUG", 0);//打印流程详细信息,如有需要,设为1
  7. define("LIMIT_NUM", 0);//限制导入条数,0不限制,方便测试,上线设为0
  8. date_default_timezone_set('PRC');
  9. # 同步六小时内的订单
  10. $_time = 1200;
  11. $now = time();
  12. sync_orders($_time, $now);
  13. //sync_balances($_time, $now);
  14. /**
  15. * 同步新订单
  16. */
  17. function sync_orders($_time, $now){
  18. //同步订单脚本
  19. # 1.同步N分钟内所有订单
  20. //$now = strtotime('2019-01-21 16:54:00');
  21. $time = date("Y-m-d H:i:s", strtotime('-3 hour'));
  22. echo "\n\nCreate_time:".$time;
  23. $orderResult = array();
  24. for($x=9;$x>0;$x--){
  25. $dstr = '-'.($x*20).' minutes';
  26. $stime = date("Y-m-d H:i:s",strtotime($dstr));
  27. //渠道二方
  28. for($i=1;$i<100;$i++){
  29. $res1 = TBK::syncOrder($i,$stime,$_time,1,'create_time',1,2); //$page,$stime,$time,$tk_status,$time_type,$order_count_type,$order_scene
  30. if(empty($res1)){
  31. break;
  32. }
  33. $orderResult = array_merge($orderResult, $res1);
  34. }
  35. //会员二方
  36. for($i=1;$i<100;$i++){
  37. $res2 = TBK::syncOrder($i,$stime,$_time,1,'create_time',1,3); //$page,$stime,$time,$tk_status,$time_type,$order_count_type,$order_scene
  38. if(empty($res2)){
  39. break;
  40. }
  41. $orderResult = array_merge($orderResult, $res2);
  42. }
  43. //渠道三方
  44. for($i=1;$i<100;$i++){
  45. $res3 = TBK::syncOrder($i,$stime,$_time,1,'create_time',2,2); //$page,$stime,$time,$tk_status,$time_type,$order_count_type,$order_scene
  46. if(empty($res3)){
  47. break;
  48. }
  49. $orderResult = array_merge($orderResult, $res3);
  50. }
  51. //会员三方
  52. for($i=1;$i<100;$i++){
  53. $res4 = TBK::syncOrder($i,$stime,$_time,1,'create_time',2,3); //$page,$stime,$time,$tk_status,$time_type,$order_count_type,$order_scene
  54. if(empty($res4)){
  55. break;
  56. }
  57. $orderResult = array_merge($orderResult, $res4);
  58. }
  59. }
  60. # 统计
  61. $res_count = array(
  62. 'total_num' => 0,
  63. 'insert_num' => 0,
  64. 'fail_num' => 0,
  65. 'update_num' => 0,
  66. 'nshoud_update_num' =>0 ,
  67. );
  68. #初始化pdo
  69. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  70. foreach($orderResult as $k=>$orderDetail){
  71. $res_count['total_num']++;
  72. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  73. if(!isset($orderDetail['earning_time'])) $orderDetail['earning_time'] = '2000-00-00 00:00:00';
  74. if(!isset($orderDetail['click_time'])) $orderDetail['click_time'] = $orderDetail['create_time'];
  75. if(!isset($orderDetail['special_id'])) $orderDetail['special_id'] = 0;
  76. if(!isset($orderDetail['relation_id'])) $orderDetail['relation_id'] = 0;
  77. if(!isset($orderDetail['total_commission_fee'])) $orderDetail['total_commission_fee'] = 0;
  78. if(!isset($orderDetail['subsidy_fee'])) $orderDetail['subsidy_fee'] = 0;
  79. if(!isset($orderDetail['pay_price'])) $orderDetail['pay_price'] = 0;
  80. if(!isset($orderDetail['commission'])) $orderDetail['commission'] = 0;
  81. if(!isset($orderDetail['subsidy_type'])) $orderDetail['subsidy_type'] = 0;
  82. if(!isset($orderDetail['tk3rd_type'])) $orderDetail['tk3rd_type'] = '--';
  83. if(!isset($orderDetail['site_name'])) $orderDetail['site_name'] = '';
  84. if(!isset($orderDetail['adzone_name'])) $orderDetail['adzone_name'] = '';
  85. if(!isset($orderDetail['pub_share_pre_fee'])) $orderDetail['pub_share_pre_fee'] = 0;
  86. if(!isset($orderDetail['alipay_total_price'])) $orderDetail['alipay_total_price'] = 0;
  87. echo 'order_id:'.$orderDetail['trade_id'].'|| goods_id:'.$orderDetail['num_iid'].'|| orderstatus:'.$orderDetail['tk_status'].'|| order_balance_at:'.$orderDetail['earning_time'].'|| order_create_time:'.$orderDetail['create_time'];
  88. if( DETAILDEBUG == 1){
  89. echo 'orderDetail:';
  90. var_dump($orderDetail);
  91. }
  92. # 花生联盟活跃relation逻辑
  93. if($orderDetail['adzone_id'] == '102465800487'){
  94. $hs_res = hslmSyncOrder($orderDetail);
  95. continue;
  96. }
  97. $sql = "select id,tk_status from liedou_taoke_orders where trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['num_iid']."' limit 1";
  98. $stmt = $_PDO->prepare($sql);
  99. $stmt->execute();
  100. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  101. if(!empty($result)){
  102. echo "\n 该订单已存在,更新操作";
  103. if($orderDetail['tk_status'] != $result['tk_status']){
  104. //更新操作
  105. $sql = "update liedou_taoke_orders set tk_status={$orderDetail['tk_status']},earning_time='".$orderDetail['earning_time']."' WHERE trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['num_iid']."'";
  106. $stmt = $_PDO->prepare($sql);
  107. $stmt->execute();
  108. $res = $stmt->rowCount();
  109. if($res){
  110. echo "\n 更新成功 +1";
  111. $res_count['update_num']++;
  112. }else{
  113. echo "\n 更新失败 +1";
  114. $res_count['fail_num']++;
  115. }
  116. }else{
  117. echo "\n 状态未变,不做处理 +1";
  118. $res_count['nshoud_update_num']++;
  119. }
  120. }else{
  121. //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
  122. $res = insertOrder($orderDetail,$res_count);
  123. }
  124. echo "\n/ ** order{$res_count['total_num']}-End ** /";
  125. }
  126. echo "\n同步订单:";
  127. var_dump($res_count);
  128. }
  129. /**
  130. * 同步结算订单
  131. */
  132. function sync_balances($_time,$now){
  133. //同步订单脚本
  134. # 1.同步3分钟内结算订单
  135. $stime = date("Y-m-d H:i:s", $now-$_time);
  136. $orderResult = array();
  137. # 渠道二方结算
  138. for($i=1;$i<100;$i++){
  139. $res = TBK::syncOrder($i,$stime,$_time,3,'settle_time',1,2); //$page,$stime,$time,$tk_status,$time_type,$order_count_type,$order_scene
  140. if(empty($res)){
  141. break;
  142. }
  143. $orderResult = array_merge($orderResult, $res);
  144. }
  145. # 会员二方结算
  146. for($i=1;$i<100;$i++){
  147. $res = TBK::syncOrder($i,$stime,$_time,3,'settle_time',1,3); //$page,$stime,$time,$tk_status,$time_type,$order_count_type,$order_scene
  148. if(empty($res)){
  149. break;
  150. }
  151. $orderResult = array_merge($orderResult, $res);
  152. }
  153. # 渠道三方结算
  154. for($i=1;$i<100;$i++){
  155. $res = TBK::syncOrder($i,$stime,$_time,3,'settle_time',2,2); //$page,$stime,$time,$tk_status,$time_type,$order_count_type,$order_scene
  156. if(empty($res)){
  157. break;
  158. }
  159. $orderResult = array_merge($orderResult, $res);
  160. }
  161. # 会员三方结算
  162. for($i=1;$i<100;$i++){
  163. $res = TBK::syncOrder($i,$stime,$_time,3,'settle_time',2,3); //$page,$stime,$time,$tk_status,$time_type,$order_count_type,$order_scene
  164. if(empty($res)){
  165. break;
  166. }
  167. $orderResult = array_merge($orderResult, $res);
  168. }
  169. # 统计
  170. $res_count = array(
  171. 'total_num' => 0,
  172. 'insert_num' => 0,
  173. 'update_num' => 0,
  174. 'fail_num' => 0,
  175. 'nshoud_update_num' =>0 ,
  176. );
  177. #初始化pdo
  178. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  179. foreach($orderResult as $k=>$orderDetail){
  180. $res_count['total_num']++;
  181. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  182. # 花生联盟活跃relation逻辑 //结算逻辑跳过
  183. if($orderDetail['adzone_id'] == '102465800487'){
  184. echo "\n花生订单跳过\n";
  185. continue;
  186. }
  187. if(!isset($orderDetail['earning_time'])) $orderDetail['earning_time'] = '2000-00-00 00:00:00';
  188. if(!isset($orderDetail['click_time'])) $orderDetail['click_time'] = $orderDetail['create_time'];
  189. if(!isset($orderDetail['special_id'])) $orderDetail['special_id'] = 0;
  190. if(!isset($orderDetail['relation_id'])) $orderDetail['relation_id'] = 0;
  191. if(!isset($orderDetail['total_commission_fee'])) $orderDetail['total_commission_fee'] = 0;
  192. if(!isset($orderDetail['subsidy_fee'])) $orderDetail['subsidy_fee'] = 0;
  193. if(!isset($orderDetail['pay_price'])) $orderDetail['pay_price'] = 0;
  194. if(!isset($orderDetail['commission'])) $orderDetail['commission'] = 0;
  195. if(!isset($orderDetail['subsidy_type'])) $orderDetail['subsidy_type'] = 0;
  196. if(!isset($orderDetail['tk3rd_type'])) $orderDetail['tk3rd_type'] = '--';
  197. if(!isset($orderDetail['site_name'])) $orderDetail['site_name'] = '';
  198. if(!isset($orderDetail['adzone_name'])) $orderDetail['adzone_name'] = '';
  199. echo 'order_id:'.$orderDetail['trade_id'].'|| goods_id:'.$orderDetail['num_iid'].'|| orderstatus:'.$orderDetail['tk_status'].'|| order_balance_at:'.$orderDetail['earning_time'];
  200. if( DETAILDEBUG == 1){
  201. echo 'orderDetail:';
  202. var_dump($orderDetail);
  203. }
  204. $sql = "select id,tk_status from liedou_taoke_orders where trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['num_iid']."' limit 1";
  205. $stmt = $_PDO->prepare($sql);
  206. $stmt->execute();
  207. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  208. if(!empty($result)){
  209. if($orderDetail['tk_status'] != $result['tk_status']){
  210. //更新操作
  211. $sql = "update liedou_taoke_orders set tk_status={$orderDetail['tk_status']},earning_time='".$orderDetail['earning_time']."' WHERE trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['num_iid']."'";
  212. $stmt = $_PDO->prepare($sql);
  213. $stmt->execute();
  214. $res = $stmt->rowCount();
  215. if($res){
  216. echo "\n 处理成功 +1";
  217. $res_count['update_num']++;
  218. }else{
  219. echo "\n 处理失败 +1";
  220. $res_count['fail_num']++;
  221. }
  222. }else{
  223. echo "\n 状态未变,不做处理 +1";
  224. $res_count['nshoud_update_num']++;
  225. }
  226. }else{
  227. //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
  228. $res = insertOrder($orderDetail, $res_count);
  229. }
  230. echo "\n/ ** order{$res_count['total_num']}-End ** /";
  231. }
  232. echo "\n结算订单:";
  233. var_dump($res_count);
  234. }
  235. function insertOrder($orderDetail, &$res_count){
  236. $orderDetail['item_title'] = addslashes($orderDetail['item_title']);
  237. $orderDetail['seller_nick'] = addslashes($orderDetail['seller_nick']);
  238. $orderDetail['seller_shop_title'] = addslashes($orderDetail['seller_shop_title']);
  239. $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) values('".$orderDetail['create_time']."', '". $orderDetail['click_time']."', '". $orderDetail['item_title']."', '". $orderDetail['num_iid']."', '". $orderDetail['seller_nick']."', '". $orderDetail['seller_shop_title']."', '". $orderDetail['item_num']."', '". $orderDetail['price']."', '". $orderDetail['tk_status']."', '". $orderDetail['order_type']."', '". $orderDetail['income_rate']."', '". $orderDetail['commission_rate']."', '". $orderDetail['pay_price']."', '". $orderDetail['pub_share_pre_fee']."', '". $orderDetail['alipay_total_price']."', '". $orderDetail['commission']."', '". $orderDetail['earning_time']."', '". $orderDetail['total_commission_rate']."', '". $orderDetail['total_commission_fee']."', '". $orderDetail['subsidy_rate']."', '". $orderDetail['subsidy_fee']."', '". $orderDetail['subsidy_type']."', '". $orderDetail['terminal_type']."', '". $orderDetail['tk3rd_type']."', '". $orderDetail['trade_parent_id']."', '". $orderDetail['trade_id']."', '". $orderDetail['auction_category']."', '". $orderDetail['site_id']."', '". $orderDetail['site_name']."', '". $orderDetail['adzone_id']."', '". $orderDetail['adzone_name']."', '". $orderDetail['relation_id']."', '". $orderDetail['special_id']."')";
  240. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  241. $stmt = $_PDO->prepare($sql);
  242. $stmt->execute();
  243. $res = $_PDO->lastinsertid();
  244. if($res){
  245. echo "\n 插入成功 +1";
  246. $res_count['insert_num']++;
  247. }else{
  248. echo "\n 插入失败 +1";
  249. $res_count['fail_num']++;
  250. }
  251. return $res;
  252. }
  253. function hslmSyncOrder($orderDetail){
  254. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  255. $sql = "select id,tk_status from hslm_taoke_orders where trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['num_iid']."' limit 1";
  256. $stmt = $_PDO->prepare($sql);
  257. $stmt->execute();
  258. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  259. if(!empty($result)){
  260. if($orderDetail['tk_status'] != $result['tk_status']){
  261. //更新操作
  262. $sql = "update hslm_taoke_orders set tk_status={$orderDetail['tk_status']},earning_time='".$orderDetail['earning_time']."' WHERE trade_id = '".$orderDetail['trade_id']."' and num_iid = '".$orderDetail['num_iid']."'";
  263. $stmt = $_PDO->prepare($sql);
  264. $stmt->execute();
  265. $res = $stmt->rowCount();
  266. if($res){
  267. echo "\n 处理成功 +1";
  268. }else{
  269. echo "\n 处理失败 +1";
  270. }
  271. }else{
  272. echo "\n 状态未变,不做处理 +1";
  273. }
  274. }else{
  275. //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
  276. $res = hslminsertOrder($orderDetail);
  277. }
  278. }
  279. function hslminsertOrder($orderDetail){
  280. $orderDetail['item_title'] = addslashes($orderDetail['item_title']);
  281. $orderDetail['seller_nick'] = addslashes($orderDetail['seller_nick']);
  282. $orderDetail['seller_shop_title'] = addslashes($orderDetail['seller_shop_title']);
  283. $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['create_time']."', '". $orderDetail['click_time']."', '". $orderDetail['item_title']."', '". $orderDetail['num_iid']."', '". $orderDetail['seller_nick']."', '". $orderDetail['seller_shop_title']."', '". $orderDetail['item_num']."', '". $orderDetail['price']."', '". $orderDetail['tk_status']."', '". $orderDetail['order_type']."', '". $orderDetail['income_rate']."', '". $orderDetail['commission_rate']."', '". $orderDetail['pay_price']."', '". $orderDetail['pub_share_pre_fee']."', '". $orderDetail['alipay_total_price']."', '". $orderDetail['commission']."', '". $orderDetail['earning_time']."', '". $orderDetail['total_commission_rate']."', '". $orderDetail['total_commission_fee']."', '". $orderDetail['subsidy_rate']."', '". $orderDetail['subsidy_fee']."', '". $orderDetail['subsidy_type']."', '". $orderDetail['terminal_type']."', '". $orderDetail['tk3rd_type']."', '". $orderDetail['trade_parent_id']."', '". $orderDetail['trade_id']."', '". $orderDetail['auction_category']."', '". $orderDetail['site_id']."', '". $orderDetail['site_name']."', '". $orderDetail['adzone_id']."', '". $orderDetail['adzone_name']."', '". $orderDetail['relation_id']."', '". $orderDetail['special_id']."')";
  284. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  285. $stmt = $_PDO->prepare($sql);
  286. $stmt->execute();
  287. $res = $_PDO->lastinsertid();
  288. if($res){
  289. //更新redis
  290. $redis = RedisModel::srem( 'PEANUT_LIEDOU_RELATION_ID', $orderDetail['relation_id'] );
  291. echo "\n 插入成功 +1";
  292. }else{
  293. echo "\n 插入失败 +1";
  294. }
  295. return $res;
  296. }