DeviceRegistrationController.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class DeviceRegistrationController extends ComController
  5. {
  6. public function index()
  7. {
  8. $this->display('index');
  9. }
  10. public function upfile()
  11. {
  12. $data = I();
  13. if (isset($data['upfile'])) {
  14. if ($_FILES['dianfile']['name'] != '') {
  15. if ($_FILES["dianfile"]["error"] > 0) {
  16. echo json_encode(array('statusCode' => 300, 'message' => $_FILES["dianfile"]["error"]));
  17. } else {
  18. $files = $_FILES["dianfile"]["name"];
  19. $ftypes = strtolower(substr(strrchr($files, '.'), 1));
  20. if ($ftypes != "xls") {
  21. echo json_encode(array('statusCode' => 300, 'message' => '格式不对,请上传.xls结尾的文件'));
  22. } else {
  23. $filetrue = move_uploaded_file($_FILES["dianfile"]["tmp_name"], "/var/www/html/jdxf/admin1/knowfile/devicedata/DeviceRegistration.xls");
  24. if ($filetrue) {
  25. $this->xlsin("/var/www/html/jdxf/admin1/knowfile/devicedata/DeviceRegistration.xls");
  26. } else {
  27. echo json_encode(array('statusCode' => 300, 'message' => '上传失败'));
  28. }
  29. }
  30. }
  31. } else {
  32. echo json_encode(array('statusCode' => 300, 'message' => '没有文件!'));
  33. }
  34. } else {
  35. $this->display('index');
  36. }
  37. }
  38. private function xlsin($filename)
  39. {//导入
  40. import("Org.Util_it.PHPExcel");
  41. $PHPExcel = new \PHPExcel();
  42. //如果excel文件后缀名为.xls,导入这个类
  43. import("Org.Util_it.PHPExcel.Reader.Excel5");
  44. //如果excel文件后缀名为.xlsx,导入这下类
  45. //import("Org.Util.PHPExcel.Reader.Excel2007");
  46. //$PHPReader=new \PHPExcel_Reader_Excel2007();
  47. $PHPReader = new \PHPExcel_Reader_Excel5();
  48. //载入文件
  49. $PHPExcel = $PHPReader->load($filename);
  50. //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
  51. $currentSheet = $PHPExcel->getSheet(0);
  52. //获取总列数
  53. $highestColumn = $currentSheet->getHighestColumn();
  54. //获取总行数
  55. $highestRow = $currentSheet->getHighestRow();
  56. //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
  57. $arr = array(1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E', 6 => 'F', 7 => 'G', 8 => 'H', 9 => 'I', 10 => 'J', 11 => 'K', 12 => 'L', 13 => 'M', 14 => 'N', 15 => 'O', 16 => 'P', 17 => 'Q', 18 => 'R', 19 => 'S', 20 => 'T', 21 => 'U', 22 => 'V', 23 => 'W', 24 => 'X', 25 => 'Y', 26 => 'Z');
  58. //echo $highestRow.$highestColumn;
  59. // 一次读取一列
  60. //$type=M('know_type')->field('id')->where('name="'.$types.'"')->select();
  61. // $customer = M('userinfo_point');
  62. // $obtasksql = "insert into sp_userinfo_point(owner_code,owner_name,unitinfo,louyu,sim,maintainTime,dest_address,addr1,addr2,
  63. // addr3,address,posistion,install_time,install_man,dwtype,owner_xh,s_interval,community,company,rtmp,
  64. // hls,html5,danwei,owner_bl,push_param1,push_param2,push_param3,push_param4,login_param,transfer_type) values ";
  65. $expCellName = array('设备编号', '状态', '描述');
  66. $expTitle = '导入信息';
  67. $expTableData =[];
  68. $addall=0;
  69. $downnum = 0;
  70. for ($row = 2; $row <= $highestRow; $row++) {//行
  71. $val0 = $currentSheet->getCellByColumnAndRow(0, $row)->getValue();
  72. $val1 = $currentSheet->getCellByColumnAndRow(1, $row)->getValue();
  73. $val2 = $currentSheet->getCellByColumnAndRow(2, $row)->getValue();
  74. $val3 = $currentSheet->getCellByColumnAndRow(3, $row)->getValue();
  75. $val4 = $currentSheet->getCellByColumnAndRow(4, $row)->getValue();
  76. $val5 = $currentSheet->getCellByColumnAndRow(5, $row)->getValue();
  77. $val6 = $currentSheet->getCellByColumnAndRow(6, $row)->getValue();
  78. $val7 = $currentSheet->getCellByColumnAndRow(7, $row)->getValue();
  79. $val8 = $currentSheet->getCellByColumnAndRow(8, $row)->getValue();
  80. $val9 = $currentSheet->getCellByColumnAndRow(9, $row)->getValue();
  81. $val10 = $currentSheet->getCellByColumnAndRow(10, $row)->getValue();
  82. if (empty($val4)){
  83. $val4='';
  84. }
  85. if (empty($val8)){
  86. $val8='z';
  87. }
  88. if (empty($val9)){
  89. $val9='z';
  90. }
  91. if (empty($val10)){
  92. $val10='z';
  93. }
  94. if (empty($val6)){
  95. $val6='';
  96. }
  97. $devicedata['owner_code']=$val0;
  98. $devicedata['owner_name']=$val1;
  99. $devicedata['dwtype']=$val2;
  100. $devicedata['company']=$val3;
  101. $devicedata['sim']=$val4;
  102. $devicedata['louyu']=$val5;
  103. $devicedata['owner_bl']=$val6;
  104. $devicedata['unitinfo']=$val7;
  105. $devicedata['owner_xh']='0,'.$val8.$val9.$val10;
  106. $devicedata['install_time']=date('Y-m-d H:i:s', time());
  107. $res_one=M('owner')->add($devicedata);
  108. if ($res_one){
  109. $devicestatusdata['deviceid']=$val0;
  110. $devicestatusdata['deviceno']='0';
  111. $devicestatusdata['statustime']='0000-00-00 00:00:00';
  112. $res_two=M('devices_status')->add($devicestatusdata);
  113. if ($res_two){
  114. if($val2 == '128'){
  115. $tab=M('owner')->execute("CALL device_table('".$val0."','".$val2."')");
  116. }else{
  117. $tab=M('owner')->execute("CALL device_table1('".$val0."','".$val2."')");
  118. }
  119. $addall++;
  120. if ($tab){
  121. // $expTableData[$row-2]['device_code']=$val0;
  122. // $expTableData[$row-2]['status']='导入成功';
  123. // $expTableData[$row-2]['describe']='成功';
  124. }else{
  125. // $expTableData[$row-2]['device_code']=$val0;
  126. // $expTableData[$row-2]['status']='导入失败';
  127. // $expTableData[$row-2]['describe']='单独表创建失败';
  128. }
  129. }else{
  130. // $expTableData[$row-2]['device_code']=$val0;
  131. // $expTableData[$row-2]['status']='导入失败';
  132. // $expTableData[$row-2]['describe']='设备状态表插入失败';
  133. }
  134. }else{
  135. // $expTableData[$row-2]['device_code']=$val0;
  136. // $expTableData[$row-2]['status']='导入失败';
  137. // $expTableData[$row-2]['describe']='设备表插入失败';
  138. }
  139. $downnum++;
  140. }
  141. unlink('/var/www/html/jdxf/admin1/knowfile/devicedata/DeviceRegistration.xls');
  142. // $this->exportExcel($expTitle, $expCellName, $expTableData);
  143. $res = '共' . $downnum . '条,成功导入' . $addall . '条。';
  144. echo json_encode(array('statusCode' => 200, 'message' => '导入成功:' . $res)); //.'时间:'.$stra.','.$strb
  145. }
  146. // public function deldata()
  147. // {
  148. // $Model = M('userinfo_point');
  149. // $Data = I();
  150. // if (session('power') == 2) {
  151. // $this->mtReturn(300, '本账号没有该权限,如需开通请联系管理员', 1);
  152. // } else {
  153. // $used_data = M()->query("select * from sp_userinfo_point where id=" . $Data['id']);
  154. // $res = $Model->where('id=' . $Data['id'])->delete();
  155. // if ($res) {
  156. //
  157. // //日志开始
  158. // $filename_log = "/var/www/html/jdxf/log/config_log" . date("Y-m-d") . ".log";
  159. // $data_log = "\r\n" . '[' . date("Y-m-d H:i:s") . ']操作人员手机号:' . session('phone_login') . '---删除sp_userinfo_point表---ID:' . $Data['id'] . "\r\n";
  160. // $data_log1 = '删除前数据:' . json_encode($used_data) . "\r\n";
  161. // file_put_contents($filename_log, $data_log, FILE_APPEND | LOCK_EX);
  162. // file_put_contents($filename_log, $data_log1, FILE_APPEND | LOCK_EX);
  163. // //日志开始结束
  164. //
  165. // $this->mtReturn(200, '删除成功' . $Data['id']);
  166. // } else {
  167. // $this->mtReturn(300, '删除失败');
  168. // }
  169. //
  170. // }
  171. //
  172. // }
  173. //
  174. public function downdata()
  175. {//导出Excel
  176. $Data = I();
  177. $UserVIew = M("userinfo_point");
  178. $Map = array();
  179. if (!empty($Data['deviceid'])) {
  180. $Map['deviceid'] = $Data['deviceid'];
  181. }
  182. if (!empty($Data['phone'])) {
  183. $Map['phone'] = $Data['phone'];
  184. }
  185. $permdata = $this->permdata();
  186. if (!empty($permdata)) {
  187. $Map['addman'] = $permdata;
  188. }
  189. //$Count= $UserVIew->where($Map)->count();// 查询满足要求的总记录数 ,contactman,contactphone
  190. $expTableData = $UserVIew->field("deviceid,`host`,circuit,point,desc")->where($Map)->order('addtime desc')->select();
  191. $expCellName = array('设备id', '主机号', '回路号', '设备号', '描述');
  192. $expTitle = '点位信息';
  193. $this->exportExcel($expTitle, $expCellName, $expTableData);
  194. // echo json_encode($expTableData);
  195. }
  196. }