Code.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Code extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_code','code');
  7. }
  8. //定义方法的调用规则 获取URI第二段值
  9. public function _remap($arg,$arg_array)
  10. {
  11. if($arg == 'add')//添加
  12. {
  13. $this->_add();
  14. }
  15. else if($arg == 'edit')//修改
  16. {
  17. $this->_edit($arg_array);
  18. }
  19. else if($arg == 'del')//修改
  20. {
  21. $this->_del();
  22. }
  23. else
  24. {
  25. $this->_index();
  26. }
  27. }
  28. //管理
  29. public function _index()
  30. {
  31. $post = $this->input->post(NULL, TRUE);
  32. if(isset($post['page']))
  33. {
  34. $page = $this->input->post('page',true);
  35. $perpage = $this->input->post('perpage',true);
  36. $type = $this->input->post('type',true);
  37. /**
  38. $timek = $this->input->post('timetk',true);
  39. $timej = $this->input->post('timetj',true);
  40. $timek = strtotime($timek);
  41. $timej = strtotime($timej);
  42. **/
  43. $where = "1=1 ";
  44. /**
  45. if($timetk && $timetj)
  46. {
  47. $where .= " and printtime > '$timek' and printtime < '$timej'";
  48. }
  49. **/
  50. if($type || $type==0)
  51. {
  52. $where .= " and type = '$type'";
  53. }
  54. //数据排序
  55. $order_str = "addtime desc";
  56. if(empty($page))
  57. {
  58. $start = 0;
  59. $perpage = 1;
  60. }
  61. else
  62. {
  63. $start = ($page - 1)*$perpage;
  64. }
  65. //取得信息列表
  66. $info_list = $this->code->find_all($where,'id,number,content,addtime,print',$order_str,$start,$perpage);
  67. foreach ($info_list as $key=>$value)
  68. {
  69. $info_list[$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
  70. }
  71. $total = $this->code->find_count($where);
  72. $pagenum = ceil($total/$perpage);
  73. $over = $total-($start+$perpage);
  74. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  75. echo json_encode($rows);exit;
  76. }
  77. $this->_Template('code',$this->data);
  78. }
  79. //添加
  80. public function _add()
  81. {
  82. $dir = '/data/excel/'.date('Ymd',time()).'/';
  83. $config['upload_path'] = '.'.$dir ;
  84. $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
  85. $config['allowed_types'] = 'xls|xlsx|csv';
  86. $config['max_size'] = 10240;
  87. $this->load->library('upload', $config);
  88. $this->upload->initialize($config);
  89. if ($this->upload->do_upload('userfile'))
  90. {
  91. $full_path = $dir.$this->upload->data('file_name');
  92. $fileName = '.' . $full_path;
  93. if (!file_exists($fileName))
  94. {
  95. echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
  96. }
  97. else
  98. {
  99. require_once "./data/excel/PHPExcel/IOFactory.php";
  100. $phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
  101. $phpExcel->setActiveSheetIndex(0);// 设置为默认表
  102. $sheetCount = $phpExcel->getSheetCount();// 获取表格数量
  103. $row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
  104. $column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
  105. ++$column;//如果列数大于26行
  106. $list = array();
  107. for ($i = 2; $i <= $row; $i++) // 行数循环
  108. {
  109. $data = array();
  110. for ($c = 'A'; $c != $column; $c++) // 列数循环
  111. {
  112. $data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
  113. }
  114. $list[] = $data;
  115. }
  116. }
  117. $i = 0;$j = 0;$ed = array();$tytime = time();
  118. foreach ($list as $key=>$value)
  119. {
  120. $value['6'] = preg_replace('/\D/s','',$value['6']);
  121. if($value['6'] && !isset($n[$value['6']]))
  122. {
  123. $this->code->insert(array('content'=>$value['5'],'number'=>$value['6'],'addtime'=>time()));
  124. }
  125. else
  126. {
  127. $ed[] = array($value['6'].'-错误或重复操作');
  128. $i++;
  129. }
  130. }
  131. if($i+$j > 0)
  132. {
  133. $time = date('Ymd',time());
  134. $title = '错误信息-'.$time;
  135. $titlename = "<table border=1><tr><td>错误详情</td></tr></table>";
  136. $tail = "\n";
  137. $filename = $title.".xls";
  138. $ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail);
  139. $dir = '/data/excel/'.$time.'/';
  140. $file_name = 'error_'.$time.rand(1000,9999);
  141. if(!is_dir('.'.$dir))mkdir('.'.$dir,0777);
  142. $myfile = fopen(".".$dir.$file_name.".xls", "w") or die();
  143. fwrite($myfile,$ecl);
  144. fclose($myfile);
  145. $error = $dir.$file_name.'.xls';
  146. echo json_encode(array('msg'=>'添加成功,'.$i.'条异常','error'=>$error,'success'=>true));exit;
  147. }
  148. else
  149. {
  150. echo json_encode(array('msg'=>'添加成功!','error'=>1,'success'=>true));exit;
  151. }
  152. }
  153. else
  154. {
  155. echo json_encode(array('msg'=>'上传失败!','t'=>$this->upload->display_errors(),'success'=>false));exit;
  156. }
  157. }
  158. //修改
  159. public function _edit($arg_array)
  160. {
  161. $post = $this->input->post(NULL, TRUE);
  162. if(isset($post['s']))
  163. {
  164. $id_arr = $this->input->post('s');
  165. $id_arr = explode(',',$id_arr);
  166. if(!$id_arr)
  167. {
  168. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  169. }
  170. //循环删除记录
  171. foreach ($id_arr as $v)
  172. {
  173. $d = $this->code->read($v);
  174. $this->code->save(array('print'=>$d['print']+1,'printtime'=>time(),'type'=>1),$v);
  175. }
  176. echo json_encode(array('success'=>true));
  177. }
  178. }
  179. //删除
  180. public function _del()
  181. {
  182. $post = $this->input->post(NULL, TRUE);
  183. if(isset($post['s']))
  184. {
  185. $id_arr = $this->input->post('s');
  186. $id_arr = explode(',',$id_arr);
  187. if(!$id_arr)
  188. {
  189. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  190. }
  191. //循环删除记录
  192. foreach ($id_arr as $v)
  193. {
  194. $this->code->remove($v);
  195. }
  196. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  197. }
  198. }
  199. }