ActivateController.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * author jade
  4. * intro 激活类
  5. */
  6. namespace Admin\Controller;
  7. class activateController extends AdminController
  8. {
  9. protected $config;
  10. /**
  11. * author jade
  12. * intro 类初始化方法
  13. * param
  14. * return
  15. */
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->config=D('Config');
  20. }
  21. /**
  22. * author xtj
  23. * intro 上传txt码包方法
  24. * param $group 调用系统配置项目
  25. * return bool
  26. */
  27. public function index($batch_id)
  28. {
  29. $file_log = M('file_log');
  30. ini_set('memory_limit', '2048M');
  31. set_time_limit(0);
  32. /*获取文件*/
  33. //$batch_id = 2;
  34. $where = array();
  35. $where['batch_id'] = $batch_id;
  36. $where['creator'] = 'admin';
  37. $file = $file_log->where($where)->getField('path',true);
  38. $printer_id = M('bach')->where('id = '.$batch_id)->getField('printer_id');
  39. $cont = '';
  40. $cont1 = '';
  41. $where = array();
  42. $where['batch_id'] = array('eq',$batch_id);
  43. $where['creator'] = array('neq','admin');
  44. $where['status'] = array('eq',0);
  45. $file1 = $file_log->field('id,path')->where($where)->select();
  46. if(count($file1)>0){
  47. foreach($file as $v){
  48. $fp = fopen(iconv("utf-8","gbk",$v), "r");
  49. $cont .= fread($fp,filesize(iconv("utf-8","gbk",$v)));
  50. $cont .="\n";
  51. $cont = preg_replace('/\n|\r\n/','*',$cont);
  52. }
  53. $data = array_filter(explode("*",$cont));
  54. foreach($file1 as $v){
  55. $name = explode('/',$v['path']);
  56. $name = explode('_',basename(array_pop($name),'.txt'));
  57. $tasks_id = $name[2] ;
  58. //查询任务对应的产品id
  59. $where= array();
  60. $where['id'] = $tasks_id ;
  61. $product = M('task')->field('product_id,is_verif')->where($where)->find();
  62. $product_id = $product['product_id'];
  63. $is_verif = $product['is_verif'];
  64. $cont1 = '';
  65. $cont1 .= file_get_contents(iconv("utf-8","gbk",$v['path']));
  66. $cont1 = preg_replace('/\n|\r\n/','*',$cont1);
  67. $data1 = explode("*",$cont1);
  68. $data1 = array_filter($data1);
  69. $total = count($data1);
  70. unset($cont1);
  71. $result=array_intersect($data,$data1);
  72. unset($data1);
  73. $this->add_data($result,$total,$batch_id,$tasks_id,$product_id,$printer_id,$v['id'],$is_verif);
  74. }
  75. }
  76. //批量修改task表
  77. $where= array();
  78. $where['bach_id'] = $batch_id ;
  79. $where['status'] = 2;
  80. $task_id = M('task')->where($where)->getField('id',true);
  81. foreach($task_id as $v){
  82. $where= array();
  83. $where['creator'] = array('neq','admin');
  84. $where['task_id'] = $v;
  85. $success_num = $file_log -> where($where)->sum(success_num);
  86. $error_num = $file_log -> where($where)->sum('error_num');
  87. if($success_num != ''){
  88. M()->execute('update `qr_task` set checked_num = '.$success_num.', wrong_num= '.$error_num.', status=3 '.'where id='.$v );
  89. }
  90. }
  91. }
  92. public function add_data($result,$total,$bach_id,$tasks_id,$product_id,$printer_id,$id,$is_verif){
  93. $data_new = array_chunk($result,10000);
  94. $i = count($data_new);
  95. if($i>0){
  96. $j = 0;
  97. $num = 0;
  98. for($j=0;$j<$i;$j++){
  99. $res = $this->data($data_new[$j],$bach_id,$tasks_id,$product_id,$printer_id,$is_verif);
  100. $num+=$res;
  101. }
  102. $succ = $num;
  103. $error = $total-$succ;
  104. //修改日志表
  105. M()->execute('update `qr_file_log` set total='.$total.', success_num='.$succ.',error_num='.$error.', status = 1 where id= '.$id);
  106. unset($data_new);
  107. }else{
  108. //直接修改file_log表
  109. M()->execute('update `qr_file_log` set total = '.$total.', success_num= 0 , error_num='.$total.', status = 1 where id='.$id );
  110. }
  111. unset($result);
  112. }
  113. public function data($data,$bach_id,$tasks_id,$product_id,$printer_id,$is_verif){
  114. $time = time();
  115. $p = $bach_id.",".$tasks_id.",".$product_id.",".$printer_id.",".$time.",".$time.",".$time.","."0,1";
  116. if($is_verif){
  117. array_walk($data, function(&$value,$key,$p){
  118. $value =parse_url($value);
  119. $value = substr($value['path'],1,-1);
  120. $value = "('" .str_replace(",","','",$value)."',".$p.")";
  121. },$p);
  122. }else{
  123. array_walk($data, function(&$value,$key,$p){
  124. $value =parse_url($value);
  125. $value = substr($value['path'],1,-1);
  126. $value = "('" .$value."',"."'',".$p.")";
  127. },$p);
  128. }
  129. $resultStr = join(",", $data);
  130. M()->execute("insert ignore INTO `qr_codes` (`code`,`varify_code`,`bach_id`,`tasks_id`,`product_id`,`printer_id`,`add_time`,`update_time`,`active_time`,`delete_time`,`Status`) values {$resultStr}");
  131. $res = M()->query('SELECT ROW_COUNT()');
  132. $num = $res[0]['ROW_COUNT()'];
  133. return $num;
  134. }
  135. }