HouseCommunity.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class HouseCommunity extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'title' => input('title','','trim'),
  12. 'district_id' => input('districtId',0),
  13. 'org_id' => input('orgId/d',0),
  14. 'type' => input('type/d',1)
  15. ];
  16. $result = validate('HouseCommunity')->check($data,[],'');
  17. if(true !== $result){
  18. HelpHander::error(validate('HouseCommunity')->getError());
  19. }
  20. $id = $data['id'];
  21. unset($data['id']);
  22. if($id > 0){
  23. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  24. }else{
  25. $ret = $this->allowField(true)->save($data);
  26. }
  27. if(!$ret){
  28. HelpHander::error('操作失败');
  29. }
  30. return true;
  31. }
  32. public function del($id){
  33. $res = Db::name('HouseBuilding')->where('community_id',$id)->find();
  34. if($res){
  35. HelpHander::error('已被使用无法删除');
  36. }
  37. $ret = $this->where('id',$id)->delete();
  38. if(!$ret){
  39. HelpHander::error('删除失败');
  40. }
  41. return true;
  42. }
  43. public function lists($page,$size,$title,$districtId,$type){
  44. if($title != ''){
  45. $map[] = ['title','like','%'.$title.'%'];
  46. }
  47. if($districtId > 0){
  48. $map[] = ['district_id','=',$districtId];
  49. }
  50. $map[] = ['type','=',$type];
  51. $map = isset($map)?$map:true;
  52. $lists = $this
  53. ->where($map)
  54. ->page($page,$size)
  55. ->order('id desc')
  56. ->select();
  57. foreach ($lists as $k=>$v){
  58. $lists[$k]['districtName'] = Db::name('house_district')->where('id',$v['district_id'])->value('title');
  59. }
  60. $total = $this->where($map)->count();
  61. $data = [
  62. 'total' => $total,
  63. 'list' => $lists?$lists->toArray():[]
  64. ];
  65. return $data;
  66. }
  67. }