<?php
namespace app\common\validate;

use think\Db;
use think\Validate;

class WorkNews extends Validate{

    protected $rule = [
        'work_type_id' => 'require|gt:0|checkType',
        'content|内容'  =>  'require|checkContent',
//        'receiver|接收人'  =>  'require',
    ];

    protected $message = [
//        'receiver.require' => '未选择接收人',
        'work_type_id.require' => '未选择类型',
        'work_type_id.gt' => '未选择类型',
    ];

    protected $scene = [

    ];

    protected function checkType($value,$rule,$data=[]){
        if(!in_array($value,[1,2,3,4,5,6,7,8,9,10])){
            return '类型错误';
        }

        return true;
    }

    protected function checkContent($value,$rule,$data=[]){
        $content = json_decode($value,true);
        if(!$content){
            return '参数错误';
        }
        $arr = [];
        foreach ($content as $k=>$v){
            if(!$v['content']){
                return '工作内容不能为空';
            }
            if(!$v['date']){
                return '完成时间不能为空';
            }
            if($v['content'] || $v['date']){
                $arr[] = $v;
            }
        }
        if(!$arr){
            return '未输入内容';
        }
        return true;
    }

}