<?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\project;


use app\common\model\auth\Admin;
use app\common\model\dept\Orgs;
use app\common\model\dict\DictData;
use app\common\model\project\Project;
use app\common\model\project\ProjectPersonnel;
use app\common\validate\BaseValidate;


/**
 * 考勤记录验证器
 * Class ProjectAttendanceRecordValidate
 * @package app\adminapi\validate\project
 */
class ProjectAttendanceRecordValidate extends BaseValidate
{

     /**
      * 设置校验规则
      * @var string[]
      */
    protected $rule = [
        'id' => 'require',
        'project_id' => 'require|checkProject',
        'attendance_date' => 'require|dateFormat:Y-m-d',
	    'attendance_detail' => 'require|checkAttendanceDetail',
	    'file' => 'checkFile',
    ];
	
	protected $message = [
		'id.require' => '缺少必要参数',
		'project_id.require' => '请选择项目',
		'attendance_date.require' => '请选择考勤日期',
		'attendance_date.dateFormat' => '考勤日期格式错误',
		'attendance_detail.require' => '考勤记录清单信息不能为空',
	];
	


    /**
     * @notes 添加场景
     * @return ProjectAttendanceRecordValidate
     * @author likeadmin
     * @date 2023/12/26 09:44
     */
    public function sceneAdd()
    {
		return $this->remove('id',true);
    }


    /**
     * @notes 编辑场景
     * @return ProjectAttendanceRecordValidate
     * @author likeadmin
     * @date 2023/12/26 09:44
     */
    public function sceneEdit()
    {
		return $this->only(['id','attendance_date']);
    }


    /**
     * @notes 删除场景
     * @return ProjectAttendanceRecordValidate
     * @author likeadmin
     * @date 2023/12/26 09:44
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }


    /**
     * @notes 详情场景
     * @return ProjectAttendanceRecordValidate
     * @author likeadmin
     * @date 2023/12/26 09:44
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }
	
	public function checkProject($value): bool|string
	{
		$data = Project::where('id',$value)->findOrEmpty();
		if($data->isEmpty()){
			return '项目不存在';
		}
		return true;
	}
	
	public function checkFile($value): bool|string
	{
		if($value != ''){
			$file = $value;//json_decode($value,true);
			if(empty($file)){
				return '附件必须是json数组';
			}
		}
		return true;
	}
	
	public function checkAttendanceDetail($value,$rule,$data): bool|string
	{
		$attendance_detail = $value;//json_decode($value,true);
		if(empty($attendance_detail) || !is_array($attendance_detail)){
			return '考勤记录清单数据格式错误';
		}
		foreach($attendance_detail as $v) {
			if(empty($v['person_id'])){
				return '请选择项目人员';
			}else{
				$person = ProjectPersonnel::where('id',$v['person_id'])->findOrEmpty();
				if($person->isEmpty() || $person['project_id'] != $data['project_id']){
					return '项目人员不存在';
				}
			}
			if(isset($v['work_start_time']) && $v['work_start_time'] != ''){
				if(date('Y-m-d H:i:s',strtotime($v['work_start_time'])) != $v['work_start_time']){
					return '上班时间格式错误';
				}
			}
			if(isset($v['work_end_time']) && $v['work_end_time'] != ''){
				if(date('Y-m-d H:i:s',strtotime($v['work_end_time'])) != $v['work_end_time']){
					return '下班时间格式错误';
				}
				if(strtotime($v['work_end_time']) - strtotime($v['work_start_time']) < 0){
					return '下班时间不能小于上班时间';
				}
			}
			if(empty($v['work_record_num'])){
				return '记工数量不能为空';
			}else{
				if(!is_numeric($v['work_record_num']) || $v['work_record_num'] < 0){
					return '记工数量必须是大于0的数字';
				}
			}
			if(empty($v['daily_salary'])){
				return '日工资不能为空';
			}else{
				if(!is_numeric($v['daily_salary']) || $v['daily_salary'] < 0){
					return '日工资必须是大于0的数字';
				}
			}
			if(empty($v['daily_living'])){
				return '日生活费不能为空';
			}else{
				if(!is_numeric($v['daily_living']) || $v['daily_living'] < 0){
					return '日生活费必须是大于0的数字';
				}
			}
			if(isset($v['daily_subsidy']) && $v['daily_subsidy'] != ''){
				if(!is_numeric($v['daily_subsidy']) || $v['daily_subsidy'] < 0){
					return '日补贴必须是大于等于0的数字';
				}
			}
			if(isset($v['daily_other']) && $v['daily_other'] != ''){
				if(!is_numeric($v['daily_other']) || $v['daily_other'] < 0){
					return '日其它必须是大于等于0的数字';
				}
			}
			if(empty($v['daily_income'])){
				return '日收入不能为空';
			}else{
				if(!is_numeric($v['daily_income']) || $v['daily_income'] < 0){
					return '日收入必须是大于0的数字';
				}
			}
		}
		return true;
	}
}