<?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\supervision_project\SupervisionProject;
use app\common\model\supervision_work\SupervisionWorkContact;
use app\common\model\supervision_work\SupervisionWorkContactProblem;
use app\common\validate\BaseValidate;


/**
 * 工程监理--监理工作联系单验证器
 * Class SupervisionWorkContactValidate
 * @package app\adminapi\validate\supervision_work
 */
class SupervisionWorkContactValidate extends BaseValidate
{

     /**
      * 设置校验规则
      * @var string[]
      */
    protected $rule = [
        'id' => 'require|checkData',
        'project_id' => 'require|checkProject',
        'initiator' => 'require',
        'initiation_date' => 'require|dateFormat:Y-m-d',
        'theme' => 'require',
	    'annex' => 'checkAnnex',
	    'problem' => 'checkProblem'
    ];


    /**
     * 参数描述
     * @var string[]
     */
    protected $field = [
        'id' => 'id',
        'project_id' => '项目id',
        'code' => '联系编号',
        'initiator' => '发起人',
        'initiation_date' => '发起日期',
        'theme' => '主题',
        'copy_user' => '抄送人员',
    ];


    /**
     * @notes 添加场景
     * @return SupervisionWorkContactValidate
     * @author likeadmin
     * @date 2024/03/01 11:01
     */
    public function sceneAdd()
    {
        return $this->remove('id',true);
    }


    /**
     * @notes 编辑场景
     * @return SupervisionWorkContactValidate
     * @author likeadmin
     * @date 2024/03/01 11:01
     */
    public function sceneEdit()
    {}


    /**
     * @notes 删除场景
     * @return SupervisionWorkContactValidate
     * @author likeadmin
     * @date 2024/03/01 11:01
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }


    /**
     * @notes 详情场景
     * @return SupervisionWorkContactValidate
     * @author likeadmin
     * @date 2024/03/01 11:01
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }
	
	public function checkData($value): bool|string
	{
		$data = SupervisionWorkContact::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 checkAnnex($value): bool|string
	{
		if(!empty($value) && $value != '' && !is_array($value)){
			return '附件格式错误';
		}
		return true;
	}
	
	public function checkProblem($value): bool|string
	{
		if(!isset($value) || $value == '') return true;
		if(!is_array($value)) return '工作联系单问题数据格式错误';
		foreach($value as $k=>$v){
			if(!empty($v['id'])){
				$data = SupervisionWorkContactProblem::where('id',$v['id'])->findOrEmpty();
				if($data->isEmpty()){
					return '工作联系单问题列表第'.($k+1).'行数据不存在';
				}
			}
			if(empty($v['problem_name'])) return '工作联系单问题列表第'.($k+1).'行问题名称为空';
			if(empty($v['problem_description'])) return '工作联系单问题列表第'.($k+1).'行问题说明为空';
		}
		return true;
	}

}