| New file | 
 |  |  | 
 |  |  | package com.dayu.qihealonelibrary.activity; | 
 |  |  |  | 
 |  |  | import android.annotation.SuppressLint; | 
 |  |  | 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.tools.HexUtil; | 
 |  |  | import com.dayu.baselibrary.utils.MornyUtil; | 
 |  |  | import com.dayu.baselibrary.utils.TipUtil; | 
 |  |  | import com.dayu.qihealonelibrary.R; | 
 |  |  | import com.dayu.qihealonelibrary.card.UserCard; | 
 |  |  | import com.dayu.qihealonelibrary.databinding.ActivityRechargeQhaBinding; | 
 |  |  | import com.dayu.qihealonelibrary.dbBean.AdminDataBean; | 
 |  |  | import com.dayu.qihealonelibrary.dbBean.ElectricPriceBean; | 
 |  |  | import com.dayu.qihealonelibrary.dbBean.UserCardBean; | 
 |  |  | import com.dayu.qihealonelibrary.tools.NfcReadHelper; | 
 |  |  | import com.dayu.qihealonelibrary.utils.CardCommon; | 
 |  |  | import com.dayu.qihealonelibrary.utils.DeviceNumberUtils; | 
 |  |  | import com.dayu.qihealonelibrary.view.ProgressDialog; | 
 |  |  | import com.tencent.bugly.crashreport.CrashReport; | 
 |  |  |  | 
 |  |  | import java.util.Calendar; | 
 |  |  | import java.util.List; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * Copyright (C), 2023, | 
 |  |  |  * Author: zuo | 
 |  |  |  * Date: 2023-11-5 09:52 | 
 |  |  |  * Description: 充值界面 | 
 |  |  |  */ | 
 |  |  | public class RechargeActivity extends BaseNfcActivity { | 
 |  |  |  | 
 |  |  |     Intent intent; | 
 |  |  |  | 
 |  |  |     ActivityRechargeQhaBinding binding; | 
 |  |  |     public static RechargeActivity rechargeActivity; | 
 |  |  |  | 
 |  |  |     AdminDataBean adminData; | 
 |  |  |     String userName; | 
 |  |  |     String cardNumber = null; | 
 |  |  |     UserCardBean userCardBean; | 
 |  |  |     int waterPrice; | 
 |  |  |     String rechageWater;//充值的水量 | 
 |  |  |     ElectricPriceBean priceBean; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     protected void onCreate(Bundle savedInstanceState) { | 
 |  |  |         super.onCreate(savedInstanceState); | 
 |  |  |         try { | 
 |  |  |             binding = ActivityRechargeQhaBinding.inflate(LayoutInflater.from(this)); | 
 |  |  |             setContentView(binding.getRoot()); | 
 |  |  |             setPricePoint(binding.rechargeWater); | 
 |  |  |             rechargeActivity = this; | 
 |  |  |             waterPrice = baseDao.adminDao().findFirst().getWaterPrice(); | 
 |  |  |             initView(); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |             CrashReport.postCatchedException(e); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void onNewIntent(Intent intent) { | 
 |  |  |         this.intent = intent; | 
 |  |  |         try { | 
 |  |  |             readAllData(); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |             CrashReport.postCatchedException(e); | 
 |  |  |         } | 
 |  |  |         super.onNewIntent(intent); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     private void readAllData() { | 
 |  |  |         String cardType = NfcReadHelper.getInstence(intent, this).getCradTypeAndCardNumber(); | 
 |  |  |  | 
 |  |  |         String[] cardTypes = cardType.split(","); | 
 |  |  |         if (cardTypes != null && cardTypes.length == 2) { | 
 |  |  |             cardNumber = cardType.split(",")[0]; | 
 |  |  |             cardNumber = HexUtil.spaceHex(cardNumber); | 
 |  |  |             cardNumber = HexUtil.HighLowHex(cardNumber); | 
 |  |  |             cardType = cardType.split(",")[1]; | 
 |  |  |         } | 
 |  |  |         List<byte[]> data = NfcReadHelper.getInstence(intent, this).getOnesectorData(); | 
 |  |  |  | 
 |  |  |         if (!data.isEmpty()) { | 
 |  |  |             if (!TextUtils.isEmpty(cardNumber)) { | 
 |  |  |                 setUserData(cardType, cardNumber, data); | 
 |  |  |                 if(priceBean!=null){ | 
 |  |  |                     binding.redRechargeElectric.setText("电量单价:"+priceBean.getPrice()+"元/度"); | 
 |  |  |                 } | 
 |  |  |             } else { | 
 |  |  |                 TipUtil.show(this, "读取卡片失败,请重新贴卡"); | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             TipUtil.show(this, "卡片识别错误,请重试!"); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @SuppressLint("ResourceAsColor") | 
 |  |  |     private void setUserData(String cardType, String cardNumber, List<byte[]> data) { | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         String state = ""; | 
 |  |  |         if (cardType.equalsIgnoreCase(CardCommon.USER_CARD_TYPE_1)) { | 
 |  |  |             state = "终端写卡"; | 
 |  |  |             binding.rechargeLL.setVisibility(View.VISIBLE); | 
 |  |  |             binding.rechargeRegistBtn.setVisibility(View.VISIBLE); | 
 |  |  |             binding.redStatu.setTextColor(R.color.black); | 
 |  |  |         } else if (cardType.equalsIgnoreCase(CardCommon.USER_CARD_TYPE_2)) { | 
 |  |  |             state = "未刷卡关泵,当前不可充值"; | 
 |  |  |             binding.rechargeLL.setVisibility(View.GONE); | 
 |  |  |             binding.rechargeRegistBtn.setVisibility(View.GONE); | 
 |  |  |             binding.redStatu.setTextColor(com.dayu.baselibrary.R.color.red); | 
 |  |  |         } else if (cardType.equalsIgnoreCase(CardCommon.USER_CARD_TYPE_3)) { | 
 |  |  |             state = "叠加充值"; | 
 |  |  |             binding.rechargeLL.setVisibility(View.GONE); | 
 |  |  |             binding.rechargeRegistBtn.setVisibility(View.GONE); | 
 |  |  |         } else { | 
 |  |  |             TipUtil.show(this, "非用户卡,不能充值"); | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         userCard = UserCard.getBean(data); | 
 |  |  |         List<UserCardBean> userCardBeans = baseDao.userCardDao().findUserName(cardNumber); | 
 |  |  |         AdminDataBean adminDataBean = baseDao.adminDao().findFirst(); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         if (Integer.valueOf(adminDataBean.getAddressCode()) == userCard.getArerNumber()) { | 
 |  |  |             if (!userCardBeans.isEmpty()) { | 
 |  |  |                 userCardBean = userCardBeans.get(0); | 
 |  |  |                 this.userName = userCardBean.getUserName(); | 
 |  |  |             } else { | 
 |  |  |                 userCardBean = new UserCardBean(); | 
 |  |  |                 userCardBean.setCardNumber(cardNumber); | 
 |  |  |                 userCardBean.setUserName("未知用户"); | 
 |  |  |                 userCardBean.setPhone("未知"); | 
 |  |  |                 userCardBean.setUserID("未知"); | 
 |  |  |                 userCardBean.setAddressCode(String.valueOf(userCard.getArerNumber())); | 
 |  |  |                 userCardBean.setSerial(String.valueOf(userCard.getArerNumber())); | 
 |  |  |                 this.userName = "未知用户"; | 
 |  |  |             } | 
 |  |  |             binding.rechargeReadLL.setVisibility(View.GONE); | 
 |  |  |             binding.rechargeTextLL.setVisibility(View.VISIBLE); | 
 |  |  |             binding.userName.setText(userName); | 
 |  |  |             binding.redStatu.setText(state); | 
 |  |  |             if (userCard != null) { | 
 |  |  |                 Calendar calendar = userCard.getRechargeDate(); | 
 |  |  |                 if (calendar != null) { | 
 |  |  |                     int year = calendar.get(Calendar.YEAR); | 
 |  |  |                     int month = calendar.get(Calendar.MONTH); // 月份从0开始,所以需要加1 | 
 |  |  |                     int day = calendar.get(Calendar.DAY_OF_MONTH); | 
 |  |  |                     binding.redRechargeDate.setText("本卡最后购水日期:" + year + "年" + month + "月" + day + "日"); | 
 |  |  |                 } else { | 
 |  |  |                     binding.redRechargeDate.setText("本卡最后购水日期:无"); | 
 |  |  |                 } | 
 |  |  |                 binding.redInitCode.setText(cardNumber); | 
 |  |  | //                binding.redRechargeNumber.setText("本卡充值次数:" + userCard.getRechargeTimes() + " 次"); | 
 |  |  | //                binding.redTotalWater.setText("总用水量:" + MornyUtil.changeF2Y(userCard.getTotalWater()) + " 吨"); | 
 |  |  | //                binding.redTotalPower.setText("总用电量:" + MornyUtil.changeF2Y(userCard.getTotalElectric()) + " 度"); | 
 |  |  |                 binding.redRemainderBlance.setText(MornyUtil.changeF2Y(userCard.getBalance()) + " 元"); | 
 |  |  |                 binding.redRemainderWater.setText("剩余水量:" + MornyUtil.changeF2Y(userCard.getSurplusWater()) + " 吨"); | 
 |  |  | //                binding.redWaterPrice.setText("每立方米水价格:" + MornyUtil.changeF2Y(adminData.getWaterPrice()) + " 元"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         } else { | 
 |  |  |             TipUtil.show(this, "非本区域卡\n不能充值!!!"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     private void initView() { | 
 |  |  |         try { | 
 |  |  |             adminData = baseDao.adminDao().findFirst(); | 
 |  |  |             priceBean = baseDao.electricPriceDao().findFirst(); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |             CrashReport.postCatchedException(e); | 
 |  |  |         } | 
 |  |  |         binding.rechargeRegistBtn.setOnClickListener(v -> { | 
 |  |  |             String morny = binding.rechargeWater.getText().toString(); | 
 |  |  |             if (!TextUtils.isEmpty(morny)) { | 
 |  |  |                 Intent intent = new Intent(RechargeActivity.this, NFCWreatActivity.class); | 
 |  |  |                 //当前金额单位为元 | 
 |  |  |                 intent.putExtra("morny", morny); | 
 |  |  |                 intent.putExtra("userName", userName); | 
 |  |  |                 intent.putExtra("cardNumber", cardNumber); | 
 |  |  |                 intent.putExtra("userCard", userCard); | 
 |  |  |                 intent.putExtra("userCardBean", userCardBean); | 
 |  |  | //                intent.putExtra("rechageWater", rechageWater); | 
 |  |  |                 startActivity(intent); | 
 |  |  |             } else { | 
 |  |  |                 TipUtil.show(RechargeActivity.this, "请输入充值金额(元)"); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     //校验金额 | 
 |  |  |     public 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); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 String morny = editText.getText().toString(); | 
 |  |  |                 if (!TextUtils.isEmpty(morny)) { | 
 |  |  |                     rechageWater = MornyUtil.intDiv(MornyUtil.changeY2F(editText.getText().toString()), waterPrice); | 
 |  |  | //                    binding.redRechargeWater.setText("充值水量:" + rechageWater + " 立方米(吨)"); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             @Override | 
 |  |  |             public void beforeTextChanged(CharSequence s, int start, int count, | 
 |  |  |                                           int after) { | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             @Override | 
 |  |  |             public void afterTextChanged(Editable s) { | 
 |  |  |                 // TODO Auto-generated method stub | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         }); | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     protected void onDestroy() { | 
 |  |  |         super.onDestroy(); | 
 |  |  |         rechargeActivity = null; | 
 |  |  |     } | 
 |  |  | } |