<?php
namespace 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 : [];
    }
}