<?php
namespace app\common\model;

use app\hander\HelpHander;
use app\Request;
use think\Db;
use think\Exception;
use think\Model;

class HouseTurnLessee extends Model
{

    public function add(){
        $data = [
            'house_turn_id' => input('houseTurnId/d',0),
            'lessee_id' => input('lesseeId/d',0),
            'start_time' => input('startTime','','trim'),
            'remark' => input('remark','','trim'),
            'create_time' => date("Y-m-d H:i:s"),
            'org_id' => input('orgId/d',0),
        ];
        $result = validate('HouseTurnLessee')->check($data,[],'');
        if(true !== $result){
            HelpHander::error(validate('HouseTurnLessee')->getError());
        }

        Db::startTrans();
        try{
            $ret = $this->allowField(true)->save($data);
            if(!$ret){
                \exception('操作失败');
            }

            $res = Db::name('house_turn')->where('id',$data['house_turn_id'])->setField('cur_lessee_id',$data['lessee_id']);
            if(!$res){
                \exception('操作失败');
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            HelpHander::error($e->getMessage());
        }


        return true;
    }

    public function edit(){
        $data = [
            'id' => input('id/d',0),
            'end_time' => input('endTime','','trim'),
            'remark2' => input('remark2','','trim'),
            'update_time' => date("Y-m-d H:i:s"),
            'status' => 1
        ];

        if($data['id'] <= 0){
            HelpHander::error('参数错误');
        }
        if(!$data['end_time']){
            HelpHander::error('未选择退房日期');
        }

        $info = Db::name('house_turn_lessee')->where('id',$data['id'])->find();
        if(!$info){
            HelpHander::error('参数错误');
        }
        if($info['status'] == 1){
            HelpHander::error('已退房,无需重复操作');
        }

        Db::startTrans();
        try{
            $ret = $this->allowField(true)->save($data,['id'=>$data['id']]);
            if(!$ret){
                \exception('操作失败');
            }

            $res = Db::name('house_turn')->where('id',$info['house_turn_id'])->setField('cur_lessee_id',0);
            if(!$res){
                \exception('操作失败');
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            HelpHander::error($e->getMessage());
        }
        return true;
    }

    public function lists($page,$size,$title,$id,$orgId){
        $map[] = ['htl.org_id','=',$orgId];
        $map[] = ['htl.house_turn_id','=',$id];
        if($title != ''){
            $map[] = ['hl.title','like','%'.$title.'%'];
        }

        $lists = Db::name('house_turn_lessee')
            ->alias('htl')
            ->join('house_lessee hl','hl.id = htl.lessee_id')
            ->field('htl.*,hl.title as lesseeName, hl.phone as phone')
            ->where($map)
            ->page($page,$size)
            ->order('htl.id desc')
            ->select();

        $total = Db::name('house_turn_lessee')
            ->alias('htl')
            ->join('house_lessee hl','hl.id = htl.lessee_id')
            ->where($map)->count();
        $data = [
            'total' => $total,
            'list' => $lists?$lists:[]
        ];
        return $data;
    }

    public function del(){
        $id = input('id/d',0);
        $res = Db::name('house_turn_lessee')->where('id',$id)->delete();
        if(!$res){
            HelpHander::error('操作失败');
        }
        return true;
    }

}