优惠券订单及其他脚本

excel_brandgoods.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. require_once 'DB_PDO.class.php';
  3. require_once 'TBK.class.php';
  4. require_once 'conf.class.php';
  5. require_once 'PHPExcel-1.8/Classes/PHPExcel.php';
  6. require_once 'PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
  7. require_once 'PHPExcel-1.8/Classes/PHPExcel/Reader/Excel5.php';
  8. # 定义下载订单时间段
  9. //define("END_TIME", date('Y-m-d%20H:i:s', strtotime('2018-11-04 00:00:00')) );
  10. //define("START_TIME", date('Y-m-d%20H:i:s', strtotime('2018-11-03 14:00:00')) );
  11. define("DETAILDEBUG", 1);//打印流程详细信息,如有需要,设为1
  12. define("LIMIT_NUM", 0);//限制导入条数,0不限制,方便测试,上线设为0
  13. //define("PUSH_URL",'http://yhq.quyaqu.com/api/v2/MessagePush/rebatepush');//push接口地址,测试
  14. //define("PUSH_URL",'https://tbk.726p.com/api/v2/MessagePush/rebatepush');//push接口地址
  15. define("KEYURL","https://item.taobao.com/item.htm?id=");
  16. /**
  17. * 导入及更新 order/order_rebate数据
  18. */
  19. #设置时区
  20. date_default_timezone_set('PRC');
  21. function readDataFromDb(){
  22. # 1.下载excel
  23. $res = './gua.xlsx';
  24. # 统计
  25. $res_count = array(
  26. 'total_num' => 0,
  27. 'insert_num' => 0,
  28. 'fail_num' => 0,
  29. );
  30. #初始化pdo
  31. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  32. # 读取excel
  33. $templateName = $res;
  34. //实例化Excel读取类
  35. $objReader = new PHPExcel_Reader_Excel2007();
  36. if(!$objReader->canRead($templateName)){
  37. $objReader = new PHPExcel_Reader_Excel5();
  38. if(!$objReader->canRead($templateName)){
  39. echo "\n".'无法识别的Excel文件!';
  40. return false;
  41. }
  42. }
  43. $objPHPExcel=$objReader->load($templateName);
  44. $sheet=$objPHPExcel->getSheet(0);//获取第一个工作表
  45. $highestRow=$sheet->getHighestRow();//取得总行数
  46. $highestColumn=$sheet->getHighestColumn(); //取得总列数
  47. if( strlen($highestColumn) == 2 ){
  48. $highestColumn = 'Z';
  49. }
  50. //循环读取excel文件,读取一条,插入一条
  51. for($j=2;$j<=$highestRow;$j++){//从第二行开始读取数据
  52. $str='';
  53. for($k='A';$k<=$highestColumn;$k++){ //从A列读取数据
  54. //这种方法简单,但有不妥,以'\\'合并为数组,再分割\\为字段值插入到数据库,实测在excel中,如果某单元格的值包含了\\导入的数据会为空
  55. $str.=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'\\';//读取单元格
  56. if( $k=='AE' ){
  57. break;
  58. }
  59. }
  60. //explode:函数把字符串分割为数组。
  61. $orderDetail = explode('\\',trim($str,'\\') );
  62. $res_count['total_num']++;
  63. echo "/ ************************ order{$res_count['total_num']}-Start ************************* / \n";
  64. if( DETAILDEBUG == 1){
  65. echo 'orderDetail:';
  66. // var_dump($orderDetail);
  67. }
  68. $goods_id = $orderDetail[6];
  69. $cate_id = $orderDetail[1];
  70. $cate_id2 = $orderDetail[2];
  71. $keyword = KEYURL.$goods_id;
  72. $result = TBK::search( $keyword );
  73. $title = !empty($result->short_title)? $result->short_title : $orderDetail[7];
  74. $goods_url = $orderDetail[8];
  75. $nick = $orderDetail[17];
  76. $sellerId = $result->seller_id;
  77. $img = !empty($result->white_image)? serialize($result->white_image) : serialize($orderDetail[9]);
  78. $small_img = serialize($result->small_images->string);
  79. $price = $result->zk_final_price;
  80. if(isset($result->coupon_id) && !empty($result->coupon_id)){
  81. preg_match_all('/[\d]+/',$result->coupon_info,$d);
  82. $coupon_price = isset($d[0][2]) ? $d[0][2] : $d[0][1];
  83. $discount_price = ($price*100 - $coupon_price*100)/100;
  84. $discount_price = Number_format($discount_price,2, '.','');
  85. } else {
  86. $coupon_price = "0";
  87. $discount_price = $price;
  88. }
  89. $click_url = $result->url;
  90. $volume = $result->volume;
  91. $commission = $result->commission_rate/100;
  92. $shop_type = $result->user_type;
  93. $shop_title = $result->shop_title;
  94. $coupon_url = $result->coupon_share_url;
  95. $coupon_surplus = $result->coupon_remain_count;
  96. $coupon_total = $result->coupon_total_count;
  97. $coupon_explain = $result->coupon_info;
  98. $coupon_receive = $coupon_total-$coupon_surplus;
  99. $start_time = $result->coupon_start_time;
  100. $end_time = $result->coupon_end_time;
  101. $is_coupon = 1;
  102. $sql = "select * from goods where goods_id = $goods_id ";
  103. $stmt = $_PDO->prepare($sql);
  104. $stmt->execute();
  105. $gResult = $stmt->fetch(PDO::FETCH_ASSOC);
  106. if(empty($gResult)){
  107. $sql = "insert into goods (goods_id,title,goods_url,nick,sellerId,img,small_img,price,discount_price,coupon_price,click_url,volume,commission,shop_type,shop_title,coupon_url,coupon_surplus,coupon_total,coupon_explain,coupon_receive,start_time,end_time,is_coupon) values ('$goods_id','$title','$goods_url','$nick','$sellerId','$img','$small_img','$price','$discount_price','$coupon_price','$click_url','$volume','$commission','$shop_type','$shop_title','$coupon_url','$coupon_surplus','$coupon_total','$coupon_explain','$coupon_receive','$start_time','$end_time','$is_coupon')";
  108. $stmt = $_PDO->prepare($sql);
  109. $stmt->execute();
  110. $res = $_PDO->lastinsertid();
  111. echo "\ngoods:".$res;
  112. }
  113. if($cate_id>3000){
  114. $time = time();
  115. $sql = "insert into group_goods (group_id,goods_id,status,type,create_time) values($cate_id,$goods_id,1,1,$time)";
  116. $stmt = $_PDO->prepare($sql);
  117. $stmt->execute();
  118. $res = $_PDO->lastinsertid();
  119. echo "\ncate1:".$res;
  120. }
  121. if($cate_id2>3000){
  122. $time = time();
  123. $sql = "insert into group_goods (group_id,goods_id,status,type,create_time) values($cate_id2,$goods_id,1,1,$time)";
  124. $stmt = $_PDO->prepare($sql);
  125. $stmt->execute();
  126. $res = $_PDO->lastinsertid();
  127. echo "\ncate2:".$res;
  128. }
  129. echo "/ ***** order{$res_count['total_num']}-End *****/ \n";
  130. }
  131. var_dump($res_count);
  132. }
  133. #返利表订单插入运营商津贴信息
  134. function YysInsertRebate($user_level,$order_balance_at,$order_create_at,$goods_img,$goods_name,$order_id,$userId,$rebaseType,$last_orderSelfRebaseRebase,$sum_price,$orderStatus,$goods_id,$pre_income){
  135. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  136. if( !empty($order_balance_at) ){
  137. $sql="insert into order_rebate_profit (user_level,order_balance_at,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income) VALUES (:user_level,:order_balance_at,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income)";
  138. $stmt = $_PDO->prepare($sql);
  139. $stmt->execute(array(':user_level'=>$user_level,':order_balance_at'=>$order_balance_at,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income));
  140. return $_PDO->lastinsertid();
  141. }
  142. else{
  143. $sql="insert into order_rebate_profit (user_level,order_create_at,img,name,order_id,user_id,type,rebate,money,status,goods_id,pre_income) VALUES (:user_level,:order_create_at,:goods_img,:goods_name,:order_id,:userId,:rebaseType,:last_orderSelfRebaseRebase,:sum_price,:orderStatus,:goods_id,:pre_income)";
  144. $stmt = $_PDO->prepare($sql);
  145. $stmt->execute(array(':user_level'=>$user_level,':order_create_at'=>$order_create_at,':goods_img'=>$goods_img,':goods_name'=>$goods_name,':order_id'=>$order_id,':userId'=>$userId,':rebaseType'=>$rebaseType,':last_orderSelfRebaseRebase'=>$last_orderSelfRebaseRebase,':sum_price'=>$sum_price,':orderStatus'=>$orderStatus,':goods_id'=>$goods_id,':pre_income'=>$pre_income));
  146. return $_PDO->lastinsertid();
  147. }
  148. }
  149. #order订单表返利表同步更新
  150. function generateSqlupdate($status,$order_balance_at,$order_id,$goods_id){
  151. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  152. if( !empty($order_balance_at) ){
  153. $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  154. $stmt = $_PDO->prepare($sql);
  155. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  156. $res = $stmt->rowCount();
  157. //更新order_rebate
  158. $sql="update order_rebate set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  159. $stmt = $_PDO->prepare($sql);
  160. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  161. $res = $stmt->rowCount();
  162. //更新order_rebate_profit
  163. $sql="update order_rebate_profit set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  164. $stmt = $_PDO->prepare($sql);
  165. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  166. $res = $stmt->rowCount();
  167. }
  168. else{
  169. $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  170. $stmt = $_PDO->prepare($sql);
  171. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  172. $res = $stmt->rowCount();
  173. $sql="update order_rebate set status=:status where order_id=:order_id and goods_id=:goods_id ";
  174. $stmt = $_PDO->prepare($sql);
  175. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  176. $res = $stmt->rowCount();
  177. //更新order_rebate_profit
  178. $sql="update order_rebate_profit set status=:status where order_id=:order_id and goods_id=:goods_id ";
  179. $stmt = $_PDO->prepare($sql);
  180. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  181. $res = $stmt->rowCount();
  182. }
  183. return $res;
  184. }
  185. #订单表单独更新
  186. function generateSqlUpdateOther($status,$order_balance_at,$order_id,$goods_id){
  187. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  188. if( !empty($order_balance_at) ){
  189. $sql="update `order` set status=:status,order_balance_at=:order_balance_at where order_id=:order_id and goods_id=:goods_id ";
  190. $stmt = $_PDO->prepare($sql);
  191. $stmt->execute(array(':status'=>$status,':order_balance_at'=>$order_balance_at,':order_id'=>$order_id,':goods_id'=>$goods_id));
  192. $res = $stmt->rowCount();
  193. }
  194. else{
  195. $sql="update `order` set status=:status where order_id=:order_id and goods_id=:goods_id ";
  196. $stmt = $_PDO->prepare($sql);
  197. $stmt->execute(array(':status'=>$status,':order_id'=>$order_id,':goods_id'=>$goods_id));
  198. $res = $stmt->rowCount();
  199. }
  200. return $res;
  201. }
  202. readDataFromDb();