SendInfoConfirmFragment.java 12 KB

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