House.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class House extends Base
  6. {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'house';
  10. protected $validateName = 'House';
  11. public $cate = [
  12. 1=>'住房',
  13. 2=>'商铺',
  14. 3=>'营业房',
  15. 4=>'储藏室',
  16. 5=>'停车位',
  17. ];
  18. public $cate1 = [
  19. '住房'=>1,
  20. '商铺'=>2,
  21. '营业房'=>3,
  22. '储藏室'=>4,
  23. '停车位'=>5,
  24. ];
  25. public function updates(){
  26. $data = request()->post();
  27. $data['org_id'] =cur_org_id();
  28. $result = validate($this->validateName)->check($data,[],'');
  29. if(true !== $result){
  30. $this->error = validate($this->validateName)->getError();
  31. return false;
  32. }
  33. if(in_array($data['cate'],[1,2,3,4])){
  34. if(!isset($data['area']) || empty($data['area'])){
  35. $this->error='请输入面积';
  36. return false;
  37. }
  38. }else{
  39. $data['area'] = '0.00';
  40. }
  41. if(in_array($data['cate'],[1,2,3])){
  42. $data['type'] =1;
  43. }elseif ($data['cate']==4){
  44. $data['type'] =2;
  45. }elseif ($data['cate']==5){
  46. $data['type'] =3;
  47. }
  48. $id = $data['id'];
  49. unset($data['id']);
  50. $this->startTrans();
  51. if($id > 0){
  52. $oldInfo = $this
  53. ->where('id',$id)
  54. ->find()->toArray();
  55. $data['update_time'] = date('Y-m-d H:i:s');
  56. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  57. $newOwnerId= $data['owner_id'];
  58. if($newOwnerId != $oldInfo['owner_id']){
  59. $rs = $this->addLog($id,$oldInfo['owner_id']);
  60. if(!$rs){
  61. $this->rollback();
  62. return false;
  63. };
  64. }
  65. }else{
  66. $data['create_time'] = date('Y-m-d H:i:s');
  67. $ret = $this->allowField(true)->save($data);
  68. if(!empty($data['owner_id'])){
  69. $rs = $this->addLog($this->getLastInsID());
  70. if(!$rs){
  71. $this->rollback();
  72. return false;
  73. };
  74. }
  75. }
  76. if(!$ret){
  77. $this->error = '操作失败';
  78. return false;
  79. }
  80. $this->commit();
  81. return true;
  82. }
  83. public function getList(){
  84. $list =$this
  85. ->where('org_id',cur_org_id())
  86. ->where('del',0)
  87. ->where('enable',1)
  88. ->select()
  89. ->toArray();
  90. return $list;
  91. }
  92. //获取资源类型
  93. public function getCate(){
  94. $r =[
  95. [
  96. "id"=>'1',
  97. "title"=>'住房',
  98. ],
  99. [
  100. "id"=>'2',
  101. "title"=>'商铺',
  102. ],
  103. [
  104. "id"=>'3',
  105. "title"=>'营业房',
  106. ],
  107. [
  108. "id"=>'4',
  109. "title"=>'储藏室',
  110. ],
  111. [
  112. "id"=>'5',
  113. "title"=>'停车位',
  114. ],
  115. ];
  116. return $r;
  117. }
  118. public function getCType($cateId){
  119. $list = Db::name('house_c_type')
  120. ->where('cate',$cateId)
  121. ->where('del',0)
  122. ->where('enable',1)
  123. ->select();
  124. return $list;
  125. }
  126. //增加变更信息
  127. public function addLog($id,$oldOwner = ''){
  128. $this->starttrans();
  129. try{
  130. $data = db::name('house_view')
  131. ->where('id',$id)
  132. ->find();
  133. $info = db::name('owner')
  134. ->where('id',$oldOwner)
  135. ->find();
  136. if(empty($data['owner_id'])){
  137. $content = '['.$data['xq_title'].']['.$this->cate[$data['cate']].']['.$data['title'].']'.'解绑了: '.$info['name'];
  138. $oid = $oldOwner;
  139. }else{
  140. if($oldOwner){
  141. $content1 = '['.$data['xq_title'].']['.$this->cate[$data['cate']].']['.$data['title'].']'.'解绑了: '.$info['name'];
  142. $s = [
  143. 'org_id'=>$data['org_id'],
  144. 'house_id'=>$id,
  145. 'owner_id'=>$oldOwner,
  146. 'content'=>$content1,
  147. 'create_time'=>getTime(),
  148. ];
  149. $rs = Db::name('house_owner_log')
  150. ->insert($s);
  151. if(!$rs) exception('增加变更信息失败1');
  152. }
  153. $content = '['.$data['xq_title'].']['.$this->cate[$data['cate']].']['.$data['title'].']'.'绑定了: '.$data['name'];
  154. $oid = $data['owner_id'];
  155. }
  156. $s = [
  157. 'org_id'=>$data['org_id'],
  158. 'house_id'=>$id,
  159. 'owner_id'=>$oid,
  160. 'content'=>$content,
  161. 'create_time'=>getTime(),
  162. ];
  163. $rs = Db::name('house_owner_log')
  164. ->insert($s);
  165. if(!$rs) exception('增加变更信息失败2');
  166. $this->commit();
  167. return true;
  168. }catch (Exception $e){
  169. $this->rollback();
  170. $this->error = $e->getmessage();
  171. return false;
  172. }
  173. }
  174. // 根据业主获取资源列表
  175. public function getListByOwner($ownerId,$areaId)
  176. {
  177. $lists = $this
  178. ->where('del', 0)
  179. ->where('enable', 1)
  180. ->where('area_id', $areaId)
  181. ->where('owner_id', $ownerId)
  182. ->field('id,title, area, cate, type,c_type')
  183. ->select()->toArray();
  184. $lists = $lists ? $lists : [];
  185. foreach ($lists as $k=>$v){
  186. $lists[$k]['c_type_name'] = Db::name('house_c_type')->where('id',$v['c_type'])->value('name');
  187. }
  188. return $lists ? $lists : [];
  189. }
  190. }