| package com.dayu.qiheonlinelibrary.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.net.subscribers.SubscriberListener; | 
| import com.dayu.baselibrary.utils.DeviceUtils; | 
| import com.dayu.baselibrary.utils.MornyUtil; | 
| import com.dayu.baselibrary.utils.TipUtil; | 
| import com.dayu.qiheonlinelibrary.QHOnLineApplication; | 
| import com.dayu.qiheonlinelibrary.R; | 
| import com.dayu.qiheonlinelibrary.bean.CardInfoResult; | 
| import com.dayu.qiheonlinelibrary.bean.RechargeResult; | 
| import com.dayu.qiheonlinelibrary.databinding.ActivityRechargeQhlBinding; | 
| import com.dayu.qiheonlinelibrary.dbBean.RechargeBean; | 
| import com.dayu.qiheonlinelibrary.net.ApiManager; | 
| import com.dayu.qiheonlinelibrary.net.BaseResponse; | 
| import com.dayu.qiheonlinelibrary.tools.NfcReadHelper; | 
| import com.dayu.qiheonlinelibrary.utils.CardCommon; | 
| import com.tencent.bugly.crashreport.CrashReport; | 
|   | 
| import java.util.Calendar; | 
| import java.util.HashMap; | 
| import java.util.Map; | 
|   | 
|   | 
| /** | 
|  * Copyright (C), 2023, | 
|  * Author: zuo | 
|  * Date: 2023-11-5 09:52 | 
|  * Description: 充值界面 | 
|  */ | 
| public class RechargeActivityQHOline extends QHOlineBaseNfcActivity { | 
|   | 
|     Intent intent; | 
|   | 
|     ActivityRechargeQhlBinding binding; | 
|     public static RechargeActivityQHOline rechargeActivity; | 
|   | 
|   | 
|     String cardNumber = null;//实体卡号 | 
|     RechargeBean rechargeBean; | 
|   | 
|     String rechageWater;//充值的水量 | 
|   | 
|     String cardType;//卡状态 | 
|   | 
|     CardInfoResult cardInfoResult; | 
|   | 
|   | 
|     @Override | 
|     protected void onCreate(Bundle savedInstanceState) { | 
|         super.onCreate(savedInstanceState); | 
|         try { | 
|             binding = ActivityRechargeQhlBinding.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) { | 
|         this.intent = intent; | 
|         try { | 
|             readAllData(); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             CrashReport.postCatchedException(e); | 
|         } | 
|     } | 
|   | 
|   | 
|     private void readAllData() { | 
|         cardType = NfcReadHelper.getInstence(intent, this).getCradTypeAndCardNumber(); | 
|   | 
|         String[] cardTypes = cardType.split(","); | 
|         if (cardTypes != null && cardTypes.length == 2) { | 
|             cardNumber = cardType.split(",")[0]; | 
|             cardType = cardType.split(",")[1]; | 
|         } | 
|         //获取卡内信息 | 
|         userCard = NfcReadHelper.getInstence(intent, this).getUserCardData(); | 
|         if (userCard != null) { | 
|             try { | 
|                 getUserInfoAndCardInfo(cardNumber, userCard.getMyUserCode()); | 
|             } catch (Exception e) { | 
|                 e.printStackTrace(); | 
|                 CrashReport.postCatchedException(e); | 
|                 TipUtil.show(this, "读卡失败,请刷卡后不要立即移开"); | 
|             } | 
|         } else { | 
|             TipUtil.show(this, "读卡失败,请刷卡后不要立即移开"); | 
|         } | 
|   | 
|   | 
|     } | 
|   | 
|   | 
|     private void setUserData(String cardNumber, String userName, String lastRechargeTime) { | 
|         if (cardType.equalsIgnoreCase(CardCommon.USER_CARD_TYPE_1)) { | 
|             setCardState("终端写卡"); | 
|             binding.rechargeLL.setVisibility(View.VISIBLE); | 
|             binding.rechargeRegistBtn.setVisibility(View.VISIBLE); | 
|             binding.redStatu.setTextColor(this.getResources().getColor(R.color.black)); | 
|         } else if (cardType.equalsIgnoreCase(CardCommon.USER_CARD_TYPE_2)) { | 
|             setCardState("未刷卡关泵,当前不可充值"); | 
|             TipUtil.show(RechargeActivityQHOline.this, "未刷卡关泵,当前不可充值"); | 
|         } else if (cardType.equalsIgnoreCase(CardCommon.USER_CARD_TYPE_3)) { | 
|             setCardState("叠加充值"); | 
|         } else { | 
|             TipUtil.show(this, "非用户卡,不能充值"); | 
|             return; | 
|         } | 
|         if (userCard != null) { | 
|             try { | 
|                 if (Integer.valueOf(QHOnLineApplication.getInstance().arerNumber) == userCard.getArerNumber()) { | 
|                     binding.rechargeReadLL.setVisibility(View.GONE); | 
|                     binding.rechargeTextLL.setVisibility(View.VISIBLE); | 
|                     binding.userName.setText(userName); | 
|                     binding.redInitCode.setText(cardNumber); | 
|                     binding.redRechargeDate.setText("本卡最后购水日期:\n\n" + lastRechargeTime); | 
|                     binding.redUserCode.setText(cardInfoResult.getIccardCode()); | 
|                     if (userCard != null) { | 
|                         binding.redRemainderBlance.setText(MornyUtil.changeF2Y(userCard.getBalance()) + " 元"); | 
|                         binding.redBalanceElectric.setText("充值后剩余电量:" + MornyUtil.changeF2Y(userCard.getSurplusElecticity()) + " 度"); | 
|                     } | 
|                     binding.redRechargeElectric.setText("电量单价:" + QHOnLineApplication.getInstance().electriclePriceStr + "元/度"); | 
|   | 
|                 } else { | 
|                     TipUtil.show(this, "非本区域卡\n不能充值!!!"); | 
|                 } | 
|             } catch (Exception e) { | 
|                 e.printStackTrace(); | 
|                 CrashReport.postCatchedException(e); | 
|                 TipUtil.show(this, "读卡失败,请刷卡后不要立即移开"); | 
|             } | 
|         } else { | 
|             TipUtil.show(this, "读卡失败,请刷卡后不要立即移开"); | 
|         } | 
|   | 
|   | 
|     } | 
|   | 
|     private void initView() { | 
|         binding.rechargeRegistBtn.setOnClickListener(v -> { | 
|             String morny = binding.rechargeWater.getText().toString(); | 
|             if (!TextUtils.isEmpty(morny)) { | 
|                 rechaege(morny); | 
|             } else { | 
|                 TipUtil.show(RechargeActivityQHOline.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 | 
|   | 
|             } | 
|   | 
|         }); | 
|   | 
|     } | 
|   | 
|     @Override | 
|     protected void onDestroy() { | 
|         super.onDestroy(); | 
|         rechargeActivity = null; | 
|     } | 
|   | 
|   | 
|     private void startNfcActivity(String morny, String balance, float rechargeElectric, float iccardBalanceElectric, float totalMorny, String iccardRechargeRecordId) { | 
|         //先处理补扣 | 
|         //充值补扣金额=充值金额+充值前读取的卡内余额-充值回调返回卡内余额 | 
|         int deductionMorny = MornyUtil.changeY2F(morny) + userCard.getBalance() - MornyUtil.changeY2F(balance); | 
|   | 
|         if (MornyUtil.changeY2F(morny) < deductionMorny) { | 
|             TipUtil.show(RechargeActivityQHOline.this, "当前充值金额小于补扣金额,补扣金额为" + MornyUtil.changeF2Y(deductionMorny) + "元,充值金额应大于补扣金额!"); | 
|             return; | 
|         } | 
|   | 
|         //添加充值信息 | 
|         rechargeBean = new RechargeBean(); | 
|         rechargeBean.setCardNumber(cardNumber); | 
|         rechargeBean.setUserCode(userCard.getUserCode() + userCard.getUserCodeNumber()); | 
|         rechargeBean.setUserName(cardInfoResult.getPeasantName()); | 
|         rechargeBean.setPersonId(cardInfoResult.getPeasantId()); | 
|         rechargeBean.setSerial(DeviceUtils.getSN()); | 
|         rechargeBean.setArerNumber(QHOnLineApplication.getInstance().arerNumber); | 
|         rechargeBean.setMorny(morny); | 
|         rechargeBean.setBalance(balance); | 
|         rechargeBean.setRechargeElectric(String.valueOf(rechargeElectric)); | 
|         rechargeBean.setPrice(QHOnLineApplication.getInstance().electriclePriceStr); | 
|         rechargeBean.setSurplusElectic(String.valueOf(iccardBalanceElectric)); | 
|         long time = System.currentTimeMillis(); | 
|         rechargeBean.setRechargeDate(time); | 
|         rechargeBean.setUserId(""); | 
|         rechargeBean.setTotalMorny(String.valueOf(totalMorny)); | 
|         rechargeBean.setState(1); | 
|         rechargeBean.setOrderId(iccardRechargeRecordId); | 
|         rechargeBean.setDeductionMorny(MornyUtil.changeF2Y(deductionMorny)); | 
|         rechargeBean.setAdminId(QHOnLineApplication.getInstance().adminId); | 
|   | 
|   | 
|         userCard.setCardWriteState(0); | 
|         userCard.setCardState(1); | 
|         Calendar calendar = Calendar.getInstance(); | 
|         calendar.setTimeInMillis(time); | 
|         userCard.setBalance(MornyUtil.changeY2F(String.valueOf(balance))); | 
|         userCard.setSurplusElecticity(MornyUtil.changeY2F(String.valueOf(iccardBalanceElectric))); | 
|         userCard.setTotalMorny(MornyUtil.changeY2F(String.valueOf(totalMorny))); | 
|         userCard.setRechargeDate(calendar); | 
|         userCard.setElectricPrice(Float.valueOf(QHOnLineApplication.getInstance().electriclePriceStr)); | 
|         if (deductionMorny == 0) { | 
|             Intent intent = new Intent(RechargeActivityQHOline.this, NFCWreatActivityQHOline.class); | 
|             intent.putExtra("morny", morny); | 
|             intent.putExtra("userCard", userCard); | 
|             intent.putExtra("rechargeBean", rechargeBean); | 
|             startActivity(intent); | 
|         } else { | 
|             TipUtil.show(RechargeActivityQHOline.this, "挂失卡后有补交金额!补交金额为:" + MornyUtil.changeF2Y(deductionMorny) + "元,详情可在平台查询。", new TipUtil.TipListener() { | 
|                 @Override | 
|                 public void onCancle() { | 
|                     Intent intent = new Intent(RechargeActivityQHOline.this, NFCWreatActivityQHOline.class); | 
|                     intent.putExtra("morny", morny); | 
|                     intent.putExtra("userCard", userCard); | 
|                     intent.putExtra("rechargeBean", rechargeBean); | 
|                     startActivity(intent); | 
|                 } | 
|             }); | 
|         } | 
|   | 
|     } | 
|   | 
|   | 
|     /** | 
|      * IC卡充值 | 
|      * | 
|      * @param morny | 
|      */ | 
|     public void rechaege(String morny) { | 
|         Map<String, Object> data = new HashMap<>(); | 
|         //终端设备号 | 
|         data.put("equipCode", DeviceUtils.getSN()); | 
|         //ic卡信息id | 
|         data.put("iccardNum", cardNumber); | 
|         //交易水费金额(元) | 
|         data.put("rechargeWaterMoney", morny); | 
|         //卡内剩余金额(元) | 
|         data.put("iccardBalanceMoney", MornyUtil.changeF2Y(userCard.getBalance())); | 
|         //卡内剩余水量(m³) | 
|         data.put("iccardBalanceAmonunt", "0"); | 
|         //卡内剩余电量 | 
|         data.put("iccardBalanceElectric", MornyUtil.changeF2Y(userCard.getSurplusElecticity())); | 
|         ApiManager.getInstance().requestPostLoading(this, "iccard/recharge/rechargeCard", RechargeResult.class, data, new SubscriberListener<BaseResponse<RechargeResult>>() { | 
|             @Override | 
|             public void onNext(BaseResponse<RechargeResult> t) { | 
|                 if (t.isSuccess()) { | 
|                     if (t.getData() != null) { | 
|                         RechargeResult rechargeResult = t.getData(); | 
|                         startNfcActivity(morny, rechargeResult.getIccardBalanceMoney(), rechargeResult.getRechargeElectricAmount(), rechargeResult.getIccardBalanceElectric(), rechargeResult.getTotalRechargeMoney(), rechargeResult.getIccardRechargeRecordId()); | 
|                     } else { | 
|                         TipUtil.show(RechargeActivityQHOline.this, "充值失败请重试!"); | 
|                     } | 
|                 } else { | 
|                     TipUtil.show(RechargeActivityQHOline.this, t.getMsg()); | 
|                 } | 
|             } | 
|         }); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 获取卡信息和用户信息 | 
|      * | 
|      * @param iccardCode | 
|      */ | 
|     public void getUserInfoAndCardInfo(String iccardNum, String iccardCode) { | 
|         Map<String, Object> data = new HashMap<>(); | 
|         //ic卡信息id | 
|         data.put("iccardNum", iccardNum); | 
|         data.put("iccardCode", iccardCode); | 
|         ApiManager.getInstance().requestPostLoading(this, "iccard/peasant/getIccardInfoVoByCardCode", CardInfoResult.class, data, new SubscriberListener<BaseResponse<CardInfoResult>>() { | 
|             @Override | 
|             public void onNext(BaseResponse<CardInfoResult> t) { | 
|                 String state = ""; | 
|                 if (t.isSuccess()) { | 
|                     if (t.getData() != null) { | 
|                         //ic卡状态 0 正常 1 已注销 2 已挂失 | 
|                         if (t.getData().getIccardStatus() == 0) { | 
| //                            List<byte[]> data = NfcReadHelper.getInstence(intent, RechargeActivity.this).getOnesectorData(); | 
|                             if (!data.isEmpty()) { | 
|                                 if (!TextUtils.isEmpty(cardNumber)) { | 
|                                     cardInfoResult = t.getData(); | 
|                                     setUserData(cardNumber, cardInfoResult.getPeasantName(), cardInfoResult.getLastRechargeTime()); | 
|                                 } else { | 
|                                     TipUtil.show(RechargeActivityQHOline.this, "读取卡片失败,请重新贴卡"); | 
|                                 } | 
|                             } else { | 
|                                 TipUtil.show(RechargeActivityQHOline.this, "卡片识别错误,请重试!"); | 
|                             } | 
|                         } else if (t.getData().getIccardStatus() == 1) { | 
|                             TipUtil.show(RechargeActivityQHOline.this, "当前卡已注销"); | 
|                         } else { | 
|                             TipUtil.show(RechargeActivityQHOline.this, "当前卡已挂失"); | 
|                         } | 
|                     } else { | 
|                         TipUtil.show(RechargeActivityQHOline.this, "当前卡片错误,未能查到该卡信息!"); | 
|                     } | 
|   | 
|                 } else { | 
|                     TipUtil.show(RechargeActivityQHOline.this, t.getMsg()); | 
|                 } | 
|             } | 
|         }); | 
|     } | 
|   | 
|   | 
|     private void setCardState(String state) { | 
|         binding.rechargeLL.setVisibility(View.GONE); | 
|         binding.rechargeRegistBtn.setVisibility(View.GONE); | 
|         binding.redStatu.setTextColor(RechargeActivityQHOline.this.getResources().getColor(com.dayu.baselibrary.R.color.red)); | 
|         binding.redStatu.setText(state); | 
|     } | 
|   | 
|   | 
| } |