House-copy.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class House extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'floor_id' => input('floorId',0),
  12. 'number' => input('number','','trim'),
  13. 'total_area' => input('totalArea','','trim'),
  14. 'constuction_area' => input('constuctionArea','','trim'),
  15. 'use_area' => input('useArea','','trim'),
  16. 'tenant_area' => input('tenantArea','','trim'),
  17. 'rooms' => input('rooms/d',0),
  18. 'cate_id' => input('cateId/d',0),
  19. 'company_id' => input('companyId/d',0),
  20. 'right_id' => input('rightId/d',0),
  21. 'remark' => input('remark','','trim'),
  22. 'img' => input('img','','trim'),
  23. 'img2' => input('img2','','trim'),
  24. 'enable' => input('enable/d',0),
  25. 'level_id' => input('levelId/d'),
  26. 'org_id' => input('orgId/d',0),
  27. ];
  28. $logdata = json_encode($data);
  29. $result = validate('House')->check($data,[],'');
  30. if(true !== $result){
  31. HelpHander::error(validate('House')->getError());
  32. }
  33. $id = $data['id'];
  34. unset($data['id']);
  35. if($id > 0){
  36. $data['update_time'] = date('Y-m-d H:i:s');
  37. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  38. }else{
  39. $data['create_time'] = date('Y-m-d H:i:s');
  40. $ret = $this->allowField(true)->save($data);
  41. }
  42. if(!$ret){
  43. HelpHander::error('操作失败');
  44. }
  45. if($id > 0){
  46. $content = '修改经营性房产';
  47. }else{
  48. $content = '添加经营性房产';
  49. }
  50. model('ActionLog')->add(14,$content,0,$logdata);
  51. return true;
  52. }
  53. public function info($id){
  54. $info = $this->where('id',$id)->where('del',0)->find();
  55. if(!$info){
  56. HelpHander::error('数据不存在');
  57. }
  58. $data = $info->toArray();
  59. $floor = Db::name('house_floor')->where('id',$data['floor_id'])->find();
  60. $building = Db::name('house_building')->where('id',$floor['building_id'])->find();
  61. $community = Db::name('house_community')->where('id',$building['community_id'])->find();
  62. $district = Db::name('house_district')->where('id',$community['district_id'])->find();
  63. $data['floorName'] = $floor['title'];
  64. $data['building_id'] = $floor['building_id'];
  65. $data['buildingName'] = $building['title'];
  66. $data['district_id'] = $community['district_id'];
  67. $data['community_id'] = $building['community_id'];
  68. $data['communityName'] = $community['title'];
  69. $data['districtName'] = $district['title'];
  70. $data['levelName'] = Db::name('house_level')->where('id',$data['level_id'])->value('title');
  71. $data['companyName']=Db::name('house_company')->where('id',$data['company_id'])->value('title');
  72. $data['rightName']=Db::name('house_right')->where('id',$data['right_id'])->value('title');
  73. $data['cateName']=Db::name('house_cate')->where('id',$data['cate_id'])->value('title');
  74. $data['lesseeName'] = $info['cur_lessee_id']?Db::name('house_lessee')->where('id',$info['cur_lessee_id'])->value('title'):'';
  75. $data['lesseeTime'] = '';
  76. if($info['cur_contract_id']){
  77. $contract = Db::name('house_contract')->where('id',$info['cur_contract_id'])->field('id,start_time,end_time')->find();
  78. $data['lesseeTime'] = $contract['start_time'].'~'.$contract['end_time'];
  79. }
  80. return $data;
  81. }
  82. // 获取房间当前承租人id
  83. public function getHouseCurLessee($houseId){
  84. $curDay = date('Y-m-d');
  85. $lesseeId = Db::name('house_contract_relation')
  86. ->alias('hcr')
  87. ->join('house_contract hc','hc.id = hcr.contract_id')
  88. ->where('hc.del',0)
  89. ->where('hc.status',1)
  90. ->where('hcr.house_id',$houseId)
  91. ->where('hc.start_time','<=',$curDay)
  92. ->where('hc.end_time','>=',$curDay)
  93. ->value('hc.lessee_id');
  94. return $lesseeId?$lesseeId:0;
  95. }
  96. public function del($id){
  97. $res = Db::name('house_contract_relation')
  98. ->alias('hcr')
  99. ->join('house_contract hc','hc.id = hcr.contract_id')
  100. ->where('hc.del',0)
  101. ->where('hcr.house_id',$id)
  102. ->find();
  103. if($res){
  104. HelpHander::error('已被使用,无法删除');
  105. }
  106. $ret = $this->where('id',$id)->setField('del',1);
  107. if(!$ret){
  108. HelpHander::error('删除失败');
  109. }
  110. $logdata = json_encode(['id' => $id]);
  111. model('ActionLog')->add(14,'删除经营性房产',0,$logdata);
  112. return true;
  113. }
  114. public function lists($page,$size,$title,$userId,$orgId,$floorId=0,$cateId=0,$rightId=0,$companyId=0){
  115. $map[] = ['h.org_id','=',$orgId];
  116. $map[] = ['h.del','=',0];
  117. if($floorId > 0){
  118. $map[] = ['h.floor_id','=',$floorId];
  119. }
  120. if($cateId > 0){
  121. $map[] = ['h.cate_id','=',$cateId];
  122. }
  123. if($rightId > 0){
  124. $map[] = ['h.right_id','=',$rightId];
  125. }
  126. if($companyId > 0){
  127. $map[] = ['h.company_id','=',$companyId];
  128. }
  129. $auths = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
  130. if(!$auths){
  131. $map[] = ['h.level_id','=',-1];
  132. }else{
  133. $map[] = ['h.level_id','in',$auths];
  134. }
  135. if($title){
  136. $m1 = $m2 = $m3 = $m4 = $m5 = $map;
  137. $m1[] = ['h.number','like','%'.$title.'%'];
  138. $m2[] = ['hb.title','like','%'.$title.'%'];
  139. $m3[] = ['hc.title','like','%'.$title.'%'];
  140. $m4[] = ['hd.title','like','%'.$title.'%'];
  141. $m5[] = ['hl.title','like','%'.$title.'%'];
  142. $m6[] = ['hf.title','like','%'.$title.'%'];
  143. $lists = Db::name('house')
  144. ->alias('h')
  145. ->join('house_floor hf','hf.id = h.floor_id')
  146. ->join('house_building hb','hb.id = hf.building_id')
  147. ->join('house_community hc','hc.id = hb.community_id')
  148. ->join('house_district hd','hd.id = hc.district_id')
  149. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  150. ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6])
  151. ->page($page,$size)
  152. ->field('h.*,hf.title as floorName,hb.title as buildingName,hc.title as communityName,hd.title as districtName,hl.title as lesseeName')
  153. ->order('h.id desc')
  154. ->select();
  155. }else{
  156. $lists = Db::name('house')
  157. ->alias('h')
  158. ->join('house_floor hf','hf.id = h.floor_id')
  159. ->join('house_building hb','hb.id = hf.building_id')
  160. ->join('house_community hc','hc.id = hb.community_id')
  161. ->join('house_district hd','hd.id = hc.district_id')
  162. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  163. ->where($map)
  164. ->page($page,$size)
  165. ->field('h.*,hf.title as floorName,hb.title as buildingName,hc.title as communityName,hd.title as districtName,hl.title as lesseeName')
  166. ->order('h.id desc')
  167. ->select();
  168. }
  169. foreach ($lists as $k=>$v){
  170. // $floor = Db::name('house_floor')->where('id',$v['floor_id'])->find();
  171. // $building = Db::name('house_building')->where('id',$floor['building_id'])->find();
  172. // $community = Db::name('house_community')->where('id',$building['community_id'])->find();
  173. // $district = Db::name('house_district')->where('id',$community['district_id'])->find();
  174. //
  175. // $lists[$k]['floorName'] = $floor['title'];
  176. // $lists[$k]['buildingName'] = $building['title'];
  177. // $lists[$k]['communityName'] = $community['title'];
  178. // $lists[$k]['districtName'] = $district['title'];
  179. $lists[$k]['cateName'] = Db::name('house_cate')->where('id',$v['cate_id'])->value('title');
  180. $lists[$k]['companyName'] = Db::name('house_company')->where('id',$v['company_id'])->value('title');
  181. $lists[$k]['rightName'] = Db::name('house_right')->where('id',$v['right_id'])->value('title');
  182. $lists[$k]['levelName'] = Db::name('house_level')->where('id',$v['level_id'])->value('title');
  183. // if($v['cur_lessee_id'] > 0){
  184. // $lists[$k]['lesseeName'] = Db::name('house_lessee')->where('id',$v['cur_lessee_id'])->value('title');
  185. // }else{
  186. // $lists[$k]['lesseeName'] = '';
  187. // }
  188. if($v['cur_contract_id'] > 0){
  189. $contract = Db::name('house_contract')->where('id',$v['cur_contract_id'])->field('id,start_time,end_time')->find();
  190. $lists[$k]['lesseeTime'] = $contract['start_time'].'~'.$contract['end_time'];
  191. }else{
  192. $lists[$k]['lesseeTime'] = '';
  193. }
  194. }
  195. if($title){
  196. $total = Db::name('house')
  197. ->alias('h')
  198. ->join('house_floor hf','hf.id = h.floor_id')
  199. ->join('house_building hb','hb.id = hf.building_id')
  200. ->join('house_community hc','hc.id = hb.community_id')
  201. ->join('house_district hd','hd.id = hc.district_id')
  202. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  203. ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6])
  204. ->count();
  205. $useTotal = Db::name('house')
  206. ->alias('h')
  207. ->join('house_floor hf','hf.id = h.floor_id')
  208. ->join('house_building hb','hb.id = hf.building_id')
  209. ->join('house_community hc','hc.id = hb.community_id')
  210. ->join('house_district hd','hd.id = hc.district_id')
  211. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  212. ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6])
  213. ->where('h.cur_lessee_id','>',0)
  214. ->count();
  215. }else{
  216. $total = Db::name('house')
  217. ->alias('h')
  218. ->join('house_floor hf','hf.id = h.floor_id')
  219. ->join('house_building hb','hb.id = hf.building_id')
  220. ->join('house_community hc','hc.id = hb.community_id')
  221. ->join('house_district hd','hd.id = hc.district_id')
  222. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  223. ->where($map)->count();
  224. $useTotal = Db::name('house')
  225. ->alias('h')
  226. ->join('house_floor hf','hf.id = h.floor_id')
  227. ->join('house_building hb','hb.id = hf.building_id')
  228. ->join('house_community hc','hc.id = hb.community_id')
  229. ->join('house_district hd','hd.id = hc.district_id')
  230. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  231. ->where($map)
  232. ->where('h.cur_lessee_id','>',0)
  233. ->count();
  234. }
  235. $data = [
  236. 'total' => $total,
  237. 'useTotal' => $useTotal,
  238. 'list' => $lists?$lists:[]
  239. ];
  240. return $data;
  241. }
  242. // 全部未出租的
  243. public function all($userId,$orgId){
  244. $auth = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
  245. if(!$auth){
  246. return [];
  247. }
  248. $map[] = ['level_id','in',$auth];
  249. $map[] = ['org_id','=',$orgId];
  250. $map[] = ['del','=',0];
  251. $map[] = ['enable','=',1];
  252. $map[] = ['cur_lessee_id','=',0];
  253. $lists = Db::name('house')
  254. ->where($map)
  255. ->field('id,floor_id,number')
  256. ->order('id desc')
  257. ->select();
  258. foreach ($lists as $k=>$v){
  259. $floor = Db::name('house_floor')->where('id',$v['floor_id'])->find();
  260. $building = Db::name('house_building')->where('id',$floor['building_id'])->find();
  261. $community = Db::name('house_community')->where('id',$building['community_id'])->find();
  262. $district = Db::name('house_district')->where('id',$community['district_id'])->find();
  263. $lists[$k]['title'] = $district['title'].$community['title'].$building['title'].$floor['title'].$v['number'];
  264. }
  265. return $lists;
  266. }
  267. public function houseList($floorId,$orgId,$userId){
  268. $auth = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
  269. $newlist= [];
  270. if(!$auth){
  271. return $newlist;
  272. }
  273. $m[] = ['floor_id','=',$floorId];
  274. $m[] = ['org_id','=',$orgId];
  275. $m[] = ['enable','=',1];
  276. $m[] = ['del','=',0];
  277. $m[] = ['level_id','in',$auth];
  278. $lists = Db::name('house')
  279. ->field('id,number,company_id,right_id,cate_id,cur_lessee_id')
  280. ->where($m)
  281. ->select();
  282. foreach ($lists as $k=>$val){
  283. $lists[$k]['companyName']=Db::name('house_company')->where('id',$val['company_id'])->value('title');
  284. $lists[$k]['rightName']=Db::name('house_right')->where('id',$val['right_id'])->value('title');
  285. $lists[$k]['cateName']=Db::name('house_cate')->where('id',$val['cate_id'])->value('title');
  286. $lists[$k]['lesseeName']=$val['cur_lessee_id']?Db::name('house_cate')->where('id',$val['cur_lessee_id'])->value('title'):'';
  287. }
  288. $newlist = $lists;
  289. return $newlist;
  290. }
  291. public function houseDetail($id){
  292. $info = Db::name('house')
  293. ->where('id',$id)
  294. ->find();
  295. $info['floorName']=Db::name('house_floor')->where('id',$info['floor_id'])->value('title');
  296. $info['companyName']=Db::name('house_company')->where('id',$info['company_id'])->value('title');
  297. $info['rightName']=Db::name('house_right')->where('id',$info['right_id'])->value('title');
  298. $info['cateName']=Db::name('house_cate')->where('id',$info['cate_id'])->value('title');
  299. $info['levelName']=Db::name('house_level')->where('id',$info['level_id'])->value('title');
  300. unset($info['org_id'],$info['floor_id'],$info['cate_id'],$info['company_id'],$info['right_id'],$info['enable'],$info['del']);
  301. return $info;
  302. }
  303. /**
  304. * 获取数量
  305. * @param $lessee
  306. * @return float|string
  307. */
  308. public function getCountByLessee(){
  309. $map[] = ['del','=',0];
  310. $map[] = ['enable','=',1];
  311. $curDay = date('Y-m-d');
  312. $total = Db::name('house')
  313. ->where($map)
  314. ->count();
  315. $houses = Db::name('house_contract_relation')
  316. ->alias('hcr')
  317. ->join('house_contract hc','hc.id = hcr.contract_id')
  318. ->where('hc.del',0)
  319. ->where('hc.status',1)
  320. ->where('hc.start_time','>=',$curDay)
  321. ->where('hc.end_time','<=',$curDay)
  322. ->column('hcr.house_id');
  323. $houses = $houses?array_unique($houses):[];
  324. $count = 0;
  325. if($houses){
  326. $map[] = ['id','in',$houses];
  327. $count = Db::name('house')
  328. ->where($map)
  329. ->count();
  330. }
  331. $data = [
  332. 'total' => $total, // 总数量
  333. 'count1' => $count, // 租出数量
  334. 'count2' => $total - $count
  335. ];
  336. return $data;
  337. }
  338. }