优惠券订单及其他脚本

excel_vq.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. require_once 'DB_PDO.class.php';
  3. require_once 'conf.class.php';
  4. require_once '/root/php_script/PHPExcel-1.8/Classes/PHPExcel.php';
  5. require_once '/root/php_script/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
  6. require_once '/root/php_script/PHPExcel-1.8/Classes/PHPExcel/Reader/Excel5.php';
  7. # 定义下载订单时间段
  8. date_default_timezone_set('PRC');
  9. define("END_TIME", date('Y-m-d%20H:i:s', strtotime('2019-04-12 00:00:00')) );
  10. define("START_TIME", date('Y-m-d%20H:i:s', strtotime('2019-04-06 00:00:00')) );
  11. define("DETAILDEBUG", 0);//打印流程详细信息,如有需要,设为1
  12. //define("LIMIT_NUM", 0);//限制导入条数,0不限制,方便测试,上线设为0
  13. /**
  14. * 导入及更新 order_rebate 维权订单数据
  15. */
  16. #设置时区
  17. set_time_limit(0);
  18. ini_set('memory_limit','1024M');
  19. error_reporting(E_ALL);
  20. function readDataFromDb(){
  21. # 1.下载excel
  22. $res = down_excel();
  23. if( !$res ){
  24. echo '下载excel文件失败';
  25. exit;
  26. }
  27. # 遍历订单
  28. # 统计
  29. $res_count = array(
  30. 'total_num' => 0,
  31. 'generalupdate_num' => 0,
  32. 'fail_update_num' => 0,
  33. );
  34. #初始化pdo
  35. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  36. # 读取excel
  37. $templateName = $res;
  38. //实例化Excel读取类
  39. $objReader = new PHPExcel_Reader_Excel2007();
  40. if(!$objReader->canRead($templateName)){
  41. $objReader = new PHPExcel_Reader_Excel5();
  42. if(!$objReader->canRead($templateName)){
  43. echo "\n".'无法识别的Excel文件!';
  44. return false;
  45. }
  46. }
  47. $objPHPExcel=$objReader->load($templateName);
  48. $sheet=$objPHPExcel->getSheet(0);//获取第一个工作表
  49. $highestRow=$sheet->getHighestRow();//取得总行数
  50. $highestColumn=$sheet->getHighestColumn(); //取得总列数
  51. if( strlen($highestColumn) == 2 ){
  52. $highestColumn = 'Z';
  53. }
  54. $orderList = array();
  55. //循环读取excel文件,读取一条,插入一条
  56. for($j=2;$j<=$highestRow;$j++){//从第二行开始读取数据
  57. $str='';
  58. for($k='A';$k<=$highestColumn;$k++){ //从A列读取数据
  59. //这种方法简单,但有不妥,以'\\'合并为数组,再分割\\为字段值插入到数据库,实测在excel中,如果某单元格的值包含了\\导入的数据会为空
  60. $str.=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'\\';//读取单元格
  61. if( $k=='AE' ){
  62. break;
  63. }
  64. }
  65. //explode:函数把字符串分割为数组。
  66. $orderDetail = explode('\\',trim($str,'\\') );
  67. $res_count['total_num']++;
  68. echo "\n\n/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  69. if( DETAILDEBUG == 1){
  70. echo 'orderDetail:';
  71. var_dump($orderDetail);
  72. }
  73. //0淘宝订单编号 1淘宝子订单编号 2商品名称 3维权退款金额 4应返还金额 5维权状态 6订单结算时间 7维权创建时间 8维权完成时间
  74. $order_id=$orderDetail[1];
  75. if($orderDetail[5] == '维权创建' || $orderDetail[5] == '等待处理' ){
  76. $maintain_power = 1;
  77. }elseif($orderDetail[5] == '维权失败'){
  78. $maintain_power = 2;
  79. }elseif($orderDetail[5] == '维权成功'){
  80. $maintain_power = 3;
  81. }
  82. $power_create_at = $orderDetail[7];
  83. $power_end_at = isset($orderDetail[8]) ? $orderDetail[8] : null;
  84. echo 'order_id:'.$order_id.'|| goods_name:'.$orderDetail[2].'|| maintain_power:'.$maintain_power.'|| power_create_at:'.$power_create_at;
  85. echo "\n";
  86. try{
  87. $_PDO->beginTransaction();
  88. //更新order_rebate
  89. $sql="update order_rebate set maintain_power=:maintain_power,power_create_at=:power_create_at,power_end_at=:power_end_at where order_id=:order_id";
  90. if($maintain_power==3) $sql="update order_rebate set maintain_power=:maintain_power,power_create_at=:power_create_at,power_end_at=:power_end_at,status=0 where order_id=:order_id";
  91. $stmt = $_PDO->prepare($sql);
  92. $stmt->execute(array(':maintain_power'=>$maintain_power,':power_create_at'=>$power_create_at,':power_end_at'=>$power_end_at,':order_id'=>$order_id));
  93. $res = $stmt->rowCount();
  94. echo "更新order_rebate:".$res."\n";
  95. if($res){
  96. $res_count['generalupdate_num']++;
  97. }else{
  98. $res_count['fail_update_num']++;
  99. }
  100. $_PDO->commit();
  101. }catch(Exception $e){
  102. echo 'err_msg'.$e->getMessage()."\n";
  103. $_PDO->rollback();
  104. }
  105. echo "/ ***order{$res_count['total_num']}-End *** / \n";
  106. }
  107. echo "<pre>";
  108. print_r($res_count);
  109. }
  110. # curl调用接口push
  111. function curl_post($url, $params){
  112. $ch = curl_init();
  113. curl_setopt($ch, CURLOPT_URL, $url);
  114. curl_setopt($ch, CURLOPT_HEADER, 0);
  115. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  116. curl_setopt($ch, CURLOPT_POST, 1);
  117. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  118. $response = curl_exec($ch);
  119. curl_close($ch);
  120. return $response;
  121. }
  122. # 下载excel订单文件
  123. function down_excel(){
  124. $str_cookie = 'cna=gGnDEwQLQAACATqEyYplpj5I; account-path-guide-s1=true; 209580064_yxjh-filter-1=true; undefined_yxjh-filter-1=true; t=8c549c3cedcac7891a2c3c7f36ca91f4; 250470096_yxjh-filter-1=true; 299550010_yxjh-filter-1=true; cookie2=1b3c8c127c250da4d7147c8d98b87161; v=0; _tb_token_=ee810369b3a4b; alimamapwag=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzY5LjAuMzQ5Ny4xMDAgU2FmYXJpLzUzNy4zNg%3D%3D; cookie32=4a03ef32fe3708949595117d92de750b; alimamapw=FnF%2BFCZSFCAnQ3dUCgIGCjoCBlcMBwJUBlUGUwNYClFRA1ABBAALU1JUAANQV1AEWg%3D%3D; cookie31=Mjk5NTUwMDEwLCVFOSU4NSVCNyVFNyU4MiVBQjIwMTksa3V4dWFubGl1aGFvQDE2My5jb20sVEI%3D; login=U%2BGCWk%2F75gdr5Q%3D%3D; l=bBEoP2SHvN8ixrI8BOfNmuI-hd_TBCRfcsPzw4ww3ICP_yW9Q0rhWZ_Al6tpC3GVa6BDJ3Wc5ILQBPTSWyUIh; isg=BJWV2rh0ZWSC9kYU_GrxAuWEpJGPOlO1_WyCIhc-qIwzbr1g3uZfdH1sODL97mFc';
  125. $nowTime = END_TIME;
  126. $pastTime = START_TIME;
  127. //$url = "https://pub.alimama.com/report/getNewTbkRefundPaymentDetails.json?spm=a219t.7664554.1998457203.54.1ef735d9aJK7cQ&refundType=1&searchType=1&DownloadID=DOWNLOAD_EXPORT_CPSPAYMENT_REFUND_OVERVIEW&startTime=".$pastTime."&endTime=".$nowTime;
  128. $url = "https://pub.alimama.com/report/getNewTbkRefundPaymentDetails.json?spm=a219t.7664554.1998457203.50.1ef735d99JauPX&refundType=1&searchType=1&DownloadID=DOWNLOAD_EXPORT_CPSPAYMENT_REFUND_OVERVIEW&startTime=".$pastTime."&endTime=".$nowTime;
  129. echo $url."\n";
  130. $ch = curl_init($url);
  131. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  132. curl_setopt($ch, CURLOPT_COOKIE, $str_cookie); //使用上面获取的cookies
  133. $response = curl_exec($ch);
  134. curl_close($ch);
  135. if( $response ){
  136. $file = fopen("./order_excel_vq.csv", "w+");
  137. $a = fputs($file, $response);
  138. fclose($file);
  139. return './order_excel_vq.csv';
  140. }
  141. else{
  142. return false;
  143. }
  144. }
  145. readDataFromDb();