<?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\SupervisionParticipatingUnits;
use app\common\model\supervision_project\SupervisionProject;
use app\common\model\supervision_work\SupervisionCheckItem;
use app\common\model\supervision_work\SupervisionInspection;
use app\common\model\supervision_work\SupervisionInspectionResult;
use app\common\model\supervision_work\SupervisionProblem;
use app\common\validate\BaseValidate;


/**
 * 工程监理--巡视登记验证器
 * Class SupervisionInspectionValidate
 * @package app\adminapi\validate\supervision_work
 */
class SupervisionInspectionValidate extends BaseValidate
{

     /**
      * 设置校验规则
      * @var string[]
      */
    protected $rule = [
        'id' => 'require|checkData',
        'project_id' => 'require|checkProject',
        'inspection_type' => 'require|checkInspectionType',
        'check_item_id' => 'require|checkCheckItem',
        'workers' => 'integer',
        'managers' => 'integer',
        'start_time' => 'require|dateFormat:Y-m-d H:i:s',
        'end_time' => 'require|dateFormat:Y-m-d H:i:s',
        'inspection_user' => 'require',
	    'company_id' => 'checkCompany',
        'inspection_content' => 'require',
        'is_important' => 'in:0,1',
	    'check_item_detail_ids' => 'checkCheckItemDetail',
	    'annex' => 'checkAnnex',
	    'inspection_result' => 'checkInspectionResult',
	    'inspection_problem' => 'checkInspectionProblem'
    ];


    /**
     * 参数描述
     * @var string[]
     */
    protected $field = [
        'id' => 'id',
        'project_id' => '项目id',
        'inspection_code' => '编号',
        'inspection_type' => '巡视类型',
        'check_item_id' => '单位工程',
        'inspection_position' => '巡视部位',
        'workers' => '施工人数',
        'managers' => '管理人数',
        'start_time' => '巡视开始时间',
        'end_time' => '巡视结束时间',
        'inspection_user' => '巡视人员',
        'inspection_content' => '巡视内容',
        'is_important' => '设置重点关注事项',
        'follow_user' => '关注人',
        'check_item_detail_ids' => '检查表单选择',
        'create_user' => '创建人',
    ];


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


    /**
     * @notes 编辑场景
     * @return SupervisionInspectionValidate
     * @author likeadmin
     * @date 2024/02/26 17:18
     */
    public function sceneEdit()
    {}


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


    /**
     * @notes 详情场景
     * @return SupervisionInspectionValidate
     * @author likeadmin
     * @date 2024/02/26 17:18
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }
	
	public function checkData($value): bool|string
	{
		$data = SupervisionInspection::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 checkInspectionType($value): bool|string
	{
		$dict = DictData::where('type_value','inspection_type')->column('value');
		if(!in_array($value,$dict)){
			return '巡视类型数据值无效';
		}
		return true;
	}
	
	public function checkCheckItem($value,$rule,$params): bool|string
	{
		$data = SupervisionCheckItem::where('id',$value)->where('project_id',$params['project_id'])->where('node_type',2)->findOrEmpty();
		if($data->isEmpty()){
			return '单位工程数据不存在';
		}
		return true;
	}
	
	public function checkCompany($value,$rule,$params): bool|string
	{
		$data = SupervisionParticipatingUnits::where('id',$value)->where('project_id',$params['project_id'])->findOrEmpty();
		if($data->isEmpty()){
			return '施工单位信息不存在';
		}
		return true;
	}
	
	public function checkCheckItemDetail($value,$rule,$params): bool|string
	{
		if(!isset($value) || $value == '') return true;
		if(!is_array($value)) return '检查表单选择数据格式错误';
		foreach($value as $v){
			$data = SupervisionCheckItem::where('id',$v)->where('project_id',$params['project_id'])->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 checkInspectionResult($value): bool|string
	{
		if(!isset($value) || $value == '') return true;
		if(!is_array($value)) return '巡视结果数据格式错误';
		foreach($value as $k=>$v){
			if(!empty($v['id'])){
				$data = SupervisionInspectionResult::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).'行是否必检数据值无效';
			if(!isset($v['check_result']) || $v['check_result'] == '') return '巡视结果列表第'.($k+1).'行检查结果为空';
			if(!in_array($v['check_result'],[0,1])) return '巡视结果列表第'.($k+1).'行检查结果数据值无效';
		}
		return true;
	}
	
	public function checkInspectionProblem($value): bool|string
	{
		if(!isset($value) || $value == '') return true;
		if(!is_array($value)) return '巡视问题数据格式错误';
		foreach($value as $k=>$v){
			if(!empty($v['id'])){
				$data = SupervisionProblem::where('id',$v['id'])->findOrEmpty();
				if($data->isEmpty()){
					return '巡视问题列表第'.($k+1).'行数据不存在';
				}
			}
			if(empty($v['problem_cate'])){
				return '巡视问题列表第'.($k+1).'行问题分类为空';
			}else{
				$dict = DictData::where('type_value','problem_cate')->column('value');
				if(!in_array($v['problem_cate'],$dict)) return '巡视问题列表第'.($k+1).'行问题分类数据值无效';
			}
			if(empty($v['problem_description'])) return '巡视问题列表第'.($k+1).'行问题说明为空';
			if(empty($v['problem_name'])) return '巡视问题列表第'.($k+1).'行问题名称为空';
		}
		return true;
	}

}