| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- <?php
- namespace Admin\Controller;
- use Think\Page;
- use Think\Cache\Driver\Redis;
- /**
- * 后台默认控制器
- *
- */
- class AutoController extends AdminController
- {
- protected $active_batch;
- /**
- * @return int
- * @ flag 1清空 Code_Active_list 清空二维码激活缓存队列
- * @ flag 2清空 Code_Add_list 清空二维码新增缓存队列
- * @ flag 3清空 fenbiao_page清空分页缓存变量
- * @ flag 4清空 $table_name.'_count' 分表操作缓存列表 无需清空
- *
- */
-
- /*参数的配置 start*/
- protected $tasks_id;
- protected $bach_id;
- protected $product_id;
- protected $printer_id;
- protected $task_name;
- /*参数的配置 stop*/
-
- public function clearS()
- {
- if(empty(I('flag'))){
- echo '请输入操作动作';
- }
- $redis=new Redis();
- $flag=I('flag');
- $status=0;
- switch ($flag){
- case 1:
- $num = $redis->lSize("Code_Active_list");
- for($i=0;$i< $num;$i++){
- $redis->rPop("Code_Active_list");
- }
- $status=1;
- break;
- case 2:
- $num = $redis->lSize("Code_Add_list");
- for($i=0;$i< $num;$i++){
- $redis->rPop("Code_Add_list");
- }
- $status=1;
- break;
- case 3:
- S('fenbiao_page',Null);
- $status=1;
- break;
- default:
- $status=2;
- }
- if($status==1){
- echo '清空成功';
- }
- else{
- echo '清空失败';
- }
- }
-
- /*查款redis里面的数据*/
- public function getS()
- {
- if(empty(I('flag'))){
- echo '请输入操作动作';
- }
- $redis=new Redis();
- $flag=I('flag');
- switch ($flag){
- case 1:
- echo '待更新二维码:'. $redis->lSize("Code_Active_list");
- break;
- case 2:
- echo '待新增二维码:'. $redis->lSize("Code_Add_list");
- break;
- case 3:
- echo '待分表二维码:'. $redis->lSize("fenbiao_page");
- break;
- default:
- $status=2;
- }
- }
-
- public function togetTxtActivefile(){
- /* $redis=new Redis();
- $redis->rm('Code_Active_list');
- echo '待更新二维码:'. $redis->lSize("Code_Active_list");die;*/
- $result = array();
- if(empty(I('active_batch'))){
- $result['status'] = 0;
- $result['cont'] = '激活批次号缺少';
- $this->ajaxReturn($result);
- }
- /*修改激活批次表*/
- $return1 = M('activate_list')-> where('active_batch ='.I('active_batch'))->setField('status',1);
- $return2 = $this->getTxtActivefile(I('active_batch'));
- /*修改激活批次表*/
- if($return1 && $return2){
- $result['status'] = 1;
- $result['cont'] = '任务已提交';
- $this->ajaxReturn($result);
- }
- }
- /*
- * 获取需要激活的二维码
- * 并插入到redis 缓存中去
- * 默认 需要插入的目录Data/active/
- * 激活批次号:active_batch
- */
- public function getTxtActivefile($active_batch){
- /*$redis=new Redis();
- echo '待更新二维码:'. $redis->lSize("Code_Active_list"); die;
- $active_batch = 991156;*/
- set_time_limit(0);
- $this->active_batch = $active_batch;
- $dir="Data/Code_Active_list/".$active_batch.'/0/';
- $files=scandir($dir);
- $txt_file_array=array();
- $redis=new Redis();
- foreach($files as $key ){
- if(get_extension($key)=='txt'){
- //更新缓存
- $this->CodeStatusUpdateCache($key,'Code_Active_list','http://hn.6in7.cn/');
- }
- }
- /*修改表数据*/
- $return = M('activate_list')-> where('active_batch ='.$this->active_batch)->setField('status',0);
- return true;
- //echo '待更新二维码:'. $redis->lSize("Code_Active_list");
- }
-
- public function togetTxtAddfile(){
- $result = array();
- if( empty(I('bach_id')) || empty(I('tasks_id')) || empty(I('product_id')) || empty(I('printer_id')) || empty(I('task_name'))){
- $result['status'] = 0;
- $result['cont'] = '批次参数缺少';
- $this->ajaxReturn($result);
- }else{
- $this->tasks_id = I('tasks_id');
- $this->bach_id = I('bach_id');
- $this->product_id = I('product_id');
- $this->printer_id = I('printer_id');
- $this->task_name = I('task_name');
- }
- /*修改任务表*/
- $return1 = M('task')-> where('id ='.I('tasks_id'))->setField('lock',1);
- $return2 = $this->getTxtAddfile();
- /*修改激活批次表*/
- if($return1 && $return2){
- $result['status'] = 1;
- $result['cont'] = '任务已提交';
- $this->ajaxReturn($result);
- }
- }
- /*
- * 该函数需要后期后端操作,一个文件要对应 任务id,产品id,批次号等绑定
- * 获取需要新增的二维码
- * 并插入到redis 缓存中去
- * 默认 需要插入的目录Data/add/
- *
- */
- public function getTxtAddfile(){
- $dir="Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/0/';
- $files=scandir($dir);
- $txt_file_array=array();
- $redis=new Redis();
- foreach($files as $key ){
- if(get_extension($key)=='txt'){
- //更新缓存
- $this->CodeStatusUpdateCache($key,'Code_Add_list','http://hn.6in7.cn/');
- }
- }
- $return = M('task')-> where('id ='.I('tasks_id'))->setField('lock',0);
- return true;
- }
- /*
- * 二维码插入
- * redis主键 Code_Add_list
- * Code_Add_list每一条数据为serialize 数据,
- * 平均单条插别人耗时间:0.0045s,单次插入数量:5000;预计耗时:22.5s~30s
- * 一分钟访问两次 执行时间在30s~60s
- * 一分钟处理1w条数据,100w 平均耗时 100分钟=1 h 40 m
- */
- public function addCode(){
- $redis=new Redis();
- if(!$redis->lSize("Code_Add_list")){
- echo '没有需要插入的二维码数据';
- }
- $i=0;$j=0;
- // $CodeMode->startTrans();
- w_log('Data/Add_log.txt',time()." : 插入开始");
- $t1 = microtime(true);
- //echo $redis->lSize("Code_Add_list").'<br/>';
- //die;
- for($kk=5000;$kk>0 && $redis->lSize("Code_Add_list")>0;$kk--){
- $where=array();
- $where=unserialize($redis->rPop("Code_Add_list")) ;
- $where['add_time']=time();
- $where['update_time']=time();
- $where['varify_code']='';
- if(strpos($where['code'], ',')){
- $temp_code=explode(',', $where['code']);
- $where['code']=current($temp_code);
- $where['varify_code']=end($temp_code);
- }
- $task_name = $where['task_name'];
- $tasks_id = $where['tasks_id'];
- $file = $where['file'];
- unset($where['file']);
- unset($where['task_name']);
- $where_str='';
- $field_str='';
- foreach ( $where as $key=>$value){
- if($key=='varify_code'){
- $field_str.="`".$key."`";
- $where_str.="'".$value."'";
- }else{
- $field_str.="`".$key."`,";
- $where_str.="'".$value."',";
- }
- }
- $table_name= getSubTable('codes',$where['code']);
- $table_name = "INSERT ignore INTO `$table_name` ($field_str) VALUES ($where_str)";
- $Model = M();
- $status = $Model->execute($table_name);
- if($status)
- {
- $i++;
- M('task')->where('id = '.$tasks_id)->setInc('checked_num',1);
- }
- else{
- //插入错误日志
- $j++;
- $dir = "Data/Code_Add_list/".$task_name.'/admin/'.$task_name.'_'.$tasks_id.'/2/';
- if(!file_exists($dir)){
- //判断文件是否存在
- mkdir($dir, 0777, true);
- }
- w_log($dir.$file,time().':'.$where['code'].$where['varify_code'].'添加失败');
- /*错误记录表 +1*/
- M('task')->where('id = '.$tasks_id)->setInc('wrong_num',1);
- }
- }
- $t2 = microtime(true);
- echo '耗时'.round($t2-$t1,3).'秒<br>';
- echo '成功插入:'.$i.'条数据,失败:'.$j.'条数据<br>';
- }
- /*
- * 二维码更新状态变更
- * redis主键 Code_Active_list
- * 平均单条插别人耗时间:0.0045s,单次插入数量:5000;预计耗时:22.5s~30s
- * 一分钟访问两次 执行时间在30s~60s
- * 一分钟处理1w条数据,100w 平均耗时 100分钟=1 h 40 m
- */
- public function activeCode(){
-
- $redis=new Redis();
- if(!$redis->lSize("Code_Active_list")){
- echo '没有需要更新的二维码数据';
- return 'ok';
- }
- w_log('Data/Active_log.txt',time()."更新开始");
- $t1 = microtime(true);
- $i=0;$j=0;
- $time = time();
- for($kk=5000;$kk>0;$kk--){
- $where=array();
- $Active_data=unserialize($redis->rPop("Code_Active_list"));
- $where['code']=$Active_data['code'];
- if(strpos($where['code'], ',')){
- $where['code']=current(explode(',', $where['code']));
- }
- $where['Status']=0;
- $data=array();
- $data['Status']=1;
- $data['active_batch']=$Active_data['active_batch'];
- $data['update_time']= $time;
- $data['active_time']= $time;
- if($data['active_batch']){
- $table_name= getSubTable('codes',$where['code'],1);
- $CodeMode= M($table_name);
-
- $status=$CodeMode->where($where)->save($data);
- if($status)
- {
- $i++;
- //修改激活批次表
- M('activate_list')->where('active_batch = '.$data['active_batch'])->setInc('succ',1);
- }
- else{
- //0代表码错误,1代表重复激活,2代表系统错误
- $j++;
- //写日志
- $insert_data = array();
- $insert_data['code'] = $where['code'];
- $insert_data['active_batch'] = $data['active_batch'];
- $insert_data['time'] = $time;
- $return1 = $CodeMode->field('id')->where('code = '.'"'.$where['code'].'"')->select();
- if(count($return1)>0){
- $return2 = $CodeMode->field('id')->where($where)->find();
- if(count($return2)>0){
- $insert_data['error'] = 2;
- }else{
- $insert_data['error'] = 1;
- }
- }else{
- $insert_data['error'] = 0;
- }
- if( M('activate_list_log')->add($insert_data)){
- M('activate_list')->where('active_batch = '.$data['active_batch'])->setInc('fail',1);
- }
- //判断开始
- //w_log('Data/Active_wrong_'.$data['active_batch'].'_log.txt',time().':'.$where['code'].'更新失败');
- }
- }else{
- $j++;
- w_log('Data/Active_wrong_log.txt',time().':'.$where['code'].'更新失败,激活批次为空');
- }
- }
- $t2 = microtime(true);
- echo '耗时'.round($t2-$t1,3).'秒<br>';
- echo '更新成功:'.$i.'条数据,失败:'.$j.'条数据<br>';
- echo '还剩'.$redis->lSize("Code_Active_list").'条数据待更新<br/>';
- w_log('Data/Active_log.txt',time().'更新结束 共插入 条数据');
- }
- /*
- * redis 操作
- * 把需要更新的二维码
- * push 到 Code_Active_list主键
- * & redis_key redis主键
- * 二维码中包含的主域名
- */
- protected function CodeStatusUpdateCache($file,$redis_key='Code_Active_list',$host='http://hn.6in7.cn/'){
- if($redis_key=='Code_Active_list'){
- $dir = 'Data/'.$redis_key.'/'.$this->active_batch.'/0/'.$file;
- $path = 'Data/'.$redis_key.'/'.$this->active_batch.'/1/'.$file;
- }else{
- $dir = "Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/0/'.$file;
- $path = "Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/1/'.$file;
- }
- $cont= file_get_contents(iconv("utf-8","gbk",$dir));
- $cont = preg_replace('/\n|\r\n/','*',$cont);
- $cont =str_replace($host,"",$cont);
- $data = explode("*",$cont);
- $data = array_filter($data);
- $total = count($data);
- //写入日志
- w_log('Data/redis_log.txt',time().":".$file."文件插入Code_Active_list 队列中");
- $redis=new Redis();
- $i=0;
- foreach($data as $k=>$v){
- /*
- * 如果是插入需要对数据重组
- */
- if($redis_key=='Code_Add_list'){
- /*相关的参数 start*/
- $Insert_data['code']=$v;
- $Insert_data['bach_id']=$this->bach_id;
- $Insert_data['tasks_id']=$this->tasks_id;
- $Insert_data['product_id']=$this->product_id;
- $Insert_data['printer_id']=$this->printer_id;
- $Insert_data['file'] = $file;
- $Insert_data['task_name'] = $this->task_name;
- $Insert_data['Status']=0;
- /*相关的参数 stop*/
- $redis->lpush($redis_key,serialize($Insert_data));
- }else if($redis_key=='Code_Active_list'){
- $Active_data['code']=$v;
- $Active_data['active_batch']=$this->active_batch;
- $redis->lpush($redis_key,serialize($Active_data));
- }
- else{
- $redis->lpush($redis_key,$v);
- }
- $i++;
- }
- copy($dir, $path);
- unlink($dir);
- w_log('Data/redis_log.txt',time().":".$file."共有数据:".$total.',已插入数据:'.$i.',{$redis_key} 目前共有数据:'.$redis->lSize($redis_key));
- }
- /*
- * 获取分表表名
- */
-
- }
|