Car.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class Car extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model= new \app\common\model\Car();
  11. $this->table= 'car_view';
  12. }
  13. public function index($import_id=0){
  14. if(request()->isAjax()){
  15. //分页参数
  16. $length = input('rows',10,'intval'); //每页条数
  17. $page = input('page',1,'intval'); //第几页
  18. $start = ($page - 1) * $length; //分页开始位置
  19. //排序
  20. $sortRow = input('sidx','id','trim'); //排序列
  21. $sort = input('sord','desc','trim'); //排序方式
  22. $order = $sortRow.' '.$sort;
  23. $plate_num = input('plate_num','','trim');
  24. if($plate_num){
  25. $map[] = ['plate_num','like','%'.$plate_num.'%'];
  26. }
  27. $name = input('name','','trim');
  28. if($name){
  29. $map[] = ['name','like','%'.$name.'%'];
  30. }
  31. $tcw_title = input('tcw_title','','trim');
  32. if($tcw_title){
  33. $map[] = ['tcw_title','like','%'.$tcw_title.'%'];
  34. }
  35. $enable = input('enable','','trim');
  36. if($enable != ''){
  37. $map[] = ['enable','=',$enable];
  38. }
  39. if($import_id >0){
  40. $map[] = ['import_log_id','=',$import_id];
  41. }else{
  42. $map[] = ['del','=',0];
  43. }
  44. $map[] = ['org_id','=',$this->orgId];
  45. $map= empty($map) ? true: $map;
  46. //数据查询
  47. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  48. foreach ($lists as $k=>$v){
  49. $lists[$k]['is_show_option'] =$import_id>0?false:true;
  50. }
  51. //数据返回
  52. $totalCount = db($this->table)->where($map)->count();
  53. $totalPage = ceil($totalCount/$length);
  54. $result['page'] = $page;
  55. $result['total'] = $totalPage;
  56. $result['records'] = $totalCount;
  57. $result['rows'] = $lists;
  58. return json($result);
  59. }else{
  60. $cType = Db::name('house_c_type')
  61. ->where('del',0)
  62. ->where('enable',1)
  63. ->select();
  64. $logInfo = [];
  65. if($import_id>0){
  66. $logInfo = Db::name('import_log')
  67. ->where('id',$import_id)
  68. ->find();
  69. }
  70. $this->assign('import_id',$import_id);
  71. $this->assign('importInfo',$logInfo);
  72. $this->assign('m_name','绑定车辆');
  73. $this->assign('cType',$cType);
  74. return $this->fetch();
  75. }
  76. }
  77. /**
  78. * 新增/编辑
  79. */
  80. public function add($id=0){
  81. if(request()->isPost()){
  82. $res = $this->model->updates();
  83. if($res){
  84. $this->success('操作成功',url('index'));
  85. }else{
  86. $this->error($this->model->getError());
  87. }
  88. }else{
  89. if($id){
  90. $info =db($this->table)->where('id',$id)->find();
  91. $area = Db::name('house')
  92. ->where('id',$info['carport_id'])
  93. ->find();
  94. $info['line'] = [$area['area_id'],$info['carport_id']];
  95. $this->assign('info',$info);
  96. }
  97. $tree = $this->model->tree();
  98. $this->assign('tree',$tree);
  99. return $this->fetch();
  100. }
  101. }
  102. /**
  103. * 删除记录
  104. * @param int $id
  105. */
  106. public function del($id=0){
  107. if(!$id){
  108. $this->error('参数错误');
  109. }
  110. $res = db('car')->where('id',$id)->setField('del',1);
  111. if($res){
  112. $this->success('删除成功');
  113. }else{
  114. $this->error('删除失败');
  115. }
  116. }
  117. /**
  118. * 改变字段值
  119. * @param int $fv
  120. * @param string $fn
  121. * @param int $fv
  122. */
  123. public function changeField($id=0,$fn='',$fv=0){
  124. if(!$fn||!$id){
  125. $this->error('参数错误');
  126. }
  127. $res = db('car')->where('id',$id)->setField($fn,$fv);
  128. if($res){
  129. $this->success('操作成功');
  130. }else{
  131. $this->error('操作失败');
  132. }
  133. }
  134. public function getType(){
  135. $catId = input('id');
  136. $list = Db::name('house_c_type')
  137. ->where('cate',$catId)
  138. ->where('del',0)
  139. ->where('enable',1)
  140. ->select();
  141. $this->success('','',$list);
  142. }
  143. // excel
  144. public function import(){
  145. return $this->fetch();
  146. }
  147. /**
  148. * 下载绑定车辆列表模板
  149. */
  150. public function downloadtem(){
  151. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  152. set_time_limit(0);
  153. ini_set("memory_limit","512M");
  154. $fileName = '绑定车辆列表.xlsx';
  155. $excel = new \PHPExcel();
  156. $sheet = $excel->setActiveSheetIndex(0);
  157. $arr = array('A','B','C','D','E','F');
  158. foreach($arr as $k=>$v){
  159. $excel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(true);//换行
  160. $excel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);
  161. $excel->getActiveSheet()->getColumnDimension($v)->setWidth(18);
  162. }
  163. $sheet->setCellValueByColumnAndRow(0,1,'小区名称');
  164. $sheet->setCellValueByColumnAndRow(1,1,'停车位名称');
  165. $sheet->setCellValueByColumnAndRow(2,1,'品牌及车辆');
  166. $sheet->setCellValueByColumnAndRow(3,1,'车牌号');
  167. $sheet->setCellValueByColumnAndRow(4,1,'业主姓名');
  168. $sheet->setCellValueByColumnAndRow(5,1,'业主电话');
  169. ob_end_clean();//清除缓冲区,避免乱码
  170. header('Content-Type: application/vnd.ms-excel');
  171. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  172. header('Cache-Control: max-age=0');
  173. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
  174. $objWriter->save('php://output'); //文件通过浏览器下载
  175. }
  176. /**
  177. * 导入
  178. */
  179. public function importexcel(){
  180. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  181. set_time_limit(0);
  182. ini_set("memory_limit", -1);
  183. ob_flush();//清空缓存
  184. flush();//刷新缓存
  185. $orgId = cur_org_id();
  186. $file = request()->file('file');
  187. if(!$file){
  188. exit('请上传文件');
  189. }
  190. // 移动到框架应用根目录/uploads/ 目录下
  191. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  192. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  193. if(!$info){
  194. exit('上传文件失败');
  195. }
  196. $img = env('root_path').'/public/uploads/files/' . $info->getSaveName();
  197. $finfo = $info->getInfo();
  198. $fileExt = explode('.',$finfo['name']);
  199. $finfo['file_name'] = $finfo['name'];
  200. $filepath = str_replace('\\', '/', $img);
  201. $file_type = $fileExt[count($fileExt)-1];
  202. if($file_type == 'xls'){
  203. $objReader = \PHPExcel_IOFactory::createReader('Excel5');
  204. }else{
  205. $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
  206. }
  207. $imData = [
  208. 'org_id'=>$orgId,
  209. 'title'=>$finfo['file_name'],
  210. 'type'=>3,
  211. 'create_time'=>getTime(),
  212. ];
  213. $importId =Db::name('import_log')->insertGetId($imData);
  214. $objPHPExcel = $objReader->load($filepath,$encode='utf-8');
  215. $table = array(0,1,2,3,4,5,6);
  216. $allobjWorksheets = $objPHPExcel->getAllSheets();
  217. try{
  218. @unlink($filepath);
  219. foreach($allobjWorksheets as $key=>$objWorksheet){
  220. $highestRow = $objWorksheet->getHighestRow();
  221. $success = 0;
  222. $error = 0;
  223. $errorMsg = [];
  224. for ($row = 2; $row <= $highestRow; ++$row) {
  225. $oneData = array();
  226. for ($col = 0; $col < count($table); ++$col) {
  227. $cell = $objWorksheet->getCellByColumnAndRow($col, $row);
  228. $value = (string)$cell->getValue();
  229. $oneData[$table[$col]]= $value;
  230. }
  231. if(!$oneData[0]||!$oneData[1] ||!$oneData[3] ||!$oneData[4] ||!$oneData[5]){
  232. $msg = "第".$row."行,数据存在空值,未导入";
  233. echo "<font color=\"red\">".$msg."</font><br />";
  234. $error++;
  235. $errorMsg[] = $msg;
  236. continue;
  237. }
  238. $area =Db::name('house_areas')
  239. ->where('title',trim($oneData[0]))
  240. ->where('type',1)
  241. ->where('pid',0)
  242. ->find();
  243. if(!$area){
  244. $msg = "第".$row."行,小区<".$oneData[0].">不存在,未导入";
  245. echo "<font color=\"red\">".$msg."</font><br />";
  246. $error++;
  247. $errorMsg[] = $msg;
  248. continue;
  249. }
  250. $carport = Db::name('house')
  251. ->where('title',trim($oneData[1]))
  252. ->where('area_id',$area['id'])
  253. ->where('del',0)
  254. ->where('cate',5)
  255. ->find();
  256. if(!$carport){
  257. $msg = "第".$row."行,小区<".$oneData[0].">下面停车位<".$oneData[1].">不存在,未导入";
  258. echo "<font color=\"red\">".$msg."</font><br />";
  259. $error++;
  260. $errorMsg[] = $msg;
  261. continue;
  262. }
  263. $data = [
  264. 'org_id'=>$orgId,
  265. 'carport_id'=>$carport['id'],
  266. 'brand'=>$oneData[2],
  267. 'plate_num'=>$oneData[3],
  268. 'name'=>$oneData[4],
  269. 'phone'=>$oneData[5],
  270. 'create_time'=>getTime(),
  271. 'import_log_id'=>$importId
  272. ];
  273. $Id = Db::name('car')->insertGetId($data);
  274. if(!$Id){
  275. $msg = "第".$row."行,导入失败";
  276. echo "<font color=\"red\">".$msg."</font><br />";
  277. $error++;
  278. $errorMsg[] = $msg;
  279. continue;
  280. }
  281. $success++;
  282. }
  283. Db::name('import_log')->where('id',$importId)
  284. ->update(
  285. [
  286. 'success'=>$success,
  287. 'error'=>$error,
  288. 'error_msg'=>json_encode($errorMsg),
  289. ]
  290. );
  291. }
  292. echo "<font color=\"green\">导入完成</font><br />";
  293. }catch (Exception $e){
  294. echo $e->getMessage();
  295. echo "<font color=\"red\">数据异常,已停止导入</font><br />";
  296. }
  297. }
  298. }