AutoController.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Page;
  4. use Think\Cache\Driver\Redis;
  5. /**
  6. * 后台默认控制器
  7. *
  8. */
  9. class AutoController extends \Think\Controller
  10. {
  11. protected $active_batch;
  12. /**
  13. * @return int
  14. * @ flag 1清空 Code_Active_list 清空二维码激活缓存队列
  15. * @ flag 2清空 Code_Add_list 清空二维码新增缓存队列
  16. * @ flag 3清空 fenbiao_page清空分页缓存变量
  17. * @ flag 4清空 $table_name.'_count' 分表操作缓存列表 无需清空
  18. *
  19. */
  20. /*参数的配置 start*/
  21. protected $tasks_id;
  22. protected $bach_id;
  23. protected $product_id;
  24. protected $printer_id;
  25. protected $task_name;
  26. /*参数的配置 stop*/
  27. public function clearS()
  28. {
  29. if(empty(I('flag'))){
  30. echo '请输入操作动作';
  31. }
  32. $redis=new Redis();
  33. $flag=I('flag');
  34. $status=0;
  35. switch ($flag){
  36. case 1:
  37. $num = $redis->lSize("Code_Active_list");
  38. for($i=0;$i< $num;$i++){
  39. $redis->rPop("Code_Active_list");
  40. }
  41. $status=1;
  42. break;
  43. case 2:
  44. $num = $redis->lSize("Code_Add_list");
  45. for($i=0;$i< $num;$i++){
  46. $redis->rPop("Code_Add_list");
  47. }
  48. $status=1;
  49. break;
  50. case 3:
  51. S('fenbiao_page',Null);
  52. $status=1;
  53. break;
  54. default:
  55. $status=2;
  56. }
  57. if($status==1){
  58. echo '清空成功';
  59. }
  60. else{
  61. echo '清空失败';
  62. }
  63. }
  64. /*查款redis里面的数据*/
  65. public function getS()
  66. {
  67. if(empty(I('flag'))){
  68. echo '请输入操作动作';
  69. }
  70. $redis=new Redis();
  71. $flag=I('flag');
  72. switch ($flag){
  73. case 1:
  74. echo '待更新二维码:'. $redis->lSize("Code_Active_list");
  75. break;
  76. case 2:
  77. echo '待新增二维码:'. $redis->lSize("Code_Add_list");
  78. break;
  79. case 3:
  80. echo '待分表二维码:'. $redis->lSize("fenbiao_page");
  81. break;
  82. default:
  83. $status=2;
  84. }
  85. }
  86. public function togetTxtActivefile(){
  87. /* $redis=new Redis();
  88. $redis->rm('Code_Active_list');
  89. echo '待更新二维码:'. $redis->lSize("Code_Active_list");die;*/
  90. $result = array();
  91. if(empty(I('active_batch'))){
  92. $result['status'] = 0;
  93. $result['cont'] = '激活批次号缺少';
  94. $this->ajaxReturn($result);
  95. }
  96. /*修改激活批次表*/
  97. $return1 = M('activate_list')-> where('active_batch ='.I('active_batch'))->setField('status',1);
  98. $return2 = $this->getTxtActivefile(I('active_batch'));
  99. /*修改激活批次表*/
  100. if($return1 && $return2){
  101. $result['status'] = 1;
  102. $result['cont'] = '任务已提交';
  103. $this->ajaxReturn($result);
  104. }
  105. }
  106. /*
  107. * 获取需要激活的二维码
  108. * 并插入到redis 缓存中去
  109. * 默认 需要插入的目录Data/active/
  110. * 激活批次号:active_batch
  111. */
  112. public function getTxtActivefile($active_batch){
  113. /*$redis=new Redis();
  114. echo '待更新二维码:'. $redis->lSize("Code_Active_list"); die;
  115. $active_batch = 991156;*/
  116. $this->active_batch = $active_batch;
  117. $dir="Data/Code_Active_list/".$active_batch.'/0/';
  118. $files=scandir($dir);
  119. $txt_file_array=array();
  120. $redis=new Redis();
  121. foreach($files as $key ){
  122. if(get_extension($key)=='txt'){
  123. //更新缓存
  124. $this->CodeStatusUpdateCache($key,'Code_Active_list','http://hn.6in7.cn/');
  125. }
  126. }
  127. /*修改表数据*/
  128. $return = M('activate_list')-> where('active_batch ='.$this->active_batch)->setField('status',0);
  129. return true;
  130. //echo '待更新二维码:'. $redis->lSize("Code_Active_list");
  131. }
  132. public function togetTxtAddfile(){
  133. $result = array();
  134. if( empty(I('bach_id')) || empty(I('tasks_id')) || empty(I('product_id')) || empty(I('printer_id')) || empty(I('task_name'))){
  135. $result['status'] = 0;
  136. $result['cont'] = '批次参数缺少';
  137. $this->ajaxReturn($result);
  138. }else{
  139. $this->tasks_id = I('tasks_id');
  140. $this->bach_id = I('bach_id');
  141. $this->product_id = I('product_id');
  142. $this->printer_id = I('printer_id');
  143. $this->task_name = I('task_name');
  144. }
  145. /*修改任务表*/
  146. $return1 = M('task')-> where('id ='.I('tasks_id'))->setField('lock',1);
  147. $return2 = $this->getTxtAddfile();
  148. /*修改激活批次表*/
  149. if($return1 && $return2){
  150. $result['status'] = 1;
  151. $result['cont'] = '任务已提交';
  152. $this->ajaxReturn($result);
  153. }
  154. }
  155. /*
  156. * 该函数需要后期后端操作,一个文件要对应 任务id,产品id,批次号等绑定
  157. * 获取需要新增的二维码
  158. * 并插入到redis 缓存中去
  159. * 默认 需要插入的目录Data/add/
  160. *
  161. */
  162. public function getTxtAddfile(){
  163. $dir="Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/0/';
  164. $files=scandir($dir);
  165. $txt_file_array=array();
  166. $redis=new Redis();
  167. foreach($files as $key ){
  168. if(get_extension($key)=='txt'){
  169. //更新缓存
  170. $this->CodeStatusUpdateCache($key,'Code_Add_list','http://hn.6in7.cn/');
  171. }
  172. }
  173. $return = M('task')-> where('id ='.I('tasks_id'))->setField('lock',0);
  174. return true;
  175. }
  176. /*
  177. * 二维码插入
  178. * redis主键 Code_Add_list
  179. * Code_Add_list每一条数据为serialize 数据,
  180. * 平均单条插别人耗时间:0.0045s,单次插入数量:5000;预计耗时:22.5s~30s
  181. * 一分钟访问两次 执行时间在30s~60s
  182. * 一分钟处理1w条数据,100w 平均耗时 100分钟=1 h 40 m
  183. */
  184. public function addCode(){
  185. $redis=new Redis();
  186. if(!$redis->lSize("Code_Add_list")){
  187. echo '没有需要插入的二维码数据';
  188. }
  189. $i=0;$j=0;
  190. // $CodeMode->startTrans();
  191. w_log('Data/Add_log.txt',time()." : 插入开始");
  192. $t1 = microtime(true);
  193. //echo $redis->lSize("Code_Add_list").'<br/>';
  194. //die;
  195. for($kk=5000;$kk>0 && $redis->lSize("Code_Add_list")>0;$kk--){
  196. $where=array();
  197. $where=unserialize($redis->rPop("Code_Add_list")) ;
  198. $where['add_time']=time();
  199. $where['update_time']=time();
  200. $where['varify_code']='';
  201. if(strpos($where['code'], ',')){
  202. $temp_code=explode(',', $where['code']);
  203. $where['code']=current($temp_code);
  204. $where['varify_code']=end($temp_code);
  205. }
  206. $task_name = $where['task_name'];
  207. $tasks_id = $where['tasks_id'];
  208. $file = $where['file'];
  209. unset($where['file']);
  210. unset($where['task_name']);
  211. $where_str='';
  212. $field_str='';
  213. foreach ( $where as $key=>$value){
  214. if($key=='varify_code'){
  215. $field_str.="`".$key."`";
  216. $where_str.="'".$value."'";
  217. }else{
  218. $field_str.="`".$key."`,";
  219. $where_str.="'".$value."',";
  220. }
  221. }
  222. $table_name= getSubTable('codes',$where['code']);
  223. $table_name = "INSERT ignore INTO `$table_name` ($field_str) VALUES ($where_str)";
  224. $Model = M();
  225. $status = $Model->execute($table_name);
  226. if($status)
  227. {
  228. $i++;
  229. M('task')->where('id = '.$tasks_id)->setInc('checked_num',1);
  230. }
  231. else{
  232. //插入错误日志
  233. $j++;
  234. $dir = "Data/Code_Add_list/".$task_name.'/admin/'.$task_name.'_'.$tasks_id.'/2/';
  235. if(!file_exists($dir)){
  236. //判断文件是否存在
  237. mkdir($dir, 0777, true);
  238. }
  239. w_log($dir.$file,time().':'.$where['code'].$where['varify_code'].'添加失败');
  240. /*错误记录表 +1*/
  241. M('task')->where('id = '.$tasks_id)->setInc('wrong_num',1);
  242. }
  243. }
  244. $t2 = microtime(true);
  245. echo '耗时'.round($t2-$t1,3).'秒<br>';
  246. echo '成功插入:'.$i.'条数据,失败:'.$j.'条数据<br>';
  247. }
  248. /*
  249. * 二维码更新状态变更
  250. * redis主键 Code_Active_list
  251. * 平均单条插别人耗时间:0.0045s,单次插入数量:5000;预计耗时:22.5s~30s
  252. * 一分钟访问两次 执行时间在30s~60s
  253. * 一分钟处理1w条数据,100w 平均耗时 100分钟=1 h 40 m
  254. */
  255. public function activeCode(){
  256. $redis=new Redis();
  257. if(!$redis->lSize("Code_Active_list")){
  258. echo '没有需要更新的二维码数据';
  259. return 'ok';
  260. }
  261. w_log('Data/Active_log.txt',time()."更新开始");
  262. $t1 = microtime(true);
  263. $i=0;$j=0;
  264. $time = time();
  265. for($kk=5000;$kk>0;$kk--){
  266. $where=array();
  267. $Active_data=unserialize($redis->rPop("Code_Active_list"));
  268. $where['code']=$Active_data['code'];
  269. if(strpos($where['code'], ',')){
  270. $where['code']=current(explode(',', $where['code']));
  271. }
  272. $where['Status']=0;
  273. $data=array();
  274. $data['Status']=1;
  275. $data['active_batch']=$Active_data['active_batch'];
  276. $data['update_time']= $time;
  277. $data['active_time']= $time;
  278. if($data['active_batch']){
  279. $table_name= getSubTable('codes',$where['code'],1);
  280. $CodeMode= M($table_name);
  281. $status=$CodeMode->where($where)->save($data);
  282. if($status)
  283. {
  284. $i++;
  285. //修改激活批次表
  286. M('activate_list')->where('active_batch = '.$data['active_batch'])->setInc('succ',1);
  287. }
  288. else{
  289. //0代表码错误,1代表重复激活,2代表系统错误
  290. $j++;
  291. //写日志
  292. $insert_data = array();
  293. $insert_data['code'] = $where['code'];
  294. $insert_data['active_batch'] = $data['active_batch'];
  295. $insert_data['time'] = $time;
  296. $return1 = $CodeMode->field('id')->where('code = '.'"'.$where['code'].'"')->select();
  297. if(count($return1)>0){
  298. $return2 = $CodeMode->field('id')->where($where)->find();
  299. if(count($return2)>0){
  300. $insert_data['error'] = 2;
  301. }else{
  302. $insert_data['error'] = 1;
  303. }
  304. }else{
  305. $insert_data['error'] = 0;
  306. }
  307. if( M('activate_list_log')->add($insert_data)){
  308. M('activate_list')->where('active_batch = '.$data['active_batch'])->setInc('fail',1);
  309. }
  310. //判断开始
  311. //w_log('Data/Active_wrong_'.$data['active_batch'].'_log.txt',time().':'.$where['code'].'更新失败');
  312. }
  313. }else{
  314. $j++;
  315. w_log('Data/Active_wrong_log.txt',time().':'.$where['code'].'更新失败,激活批次为空');
  316. }
  317. }
  318. $t2 = microtime(true);
  319. echo '耗时'.round($t2-$t1,3).'秒<br>';
  320. echo '更新成功:'.$i.'条数据,失败:'.$j.'条数据<br>';
  321. echo '还剩'.$redis->lSize("Code_Active_list").'条数据待更新<br/>';
  322. w_log('Data/Active_log.txt',time().'更新结束 共插入 条数据');
  323. }
  324. /*
  325. * redis 操作
  326. * 把需要更新的二维码
  327. * push 到 Code_Active_list主键
  328. * & redis_key redis主键
  329. * 二维码中包含的主域名
  330. */
  331. protected function CodeStatusUpdateCache($file,$redis_key='Code_Active_list',$host='http://hn.6in7.cn/'){
  332. if($redis_key=='Code_Active_list'){
  333. $dir = 'Data/'.$redis_key.'/'.$this->active_batch.'/0/'.$file;
  334. $path = 'Data/'.$redis_key.'/'.$this->active_batch.'/1/'.$file;
  335. }else{
  336. $dir = "Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/0/'.$file;
  337. $path = "Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/1/'.$file;
  338. }
  339. $cont= file_get_contents(iconv("utf-8","gbk",$dir));
  340. $cont = preg_replace('/\n|\r\n/','*',$cont);
  341. $cont =str_replace($host,"",$cont);
  342. $data = explode("*",$cont);
  343. $data = array_filter($data);
  344. $total = count($data);
  345. //写入日志
  346. w_log('Data/redis_log.txt',time().":".$file."文件插入Code_Active_list 队列中");
  347. $redis=new Redis();
  348. $i=0;
  349. foreach($data as $k=>$v){
  350. /*
  351. * 如果是插入需要对数据重组
  352. */
  353. if($redis_key=='Code_Add_list'){
  354. /*相关的参数 start*/
  355. $Insert_data['code']=$v;
  356. $Insert_data['bach_id']=$this->bach_id;
  357. $Insert_data['tasks_id']=$this->tasks_id;
  358. $Insert_data['product_id']=$this->product_id;
  359. $Insert_data['printer_id']=$this->printer_id;
  360. $Insert_data['file'] = $file;
  361. $Insert_data['task_name'] = $this->task_name;
  362. $Insert_data['Status']=0;
  363. /*相关的参数 stop*/
  364. $redis->lpush($redis_key,serialize($Insert_data));
  365. }else if($redis_key=='Code_Active_list'){
  366. $Active_data['code']=$v;
  367. $Active_data['active_batch']=$this->active_batch;
  368. $redis->lpush($redis_key,serialize($Active_data));
  369. }
  370. else{
  371. $redis->lpush($redis_key,$v);
  372. }
  373. $i++;
  374. }
  375. copy($dir, $path);
  376. unlink($dir);
  377. w_log('Data/redis_log.txt',time().":".$file."共有数据:".$total.',已插入数据:'.$i.',{$redis_key} 目前共有数据:'.$redis->lSize($redis_key));
  378. }
  379. /*
  380. * 获取分表表名
  381. */
  382. }