优惠券订单及其他脚本

sync_liedou_order.php 15KB

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