我有一个应用程序,它是一个停车系统,我的客户经常使用它来发送到我的数据库中的汽车数据条目。
以下是要发送到我的服务器的代码:
public void sendEntradaRotativaDataToServer(String url){
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Processando entrada..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
JsonObject json = new JsonObject();
json.addProperty("placa", entradaObj.getPlaca());
json.addProperty("dataemissao", entradaObj.getDataemissao());
json.addProperty("tipo", entradaObj.getTipo());
json.addProperty("valor", entradaObj.getValor());
json.addProperty("area", entradaObj.getArea().getId());
json.addProperty("observacao", entradaObj.getObservacao());
json.addProperty("user_id", entradaObj.getUser_id());
Ion.with(getActivity())
.load(url)
.setTimeout(5000)
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (e != null) {
newMsgWarn("Atenção","Não foi possível fazer uma conexão por favor tente novamente");
if(pDialog != null && pDialog.isShowing()){
pDialog.dismiss();
}
} else {
success = result.get(TAG_SUCCESS).getAsInt();
message = result.get(TAG_MESSAGE).getAsString();
entradaObj.set_id(result.get("entradaid").getAsString());
(new SyncEntrada()).execute(new String[0]);
if(pDialog != null && pDialog.isShowing()){
pDialog.dismiss();
}
}
}
});
}
这是一个带有离子库的简单post请求,这是服务器上的代码(我在项目中使用laravel):
public function insertDataRotativa(Request $request) {
if(isset($request)) {
$dataentradaini = Carbon::parse($request->input('dataemissao'));
$evento = $this->mdUtil->getEventByDataEmissao($dataentradaini);
if (!empty($evento)) {
foreach ($evento as $value) {
$this->mdEntrada = new Entrada;
$this->mdEntrada->placa = $request->input('placa');
$this->mdEntrada->created_at = $dataentradaini;
$this->mdEntrada->updated_at = $dataentradaini;
$this->mdEntrada->tipo = $request->input('tipo');
$this->mdEntrada->valor = $request->input('valor');
$this->mdEntrada->area = $request->input('area');
$this->mdEntrada->observacao = $request->input('observacao');
$this->mdEntrada->user_id = $request->input('user_id');
$this->mdEntrada->evento_id = $value->id;
$this->mdEntrada->flag = "U";
$this->mdEntrada->save();
}
}else {
$this->mdEntrada = new Entrada;
$this->mdEntrada->placa = $request->input('placa');
$this->mdEntrada->created_at = $dataentradaini;
$this->mdEntrada->updated_at = $dataentradaini;
$this->mdEntrada->tipo = $request->input('tipo');
$this->mdEntrada->valor = $request->input('valor');
$this->mdEntrada->area = $request->input('area');
$this->mdEntrada->observacao = $request->input('observacao');
$this->mdEntrada->user_id = $request->input('user_id');
$this->mdEntrada->flag = "U";
$this->mdEntrada->save();
}
$response["success"] = 1;
$response["message"] = "Entrada efetuada com sucesso.TIPO:1";
$response["entradaid"] = $this->mdEntrada->id;
return $response;
}
我猜是用户多次按下按钮发送,但我在按钮上设置了启用和不启用的时间,该按钮不会同时发出许多请求,下面是按钮的代码:
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
YoYo.with(Techniques.BounceIn)
.duration(500)
.playOn(v.findViewById(R.id.entradabt));
if (SystemClock.elapsedRealtime() - mLastClickTime < 3000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
enviarEntrada();
}
});
为什么我有这种重复的记录?有什么建议吗?
这是我的模式
1条答案
按热度按时间thtygnil1#
唯一\u键
在created\u at、updated\u at和placa上添加唯一键,因为这可能是此表的唯一标识符。这样可以同时防止插入。
还要检查一个请求是否多次发送同一个请求,并按placa过滤掉这些请求(carplate?)(一个请求只能发送一个placa)。
可能很方便?如果相同的placa在支付相同请求后1分钟内发送,则忽略它(sql查询过滤器,在最后一分钟内请求所有事务,并检查是否有(carplate)placa)。
result = SQL Query select all where placa is within 1 and or 2 min; if(result is same PLACA as request){ return; and ignore request)
一点伪代码来帮忙。