<?php

	/*******************************************************************\
	|																	|
	|	房开源（综合房产ERP管理系统）									|
	|																	|
	|	© 2019-2023 http://www.kaidandashi.com/ All rights reserved.	|
	|																	|
	|	本系统由淮南市银泰软件科技有限公司提供技术支持					|
	|																	|
	|	QQ号：984784483													|
	|																	|
	\*******************************************************************/

	namespace Admin\Controller;
	use Think\Controller;
	class YaoshiController extends CommonController {

		//钥匙列表
		public function index(){
			if (I('is_ajax')) {
				/*权限控制开始*/
				$condition = "e.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(747,0);
				if ($x && $x != "all" && $x != "me") {
					$condition .= " and e.bumen in ({$x})";
				}elseif ($x == "me") {
					$condition .= " and e.id=".session('uid');
				}elseif ($x == false) {
					$condition .= " and e.id=0";
				}
				/*权限控制结束*/
				$sousuo = I('sousuo');
				$bumen = I("get.bumen",0,'intval');
				$fylx = I('fylx');
				$fylxs = "1,13,14";
				$fylxs1 = explode(",",$fylxs);
				if (in_array($fylx,$fylxs1)) {
					$condition .= " and a.fylx = ".$fylx;
				} else {
					$condition .= " and a.fylx in (".$fylxs.")";
				}
				if ($bumen) {
					$string = get_bm_sz($bumen,2);
					$condition .= " and a.ys_bumen in ($string)";
				}
				$shijian1 = I('shijian1');
				$shijian2 = I('shijian2');
				if ($shijian1) {
					$shijian3 = strtotime($shijian1);
				}
				if ($shijian2) {
					$shijian4 = strtotime($shijian2)+86400;
				}
				if ($shijian3 and $shijian4) {
					if ($shijian3 > $shijian4) {
						$shijian3 = strtotime($shijian2);
						$shijian4 = strtotime($shijian1)+86400;
					}
					$condition .= " and a.addtime>=".$shijian3." and a.addtime<".$shijian4;
				} elseif ($shijian3) {
					$condition .= " and a.addtime>=".$shijian3;
				} elseif ($shijian4) {
					$condition .= " and a.addtime<".$shijian4;
				}
				if ($sousuo) {
					$condition .= " and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.fybianhao like '%$sousuo%') or (a.ys_bianhao like '%$sousuo%'))";
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$yspx = "order by a.".$field." ".$order;
				} else {
					$yspx = "order by a.id desc";
				}
				$Model = new \Think\Model();
				$sqlcount = "select count(*) from __YAOSHI__ a join __YONGHU__ e on a.ys_uid=e.id where {$condition}";
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,e.ygmingcheng from __YAOSHI__ a join __YONGHU__ e on a.ys_uid=e.id where {$condition} ".$yspx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				foreach ($data as $key => $value) {
					switch ($value['fylx']) {
						case '1':
							$fyb = M('fangyuan');
							$data[$key]['fylxm'] = "住宅";
							$data[$key]['fyxqlj'] = U('fangyuan/neirong',array('bianhao'=>$value['fybianhao']));
							break;
						case '13':
							$fyb = M('fyxiezilou');
							$data[$key]['fylxm'] = "写字楼";
							$data[$key]['fyxqlj'] = U('fyxiezilou/neirong',array('bianhao'=>$value['fybianhao']));
							break;
						case '14':
							$fyb = M('fyshangpu');
							$data[$key]['fylxm'] = "商铺";
							$data[$key]['fyxqlj'] = U('fyshangpu/neirong',array('bianhao'=>$value['fybianhao']));
							break;
						default:
							$fyb = "";
							$data[$key]['fylxm'] = "——";
							break;
					}
					if ($fyb) {
						$fy = $fyb->where(array('bianhao'=>$value['fybianhao']))->field('zhuangtai,weihurenid')->find();
					}
					$q_bumen = M('yaoshi_log')->where(array('ys_id'=>$value['id']))->getField('q_bumen');
					$data[$key]['fyxqqx'] = _rbac_btn_show(46,$fy['zhuangtai'],$fy['weihurenid']) == true ? '1' : '0';
					$data[$key]['rbacxq'] = _rbac_btn_show(748,0,$value['ys_uid']) ? '1' : '0';
					$data[$key]['rbacgx'] = _rbac_btn_show(749,0,$value['ys_uid']) ? '1' : '0';
					$data[$key]['rbacsc'] = _rbac_btn_show(752,0,$value['ys_uid']) ? '1' : '0';
					$data[$key]['tjren'] = zym($value['adduid'],5);
					$data[$key]['ybumen'] = zym($value['ys_bumen'],6);
					$data[$key]['xbumen'] = $q_bumen ? zym($q_bumen,6) : zym($value['ys_bumen'],6);
					$data[$key]['ys_time'] = date('Y-m-d',$value['ys_time']);
					$data[$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
					$data[$key]['status'] = $value['status'] ? '有效' : '待审核';
					$data[$key]['ys_menjin'] = $value['ys_menjin'] ? '是' : '否';
					$data[$key]['ys_weizhihao'] = $value['ys_weizhihao'] ? $value['ys_weizhihao'] : '——';
					$data[$key]['beizhu'] = $value['beizhu'] ? $value['beizhu'] : '——';
				}
				$return_data = array(
					'code'=>0,
					'count'=>$count['0']['count(*)'],
					'data'=>$data
				);
				$this->ajaxReturn($return_data);
			} else {
				$select_categorys = bumen_option(0,747);
				$this->assign("select_categorys", $select_categorys);
				$this->display();
			}
		}

		//更新钥匙
		public function edityaoshi(){
			$id = I('id',0,'int');
			$yaoshi = M('yaoshi')->where(array('id'=>$id))->find();
			$is_rbac = _rbac_btn_show(749,0,$yaoshi['ys_uid']);
			if (!$is_rbac) {
				$this->error('异常错误！');
			}
			if (IS_POST) {
				$this->error('请联系客服（19965783436）获取正式版功能！');
			} else {
				$yaoshi = M('yaoshi')->where(array('id'=>$id))->find();
				$yaoshi['ys_user'] = M('yonghu')->where(array('id'=>$yaoshi['ys_uid']))->getField('ygmingcheng');
				$parentid = $yaoshi['ys_bumen'];
				$select_categorys = bumen_option( $parentid, 739);
				$this->assign("select_categorys", $select_categorys);
				$this->assign("yaoshi", $yaoshi);
				$this->ygmingcheng = M('Yonghu')->where(array('gongsiid'=>session('gongsiid'),'lock1'=>0,'status'=>1,'bumen'=>$parentid))->select();
				$this->display();
			}
		}

		//钥匙详情
		public function content(){
			$id = I('get.id',0 ,'int');
			$find = M('yaoshi')->where(array('id'=>$id))->find();
			if (!$find) {
				echo "没找到资源！ -400";
				die;
			}
			$is_rbac = _rbac_btn_show(748,0,$find['ys_uid']);
			if (!$is_rbac) {
				$this->error('异常错误！');
			}
			$imgs = M('photos')->where(array('data_id'=>$id,'data_table'=>'yaoshi','status'=>1))->select();
			$user = M('yonghu')->where(array('id'=>$find['ys_uid']))->field('ygmingcheng,bumen')->find();
			$useradd = M('yonghu')->where(array('id'=>$find['adduid']))->field('ygmingcheng,bumen')->find();
			$Model = new \Think\Model();
			$sql_log = "select a.*,b.ygmingcheng as q_uid, c.ygmingcheng as adduid from (select * from __YAOSHI_LOG__ where ys_id=".$find['id'].") as a join __YONGHU__ as b on a.q_uid=b.id left join __YONGHU__ as c on a.adduid=c.id order by q_time DESC";
			$this->yaoshi_log = $Model->query($sql_log);
			$this->user = $user;
			$this->useradd = $useradd;
			$this->find = $find;
			$this->imgs = $imgs;
			$this->display();
		}

		//删除钥匙
		public function delyaoshi(){
			$id = I('post.id',0 ,'int');
			$yaoshi = M('yaoshi')->where(array('id'=>$id))->find();
			$is_rbac = _rbac_btn_show(752,0,$yaoshi['ys_uid']);
			if (!$is_rbac) {
				$this->error('异常错误！');
			}
			$this->error('请联系客服（19965783436）获取正式版功能！');
		}

	}