<?php

namespace app\admin\validate\goods;

use support\Validate;

class GoodsValidate extends Validate
{
    protected $rule = [
        'goods_id' => 'require|alphaDash',
        'join_goods_category_id' => 'integer',
        'join_goods_device_id' => 'max:32',
        'goods_classify|归类' => 'require',  // GOODS=实物商品|DEVICE=设备|SERIVCE=服务|SPECIAL=专题业务|PACKAGE=套包|OTHER=其他
        'goods_status|状态' => 'require|in:PENDING,ON,OFF,DISABLED',    // 产品状态 PENDING=待处理|ON=上架(已激活)|OFF=下架|DISABLED=已禁用
        'goods_category|分类' => 'max:32',
        'goods_prefix|前缀' => 'max:64 ',
        'goods_name|名称' => 'require|max:32',
        'goods_market_price|市场价' => 'float',
        'goods_sales_price|销售价' => 'float',
//        'goods_sku_json' => 'isJson',
//        'goods_attribute_json|属性' => 'isJson',
        'goods_service_json|服务' => 'isJson',
        'goods_title|简介' => 'max:300',
        'goods_cover|封面' => 'url',
//        'goods_on_addtimes|上架时间' => 'date',
        'goods_sort|排序' => 'integer',
        'goods_groupby|分组' => 'max:32;',
        'goods_extend_json' => 'isJson',
//        'goods_slide' => 'regex:/^[0-9a-zA-Z\.\/\,]+$/',
        'goods_specs_json|规格参数' => 'isJson',
//        'goods_content' => '',
        'goods_label|标签' => 'max:32',
//        'goods_label_extend_json' => '',
        'goods_storage|库存量' => 'float',
        'goods_sale|销售量' => 'float',
        'goods_running_off_type|下架方式' => 'require|in:H,T',
        'goods_running_off_json|下架' => 'isJson',
        'goods_sku_status|SKU状态' => 'in:PENDING,ON,OFF,DISABLED',   // 产品sku状态 PENDING=待启用|ON=上架|OFF=下架|DISABLED=已禁用
        'goods_sku_specs_json|规格' => 'isJson',
        'goods_sku_title|SKU标题' => 'max:64',
        'goods_sku_images_json|SKU图片' => 'isJson',
//        'goods_sku_content' => '',
        'goods_sku_market_price|市场价' => 'float',
        'goods_sku_sales_price|销售价' => 'float',
        'goods_sku_storage_json|库存' => 'isJson',
        'goods_sku_service_json' => 'isJson',
        'goods_sku_extend_json' => 'isJson',
    ];

    protected $message = [];

    protected $scene = [
        'add' => ['join_goods_category_id', 'join_goods_device_id', 'goods_classify', 'goods_status', 'goods_category', 'goods_prefix',
            'goods_name', 'goods_market_price', 'goods_sku_json', 'goods_attribute_json', 'goods_service_json', 'goods_title',
            'goods_cover', 'goods_on_addtimes', 'goods_sort', 'goods_groupby', 'goods_extend_json', 'goods_specs_json',
            'goods_label', 'goods_storage', 'goods_sale', 'goods_running_off_type', 'goods_running_off_json', 'goods_sku_status',
            'goods_sku_specs_json', 'goods_sku_title', 'goods_sku_images_json', 'goods_sku_market_price', 'goods_sku_sales_price',
            'goods_sku_storage_json', 'goods_sku_service_json', 'goods_sku_extend_json'],
        'update' => ['goods_id', 'join_goods_category_id', 'join_goods_device_id', 'goods_classify', 'goods_status', 'goods_category', 'goods_prefix',
            'goods_name', 'goods_market_price', 'goods_sku_json', 'goods_attribute_json', 'goods_service_json', 'goods_title',
            'goods_cover', 'goods_on_addtimes', 'goods_sort', 'goods_groupby', 'goods_extend_json', 'goods_specs_json',
            'goods_label', 'goods_storage', 'goods_sale', 'goods_running_off_type', 'goods_running_off_json', 'goods_sku_status',
            'goods_sku_specs_json', 'goods_sku_title', 'goods_sku_images_json', 'goods_sku_market_price', 'goods_sku_sales_price',
            'goods_sku_storage_json', 'goods_sku_service_json', 'goods_sku_extend_json'],
        'info' => ['goods_id'],
        'changeStatus' => ['goods_id','goods_status']
    ];

    /**
     * @Desc Json数据验证
     * @Author Gorden
     * @Date 2024/3/11 14:55
     *
     * @param $value
     * @return string|true
     */
    public function isJson($value)
    {
        if (is_json($value)) {
            return true;
        }
        return "数据格式错误";
    }
}