| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 | <?phpnamespace app\common\model;use think\Db;use think\Exception;class House extends Base{    protected $createTime = 'create_time';    protected $updateTime = 'update_time';    public $table = 'house';    protected $validateName = 'House';    public $cate = [        1=>'住房',        2=>'商铺',        3=>'营业房',        4=>'储藏室',        5=>'停车位',    ];    public $cate1 = [        '住房'=>1,        '商铺'=>2,        '营业房'=>3,        '储藏室'=>4,        '停车位'=>5,    ];    public function updates(){        $data = request()->post();        $data['org_id'] =cur_org_id();        $result = validate($this->validateName)->check($data,[],'');        if(true !== $result){            $this->error = validate($this->validateName)->getError();            return false;        }        if(in_array($data['cate'],[1,2,3,4])){            if(!isset($data['area']) || empty($data['area'])){                $this->error='请输入面积';                return false;            }        }else{            $data['area'] = '0.00';        }        if(in_array($data['cate'],[1,2,3])){            $data['type']  =1;        }elseif ($data['cate']==4){            $data['type']  =2;        }elseif ($data['cate']==5){            $data['type']  =3;        }        $id = $data['id'];        unset($data['id']);        $this->startTrans();        if($id > 0){            $oldInfo = $this                ->where('id',$id)                ->find()->toArray();            $data['update_time'] = date('Y-m-d H:i:s');            $ret = $this->allowField(true)->save($data,['id'=>$id]);            $newOwnerId= $data['owner_id'];            if($newOwnerId != $oldInfo['owner_id']){                $rs = $this->addLog($id,$oldInfo['owner_id']);                if(!$rs){                    $this->rollback();                    return false;                };            }        }else{            $data['create_time'] = date('Y-m-d H:i:s');            $ret = $this->allowField(true)->save($data);            if(!empty($data['owner_id'])){                $rs = $this->addLog($this->getLastInsID());                if(!$rs){                    $this->rollback();                    return false;                };            }        }        if(!$ret){            $this->error = '操作失败';            return false;        }        $this->commit();        return true;    }    public function getList(){        $list =$this            ->where('org_id',cur_org_id())            ->where('del',0)            ->where('enable',1)            ->select()            ->toArray();        return $list;    }    //获取资源类型    public function getCate(){        $r  =[            [                "id"=>'1',                "title"=>'住房',            ],            [                "id"=>'2',                "title"=>'商铺',            ],            [                "id"=>'3',                "title"=>'营业房',            ],            [                "id"=>'4',                "title"=>'储藏室',            ],            [                "id"=>'5',                "title"=>'停车位',            ],        ];        return $r;    }    public function getCType($cateId){        $list = Db::name('house_c_type')            ->where('cate',$cateId)            ->where('del',0)            ->where('enable',1)            ->select();        return $list;    }    //增加变更信息    public function addLog($id,$oldOwner = ''){        $this->starttrans();        try{            $data = db::name('house_view')                ->where('id',$id)                ->find();            $info =  db::name('owner')                ->where('id',$oldOwner)                ->find();            if(empty($data['owner_id'])){                $content = '['.$data['xq_title'].']['.$this->cate[$data['cate']].']['.$data['title'].']'.'解绑了: '.$info['name'];                $oid = $oldOwner;            }else{                if($oldOwner){                    $content1 = '['.$data['xq_title'].']['.$this->cate[$data['cate']].']['.$data['title'].']'.'解绑了: '.$info['name'];                    $s = [                        'org_id'=>$data['org_id'],                        'house_id'=>$id,                        'owner_id'=>$oldOwner,                        'content'=>$content1,                        'create_time'=>getTime(),                    ];                    $rs = Db::name('house_owner_log')                        ->insert($s);                    if(!$rs)  exception('增加变更信息失败1');                }                $content = '['.$data['xq_title'].']['.$this->cate[$data['cate']].']['.$data['title'].']'.'绑定了: '.$data['name'];                $oid = $data['owner_id'];            }            $s = [                'org_id'=>$data['org_id'],                'house_id'=>$id,                'owner_id'=>$oid,                'content'=>$content,                'create_time'=>getTime(),            ];            $rs = Db::name('house_owner_log')                ->insert($s);            if(!$rs)  exception('增加变更信息失败2');            $this->commit();            return true;        }catch (Exception $e){            $this->rollback();            $this->error = $e->getmessage();            return false;        }    }    // 根据业主获取资源列表    public function getListByOwner($ownerId,$areaId)    {        $lists = $this            ->where('del', 0)            ->where('enable', 1)            ->where('area_id', $areaId)            ->where('owner_id', $ownerId)            ->field('id,title, area, cate, type,c_type')            ->select()->toArray();        $lists = $lists ? $lists : [];        foreach ($lists as $k=>$v){            $lists[$k]['c_type_name'] = Db::name('house_c_type')->where('id',$v['c_type'])->value('name');        }        return $lists ? $lists : [];    }}
 |