You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MonitorController.class.php 18 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace Admin\Controller;
  3. use Think\Controller;
  4. class MonitorController extends Controller {
  5. public function _empty($name){
  6. //把所有城市的操作解析到city方法
  7. $this->display('Monitor/error404');
  8. }
  9. public function index(){
  10. $this->redirect("monitorList");
  11. }
  12. public function monitorList(){
  13. $map['site_name']=I("request.site_name");
  14. $map['hotel_name']=$_REQUEST['hotel_name'];
  15. $encode = mb_detect_encoding($map['hotel_name'], array("UTF-8","ASCII","GB2312","GBK","BIG5"));
  16. if ($encode !="UTF-8"){
  17. $map['hotel_name']=mb_convert_encoding($map['hotel_name'],"UTF-8",$encode);
  18. }
  19. //$monitor=M("Type");
  20. //$res=$monitor->select();
  21. //$res=$monitor->query($sql);
  22. //$this->assign("monitorList",$res);
  23. $monitor=M();
  24. $sql="select s.site_name,h.url,h.hotel_name,q.query_date,r.room_type,r.room_price,r.site_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where q.query_date>(GETDATE()-1)";
  25. if($map['site_name'] >0 ){
  26. $sql="select s.site_name,h.url,h.hotel_name,q.query_date,r.room_type,r.room_price,r.site_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where s.id=".$map['site_name']."and q.query_date>(GETDATE()-1)";
  27. }
  28. if ($map['hotel_name'] !=""){
  29. $sql="select s.site_name,h.url,h.hotel_name,q.query_date,r.room_type,r.room_price,r.site_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where h.hotel_name like '%".$map['hotel_name']."%'"."and q.query_date>(GETDATE()-1)";
  30. }
  31. if($map['site_name'] >0 && $map['hotel_name'] !=""){
  32. $sql="select s.site_name,h.url,h.hotel_name,q.query_date,r.room_type,r.room_price,r.site_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where s.id=".$map['site_name']." and h.hotel_name like '%".$map['hotel_name']."%'"."and q.query_date>(GETDATE()-1)";
  33. }
  34. $count= count($monitor->query($sql));// 查询满足要求的总记录数
  35. $Page= new \Think\Page($count,30);// 实例化分页类 传入总记录数和每页显示的记录数(25)
  36. $whereNotIn="select top ".$Page->firstRow." a.id from room a inner join queryDate b on a.date_id= b.id inner join hotel c on b.hotel_id=c.id inner join site d on c.site_id=d.id";
  37. $sql1="select top ".$Page->listRows." r.id,s.site_name,h.url,h.hotel_name,q.query_date,r.room_type,r.room_price,r.site_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where r.id not in (".$whereNotIn.")"."and q.query_date>(GETDATE()-1)";
  38. if($map['site_name'] >0 ){
  39. $whereNotIn="select top ".$Page->firstRow." a.id from room a inner join queryDate b on a.date_id= b.id inner join hotel c on b.hotel_id=c.id inner join site d on c.site_id=d.id where d.id=".$map['site_name'];
  40. $sql1="select top ".$Page->listRows." r.id,s.site_name,h.url,h.hotel_name,q.query_date,r.room_type,r.room_price,r.site_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where r.id not in (".$whereNotIn.")"."and q.query_date>(GETDATE()-1)";
  41. $sql1.=" and s.id=".$map['site_name'];
  42. }
  43. if ($map['hotel_name'] !=""){
  44. $whereNotIn="select top ".$Page->firstRow." a.id from room a inner join queryDate b on a.date_id= b.id inner join hotel c on b.hotel_id=c.id inner join site d on c.site_id=d.id where c.hotel_name like '%".$map['hotel_name']."%'";
  45. $sql1="select top ".$Page->listRows." r.id,s.site_name,h.url,h.hotel_name,q.query_date,r.room_type,r.room_price,r.site_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where r.id not in (".$whereNotIn.")"."and q.query_date>(GETDATE()-1)";
  46. $sql1.=" and h.hotel_name like '%".$map['hotel_name']."%'";
  47. }
  48. if ($map['site_name'] >0 && $map['hotel_name'] !=""){
  49. $whereNotIn="select top ".$Page->firstRow." a.id from room a inner join queryDate b on a.date_id= b.id inner join hotel c on b.hotel_id=c.id inner join site d on c.site_id=d.id where d.id=".$map['site_name']." and c.hotel_name like '%".$map['hotel_name']."%'";
  50. $sql1="select top ".$Page->listRows." r.id,s.site_name,h.url,h.hotel_name,q.query_date,r.room_type,r.room_price,r.site_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where r.id not in (".$whereNotIn.")"."and q.query_date>(GETDATE()-1)";
  51. $sql1.=" and s.id=".$map['site_name']." and h.hotel_name like '%".$map['hotel_name']."%'";
  52. }
  53. $sql1.= " order by r.id";
  54. // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
  55. //$list = $monitor->where('status=1')->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
  56. $list=$monitor->query($sql1);
  57. //$list=$monitor->field( "type.id,site.site_name,hotel.hotel_name,type.room_type,hotel.url,type.room_price" ) ->join( "hotel on type.hotel_id=hotel.id" ) ->join( "site on site.id=hotel.id" ) ->limit($Page->firstRow.",".$Page->listRows)->order("type.id") ->select();
  58. $Page->setConfig('prev', '上一页');
  59. $Page->setConfig('next', '下一页');
  60. $Page->setConfig('first', '首页');
  61. $Page->setConfig('last', '尾页');
  62. $Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
  63. foreach ($map as $key =>$val){
  64. $Page->parameter[$key]=$val;
  65. }
  66. $show= $Page->show();// 分页显示输出
  67. $this->assign('list',$list);// 赋值数据集
  68. $this->assign('page',$show);// 赋值分页输出
  69. $sql="select * from site order by id ";
  70. $siteRes=$monitor->query($sql);
  71. array_unshift($siteRes, array("id"=>0,"site_name"=>"所有"));
  72. $this->assign("site",$siteRes);
  73. $this->assign("site_name",$map['site_name']);
  74. $this->assign("hotel_name",$map['hotel_name']);
  75. $this->display();
  76. }
  77. public function monitorDel(){
  78. $id=I("get.id");
  79. $monitor=M();
  80. //$res=$monitor->select();
  81. $sql="delete from room where id=".$id;
  82. $res=$monitor->execute($sql);
  83. if ($res>0){
  84. echo json_encode(array("errorCode"=>0,"info"=>"删除成功"));
  85. exit;
  86. }else {
  87. echo json_encode(array("errorCode"=>1,"info"=>"删除失败"));
  88. exit;
  89. }
  90. }
  91. public function monitorAdd(){
  92. $monitor=M();
  93. if (!empty($_POST)){
  94. $validate = array(
  95. array('hotel_name','require','酒店名称不能为空!'), // 仅仅需要进行验证码的验证
  96. array('room_type','require','房间类型不能为空!'), // 仅仅需要进行验证码的验证
  97. array('url','require','url不能为空!'), // 仅仅需要进行验证码的验证
  98. array('price','require','价格不能为空!'), // 仅仅需要进行验证码的验证
  99. );
  100. $monitor-> setProperty("_validate",$validate);
  101. if($_POST['id']==""){
  102. $hotel=M("Hotel");
  103. $siteid=I("post.site_name");
  104. $url=I("post.url_addr");
  105. $hotelname=I("post.hotel_name");
  106. $id=$hotel->where("site_id=".$siteid." and url='".$url."'")->getField('id');
  107. if($id){
  108. $queryDateData['hotel_id']=$id;
  109. }else {
  110. $hotelData['site_id']=$siteid;
  111. $hotelData['hotel_name']=I("post.hotel_name");
  112. $hotelData['url']=$url;
  113. $insert=$hotel->add($hotelData);
  114. $queryDateData['hotel_id']=$insert;
  115. }
  116. $priceStr=I("price");
  117. $priceArraystr=explode('|', $priceStr);
  118. for ($index=0; $index<count($priceArraystr); $index++){
  119. $datePrice = $priceArraystr[$index];
  120. $arr = explode("P", $datePrice);
  121. $temp_date = $arr[0];
  122. $price = $arr[1];
  123. $date = date('Y-m-d',strtotime($temp_date));
  124. $queryDate=M("QueryDate");
  125. $queryDateData['query_date'] = $date;
  126. $queryDateId=$queryDate->table("queryDate")->where("hotel_id='".$queryDateData['hotel_id']."' and query_date='".$date."'")->getField('id');
  127. if ($queryDateId){
  128. $roomData['date_id']=$queryDateId;
  129. }else{
  130. $in=$queryDate->table("queryDate")->add($queryDateData);
  131. if($in){
  132. $roomData['date_id']=$in;
  133. }else{
  134. echo json_encode(array("errorCode"=>1,"errorInfo"=>"保存失败"));
  135. exit;
  136. }
  137. }
  138. $room=M("Room");
  139. $roomData['room_type']=I("post.room_type");
  140. $roomData['room_price']=$price;
  141. $roomData['breakfast']=I("post.breakfast");
  142. $roomRes=$room->where("date_id='".$roomData['date_id']."' and room_type='".$roomData['room_type']."' and breakfast='".$roomData['breakfast']."'")->getField('id');
  143. if ($roomRes){
  144. $roomsave=$room->where("id=".$roomRes)->save($roomData);
  145. }else{
  146. $roomInsert=$room->add($roomData);
  147. if ($roomInsert){
  148. }else{
  149. echo json_encode(array("errorCode"=>1,"errorInfo"=>"保存失败"));
  150. exit;
  151. }
  152. }
  153. }
  154. echo json_encode(array("errorCode"=>0,"errorInfo"=>"保存成功"));
  155. exit;
  156. }else{
  157. $id=I("post.id");
  158. $hid=I("post.hid");
  159. $hotel=M("Hotel");
  160. $siteid=I("post.site_name");
  161. $hotelname=I("post.hotel_name");
  162. $url=I("post.url_addr");
  163. $hotelData['site_id']=$siteid;
  164. $hotelData['hotel_name']=I("post.hotel_name");
  165. $hotelData['url']=$url;
  166. $hotelsave=$hotel->where("id=".$hid)->save($hotelData);
  167. if ($hotelsave !== false){
  168. }else {
  169. echo json_encode(array("errorCode"=>1,"info"=>"更新失败"));
  170. exit;
  171. }
  172. $room=M("Room");
  173. $roomData['room_type']=I("post.room_type");
  174. $roomData['room_price']=I("post.price");
  175. $roomData['breakfast']=I("post.breakfast");
  176. $roomData['hotel_id']=$id;
  177. $roomsave=$room->where("id=".$id)->save($roomData);
  178. if ($roomsave !== false){
  179. echo json_encode(array("errorCode"=>0,"info"=>"更新成功"));
  180. exit;
  181. }else {
  182. echo json_encode(array("errorCode"=>1,"info"=>"更新失败"));
  183. exit;
  184. }
  185. }
  186. }else {
  187. $sql="select * from site order by id ";
  188. $res=$monitor->query($sql);
  189. $this->assign("site",$res);
  190. //$this->redirect("monitorAdd");
  191. $this->display("monitorAdd");
  192. }
  193. }
  194. public function monitorEdit(){
  195. if (!empty($_POST)){
  196. }else{
  197. $id=I("get.id");
  198. $monitor=M();
  199. $sql="select h.id as hid,r.id as rid,s.site_name,h.hotel_name,q.query_date,r.room_type,h.url,r.room_price,r.breakfast from room r inner join queryDate q on r.date_id= q.id inner join hotel h on q.hotel_id=h.id inner join site s on h.site_id=s.id where r.id=".$id;
  200. $res=$monitor->query($sql);
  201. $this->assign("edit",$res[0]);
  202. //print_r($res);
  203. $sql="select * from site order by id";
  204. $res=$monitor->query($sql);
  205. $this->assign("site",$res);
  206. $this->display("monitorEdit");
  207. }
  208. }
  209. public function monitorImport(){
  210. if ( IS_POST){
  211. $import=I('post.import');
  212. if ($import =="导入"){
  213. if ($_FILES['importFile']['error'] ==0){
  214. $response=upload();
  215. if($response[0]==1){
  216. $type=strrchr($response[1], ".");
  217. import('Org.PHPExcel.PHPExcel');
  218. if ($type==".xlsx")
  219. $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
  220. else
  221. $objReader = \PHPExcel_IOFactory::createReader('Excel5');
  222. $objReader->setReadDataOnly(true);
  223. $objPHPExcel = $objReader->load($response[1]);
  224. $objWorksheet = $objPHPExcel->getActiveSheet();
  225. $highestRow = $objWorksheet->getHighestRow();
  226. $highestColumn = $objWorksheet->getHighestColumn();
  227. $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
  228. $excelData = array();
  229. for ($row = 1; $row<= $highestRow;$row++){
  230. for ($col=0;$col<$highestColumnIndex;$col++) {
  231. $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
  232. }
  233. }
  234. $data=array();
  235. $result=array(0,0);
  236. unset($excelData[1]);
  237. $Monitor=M('Monitor');
  238. foreach ($excelData as $k=>$v){
  239. $data['site_name']=$v[0];
  240. $data['hotel_name']=$v[1];
  241. $data['room_type']=$v[2];
  242. $data['url']=$v[3];
  243. $data['price']=$v[4];
  244. $res=$Monitor->add($data);
  245. if ($res>0){
  246. $result[0]+=1;
  247. }else {
  248. $result[1]+=1;
  249. }
  250. }
  251. $this->success("导入成功".$result[0]."条"."<br>导入失败".$result[1]."条","monitorList",3);
  252. }else{
  253. $this->error($response[1]);
  254. }
  255. }else if($_FILES['importFile']['error'] ==4){
  256. $this->error("请选择上传文件");
  257. }else{
  258. $this->error("上传异常");
  259. }
  260. }
  261. }
  262. }
  263. public function priceList(){
  264. $map['site_name']=I("request.site_name");
  265. $map['hotel_name']=$_REQUEST['hotel_name'];
  266. $encode = mb_detect_encoding($map['hotel_name'], array("UTF-8","ASCII","GB2312","GBK","BIG5"));
  267. if ($encode !="UTF-8"){
  268. $map['hotel_name']=mb_convert_encoding($map['hotel_name'],"UTF-8",$encode);
  269. }
  270. $map['room_type']=$_REQUEST['room_type'];
  271. $encode = mb_detect_encoding($map['room_type'], array("UTF-8","ASCII","GB2312","GBK","BIG5"));
  272. if ($encode !="UTF-8"){
  273. $map['room_type']=mb_convert_encoding($map['room_type'],"UTF-8",$encode);
  274. }
  275. $map['breakfast_type']=$_REQUEST['breakfast_type'];
  276. // if($_REQUEST['breakfast_type']){
  277. // $map['breakfast_type']=$_REQUEST['breakfast_type'];
  278. // }else{
  279. // $map['breakfast_type'] = -1;
  280. // }
  281. $map['start_time']=$_REQUEST['start_time'];
  282. $map['end_time']=$_REQUEST['end_time'];
  283. $map['date_type']=$_REQUEST['date_type'];
  284. $monitor=M();
  285. if($map['date_type'] == 1){
  286. $sql="select r.id,s.site_name,h.hotel_name,h.url,r.room_type,r.breakfast,q.query_date,r.room_price,r.site_price from room r inner join queryDate q on r.date_id=q.id inner join hotel h on q.hotel_id= h.id inner join site s on h.site_id=s.id where r.flag=0";
  287. }else{
  288. $sql="select r.id,s.site_name,h.hotel_name,h.url,r.room_type,r.breakfast,q.query_date,r.room_price,r.site_price from room r inner join queryDate q on r.date_id=q.id inner join hotel h on q.hotel_id= h.id inner join site s on h.site_id=s.id where r.flag=1";
  289. }
  290. if($map['site_name'] >0 ){
  291. $sql.= " and s.id=".$map['site_name'];
  292. }
  293. if ($map['hotel_name'] !=""){
  294. $sql.= " and h.hotel_name like '%".$map['hotel_name']."%'";
  295. }
  296. if($map['room_type'] !=""){
  297. $sql.= " and r.room_type='".$map['room_type']."'";
  298. }
  299. if($map['breakfast_type'] !="" && $map['breakfast_type'] !="-1"){
  300. $sql.= " and r.breakfast='".$map['breakfast_type']."'";
  301. }
  302. if($map['start_time'] !="" && $map['end_time'] !=""){
  303. $sql.= "and q.query_date>='".$map['start_time']."'";
  304. $sql.= "and q.query_date<='".$map['end_time']."'";
  305. }else{
  306. $sql.= "and q.query_date>(GETDATE()-1)";
  307. }
  308. $sql.= " order by r.id";
  309. $list=$monitor->query($sql);
  310. for ($index=0; $index<count($list); $index++){
  311. $list[$index]["query_date"]=$list[$index]["query_date"].$this->wk($list[$index]["query_date"]);
  312. $list[$index]["dwk"]=$this->dwk($list[$index]["query_date"]);
  313. }
  314. $this->assign('list',$list);// 赋值数据集
  315. $this->assign('json',json_encode($list));// 赋值数据集
  316. $sql="select * from site order by id ";
  317. $siteRes=$monitor->query($sql);
  318. array_unshift($siteRes, array("id"=>0,"site_name"=>"所有"));
  319. $this->assign("site",$siteRes);
  320. $this->assign("site_name",$map['site_name']);
  321. $this->assign("hotel_name",$map['hotel_name']);
  322. $this->assign("room_type",$map['room_type']);
  323. $this->assign("breakfast",$map['breakfast_type']);
  324. $this->assign("start_time",$map['start_time']);
  325. $this->assign("end_time",$map['end_time']);
  326. $this->assign("date_type",$map['date_type']);
  327. $this->display();
  328. }
  329. public function monitorSave(){
  330. $id=I("get.id");
  331. $price=I("get.price");
  332. $room=M("Room");
  333. $data[room_price]=$price;
  334. $data[flag]=0;
  335. $roomsave=$room->where("id=".$id)->save($data);
  336. if ($roomsave !==false){
  337. echo json_encode(array("errorCode"=>0,"info"=>"保存成功"));
  338. exit;
  339. }else {
  340. echo json_encode(array("errorCode"=>1,"info"=>"保存失败"));
  341. exit;
  342. }
  343. }
  344. public function monitorSaveAll(){
  345. $priceStr=I("get.price");
  346. $room=M("Room");
  347. $priceArraystr=explode('|', $priceStr);
  348. for ($index=0; $index<count($priceArraystr); $index++){
  349. $datePrice = $priceArraystr[$index];
  350. $arr = explode("P", $datePrice);
  351. $data[room_price]=$arr[0];
  352. $data[flag]=0;
  353. $roomsave=$room->where("id=".$arr[1])->save($data);
  354. if ($roomsave !==false){
  355. //echo json_encode(array("errorCode"=>0,"info"=>"保存成功"));
  356. }else {
  357. echo json_encode(array("errorCode"=>1,"info"=>"保存失败"));
  358. exit;
  359. }
  360. }
  361. echo json_encode(array("errorCode"=>0,"info"=>"保存成功"));
  362. }
  363. public function monitorDelete(){
  364. $id=I("get.id");
  365. $monitor=M();
  366. $sql="delete from room where id=".$id;
  367. $res=$monitor->execute($sql);
  368. if ($res>0){
  369. echo json_encode(array("errorCode"=>0,"info"=>"删除成功"));
  370. exit;
  371. }else {
  372. echo json_encode(array("errorCode"=>1,"info"=>"删除失败"));
  373. exit;
  374. }
  375. }
  376. function wk($date1) {
  377. $datearr = explode("-",$date1); //将传来的时间使用“-”分割成数组
  378. $year = $datearr[0]; //获取年份
  379. $month = sprintf('%02d',$datearr[1]); //获取月份
  380. $day = sprintf('%02d',$datearr[2]); //获取日期
  381. $hour = $minute = $second = 0; //默认时分秒均为0
  382. $dayofweek = mktime($hour,$minute,$second,$month,$day,$year); //将时间转换成时间戳
  383. $shuchu = date("w",$dayofweek); //获取星期值
  384. $weekarray=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
  385. return "(".$weekarray[$shuchu].")";
  386. }
  387. function dwk($date1) {
  388. $datearr = explode("-",$date1); //将传来的时间使用“-”分割成数组
  389. $year = $datearr[0]; //获取年份
  390. $month = sprintf('%02d',$datearr[1]); //获取月份
  391. $day = sprintf('%02d',$datearr[2]); //获取日期
  392. $hour = $minute = $second = 0; //默认时分秒均为0
  393. $dayofweek = mktime($hour,$minute,$second,$month,$day,$year); //将时间转换成时间戳
  394. $shuchu = date("w",$dayofweek)+''; //获取星期值
  395. $weekarray=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
  396. return $shuchu;
  397. }
  398. }