| package com.dayu.henanlibrary.activity; | 
|   | 
| import android.content.Intent; | 
| import android.os.Bundle; | 
| import android.text.Editable; | 
| import android.text.TextUtils; | 
| import android.text.TextWatcher; | 
| import android.view.LayoutInflater; | 
| import android.view.View; | 
| import android.widget.EditText; | 
|   | 
| import com.dayu.baselibrary.activity.BaseNfcActivity; | 
| import com.dayu.baselibrary.tools.BcdUtil; | 
| import com.dayu.baselibrary.utils.CRC8; | 
| import com.dayu.baselibrary.utils.MornyUtil; | 
| import com.dayu.baselibrary.utils.TipUtil; | 
| import com.dayu.henanlibrary.card.UserCardHN; | 
| import com.dayu.henanlibrary.dao.AppDatabase; | 
| import com.dayu.henanlibrary.dao.HNBaseDaoSingleton; | 
| import com.dayu.henanlibrary.databinding.ActivityRechargeHnBinding; | 
| import com.dayu.henanlibrary.dbBean.AdminDataBean; | 
| import com.dayu.henanlibrary.net.SocketCallBack; | 
| import com.dayu.henanlibrary.net.SocketData; | 
| import com.dayu.henanlibrary.net.SocketNet; | 
| import com.dayu.henanlibrary.socketBean.BalanceSelecteRequestBean; | 
| import com.dayu.henanlibrary.tools.NfcReadHelper; | 
| import com.dayu.henanlibrary.tools.WriteCardUtils; | 
| import com.dayu.henanlibrary.utils.CardCommon; | 
| import com.dayu.henanlibrary.utils.DeviceNumberUtils; | 
| import com.dayu.henanlibrary.utils.SocketUtil; | 
| import com.dayu.henanlibrary.view.ProgressDialog; | 
| import com.tencent.bugly.crashreport.CrashReport; | 
|   | 
| import java.util.Arrays; | 
|   | 
|   | 
| /** | 
|  * Copyright (C), 2023, | 
|  * Author: zuo | 
|  * Date: 2023-11-5 09:52 | 
|  * Description: 充值界面 | 
|  */ | 
| public class RechargeActivity extends HNBaseNfcActivity { | 
|   | 
|     Intent intent; | 
|   | 
|     boolean userFlag; | 
|     ActivityRechargeHnBinding binding; | 
|     public static RechargeActivity rechargeActivity; | 
|   | 
|     AdminDataBean adminData; | 
|     String userName; | 
|   | 
|   | 
|     @Override | 
|     protected void onCreate(Bundle savedInstanceState) { | 
|         super.onCreate(savedInstanceState); | 
|         try { | 
|             binding = ActivityRechargeHnBinding.inflate(LayoutInflater.from(this)); | 
|             setContentView(binding.getRoot()); | 
|             setPricePoint(binding.rechargeWater); | 
|             rechargeActivity = this; | 
|             initView(); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             CrashReport.postCatchedException(e); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public void onNfcBack(Intent intent) { | 
|         try { | 
|             this.intent = intent; | 
|             userCard = NfcReadHelper.getInstence(intent, this).getUserCardData(); | 
|             if (userCard != null) { | 
|                 if (userCard.getCardType().equalsIgnoreCase(CardCommon.USER_CARD_TYPE_1) | 
|                         || userCard.getCardType().equalsIgnoreCase(CardCommon.USER_CARD_TYPE_2) | 
|                         || userCard.getCardType().equalsIgnoreCase(CardCommon.USER_CARD_TYPE_3)) { | 
|                     ProgressDialog.show(this); | 
|                     selectBalance(userCard.getInitPeasantCode()); | 
|                 } else { | 
|                     TipUtil.show(RechargeActivity.this, "当前卡非用户卡"); | 
|                 } | 
|   | 
|             } else { | 
|                 TipUtil.show(RechargeActivity.this, "卡片读取失败"); | 
|             } | 
|   | 
| //            readAllData(intent); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             CrashReport.postCatchedException(e); | 
|         } | 
|     } | 
|   | 
|     private void initView() { | 
|         try { | 
|             adminData = baseDao.adminDao().findFirst(); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             CrashReport.postCatchedException(e); | 
|         } | 
|         binding.rechargeRegistBtn.setOnClickListener(v -> { | 
|             String morny = binding.rechargeWater.getText().toString(); | 
|             if (!TextUtils.isEmpty(morny)) { | 
|                 String initCode = DeviceNumberUtils.getDeviceNumber(); | 
|                 if (TextUtils.isEmpty(initCode)) { | 
|                     TipUtil.show(RechargeActivity.this, "设备注册号为空,请先设置IP和管理员地址"); | 
|                     return; | 
|                 } | 
|                 Intent intent = new Intent(RechargeActivity.this, NFCWreatActivity.class); | 
|                 intent.putExtra("morny", morny); | 
|                 intent.putExtra("userName", userName); | 
|                 intent.putExtra("userCard", userCard); | 
|                 startActivity(intent); | 
|             } else { | 
|                 TipUtil.show(RechargeActivity.this, "请输入充值金额(元)"); | 
|             } | 
|         }); | 
|     } | 
|   | 
|   | 
|     //校验金额 | 
|     public static void setPricePoint(final EditText editText) { | 
|         editText.addTextChangedListener(new TextWatcher() { | 
|             @Override | 
|             public void onTextChanged(CharSequence s, int start, int before, | 
|                                       int count) { | 
|                 if (s.toString().contains(".")) { | 
|                     if (s.length() - 1 - s.toString().indexOf(".") > 2) { | 
|                         s = s.toString().subSequence(0, | 
|                                 s.toString().indexOf(".") + 3); | 
|                         editText.setText(s); | 
|                         editText.setSelection(s.length()); | 
|                     } | 
|                 } | 
|                 if (s.toString().trim().substring(0).equals(".")) { | 
|                     s = "0" + s; | 
|                     editText.setText(s); | 
|                     editText.setSelection(2); | 
|                 } | 
|   | 
|                 if (s.toString().startsWith("0") | 
|                         && s.toString().trim().length() > 1) { | 
|                     if (!s.toString().substring(1, 2).equals(".")) { | 
|                         editText.setText(s.subSequence(0, 1)); | 
|                         editText.setSelection(1); | 
|                     } | 
|                 } | 
|             } | 
|   | 
|             @Override | 
|             public void beforeTextChanged(CharSequence s, int start, int count, | 
|                                           int after) { | 
|   | 
|             } | 
|   | 
|             @Override | 
|             public void afterTextChanged(Editable s) { | 
|                 // TODO Auto-generated method stub | 
|   | 
|             } | 
|   | 
|         }); | 
|   | 
|     } | 
|   | 
|     /** | 
|      * 查询余额 | 
|      * | 
|      * @param initPeasantCode 农户注册编号 | 
|      */ | 
|     private void selectBalance(String initPeasantCode) { | 
|         try { | 
|             BalanceSelecteRequestBean requestBean = new BalanceSelecteRequestBean(); | 
|             requestBean.setAFN("94"); | 
|             requestBean.setControl("01"); | 
|             String initCode = DeviceNumberUtils.getDeviceNumber(); | 
|             requestBean.setInitCode(initCode); | 
|             requestBean.setInitPeasantCode(initPeasantCode); | 
|             requestBean.setXuLie(SocketUtil.getXuLie(this)); | 
|             SocketNet.getInstance().sendTestMessage(requestBean.pack(), "94", new SocketCallBack() { | 
|                 @Override | 
|                 public void onSocketResponse(SocketData readData) { | 
|                     ProgressDialog.dismiss(); | 
|                     if (CRC8.isCRC8(readData.getOriginDataBytes())) { | 
|                         StringBuffer stateText; | 
|                         try { | 
|                             stateText = new StringBuffer(); | 
|                             String state = BcdUtil.bcdToStr(readData.getBodyBytes()[2]);  //BCD码00启用 01禁用 02隶属信息不符 03无此卡信息 04其它s | 
|                             int balance = 0; | 
|   | 
|                             try { | 
|                                 //只有启动状态才能充值 | 
|                                 //金额 | 
|                                 byte[] blanceByte = Arrays.copyOfRange(readData.getBodyBytes(), 3, 7); | 
|                                 balance = SocketUtil.get16to10LowHigh(blanceByte); | 
|                                 //姓名 | 
|                                 byte[] nameByte = Arrays.copyOfRange(readData.getBodyBytes(), 7, 19); | 
|                                 userName = SocketUtil.fromHexString(SocketUtil.bytesToHexClean0(nameByte)); | 
|                             } catch (Exception e) { | 
|                                 e.printStackTrace(); | 
|                             } | 
|                             binding.rechargeLL.setVisibility(View.VISIBLE); | 
|                             binding.rechargeRegistBtn.setVisibility(View.VISIBLE); | 
|                             binding.rechargeReadLL.setVisibility(View.GONE); | 
|                             binding.rechargeTextLL.setVisibility(View.VISIBLE); | 
|                             binding.userName.setText(userName); | 
|                             binding.redRemainderBlance.setText(MornyUtil.changeF2Y(balance)); | 
|                             binding.redStatu.setText(stateText.toString()); | 
|                             binding.redInitCode.setText(initPeasantCode); | 
|   | 
|                             if ("00".equals(state)) { | 
|                                 binding.rechargeRegistBtn.setVisibility(View.VISIBLE); | 
|                                 stateText.append("启用"); | 
|                                 userCard.setState("00"); | 
|                                 userCard.setBalance(balance); | 
|                                 binding.redStatu.setText(stateText.toString()); | 
|                                 return; | 
|                             } else if ("01".equals(state)) { | 
|                                 stateText.append("该卡已被禁用"); | 
|                                 userCard.setState("01"); | 
|                                 userCard.setBalance(balance); | 
|                                 userFlag = WriteCardUtils.setUser(intent, userCard, RechargeActivity.this); | 
|                             } else if ("02".equals(state)) { | 
|                                 stateText.append("隶属信息不符"); | 
|                             } else if ("03".equals(state)) { | 
|                                 stateText.append("无此卡信息"); | 
|                             } else if ("04".equals(state)) { | 
|                                 stateText.append("其它"); | 
|                             } | 
|                             binding.redStatu.setText(stateText.toString()); | 
|                             binding.rechargeLL.setVisibility(View.GONE); | 
|                             binding.rechargeRegistBtn.setVisibility(View.GONE); | 
|                             binding.rechargeRegistBtn.setVisibility(View.GONE); | 
|                             TipUtil.show(RechargeActivity.this, stateText.toString()); | 
|                         } catch (Exception e) { | 
|                             e.printStackTrace(); | 
|                         } | 
|                     } else { | 
|                         //校验CRC错误 | 
|                         TipUtil.show(RechargeActivity.this, "开卡报文校验CRC错误"); | 
|                     } | 
|                 } | 
|   | 
|                 @Override | 
|                 public void onSocketError(int code, String msg) { | 
|                     runOnUiThread(() -> { | 
|                         ProgressDialog.dismiss(); | 
|                         TipUtil.show(RechargeActivity.this, msg); | 
|                     }); | 
|                 } | 
|             }); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             CrashReport.postCatchedException(e); | 
|         } | 
|   | 
|     } | 
|   | 
|     protected void onDestroy() { | 
|         super.onDestroy(); | 
|         rechargeActivity = null; | 
|     } | 
| } |