优惠券订单及其他脚本

sc_user_condition.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. require_once 'DB_PDO.class.php';
  3. require_once 'TBK.class.php';
  4. require_once 'conf.class.php';
  5. define("DETAILDEBUG", 0);//打印流程详细信息,如有需要,设为1
  6. define("LIMIT_NUM", 0);//限制导入条数,0不限制,方便测试,上线设为0
  7. /**
  8. * 快手网红数据筛选条件中间表
  9. */
  10. #设置时区
  11. date_default_timezone_set('PRC');
  12. set_time_limit(0);
  13. ini_set('memory_limit','1024M');
  14. # 记录脚本开始时间
  15. $scriptStart = date("Y-m-d H:i:s");
  16. $sql = "SELECT user_id,photo from sc_kwai_users where user_id>0 and fan>=10000 and fan<50000 and photo>0 order by id desc ";
  17. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  18. $stmt = $_PDO->prepare($sql);
  19. $stmt->execute();
  20. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  21. echo "\n条数:".count($res);
  22. $arr = array();
  23. $str = '';
  24. $nu = 0;
  25. $up = 0;
  26. foreach($res as $k=>$v){
  27. $sql = "select avg(view_count) as avg_view_count, avg(like_count) as avg_like_count, avg(comment_count) as avg_comment_count from sc_kwai_video where user_id= {$v['user_id']}" ;
  28. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  29. $stmt = $_PDO->prepare($sql);
  30. $stmt->execute();
  31. $ret = $stmt->fetch(PDO::FETCH_ASSOC);
  32. $v['photo'] = (int)$v['photo'];
  33. if(!empty($ret)){
  34. $view_ave = (int)$ret['avg_view_count'];
  35. $like_ave = (int)$ret['avg_like_count'];
  36. $comment_ave = (int)$ret['avg_comment_count'];
  37. $view_count = $ret['avg_view_count'] * $v['photo'];
  38. $like_count = $ret['avg_like_count'] * $v['photo'];
  39. $comment_count = $ret['avg_comment_count'] * $v['photo'];
  40. }else{
  41. $view_ave = 0;
  42. $like_ave = 0;
  43. $comment_ave = 0;
  44. $view_count = 0;
  45. $like_count = 0;
  46. $comment_count = 0;
  47. }
  48. $sql = "select distinct scc.author_id,su.age,su.birthdayTs,su.cityName,scc.user_sex from sc_kwai_video_comment scc left join sc_kwai_users su on su.user_id=scc.author_id where scc.user_id = {$v['user_id']}";
  49. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  50. $stmt = $_PDO->prepare($sql);
  51. $stmt->execute();
  52. $fanlist = $stmt->fetchAll(PDO::FETCH_ASSOC);
  53. $age_t = array();
  54. $city_t = array();
  55. $sex_t = array();
  56. $age_arr = array();
  57. foreach($fanlist as $fan){
  58. if(empty($fan['age']) && !empty($fan['birthdayTs'])){
  59. $fan['age'] = getTrueAge($fan['birthdayTs']);
  60. }
  61. if($fan['age']>0) $age_arr[] = (int)$fan['age'];
  62. $age_key = getage($fan['age']);
  63. if( $age_key>0) { if(isset($age_t[$age_key])){ $age_t[$age_key]++;}else{ $age_t[$age_key] = 1;} }
  64. $city_name_arr = explode(' ', $fan['cityName']);
  65. $city_key= isset($city_name_arr[0]) ? $city_name_arr[0] : null;
  66. if(!empty($city_key)){ if(isset($city_t[$city_key])){ $city_t[$city_key]++;}else{ $city_t[$city_key] = 1;} }
  67. $sex_key = $fan['user_sex'];
  68. if(in_array($sex_key, ['F','M'])){ if(isset($sex_t[$sex_key])){ $sex_t[$sex_key]++;}else{ $sex_t[$sex_key] = 1;} }
  69. }
  70. arsort($age_t);
  71. arsort($city_t);
  72. arsort($sex_t);
  73. reset($age_t);
  74. reset($city_t);
  75. reset($sex_t);
  76. $fan_sex = key($sex_t);
  77. $fan_age = (int)key($age_t);
  78. $fan_city = key($city_t);
  79. #平均年龄
  80. if(!empty($age_arr)){ $fan_age_ave = round( array_sum($age_arr) / count($age_arr) ); }else{ $fan_age_ave =0; }
  81. #推广品类/产品价格范围
  82. $root_category_id = '';
  83. $category_ids = '';
  84. $price_type = 0;
  85. $sql = "select root_category_id, category_id, price from sc_kwai_onlive_commodity where kwai_user_id={$v['user_id']} order by sell_count desc limit 15";
  86. $stmt = $_PDO->prepare($sql);
  87. $stmt->execute();
  88. $goods_info = $stmt->fetchAll(PDO::FETCH_ASSOC);
  89. $goods_arr = array();
  90. $goods_info_new = array();
  91. if(!empty($goods_info)){
  92. foreach($goods_info as $good){
  93. $category_id = $good['category_id'];
  94. if(isset($goods_arr[$category_id])){
  95. $goods_arr[$category_id] ++;
  96. }else{
  97. $goods_arr[$category_id] = 1;
  98. }
  99. $goods_info_new[$category_id] = $good;
  100. }
  101. arsort($goods_arr);
  102. reset($goods_arr);
  103. $cate_key = key($goods_arr);
  104. $root_category_id = $goods_info_new[$cate_key]['root_category_id'];
  105. $category_ids = $goods_info_new[$cate_key]['category_id'];
  106. $price = $goods_info_new[$cate_key]['price'];
  107. $price_type = price_type($price);
  108. }
  109. #是否已存在
  110. $sql = "select id from sc_users_condition where user_id='".$v['user_id']."' limit 1";
  111. $stmt = $_PDO->prepare($sql);
  112. $stmt->execute();
  113. $if_exists = $stmt->fetch(PDO::FETCH_ASSOC);
  114. if(!empty($if_exists)){
  115. #已存在更新操作
  116. $sql = "update sc_users_condition set view_count={$view_count},like_count={$like_count},comment_count={$comment_count},view_ave={$view_ave},like_ave={$like_ave},comment_ave={$comment_ave},fan_cityName='{$fan_city}',fan_sex='{$fan_sex}',fan_age_limits={$fan_age},photo={$v['photo']},fan_age_ave={$fan_age_ave}, root_category_id='{$root_category_id}',category_ids='{$category_ids}',price_type='{$price_type}' where user_id = '".$v['user_id']."'";
  117. $stmt = $_PDO->prepare($sql);
  118. $stmt->execute();
  119. $upres = $stmt->rowCount();
  120. if($upres) $up++;
  121. }else{
  122. $sql = "insert into sc_users_condition (user_id, view_count, like_count, comment_count, view_ave, like_ave, comment_ave, fan_cityName, fan_sex, fan_age_limits, photo, fan_age_ave, root_category_id, category_ids, price_type) values({$v['user_id']}, {$view_count}, {$like_count}, {$comment_count}, {$view_ave}, {$like_ave}, {$comment_ave}, '{$fan_city}','{$fan_sex}',{$fan_age},{$v['photo']},{$fan_age_ave},'{$root_category_id}','{$category_ids}',{$price_type} ) ";
  123. $stmt = $_PDO->prepare($sql);
  124. $stmt->execute();
  125. $res_in = $_PDO->lastinsertid();
  126. if($res_in) $nu++;
  127. }
  128. }
  129. echo "\n成功更新:".$up;
  130. echo "\n成功新增:".$nu;
  131. function getsign($name){
  132. $sql = 'select id,parent_id from sc_web_signs where name = "'.$name.'"';
  133. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  134. $stmt = $_PDO->prepare($sql);
  135. $stmt->execute();
  136. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  137. if(empty($res)){
  138. return 20;
  139. }elseif($res['parent_id'] == 0){
  140. return $res['id'];
  141. }else{
  142. return $res['parent_id'];
  143. }
  144. }
  145. function getage($age){
  146. if($age>=6 && $age<=17){
  147. return 1;
  148. }elseif($age>17 && $age<=24){
  149. return 2;
  150. }elseif($age>24 && $age<=30){
  151. return 3;
  152. }elseif($age>30 && $age<=35){
  153. return 4;
  154. }elseif($age>35 && $age<=40){
  155. return 5;
  156. }elseif($age>40 ){
  157. return 6;
  158. }
  159. return 0;
  160. }
  161. function getTrueAge($age){
  162. if($age === false){
  163. return false;
  164. }
  165. list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));
  166. $now = strtotime("now");
  167. list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));
  168. $age = $y2 - $y1;
  169. if((int)($m2.$d2) < (int)($m1.$d1)) $age -= 1;
  170. return $age;
  171. }
  172. //1:10元一下;2:10-50;3:50-100;4:100-500;5:500-1000;6:1000-5000;7:5000+
  173. function price_type($price){
  174. $type = 0;
  175. if($price>=5000){
  176. $type = 7;
  177. }elseif($price>=1000){
  178. $type = 6;
  179. }elseif($price>=500){
  180. $type = 5;
  181. }elseif($price>=100){
  182. $type = 4;
  183. }elseif($price>=50){
  184. $type = 3;
  185. }elseif($price>=10){
  186. $type = 2;
  187. }elseif($price>=0){
  188. $type = 1;
  189. }
  190. return $type;
  191. }
  192. echo "\n结果:".trim($str, ',');