<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace Modules\Order\Services;

use Modules\Order\Models\Exchange;

class ExchangeService {

	/**
	 * @title 添加置换收集订单
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function create($request)
	{
		$request->validate([
			'name' => 'required',
			'address' => 'required',
			'mobile' => 'require|regex:^1[3-9]\d{9}$',
		], [
			'name.required' => '请填写姓名',
			'mobile.required' => '请填写手机号码',
			'mobile.regex' => '手机号格式不正确',
			'address.required' => '请填写所在地址'
		]);
		$exchange = new Exchange();
		$info = Exchange::where(['mobile' => $request->post('mobile')])->first();
		if($info){
			throw new \Exception("此号码已填写过", 0);
		}
		$exchange->uid = auth('api')->user()['uid'];
		$exchange->name = $request->post('name');
		$exchange->mobile = $request->post('mobile');
		$exchange->status = 1;
		$exchange->createtime = time();
		$exchange->address = $request->post('address');
		$exchange->remark = $request->post('remark') ?? '';
		$exchange->save();
		return $exchange;
	}
}