123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Model;
- class House extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'floor_id' => input('floorId',0),
- 'number' => input('number','','trim'),
- 'total_area' => input('totalArea','','trim'),
- 'constuction_area' => input('constuctionArea','','trim'),
- 'use_area' => input('useArea','','trim'),
- 'tenant_area' => input('tenantArea','','trim'),
- 'rooms' => input('rooms/d',0),
- 'cate_id' => input('cateId/d',0),
- 'company_id' => input('companyId/d',0),
- 'right_id' => input('rightId/d',0),
- 'remark' => input('remark','','trim'),
- 'img' => input('img','','trim'),
- 'img2' => input('img2','','trim'),
- 'enable' => input('enable/d',0),
- 'level_id' => input('levelId/d'),
- 'org_id' => input('orgId/d',0),
- ];
- $logdata = json_encode($data);
- $result = validate('House')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('House')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data);
- }
- if(!$ret){
- HelpHander::error('操作失败');
- }
- if($id > 0){
- $content = '修改经营性房产';
- }else{
- $content = '添加经营性房产';
- }
- model('ActionLog')->add(14,$content,0,$logdata);
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $data = $info->toArray();
- $floor = Db::name('house_floor')->where('id',$data['floor_id'])->find();
- $building = Db::name('house_building')->where('id',$floor['building_id'])->find();
- $community = Db::name('house_community')->where('id',$building['community_id'])->find();
- $district = Db::name('house_district')->where('id',$community['district_id'])->find();
- $data['floorName'] = $floor['title'];
- $data['building_id'] = $floor['building_id'];
- $data['buildingName'] = $building['title'];
- $data['district_id'] = $community['district_id'];
- $data['community_id'] = $building['community_id'];
- $data['communityName'] = $community['title'];
- $data['districtName'] = $district['title'];
- $data['levelName'] = Db::name('house_level')->where('id',$data['level_id'])->value('title');
- $data['companyName']=Db::name('house_company')->where('id',$data['company_id'])->value('title');
- $data['rightName']=Db::name('house_right')->where('id',$data['right_id'])->value('title');
- $data['cateName']=Db::name('house_cate')->where('id',$data['cate_id'])->value('title');
- $data['lesseeName'] = $info['cur_lessee_id']?Db::name('house_lessee')->where('id',$info['cur_lessee_id'])->value('title'):'';
- $data['lesseeTime'] = '';
- if($info['cur_contract_id']){
- $contract = Db::name('house_contract')->where('id',$info['cur_contract_id'])->field('id,start_time,end_time')->find();
- $data['lesseeTime'] = $contract['start_time'].'~'.$contract['end_time'];
- }
- return $data;
- }
- // 获取房间当前承租人id
- public function getHouseCurLessee($houseId){
- $curDay = date('Y-m-d');
- $lesseeId = Db::name('house_contract_relation')
- ->alias('hcr')
- ->join('house_contract hc','hc.id = hcr.contract_id')
- ->where('hc.del',0)
- ->where('hc.status',1)
- ->where('hcr.house_id',$houseId)
- ->where('hc.start_time','<=',$curDay)
- ->where('hc.end_time','>=',$curDay)
- ->value('hc.lessee_id');
- return $lesseeId?$lesseeId:0;
- }
- public function del($id){
- $res = Db::name('house_contract_relation')
- ->alias('hcr')
- ->join('house_contract hc','hc.id = hcr.contract_id')
- ->where('hc.del',0)
- ->where('hcr.house_id',$id)
- ->find();
- if($res){
- HelpHander::error('已被使用,无法删除');
- }
- $ret = $this->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- $logdata = json_encode(['id' => $id]);
- model('ActionLog')->add(14,'删除经营性房产',0,$logdata);
- return true;
- }
- public function lists($page,$size,$title,$userId,$orgId,$floorId=0,$cateId=0,$rightId=0,$companyId=0){
- $map[] = ['h.org_id','=',$orgId];
- $map[] = ['h.del','=',0];
- if($floorId > 0){
- $map[] = ['h.floor_id','=',$floorId];
- }
- if($cateId > 0){
- $map[] = ['h.cate_id','=',$cateId];
- }
- if($rightId > 0){
- $map[] = ['h.right_id','=',$rightId];
- }
- if($companyId > 0){
- $map[] = ['h.company_id','=',$companyId];
- }
- $auths = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
- if(!$auths){
- $map[] = ['h.level_id','=',-1];
- }else{
- $map[] = ['h.level_id','in',$auths];
- }
- if($title){
- $m1 = $m2 = $m3 = $m4 = $m5 = $map;
- $m1[] = ['h.number','like','%'.$title.'%'];
- $m2[] = ['hb.title','like','%'.$title.'%'];
- $m3[] = ['hc.title','like','%'.$title.'%'];
- $m4[] = ['hd.title','like','%'.$title.'%'];
- $m5[] = ['hl.title','like','%'.$title.'%'];
- $m6[] = ['hf.title','like','%'.$title.'%'];
- $lists = Db::name('house')
- ->alias('h')
- ->join('house_floor hf','hf.id = h.floor_id')
- ->join('house_building hb','hb.id = hf.building_id')
- ->join('house_community hc','hc.id = hb.community_id')
- ->join('house_district hd','hd.id = hc.district_id')
- ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
- ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6])
- ->page($page,$size)
- ->field('h.*,hf.title as floorName,hb.title as buildingName,hc.title as communityName,hd.title as districtName,hl.title as lesseeName')
- ->order('h.id desc')
- ->select();
- }else{
- $lists = Db::name('house')
- ->alias('h')
- ->join('house_floor hf','hf.id = h.floor_id')
- ->join('house_building hb','hb.id = hf.building_id')
- ->join('house_community hc','hc.id = hb.community_id')
- ->join('house_district hd','hd.id = hc.district_id')
- ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
- ->where($map)
- ->page($page,$size)
- ->field('h.*,hf.title as floorName,hb.title as buildingName,hc.title as communityName,hd.title as districtName,hl.title as lesseeName')
- ->order('h.id desc')
- ->select();
- }
- foreach ($lists as $k=>$v){
- $lists[$k]['cateName'] = Db::name('house_cate')->where('id',$v['cate_id'])->value('title');
- $lists[$k]['companyName'] = Db::name('house_company')->where('id',$v['company_id'])->value('title');
- $lists[$k]['rightName'] = Db::name('house_right')->where('id',$v['right_id'])->value('title');
- $lists[$k]['levelName'] = Db::name('house_level')->where('id',$v['level_id'])->value('title');
- if($v['cur_contract_id'] > 0){
- $contract = Db::name('house_contract')->where('id',$v['cur_contract_id'])->field('id,start_time,end_time,amount')->find();
- $lists[$k]['lesseeTime'] = $contract['start_time'].'~'.$contract['end_time'];
- $lists[$k]['lesseeAmount'] = $contract['amount'];
- }else{
- $lists[$k]['lesseeTime'] = '';
- $lists[$k]['lesseeAmount'] = '';
- }
- }
- if($title){
- $total = Db::name('house')
- ->alias('h')
- ->join('house_floor hf','hf.id = h.floor_id')
- ->join('house_building hb','hb.id = hf.building_id')
- ->join('house_community hc','hc.id = hb.community_id')
- ->join('house_district hd','hd.id = hc.district_id')
- ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
- ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6])
- ->count();
- $useTotal = Db::name('house')
- ->alias('h')
- ->join('house_floor hf','hf.id = h.floor_id')
- ->join('house_building hb','hb.id = hf.building_id')
- ->join('house_community hc','hc.id = hb.community_id')
- ->join('house_district hd','hd.id = hc.district_id')
- ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
- ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6])
- ->where('h.cur_lessee_id','>',0)
- ->count();
- }else{
- $total = Db::name('house')
- ->alias('h')
- ->join('house_floor hf','hf.id = h.floor_id')
- ->join('house_building hb','hb.id = hf.building_id')
- ->join('house_community hc','hc.id = hb.community_id')
- ->join('house_district hd','hd.id = hc.district_id')
- ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
- ->where($map)->count();
- $useTotal = Db::name('house')
- ->alias('h')
- ->join('house_floor hf','hf.id = h.floor_id')
- ->join('house_building hb','hb.id = hf.building_id')
- ->join('house_community hc','hc.id = hb.community_id')
- ->join('house_district hd','hd.id = hc.district_id')
- ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
- ->where($map)
- ->where('h.cur_lessee_id','>',0)
- ->count();
- }
- $data = [
- 'total' => $total,
- 'useTotal' => $useTotal,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- // 全部未出租的
- public function all($userId,$orgId){
- $auth = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
- if(!$auth){
- return [];
- }
- $map[] = ['level_id','in',$auth];
- $map[] = ['org_id','=',$orgId];
- $map[] = ['del','=',0];
- $map[] = ['enable','=',1];
- $map[] = ['cur_lessee_id','=',0];
- $lists = Db::name('house')
- ->where($map)
- ->field('id,floor_id,number')
- ->order('id desc')
- ->select();
- foreach ($lists as $k=>$v){
- $floor = Db::name('house_floor')->where('id',$v['floor_id'])->find();
- $building = Db::name('house_building')->where('id',$floor['building_id'])->find();
- $community = Db::name('house_community')->where('id',$building['community_id'])->find();
- $district = Db::name('house_district')->where('id',$community['district_id'])->find();
- $lists[$k]['title'] = $district['title'].$community['title'].$building['title'].$floor['title'].$v['number'];
- }
- return $lists;
- }
- public function houseList($floorId,$title,$orgId,$userId){
- $auth = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
- $newlist= [];
- if(!$auth){
- return $newlist;
- }
- $m[] = ['ht.floor_id','=',$floorId];
- $m[] = ['ht.level_id','in',$auth];
- $m[] = ['ht.del','=',0];
- $m[] = ['ht.enable','=',1];
- $m[] = ['ht.org_id','=',$orgId];
- if($title){
- $m1 = $m2 = $m3 = $m4 = $m5 = $m6 = $m7 = $m;
- $m1[] = ['ht.number','like','%'.$title.'%'];
- $m2[] = ['hb.title','like','%'.$title.'%'];
- $m3[] = ['hc.title','like','%'.$title.'%'];
- $m4[] = ['hd.title','like','%'.$title.'%'];
- $m5[] = ['hl.title','like','%'.$title.'%'];
- $m6[] = ['hf.title','like','%'.$title.'%'];
- $m7[] = ['hl.title','like','%'.$title.'%'];
- $lists = Db::name('house')
- ->alias('ht')
- ->join('house_floor hf','hf.id = ht.floor_id')
- ->join('house_building hb','hf.building_id = hb.id')
- ->join('house_community hc','hb.community_id = hc.id')
- ->join('house_district hd','hc.district_id = hd.id')
- ->join('house_lessee hl','ht.cur_lessee_id = hl.id','left')
- ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6,$m7])
- ->field('ht.id,ht.number,ht.company_id,ht.right_id,ht.cate_id,ht.cur_lessee_id')
- ->select();
- }else{
- $lists = Db::name('house')
- ->alias('ht')
- ->join('house_floor hf','hf.id = ht.floor_id')
- ->join('house_building hb','hf.building_id = hb.id')
- ->join('house_community hc','hb.community_id = hc.id')
- ->join('house_district hd','hc.district_id = hd.id')
- ->join('house_lessee hl','ht.cur_lessee_id = hl.id','left')
- ->where($m)
- ->field('ht.id,ht.number,ht.company_id,ht.right_id,ht.cate_id,ht.cur_lessee_id')
- ->select();
- }
- foreach ($lists as $k=>$val){
- $lists[$k]['companyName']=Db::name('house_company')->where('id',$val['company_id'])->value('title');
- $lists[$k]['rightName']=Db::name('house_right')->where('id',$val['right_id'])->value('title');
- $lists[$k]['cateName']=Db::name('house_cate')->where('id',$val['cate_id'])->value('title');
- $lists[$k]['lesseeName']=$val['cur_lessee_id']?Db::name('house_lessee')->where('id',$val['cur_lessee_id'])->value('title'):'';
- }
- $newlist = $lists;
- return $newlist;
- }
- public function houseDetail($id){
- $info = Db::name('house')
- ->where('id',$id)
- ->find();
- $info['floorName']=Db::name('house_floor')->where('id',$info['floor_id'])->value('title');
- $info['companyName']=Db::name('house_company')->where('id',$info['company_id'])->value('title');
- $info['rightName']=Db::name('house_right')->where('id',$info['right_id'])->value('title');
- $info['cateName']=Db::name('house_cate')->where('id',$info['cate_id'])->value('title');
- $info['levelName']=Db::name('house_level')->where('id',$info['level_id'])->value('title');
- unset($info['org_id'],$info['floor_id'],$info['cate_id'],$info['company_id'],$info['right_id'],$info['enable'],$info['del']);
- return $info;
- }
- /**
- * 获取数量
- * @param $lessee
- * @return float|string
- */
- public function getCountByLessee(){
- $map[] = ['del','=',0];
- $map[] = ['enable','=',1];
- // $curDay = date('Y-m-d');
- $total = Db::name('house')
- ->where($map)
- ->count();
- $map[] = ['cur_lessee_id','>',0];
- $count = Db::name('house')
- ->where($map)
- ->count();
- $data = [
- 'total' => $total, // 总数量
- 'count1' => $count, // 租出数量
- 'count2' => $total - $count
- ];
- return $data;
- }
- public function getHouseContractMoney(){
- $c1 = Db::name('house_contract')
- ->where('del',0)
- ->sum('amount');
- $c2 = Db::name('house_contract_pay')
- ->where('del',0)
- ->where('status',1)
- ->sum('money');
- return ['c1'=>$c1,'c2'=>$c2];
- }
- }
|