1.公司要实现一个业务逻辑:一天一个手机号码phone只能提交一次订单。
目前这个业务逻辑不涉及高并发等场景,大概也就是几分钟一次订单;
2.在编写了简单的表单验证之后,使用laravel5.4有如下代码;
/**
* 临时订单生成
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws ApiException
*/
public function AdvertisementUserOrder(Request $request){
PcUserValidator::GcCheCkAdvertisement(1001);//表单验证
$temporay_order = DB::select('select id from o2o_temporary_order where to_days(created_at) = to_days(now()) and phone = :phone', [':phone'=>$request->phone]);
if ($temporay_order) {
throw new ApiException(3020, '今天已申请过订单,请等待处理!');
}
//判断来源是否正确
$source = config('constants.user.source');
if ($request->has('source')){
if (count($source) >= $request->input('source') && $request->input('source') > 0){
$source_input = $request->input('source');
}else{
$source_input = 1;
}
}else{
$source_input = 1;
}
$temporay = DB::insert('insert into o2o_temporary_order (phone, username, source,created_at,updated_at) values (?, ?, ?,?,?)',
[$request->phone, $request->username, $source_input, date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', time())]);
if (!$temporay){
throw new ApiException(3020,'添加失败');
}
return $this->success();
}
3.在本地经过简单的表单测试后一切正常上线;
4.问题出现在数据库会在某种情况下出现数据重复录入,但前台js已经做好了重复提交等功能,而且就算是重复提交了,这时候第二次请求应该会被return;
5.数据库相关
6.为了模拟类似的情况发生,用linux做了定时任务,建了一个临时表用相同的代码每分钟访问一次接口。发现并不会出现重复的数据
MM们