优惠券订单及其他脚本

sync_liedou_order_up.php 16KB

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