HouseTurn.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use app\Request;
  5. use think\Db;
  6. use think\Model;
  7. class HouseTurn extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'title' => input('title','','trim'),
  13. 'number' => input('number'),//门牌号
  14. 'tenant_area' => input('tenantArea'),//面积
  15. 'spec' => input('spec'),//规格
  16. 'remark' => input('remark'),//备注
  17. 'imgs' => input('imgs'),//房屋图片
  18. 'img2' => input('img2'),//产权证
  19. 'enable' => input('enable'),
  20. 'create_time' => date("Y-m-d H:i:s",time()),
  21. 'update_time' => date("Y-m-d H:i:s",time()),
  22. 'building_id' => input('buildingId/d'),
  23. 'level_id' => input('levelId/d'),
  24. 'org_id' => input('orgId/d',0),
  25. 'important' => input('important/d',0),
  26. 'aspect' => input('aspect','','trim'),
  27. 'unit' => input('unit','','trim'),
  28. 'decoration' => input('decoration/d',0),
  29. 'decoration_time' => input('decorationTime','','trim'),
  30. ];
  31. $logdata = json_encode($data);
  32. if(!$data['decoration_time']){
  33. unset($data['decoration_time']);
  34. }
  35. if($data['unit'] && is_numeric($data['unit'])){ // 如果只有数据加上单元
  36. $data['unit'] = $data['unit'].'单元';
  37. }
  38. $result = validate('HouseTurn')->check($data,[],'');
  39. if(true !== $result){
  40. HelpHander::error(validate('HouseTurn')->getError());
  41. }
  42. $id = $data['id'];
  43. unset($data['id']);
  44. if($id > 0){
  45. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  46. }else{
  47. $ret = $this->allowField(true)->save($data);
  48. }
  49. if(!$ret){
  50. HelpHander::error('操作失败');
  51. }
  52. if($id > 0){
  53. $content = '修改周转房';
  54. }else{
  55. $content = '添加周转房';
  56. }
  57. model('ActionLog')->add(16,$content,0,$logdata);
  58. return true;
  59. }
  60. public function info($id){
  61. $info = $this->where('id',$id)->find();
  62. if(!$info){
  63. HelpHander::error('数据不存在');
  64. }
  65. $data = $info->toArray();
  66. $building = Db::name('house_building')->where('id',$data['building_id'])->find();
  67. $community = Db::name('house_community')->where('id',$building['community_id'])->find();
  68. $district = Db::name('house_district')->where('id',$community['district_id'])->find();
  69. $data['buildingName'] = $building['title'];
  70. $data['district_id'] = $community['district_id'];
  71. $data['community_id'] = $building['community_id'];
  72. $data['communityName'] = $community['title'];
  73. $data['districtName'] = $district['title'];
  74. $data['levelName'] = Db::name('house_level')->where('id',$data['level_id'])->value('title');
  75. if($data['cur_lessee_id'] > 0){
  76. $data['lesseeName'] = Db::name('house_lessee')->where('id',$data['cur_lessee_id'])->value('title');
  77. }else{
  78. $data['lesseeName'] = '';
  79. }
  80. return $data;
  81. }
  82. public function del($id){
  83. $ret = $this->where('id',$id)->update(['del'=>1]);
  84. if(!$ret){
  85. HelpHander::error('删除失败');
  86. }
  87. $logdata = json_encode(['id' => $id]);
  88. model('ActionLog')->add(16,'删除周转房',0,$logdata);
  89. return true;
  90. }
  91. public function lists($page,$size,$title,$status,$enable,$lessee,$userId,$orgId,$type=0,$vacancy=0){
  92. $map[] = ['org_id','=',$orgId];
  93. $map[] = ['del','=',0];
  94. if($title != ''){
  95. $map[] = ['title','like','%'.$title.'%'];
  96. }
  97. if($lessee != ''){
  98. $map[] = ['lessee_name','like','%'.$lessee.'%'];
  99. }
  100. if($status >= 0){
  101. $map[] = ['status','=','%'.$status.'%'];
  102. }
  103. if($enable >= 0){
  104. $map[] = ['enable','=','%'.$enable.'%'];
  105. }
  106. if($type > 0){
  107. $map[] = ['type','=','%'.$type.'%'];
  108. }
  109. if($vacancy == 1){ //空置
  110. $map[] = ['cur_lessee_id','=',0];
  111. }else if($vacancy == 1){ //在用
  112. $map[] = ['cur_lessee_id','>',0];
  113. }
  114. $auths = Db::name('house_auth')->where('user_id',$userId)->where('type',2)->value('ids');
  115. if(!$auths){
  116. $map[] = ['level_id','=',-1];
  117. }else{
  118. $map[] = ['level_id','in',$auths];
  119. }
  120. $lists = Db::name('house_turn_list')
  121. ->where($map)
  122. ->page($page,$size)
  123. ->order('id desc')
  124. ->select();
  125. $lists = $lists?$lists:[];
  126. foreach ($lists as $k=>$v){
  127. $lists[$k]['levelName'] = Db::name('house_level')->where('id',$v['level_id'])->value('title');
  128. }
  129. $total = Db::name('house_turn_list')
  130. ->where($map)->count();
  131. // 使用
  132. $usecount = Db::name('house_turn_list')
  133. ->where($map)
  134. ->where('cur_lessee_id','>',0)
  135. ->count();
  136. // 重点
  137. $zdcount = Db::name('house_turn_list')
  138. ->where($map)
  139. ->where('important','=',1)
  140. ->count();
  141. $data = [
  142. 'total' => $total,
  143. 'useTotal' => $usecount,
  144. 'kzTotal' => $total - $usecount,
  145. 'list' => $lists
  146. ];
  147. return $data;
  148. }
  149. public function uhlists($page,$size,$title,$status,$lessee,$userId,$orgId,$startTime,$endTime){
  150. $map[] = ['org_id','=',$orgId];
  151. if($title != ''){
  152. $map[] = ['title','like','%'.$title.'%'];
  153. }
  154. if($lessee != ''){
  155. $map[] = ['lessee_name','like','%'.$lessee.'%'];
  156. }
  157. if($status >= 0){
  158. $map[] = ['status','=','%'.$status.'%'];
  159. }
  160. if($startTime && $endTime){
  161. $map[] = ['start_time','>=',$startTime];
  162. $map[] = ['start_time','<=',$endTime];
  163. }
  164. $auths = Db::name('house_auth')->where('user_id',$userId)->where('type',2)->value('ids');
  165. if(!$auths){
  166. $map[] = ['level_id','=',-1];
  167. }else{
  168. $map[] = ['level_id','in',$auths];
  169. }
  170. $lists = Db::name('house_turn_lessee_list')
  171. ->where($map)
  172. ->page($page,$size)
  173. ->order('id desc')
  174. ->select();
  175. $lists = $lists?$lists:[];
  176. foreach ($lists as $k=>$v){
  177. $lists[$k]['levelName'] = Db::name('house_level')->where('id',$v['level_id'])->value('title');
  178. }
  179. $total = Db::name('house_turn_lessee_list')
  180. ->where($map)->count();
  181. $data = [
  182. 'total' => $total,
  183. 'list' => $lists
  184. ];
  185. return $data;
  186. }
  187. public function listsold($page,$size,$title,$status,$enable,$lessee,$userId,$orgId){
  188. $map[] = ['ht.org_id','=',$orgId];
  189. $map[] = ['ht.del','=',0];
  190. if($title != ''){
  191. $map[] = ['ht.number','like','%'.$title.'%'];
  192. }
  193. if($lessee != ''){
  194. $map[] = ['hl.title','like','%'.$lessee.'%'];
  195. }
  196. if($status >= 0){
  197. $map[] = ['ht.status','=','%'.$lessee.'%'];
  198. }
  199. if($enable >= 0){
  200. $map[] = ['ht.enable','=','%'.$enable.'%'];
  201. }
  202. $auths = Db::name('house_auth')->where('user_id',$userId)->where('type',2)->value('ids');
  203. if(!$auths){
  204. $map[] = ['ht.level_id','=',-1];
  205. }else{
  206. $map[] = ['ht.level_id','in',$auths];
  207. }
  208. $lists = Db::name('house_turn')
  209. ->alias('ht')
  210. ->join('house_building hb','hb.id = ht.building_id')
  211. ->join('house_community hc','hc.id = hb.community_id')
  212. ->join('house_district hd','hd.id = hc.district_id')
  213. ->join('house_lessee hl','hl.id = ht.cur_lessee_id','left')
  214. ->field('ht.*,hb.title as buildingName,hc.title as communityName,hc.id as community_id,hd.title as districtName,hd.id as distract_id,hl.title as lesseeName')
  215. ->where($map)
  216. ->page($page,$size)
  217. ->order('ht.id desc')
  218. ->select();
  219. // $lists = $this
  220. // ->where($map)
  221. // ->page($page,$size)
  222. // ->order('id desc')
  223. // ->select();
  224. $lists = $lists?$lists:[];
  225. foreach ($lists as $k=>$v){
  226. // $building = Db::name('house_building')->where('id',$v['building_id'])->find();
  227. // $community = Db::name('house_community')->where('id',$building['community_id'])->find();
  228. // $district = Db::name('house_district')->where('id',$community['district_id'])->find();
  229. //
  230. // $lists[$k]['buildingName'] = $building['title'];
  231. // $lists[$k]['district_id'] = $community['district_id'];
  232. // $lists[$k]['community_id'] = $building['community_id'];
  233. // $lists[$k]['communityName'] = $community['title'];
  234. // $lists[$k]['districtName'] = $district['title'];
  235. $lists[$k]['levelName'] = Db::name('house_level')->where('id',$v['level_id'])->value('title');
  236. // if($v['cur_lessee_id'] > 0){
  237. // $lists[$k]['lesseeName'] = Db::name('house_lessee')->where('id',$v['cur_lessee_id'])->value('title');
  238. // }else{
  239. // $lists[$k]['lesseeName'] = '';
  240. // }
  241. }
  242. $total = Db::name('house_turn')
  243. ->alias('ht')
  244. ->join('house_building hb','hb.id = ht.building_id')
  245. ->join('house_community hc','hc.id = hb.community_id')
  246. ->join('house_district hd','hd.id = hc.district_id')
  247. ->join('house_lessee hl','hl.id = ht.cur_lessee_id','left')
  248. ->where($map)->count();
  249. // 使用
  250. $usecount = Db::name('house_turn')
  251. ->alias('ht')
  252. ->join('house_building hb','hb.id = ht.building_id')
  253. ->join('house_community hc','hc.id = hb.community_id')
  254. ->join('house_district hd','hd.id = hc.district_id')
  255. ->join('house_lessee hl','hl.id = ht.cur_lessee_id','left')
  256. ->where($map)
  257. ->where('ht.cur_lessee_id','>',0)
  258. ->count();
  259. // 重点
  260. $zdcount = Db::name('house_turn')
  261. ->alias('ht')
  262. ->join('house_building hb','hb.id = ht.building_id')
  263. ->join('house_community hc','hc.id = hb.community_id')
  264. ->join('house_district hd','hd.id = hc.district_id')
  265. ->join('house_lessee hl','hl.id = ht.cur_lessee_id','left')
  266. ->where($map)
  267. ->where('ht.important','=',1)
  268. ->count();
  269. $data = [
  270. 'total' => $total,
  271. 'useTotal' => $usecount,
  272. 'kzTotal' => $total - $usecount,
  273. 'list' => $lists
  274. ];
  275. return $data;
  276. }
  277. public function export($title,$status,$enable,$lessee,$userId,$orgId,$vacancy=0){
  278. $map[] = ['ht.org_id','=',$orgId];
  279. $map[] = ['ht.del','=',0];
  280. if($title != ''){
  281. $map[] = ['ht.number','like','%'.$title.'%'];
  282. }
  283. if($lessee != ''){
  284. $map[] = ['hl.title','like','%'.$lessee.'%'];
  285. }
  286. if($status >= 0){
  287. $map[] = ['ht.status','=','%'.$lessee.'%'];
  288. }
  289. if($enable >= 0){
  290. $map[] = ['ht.enable','=','%'.$enable.'%'];
  291. }
  292. if($vacancy == 1){ //空置
  293. $map[] = ['ht.cur_lessee_id','=',0];
  294. }else if($vacancy == 1){ //在用
  295. $map[] = ['ht.cur_lessee_id','>',0];
  296. }
  297. $auths = Db::name('house_auth')->where('user_id',$userId)->where('type',2)->value('ids');
  298. if(!$auths){
  299. $map[] = ['ht.level_id','=',-1];
  300. }else{
  301. $map[] = ['ht.level_id','in',$auths];
  302. }
  303. $lists = Db::name('house_turn')
  304. ->alias('ht')
  305. ->join('house_building hb','hb.id = ht.building_id')
  306. ->join('house_community hc','hc.id = hb.community_id')
  307. ->join('house_district hd','hd.id = hc.district_id')
  308. ->join('house_lessee hl','hl.id = ht.cur_lessee_id','left')
  309. ->field('ht.*,hb.title as buildingName,hc.title as communityName,hc.id as community_id,hd.title as districtName,hd.id as distract_id,hl.title as lesseeName')
  310. ->where($map)
  311. ->order('ht.id desc')
  312. ->select();
  313. $lists = $lists?$lists:[];
  314. foreach ($lists as $k=>$v){
  315. $lists[$k]['levelName'] = Db::name('house_level')->where('id',$v['level_id'])->value('title');
  316. if($v['enable'] == 1){
  317. $lists[$k]['enable_text'] = "启用";
  318. }else{
  319. $lists[$k]['enable_text'] = "禁用";
  320. }
  321. if($v['decoration'] == 1){
  322. $lists[$k]['decoration_text'] = "启用";
  323. }else{
  324. $lists[$k]['decoration_text'] = "禁用";
  325. }
  326. }
  327. // foreach ($lists as $k=>$v){
  328. // $lists[$k]['cate_name'] = Db::name('office_cate')->where('id',$v['cate_id'])->value('title');
  329. // $lists[$k]['unit_name'] = Db::name('office_unit')->where('id',$v['unit_id'])->value('title');
  330. // if($v['enable'] == 1){
  331. // $lists[$k]['enable_text'] = '启用';
  332. // }else{
  333. // $lists[$k]['enable_text'] = '禁用';
  334. // }
  335. // }
  336. $columns = [
  337. ["title" => "所属城区","key" => "districtName"],
  338. ["title" => "所属小区","key" => "communityName"],
  339. ["title" => "所属楼房","key" => "buildingName"],
  340. ["title" => "单元","key" => "unit"],
  341. ["title" => "房间号","key" => "number"],
  342. ["title" => "规格","key" => "spec"],
  343. ["title" => "面积","key" => "tenantArea"],
  344. ["title" => "入住人","key" => "lesseeName"],
  345. ["title" => "级别","key" => "levelName"],
  346. ["title" => "统一装修","key" => "decorationText"],
  347. ["title" => "装修时间","key" => "decorationTime"],
  348. ["title" => "状态","key" => "enableText"],
  349. ["title" => "朝向","key" => "aspect"],
  350. ["title" => "创建日期","key" => "createTime"],
  351. ];
  352. $data = [
  353. 'columns' => $columns,
  354. 'list' => $lists
  355. ];
  356. return $data;
  357. }
  358. // 改变为商品房
  359. public function changeStatus($id,$sImg,$sRemark){
  360. $info = Db::name('house_turn')->where('id',$id)->where('del',0)->find();
  361. if(!$info){
  362. HelpHander::error('记录不存在');
  363. }
  364. if($info['status'] == 1){
  365. HelpHander::error('已经是商品房,无需重复操作');
  366. }
  367. $res = Db::name('house_turn')->where('id',$id)->update([
  368. 'status' => 1,
  369. 's_img' => $sImg,
  370. 's_remark' => $sRemark,
  371. 's_time' => date('Y-m-d H:i:s')
  372. ]);
  373. if(!$res){
  374. HelpHander::error('操作失败');
  375. }
  376. $logdata = json_encode(['id' => $id,'sImg'=>$sImg,'sRemark'=>$sRemark]);
  377. model('ActionLog')->add(16,'周转房转商品房',0,$logdata);
  378. return true;
  379. }
  380. /**
  381. * 获取数量
  382. * @param $lessee
  383. * @return float|string
  384. */
  385. public function getCountByLessee($lessee){
  386. $map[] = ['del','=',0];
  387. $map[] = ['enable','=',1];
  388. $map[] = ['status','=',0];
  389. if($lessee == 1){ //租赁出去的
  390. $map[] = ['cur_lessee_id','>',0];
  391. }else{
  392. $map[] = ['cur_lessee_id','=',0];
  393. }
  394. $count = Db::name('house_turn')
  395. ->where($map)
  396. ->count();
  397. return $count;
  398. }
  399. // 根据面积获取数量
  400. public function getArea($type){
  401. if($type == 1){
  402. $map[] = ['tenant_area','<',70];
  403. }else if($type == 2){
  404. $map[] = ['tenant_area','<',90];
  405. $map[] = ['tenant_area','>=',70];
  406. }else if($type == 3){
  407. $map[] = ['tenant_area','<',110];
  408. $map[] = ['tenant_area','>=',90];
  409. }else{
  410. $map[] = ['tenant_area','>',110];
  411. }
  412. $map[] = ['del','=',0];
  413. $map[] = ['enable','=',1];
  414. $map[] = ['status','=',0];
  415. $count = Db::name('house_turn')->where($map)->count();
  416. return $count;
  417. }
  418. }