<?php

namespace app\validate\api\store\order;

use app\entity\request\api\store\order\CalculatePostageRequestEntity;
use app\validate\CommonValidate;
use think\facade\Log;

class CalculatePostageValidate extends CommonValidate
{

    protected $rule = [
        'cityId' => 'require|integer|egt:0',
        'unique' => 'require',
        'num' => 'require|integer|gt:0'
    ];

    protected $message = [
        'cityId.require' => '城市ID不能为空！',
        'cityId.integer' => '城市ID必须是整数！',
        'cityId.egt' => '城市ID必须大于等于0！',
        'unique.require' => '唯一标识不能为空！',
        'num.require' => '购买数量不能为空！',
        'num.integer' => '购买数量必须是整数！',
        'num.gt' => '购买数量必须大于0！'
    ];

    /**
     * 将Request参数获取与验证封装到验证类中
     *
     * @return array
     */
    public function validateRequest(): array
    {
        $data = $this->request->param();
        Log::info('request-api-store-order-CalculatePostage-params：' . json_encode($data));
        if (!$this->check($data)) {
            return ['status' => 'error', 'message' => $this->getError()];
        }
        return ['status' => 'success', 'data' => CalculatePostageRequestEntity::newInstance($data)];
    }
}