<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\validate\supervision_work;


use app\common\model\dict\DictData;
use app\common\model\supervision_project\SupervisionProject;
use app\common\model\supervision_work\SupervisionCheckItem;
use app\common\model\supervision_work\SupervisionCheckItemDetail;
use app\common\validate\BaseValidate;


/**
 * 工程监理--新增检查项验证器
 * Class SupervisionCheckItemValidate
 * @package app\adminapi\validate\supervision_work
 */
class SupervisionCheckItemValidate extends BaseValidate
{

     /**
      * 设置校验规则
      * @var string[]
      */
    protected $rule = [
        'id' => 'require|checkData',
        'project_id' => 'require|checkProject',
	    'pid' => 'integer|checkPid',
        'node_name' => 'require',
        'node_type' => 'require|checkNodeType',
	    'check_item_detail' => 'checkDetail',
    ];


    /**
     * 参数描述
     * @var string[]
     */
    protected $field = [
        'id' => 'id',
        'project_id' => '项目id',
        'node_name' => '节点名称',
        'node_type' => '节点类型',
        'node_code' => '节点编号',
        'pid' => '父节点id',
        'inspection_basis' => '检查依据',
        'reference_law' => '参考法规',
    ];


    /**
     * @notes 添加场景
     * @return SupervisionCheckItemValidate
     * @author likeadmin
     * @date 2024/02/26 15:34
     */
    public function sceneAdd()
    {
		return $this->remove('id',true);
    }


    /**
     * @notes 编辑场景
     * @return SupervisionCheckItemValidate
     * @author likeadmin
     * @date 2024/02/26 15:34
     */
    public function sceneEdit()
    {}


    /**
     * @notes 删除场景
     * @return SupervisionCheckItemValidate
     * @author likeadmin
     * @date 2024/02/26 15:34
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }


    /**
     * @notes 详情场景
     * @return SupervisionCheckItemValidate
     * @author likeadmin
     * @date 2024/02/26 15:34
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }
	
	public function checkData($value): bool|string
	{
		$data = SupervisionCheckItem::where('id',$value)->findOrEmpty();
		if($data->isEmpty()){
			return '数据不存在';
		}
		return true;
	}
	
	public function checkProject($value): bool|string
	{
		$data = SupervisionProject::where('id',$value)->findOrEmpty();
		if($data->isEmpty()){
			return '项目信息不存在';
		}
		return true;
	}
	
	public function checkPid($value): bool|string
	{
		if(empty($value)) return true;
		$data = SupervisionCheckItem::where('id',$value)->findOrEmpty();
		if($data->isEmpty()){
			return '父级节点信息不存在';
		}
		return true;
	}
	
	public function checkNodeType($value): bool|string
	{
		$dict = DictData::where('type_value','check_item_node_type')->column('value');
		if(!in_array($value,$dict)){
			return '节点类型数据值无效';
		}
		return true;
	}
	
	public function checkDetail($value): bool|string
	{
		if(empty($value) || $value == '') return true;
		if(!is_array($value)) return '检查项数据格式错误';
		foreach($value as $k=>$v){
			if(isset($v['id']) && !empty($v['id'])){
				$data = SupervisionCheckItemDetail::where('id',$v['id'])->findOrEmpty();
				if($data->isEmpty()){
					return '第'.($k+1).'行检查项信息不存在';
				}
			}
			if(empty($v['check_type'])){
				return '第'.($k+1).'行检查类别为空';
			}
			if(empty($v['check_content'])){
				return '第'.($k+1).'行检查内容为空';
			}
			if(!isset($v['must_check']) || $v['must_check'] = ''){
				return '第'.($k+1).'行是否必检为空';
			}
			if(!in_array($v['must_check'],[0,1])){
				return '第'.($k+1).'行是否必检项数据值错误';
			}
		}
		return true;
	}

}