SendInfoConfirmFragment.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package com.emato.ich.fragment;
  2. import android.app.AlertDialog;
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import com.emato.ich.utils.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import androidx.annotation.NonNull;
  10. import androidx.annotation.Nullable;
  11. import androidx.fragment.app.Fragment;
  12. import androidx.navigation.fragment.NavHostFragment;
  13. import com.emato.ich.MainActivity;
  14. import com.emato.ich.R;
  15. import com.emato.ich.api.ICSPClient;
  16. import com.emato.ich.contant.ICSPConstant;
  17. import com.emato.ich.contant.DecisionConstant;
  18. import com.emato.ich.databinding.FragmentSendInfoConfirmBinding;
  19. import com.emato.ich.entity.vo.ResponseData;
  20. import com.emato.ich.entity.vo.ConfirmOrderVo;
  21. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  22. import com.emato.ich.local.LocalStorage;
  23. import com.emato.ich.utils.BaseUtils;
  24. import com.emato.ich.utils.ButtonUtils;
  25. import com.emato.ich.utils.JacksonUtils;
  26. import com.emato.ich.utils.LoggingUtils;
  27. import com.emato.ich.utils.NavUtils;
  28. import com.emato.ich.utils.StringUtils;
  29. import com.emato.ich.utils.TimeOutUtils;
  30. import com.emato.ich.utils.ToastUtils;
  31. import com.fasterxml.jackson.core.JsonProcessingException;
  32. import com.fasterxml.jackson.databind.ObjectMapper;
  33. import org.jetbrains.annotations.NotNull;
  34. import java.io.IOException;
  35. import okhttp3.Call;
  36. import okhttp3.Callback;
  37. import okhttp3.Response;
  38. /**
  39. * 投递信息确认页面
  40. */
  41. public class SendInfoConfirmFragment extends Fragment {
  42. private FragmentSendInfoConfirmBinding binding;
  43. private static final String TAG = SendInfoConfirmFragment.class.getName();
  44. private CountDownTimer timer;
  45. private long start;
  46. @Nullable
  47. @org.jetbrains.annotations.Nullable
  48. @Override
  49. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  50. start = System.currentTimeMillis();
  51. binding = FragmentSendInfoConfirmBinding.inflate(inflater, container, false);
  52. return binding.getRoot();
  53. }
  54. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  55. long end = System.currentTimeMillis();
  56. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  57. try {
  58. MainActivity activity = (MainActivity) getActivity();
  59. timer = TimeOutUtils.timeout(activity, SendInfoConfirmFragment.this, binding.timeout, 150);
  60. timer.start();
  61. } catch (Exception e) {
  62. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  63. LoggingUtils.sendErrorLog("业务异常: 订单确认页面倒计时出现异常! ", e);
  64. }
  65. Bundle arguments = null;
  66. MainActivity activity = null;
  67. try {
  68. activity = (MainActivity) getActivity();
  69. arguments = activity.getBundleMap().get(InputInfoKeyBoardFragment.class.getName());
  70. } catch (RuntimeException e) {
  71. Log.e(TAG, "onResponse: SendInfoConfirmFragment页面获取InputInfoKeyBoardFragment传值错误! ", e);
  72. LoggingUtils.sendErrorLog("业务异常: SendInfoConfirmFragment页面获取InputInfoKeyBoardFragment传值错误! ", e);
  73. }
  74. String orderSn = null;
  75. String collectorPhone;
  76. PreparedOrderResponseVo orderResponseVo;
  77. String sectionType = null;
  78. // 设置订单提示属性
  79. if (null != arguments) {
  80. try {
  81. String response = arguments.getString("preparedOrderResponse");
  82. sectionType = arguments.getString("sectionType");
  83. orderResponseVo = JacksonUtils.objectmapper.readValue(response, PreparedOrderResponseVo.class);
  84. collectorPhone = orderResponseVo.getCollectorPhone();
  85. orderSn = orderResponseVo.getOrderSn();
  86. binding.openCabinetNo.setText(orderResponseVo.getLockerName());
  87. binding.takeNo.setText(arguments.getString("takeNo"));
  88. binding.takeNumber.setText(collectorPhone);
  89. } catch (JsonProcessingException e) {
  90. Log.e(TAG, "onViewCreated: 解析预下单页面传递信息错误! ", e);
  91. LoggingUtils.sendErrorLog("业务异常: 解析预下单页面传递信息错误! ", e);
  92. }
  93. }
  94. // 投递完成确认
  95. String finalOrderSn = orderSn;
  96. binding.alreadySend.setOnClickListener(view1 -> {
  97. if (ButtonUtils.isFastClick()) {
  98. return;
  99. }
  100. // TODO 确认投递, 真正下单
  101. String clientId = BaseUtils.getClientId();
  102. // /order/deliverer/confirm
  103. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  104. confirmOrderVo.setClientId(clientId);
  105. confirmOrderVo.setOrderSn(finalOrderSn);
  106. confirmOrderVo.setDecision(DecisionConstant.CONFIRM_00);
  107. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  108. @Override
  109. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  110. // TODO 确认失败
  111. // getActivity().runOnUiThread(() -> {
  112. //
  113. // });
  114. ToastUtils.make(getContext(), "投递失败!网络异常!");
  115. Log.e(TAG, "onFailure: 确认投递失败! ", e);
  116. LoggingUtils.sendErrorLog("业务异常: 投递失败!网络异常! ", e);
  117. }
  118. @Override
  119. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  120. getActivity().runOnUiThread(() -> {
  121. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  122. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  123. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  124. try {
  125. ResponseData responseData = objectMapper.readValue(parseResponse, ResponseData.class);
  126. String code = responseData.getCode();
  127. if (code.equals(ICSPConstant.OK)) {
  128. // TODO 确认投递成功逻辑处理
  129. ToastUtils.make(getContext(), "投递成功!");
  130. if (null != timer) {
  131. timer.cancel();
  132. }
  133. NavUtils.navigate(SendInfoConfirmFragment.this, R.id.action_sendInfoConfirmFragment_to_sendSuccessFragment);
  134. } else {
  135. // TODO 确认投递失败逻辑处理
  136. ToastUtils.make(getContext(), responseData.getMsg());
  137. Log.w(TAG, "onResponse: " + responseData.getMsg());
  138. }
  139. } catch (JsonProcessingException e) {
  140. Log.e(TAG, "onResponse: 确认投递解析响应信息失败!", e);
  141. LoggingUtils.sendErrorLog("业务异常: 确认投递解析响应信息失败! ", e);
  142. } catch (RuntimeException e) {
  143. Log.e(TAG, "onResponse: 投递失败! ", e);
  144. LoggingUtils.sendErrorLog("业务异常: 投递失败! 未知异常! ", e);
  145. }
  146. } else {
  147. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  148. }
  149. });
  150. }
  151. });
  152. // 投递完成后已经是另外一单, 可以继续异常处理
  153. LocalStorage.getInstance().getSession().setException(true);
  154. });
  155. // 异常页面跳转
  156. MainActivity finalActivity = activity;
  157. binding.exceptionBtn.setOnClickListener(view1 -> {
  158. if (ButtonUtils.isFastClick()) {
  159. return;
  160. }
  161. Bundle bundle = new Bundle();
  162. if (null != timer) {
  163. timer.cancel();
  164. }
  165. bundle.putString("exception", "exception_page");
  166. finalActivity.getBundleMap().put(SendInfoConfirmFragment.class.getName(), bundle);
  167. NavUtils.navigate(this, R.id.action_sendInfoConfirmFragment_to_exceptionFragment);
  168. });
  169. // 未投递按钮
  170. String finalSectionType = sectionType;
  171. binding.noneSend.setOnClickListener(view1 -> {
  172. if (ButtonUtils.isFastClick()) {
  173. return;
  174. }
  175. // Bundle bundle = new Bundle();
  176. // bundle.putString("exception", "exception_page");
  177. // finalActivity.getBundleMap().put(SendInfoConfirmFragment.class.getName(), bundle);
  178. AlertDialog alertDialog = new AlertDialog.Builder(finalActivity)
  179. .setTitle("e站通")
  180. .setMessage("点击未投递会取消本次投递!并且取消投递后的订单状态为预下单!确认取消吗?")
  181. .setPositiveButton("确认", (dialog, which) -> {
  182. // 是, 取消本次投递
  183. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  184. confirmOrderVo.setOrderSn(finalOrderSn);
  185. confirmOrderVo.setClientId(BaseUtils.getClientId());
  186. confirmOrderVo.setDecision(DecisionConstant.CANCEL_11);
  187. confirmOrderVo.setLockerType(finalSectionType);
  188. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  189. @Override
  190. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  191. Log.e(TAG, "onFailure: 取消投递错误! 网络错误! ", e);
  192. LoggingUtils.sendErrorLog("业务异常: 取消投递错误! 网络错误! ", e);
  193. }
  194. @Override
  195. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  196. getActivity().runOnUiThread(() -> {
  197. try {
  198. if (null != timer) {
  199. timer.cancel();
  200. }
  201. NavUtils.navigate(SendInfoConfirmFragment.this, R.id.action_sendInfoConfirmFragment_to_sendSuccessFragment);
  202. } catch (Exception e) {
  203. }
  204. });
  205. }
  206. });
  207. }).setNegativeButton("取消", (dialog, which) -> {
  208. // 否
  209. dialog.dismiss();
  210. }).create();
  211. alertDialog.show();
  212. });
  213. binding.returnMainBtn.setOnClickListener(view1 -> {
  214. if (ButtonUtils.isFastClick()) {
  215. return;
  216. }
  217. AlertDialog alertDialog = new AlertDialog.Builder(finalActivity)
  218. .setTitle("e站通")
  219. .setMessage("返回主页会取消本次投递!并且本次投递的订单状态为预下单!确认取消吗?")
  220. .setPositiveButton("确认", ((dialog, which) -> {
  221. if (null != timer) {
  222. timer.cancel();
  223. }
  224. boolean isException = false;
  225. if (LocalStorage.getInstance().getSession().getException()) {
  226. isException = true;
  227. }
  228. LocalStorage.getInstance().cleanSession(((MainActivity) getActivity()), isException);
  229. NavUtils.navigate(this, R.id.action_sendInfoConfirmFragment_to_mainFragment);
  230. })).setNegativeButton("取消", ((dialog, which) -> {
  231. dialog.dismiss();
  232. })).create();
  233. alertDialog.show();
  234. });
  235. }
  236. @Override
  237. public void onDestroy() {
  238. if (timer != null) {
  239. timer.cancel();
  240. timer = null;
  241. }
  242. super.onDestroy();
  243. binding = null;
  244. Log.i(TAG, TAG + "被销毁。。。");
  245. }
  246. }