<?php
namespace app\common\validate;

use think\Validate;
use think\Db;
class HouseBuilding extends Validate{

    protected $rule = [
        'title|标题'  =>  'require|checkUnique',
        'type|类型'  => 'require|in:1,2',
        'community_id|所属小区'  => 'require',
    ];

    protected $message = [
        'type.in' => '参数错误'
    ];


    protected $scene = [

    ];

    protected function checkUnique($value,$rule,$data=[])
    {
        $info = Db::name('house_building')
            ->where('title',$data['title'])
            ->where('org_id',$data['org_id'])
            ->where('community_id',$data['community_id'])
            ->where('type',$data['type'])
            ->find();
        if($data['id'] <= 0 && $info){
            return '名称已被使用';
        }
        if($info && $data['id'] > 0 && $info['id'] != $data['id']){
            return '名称已被使用';
        }
        return true;
    }
}