优惠券订单及其他脚本

sync_liedou_order.php 16KB

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