House.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. $lists[$k]['cateName'] = Db::name('house_cate')->where('id',$v['cate_id'])->value('title');
  171. $lists[$k]['companyName'] = Db::name('house_company')->where('id',$v['company_id'])->value('title');
  172. $lists[$k]['rightName'] = Db::name('house_right')->where('id',$v['right_id'])->value('title');
  173. $lists[$k]['levelName'] = Db::name('house_level')->where('id',$v['level_id'])->value('title');
  174. if($v['cur_contract_id'] > 0){
  175. $contract = Db::name('house_contract')->where('id',$v['cur_contract_id'])->field('id,start_time,end_time,amount')->find();
  176. $lists[$k]['lesseeTime'] = $contract['start_time'].'~'.$contract['end_time'];
  177. $lists[$k]['lesseeAmount'] = $contract['amount'];
  178. }else{
  179. $lists[$k]['lesseeTime'] = '';
  180. $lists[$k]['lesseeAmount'] = '';
  181. }
  182. }
  183. if($title){
  184. $total = Db::name('house')
  185. ->alias('h')
  186. ->join('house_floor hf','hf.id = h.floor_id')
  187. ->join('house_building hb','hb.id = hf.building_id')
  188. ->join('house_community hc','hc.id = hb.community_id')
  189. ->join('house_district hd','hd.id = hc.district_id')
  190. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  191. ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6])
  192. ->count();
  193. $useTotal = Db::name('house')
  194. ->alias('h')
  195. ->join('house_floor hf','hf.id = h.floor_id')
  196. ->join('house_building hb','hb.id = hf.building_id')
  197. ->join('house_community hc','hc.id = hb.community_id')
  198. ->join('house_district hd','hd.id = hc.district_id')
  199. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  200. ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6])
  201. ->where('h.cur_lessee_id','>',0)
  202. ->count();
  203. }else{
  204. $total = Db::name('house')
  205. ->alias('h')
  206. ->join('house_floor hf','hf.id = h.floor_id')
  207. ->join('house_building hb','hb.id = hf.building_id')
  208. ->join('house_community hc','hc.id = hb.community_id')
  209. ->join('house_district hd','hd.id = hc.district_id')
  210. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  211. ->where($map)->count();
  212. $useTotal = Db::name('house')
  213. ->alias('h')
  214. ->join('house_floor hf','hf.id = h.floor_id')
  215. ->join('house_building hb','hb.id = hf.building_id')
  216. ->join('house_community hc','hc.id = hb.community_id')
  217. ->join('house_district hd','hd.id = hc.district_id')
  218. ->join('house_lessee hl','hl.id = h.cur_lessee_id','left')
  219. ->where($map)
  220. ->where('h.cur_lessee_id','>',0)
  221. ->count();
  222. }
  223. $data = [
  224. 'total' => $total,
  225. 'useTotal' => $useTotal,
  226. 'list' => $lists?$lists:[]
  227. ];
  228. return $data;
  229. }
  230. // 全部未出租的
  231. public function all($userId,$orgId){
  232. $auth = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
  233. if(!$auth){
  234. return [];
  235. }
  236. $map[] = ['level_id','in',$auth];
  237. $map[] = ['org_id','=',$orgId];
  238. $map[] = ['del','=',0];
  239. $map[] = ['enable','=',1];
  240. $map[] = ['cur_lessee_id','=',0];
  241. $lists = Db::name('house')
  242. ->where($map)
  243. ->field('id,floor_id,number')
  244. ->order('id desc')
  245. ->select();
  246. foreach ($lists as $k=>$v){
  247. $floor = Db::name('house_floor')->where('id',$v['floor_id'])->find();
  248. $building = Db::name('house_building')->where('id',$floor['building_id'])->find();
  249. $community = Db::name('house_community')->where('id',$building['community_id'])->find();
  250. $district = Db::name('house_district')->where('id',$community['district_id'])->find();
  251. $lists[$k]['title'] = $district['title'].$community['title'].$building['title'].$floor['title'].$v['number'];
  252. }
  253. return $lists;
  254. }
  255. public function houseList($floorId,$title,$orgId,$userId){
  256. $auth = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
  257. $newlist= [];
  258. if(!$auth){
  259. return $newlist;
  260. }
  261. $m[] = ['ht.floor_id','=',$floorId];
  262. $m[] = ['ht.level_id','in',$auth];
  263. $m[] = ['ht.del','=',0];
  264. $m[] = ['ht.enable','=',1];
  265. $m[] = ['ht.org_id','=',$orgId];
  266. if($title){
  267. $m1 = $m2 = $m3 = $m4 = $m5 = $m6 = $m7 = $m;
  268. $m1[] = ['ht.number','like','%'.$title.'%'];
  269. $m2[] = ['hb.title','like','%'.$title.'%'];
  270. $m3[] = ['hc.title','like','%'.$title.'%'];
  271. $m4[] = ['hd.title','like','%'.$title.'%'];
  272. $m5[] = ['hl.title','like','%'.$title.'%'];
  273. $m6[] = ['hf.title','like','%'.$title.'%'];
  274. $m7[] = ['hl.title','like','%'.$title.'%'];
  275. $lists = Db::name('house')
  276. ->alias('ht')
  277. ->join('house_floor hf','hf.id = ht.floor_id')
  278. ->join('house_building hb','hf.building_id = hb.id')
  279. ->join('house_community hc','hb.community_id = hc.id')
  280. ->join('house_district hd','hc.district_id = hd.id')
  281. ->join('house_lessee hl','ht.cur_lessee_id = hl.id','left')
  282. ->whereOr([$m1,$m2,$m3,$m4,$m5,$m6,$m7])
  283. ->field('ht.id,ht.number,ht.company_id,ht.right_id,ht.cate_id,ht.cur_lessee_id')
  284. ->select();
  285. }else{
  286. $lists = Db::name('house')
  287. ->alias('ht')
  288. ->join('house_floor hf','hf.id = ht.floor_id')
  289. ->join('house_building hb','hf.building_id = hb.id')
  290. ->join('house_community hc','hb.community_id = hc.id')
  291. ->join('house_district hd','hc.district_id = hd.id')
  292. ->join('house_lessee hl','ht.cur_lessee_id = hl.id','left')
  293. ->where($m)
  294. ->field('ht.id,ht.number,ht.company_id,ht.right_id,ht.cate_id,ht.cur_lessee_id')
  295. ->select();
  296. }
  297. foreach ($lists as $k=>$val){
  298. $lists[$k]['companyName']=Db::name('house_company')->where('id',$val['company_id'])->value('title');
  299. $lists[$k]['rightName']=Db::name('house_right')->where('id',$val['right_id'])->value('title');
  300. $lists[$k]['cateName']=Db::name('house_cate')->where('id',$val['cate_id'])->value('title');
  301. $lists[$k]['lesseeName']=$val['cur_lessee_id']?Db::name('house_lessee')->where('id',$val['cur_lessee_id'])->value('title'):'';
  302. }
  303. $newlist = $lists;
  304. return $newlist;
  305. }
  306. public function houseDetail($id){
  307. $info = Db::name('house')
  308. ->where('id',$id)
  309. ->find();
  310. $info['floorName']=Db::name('house_floor')->where('id',$info['floor_id'])->value('title');
  311. $info['companyName']=Db::name('house_company')->where('id',$info['company_id'])->value('title');
  312. $info['rightName']=Db::name('house_right')->where('id',$info['right_id'])->value('title');
  313. $info['cateName']=Db::name('house_cate')->where('id',$info['cate_id'])->value('title');
  314. $info['levelName']=Db::name('house_level')->where('id',$info['level_id'])->value('title');
  315. unset($info['org_id'],$info['floor_id'],$info['cate_id'],$info['company_id'],$info['right_id'],$info['enable'],$info['del']);
  316. return $info;
  317. }
  318. /**
  319. * 获取数量
  320. * @param $lessee
  321. * @return float|string
  322. */
  323. public function getCountByLessee(){
  324. $map[] = ['del','=',0];
  325. $map[] = ['enable','=',1];
  326. // $curDay = date('Y-m-d');
  327. $total = Db::name('house')
  328. ->where($map)
  329. ->count();
  330. $map[] = ['cur_lessee_id','>',0];
  331. $count = Db::name('house')
  332. ->where($map)
  333. ->count();
  334. $data = [
  335. 'total' => $total, // 总数量
  336. 'count1' => $count, // 租出数量
  337. 'count2' => $total - $count
  338. ];
  339. return $data;
  340. }
  341. public function getHouseContractMoney(){
  342. $c1 = Db::name('house_contract')
  343. ->where('del',0)
  344. ->sum('amount');
  345. $c2 = Db::name('house_contract_pay')
  346. ->where('del',0)
  347. ->where('status',1)
  348. ->sum('money');
  349. return ['c1'=>$c1,'c2'=>$c2];
  350. }
  351. }