123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Model;
- class HouseDistrict extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'title' => input('title','','trim'),
- 'org_id' => input('orgId/d',0),
- 'type' => input('type/d',1)
- ];
- $result = validate('HouseDistrict')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('HouseDistrict')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $ret = $this->allowField(true)->save($data);
- }
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function del($id){
- $res = Db::name('HouseCommunity')->where('district_id',$id)->find();
- if($res){
- HelpHander::error('已被使用无法删除');
- }
- $ret = $this->where('id',$id)->delete();
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- public function lists($page,$size,$title,$orgId,$type){
- $map[] = ['org_id','=',$orgId];
- if($title != ''){
- $map[] = ['title','like','%'.$title.'%'];
- }
- $map[] = ['type','=',$type];
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists->toArray():[]
- ];
- return $data;
- }
- public function all($orgId,$type){
- $map[] = ['org_id','=',$orgId];
- $map[] = ['type','=',$type];
- $lists = $this
- ->where($map)
- ->field('id,title')
- ->order('id desc')
- ->select();
- return $lists?$lists->toArray():[];
- }
- public function listAndChild($orgId,$type,$c=2){
- $map[] = ['org_id','=',$orgId];
- $map[] = ['type','=',$type];
- $lists = Db::name('house_district')
- ->where($map)
- ->field('id,title')
- ->select();
- foreach ($lists as $k=>$v){
- $children = Db::name('house_community')
- ->where('district_id',$v['id'])
- ->where('type',$type)
- ->field('id,title')
- ->select();
- if($c == 3||$c == 4){
- foreach ($children as $kk=>$vv){
- $children2 = Db::name('house_building')
- ->where('community_id',$vv['id'])
- ->where('type',$type)
- ->field('id,title')
- ->select();
- if($c == 4){
- foreach ($children2 as $kkk=>$vvv){
- $children3 = Db::name('house_floor')
- ->where('building_id',$vvv['id'])
- ->field('id,title')
- ->select();
- $children2[$kkk]['children'] = $children3?$children3:[];
- }
- }
- $children[$kk]['children'] = $children2?$children2:[];
- }
- }
- $lists[$k]['children'] = $children?$children:[];
- }
- return $lists?$lists:[];
- }
- }
|