engineering/app/adminapi/validate/custom/CustomValidate.php
2024-03-14 15:34:33 +08:00

149 lines
3.3 KiB
PHP

<?php
namespace app\adminapi\validate\custom;
use app\common\model\custom\Custom;
use app\common\model\dept\Dept;
use app\common\model\dept\Orgs;
use app\common\model\dict\DictData;
use app\common\validate\BaseValidate;
/**
* Custom验证器
* Class CustomValidate
* @package app\adminapi\validate\custom
*/
class CustomValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
'org_id' => 'require|checkOrg',
'dept_id' => 'require|checkDept',
'name' => 'require|unique:'.Custom::class,
'custom_type' => 'require|checkType',
'phone' => 'require',
'credit_rating' => 'require|checkRat',
'master_name' => 'require',
'master_phone' => 'mobile',
'master_email' => 'email',
'other_contacts' => 'checkContacts'
];
protected $field = [
'id' => '数据id',
'org_id' => '组织id',
'dept_id' => '部门id',
'name' => '客户名称',
'custom_type' => '客户属性',
'phone' => '电话',
'credit_rating' => '信用度',
'master_name' => '主要负责人姓名',
'master_phone' => '主要负责人手机',
'master_email' => '主要负责人邮箱',
];
/**
* @notes 添加场景
* @return CustomValidate
* @author likeadmin
* @date 2023/11/11 22:10
*/
public function sceneAdd()
{
return $this->remove('id', true);
}
/**
* @notes 编辑场景
* @return CustomValidate
* @author likeadmin
* @date 2023/11/11 22:10
*/
public function sceneEdit()
{
return $this->remove('other_contacts',true);
}
/**
* @notes 删除场景
* @return CustomValidate
* @author likeadmin
* @date 2023/11/11 22:10
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return CustomValidate
* @author likeadmin
* @date 2023/11/11 22:10
*/
public function sceneDetail()
{
return $this->only(['id']);
}
public function checkOrg($value): bool|string
{
$org = Orgs::where('id',$value)->findOrEmpty();
if($org->isEmpty()){
return '组织信息不存在';
}
return true;
}
public function checkDept($value,$rule,$params): bool|string
{
$dept = Dept::where('id',$value)->where('org_id',$params['org_id'])->findOrEmpty();
if($dept->isEmpty()){
return '部门信息不存在';
}
return true;
}
public function checkType($value): bool|string
{
$dictData = DictData::where('type_value','custom_type')->column('value');
if(!in_array($value,$dictData)){
return '客户属性无效';
}
return true;
}
public function checkRat($value): bool|string
{
$dictData = DictData::where('type_value','credit_rating')->column('value');
if(!in_array($value,$dictData)){
return '信用度无效';
}
return true;
}
public function checkContacts($value): bool|string
{
if(empty($value) || !is_array($value)){
return '其他联系人列表数据格式错误';
}
foreach($value as $k=>$v){
if(empty($v['name'])) return '其他联系人列表第'.($k+1).'行姓名为空';
if(isset($v['annex']) && $v['annex'] != ''){
if(!is_array($v['annex'])){
return '其他联系人列表第'.($k+1).'行附件格式错误';
}
}
}
return true;
}
}