HouseDistrict.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class HouseDistrict extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'title' => input('title','','trim'),
  12. 'org_id' => input('orgId/d',0),
  13. 'type' => input('type/d',1)
  14. ];
  15. $result = validate('HouseDistrict')->check($data,[],'');
  16. if(true !== $result){
  17. HelpHander::error(validate('HouseDistrict')->getError());
  18. }
  19. $id = $data['id'];
  20. unset($data['id']);
  21. if($id > 0){
  22. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  23. }else{
  24. $ret = $this->allowField(true)->save($data);
  25. }
  26. if(!$ret){
  27. HelpHander::error('操作失败');
  28. }
  29. return true;
  30. }
  31. public function del($id){
  32. $res = Db::name('HouseCommunity')->where('district_id',$id)->find();
  33. if($res){
  34. HelpHander::error('已被使用无法删除');
  35. }
  36. $ret = $this->where('id',$id)->delete();
  37. if(!$ret){
  38. HelpHander::error('删除失败');
  39. }
  40. return true;
  41. }
  42. public function lists($page,$size,$title,$orgId,$type){
  43. $map[] = ['org_id','=',$orgId];
  44. if($title != ''){
  45. $map[] = ['title','like','%'.$title.'%'];
  46. }
  47. $map[] = ['type','=',$type];
  48. $lists = $this
  49. ->where($map)
  50. ->page($page,$size)
  51. ->order('id desc')
  52. ->select();
  53. $total = $this->where($map)->count();
  54. $data = [
  55. 'total' => $total,
  56. 'list' => $lists?$lists->toArray():[]
  57. ];
  58. return $data;
  59. }
  60. public function all($orgId,$type){
  61. $map[] = ['org_id','=',$orgId];
  62. $map[] = ['type','=',$type];
  63. $lists = $this
  64. ->where($map)
  65. ->field('id,title')
  66. ->order('id desc')
  67. ->select();
  68. return $lists?$lists->toArray():[];
  69. }
  70. public function listAndChild($orgId,$type,$c=2){
  71. $map[] = ['org_id','=',$orgId];
  72. $map[] = ['type','=',$type];
  73. $lists = Db::name('house_district')
  74. ->where($map)
  75. ->field('id,title')
  76. ->select();
  77. foreach ($lists as $k=>$v){
  78. $children = Db::name('house_community')
  79. ->where('district_id',$v['id'])
  80. ->where('type',$type)
  81. ->field('id,title')
  82. ->select();
  83. if($c == 3||$c == 4){
  84. foreach ($children as $kk=>$vv){
  85. $children2 = Db::name('house_building')
  86. ->where('community_id',$vv['id'])
  87. ->where('type',$type)
  88. ->field('id,title')
  89. ->select();
  90. if($c == 4){
  91. foreach ($children2 as $kkk=>$vvv){
  92. $children3 = Db::name('house_floor')
  93. ->where('building_id',$vvv['id'])
  94. ->field('id,title')
  95. ->select();
  96. $children2[$kkk]['children'] = $children3?$children3:[];
  97. }
  98. }
  99. $children[$kk]['children'] = $children2?$children2:[];
  100. }
  101. }
  102. $lists[$k]['children'] = $children?$children:[];
  103. }
  104. return $lists?$lists:[];
  105. }
  106. }