DeviceLog.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\App;
  5. use think\Db;
  6. use think\Exception;
  7. class DeviceLog extends Auth
  8. {
  9. public function __construct(App $app = null) {
  10. parent::__construct($app);
  11. $this->table='device_log';
  12. $this->model = new \app\common\model\DeviceLog();
  13. }
  14. public function index(){
  15. if(request()->isAjax()){
  16. //分页参数
  17. $length = input('rows',10,'intval'); //每页条数
  18. $page = input('page',1,'intval'); //第几页
  19. $start = ($page - 1) * $length; //分页开始位置
  20. //排序
  21. $sortRow = input('sidx','id','trim'); //排序列
  22. $sort = input('sord','desc','trim'); //排序方式
  23. $order = $sortRow.' '.$sort;
  24. $title = input('title','','trim');
  25. if($title){
  26. $map[] = ['sn','like','%'.$title.'%'];
  27. }
  28. $type = input('type','','trim');
  29. if($type){
  30. $map[] = ['type','=',$type];
  31. }
  32. $map= empty($map) ? true: $map;
  33. //数据查询
  34. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  35. foreach ($lists as $k=>$v){
  36. $lists[$k]['typeName'] = $this->model->type[$v['type']];
  37. }
  38. //数据返回
  39. $totalCount = db($this->table)->where($map)->count();
  40. $totalPage = ceil($totalCount/$length);
  41. $result['page'] = $page;
  42. $result['total'] = $totalPage;
  43. $result['records'] = $totalCount;
  44. $result['rows'] = $lists;
  45. return json($result);
  46. }else{
  47. $this->assign('typeList',$this->model->type);
  48. return $this->fetch();
  49. }
  50. }
  51. /**
  52. * 新增/编辑
  53. */
  54. public function add($id=0){
  55. if(request()->isPost()){
  56. $res = $this->model->updates();
  57. if($res){
  58. $this->success('操作成功',url('index'));
  59. }else{
  60. $this->error($this->model->getError());
  61. }
  62. }else{
  63. if($id){
  64. $info =db($this->table)->where('id',$id)->find();
  65. $this->assign('info',$info);
  66. }
  67. $tt = $this->model->type;
  68. $ll = [];
  69. foreach ($tt as $k=>$v){
  70. $ll[] = [
  71. 'id'=>$k,
  72. 'title'=>$v,
  73. ];
  74. }
  75. $this->assign('typeList',$ll);
  76. return $this->fetch();
  77. }
  78. }
  79. /**
  80. * 删除记录
  81. * @param int $id
  82. */
  83. public function del($id=0){
  84. if(!$id){
  85. $this->error('参数错误');
  86. }
  87. $res = db($this->table)->where('id',$id)->setField('del',1);
  88. if($res){
  89. $this->success('删除成功');
  90. }else{
  91. $this->error('删除失败');
  92. }
  93. }
  94. /**
  95. * 下载点模板
  96. */
  97. public function downloadtem(){
  98. set_time_limit(0);
  99. ini_set("memory_limit","512M");
  100. $tt = $this->model->type;
  101. $ll = [];
  102. foreach ($tt as $k=>$v){
  103. $ll[] = $k.'='.$v;
  104. }
  105. $ts = implode(';',$ll);
  106. $header = [
  107. ['title' => '编号', 'name' => 'sn','width'=>'20'],
  108. ['title' => '类型'.$ts, 'name' => 'type','width'=>'20'],
  109. ];
  110. $lists = [
  111. [
  112. 'sn' => '121212',
  113. 'type' => '1',
  114. ]
  115. ];
  116. $filename = '设备记录模板';
  117. ExcelUtil::export($filename,$header,$lists);
  118. }
  119. public function import(){
  120. return $this->fetch();
  121. }
  122. /**
  123. * 导入
  124. */
  125. public function importexcel(){
  126. set_time_limit(0);
  127. ini_set("memory_limit", -1);
  128. ob_flush();//清空缓存
  129. flush();//刷新缓存
  130. try{
  131. $cols = ['sn','type'];
  132. $lists = ExcelUtil::importExcel('file',$cols,2);
  133. if($lists === false){
  134. exit(ExcelUtil::getError());
  135. }
  136. if(empty($lists)){
  137. exit('文件内容为空');
  138. }
  139. foreach ($lists as $k=>$v){
  140. if(!in_array($v['type'],array_keys($this->model->type))){
  141. $msg = "第".($k+2)."行,类型格式错误,导入失败";
  142. echo "<font color=\"red\">".$msg."</font><br />";
  143. continue;
  144. }
  145. $dt = [
  146. 'id'=>0,
  147. 'sn'=>$v['sn'],
  148. 'type'=>$v['type'],
  149. 'create_time'=>date('Y-m-d H:i:s'),
  150. ];
  151. $result = validate('DeviceLog')->check($dt,[],'');
  152. if(true !== $result){
  153. $error = validate('DeviceLog')->getError();
  154. $msg = "第".($k+2)."行,".$error.",导入失败";
  155. echo "<font color=\"red\">".$msg."</font><br />";
  156. continue;
  157. }
  158. unset($dt['id']);
  159. $inset = Db::name('device_log')->insert($dt);
  160. if(!$inset){
  161. $msg = "第".($k+2)."行,导入失败";
  162. echo "<font color=\"red\">".$msg."</font><br />";
  163. continue;
  164. }
  165. }
  166. echo "<font color=\"green\">导入完成</font><br />";
  167. }catch (Exception $e){
  168. trace($e->getMessage(),'error');
  169. echo $e->getMessage();
  170. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  171. }
  172. }
  173. }