ChooseCabinetFragment.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.emato.ich.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import androidx.annotation.NonNull;
  11. import androidx.annotation.Nullable;
  12. import androidx.fragment.app.Fragment;
  13. import androidx.fragment.app.FragmentActivity;
  14. import androidx.navigation.fragment.NavHostFragment;
  15. import com.emato.ich.MainActivity;
  16. import com.emato.ich.R;
  17. import com.emato.ich.api.ICSPClient;
  18. import com.emato.ich.api.ICSPResponseCodeEnum;
  19. import com.emato.ich.data.CabinetTypeEnum;
  20. import com.emato.ich.data.DecisionEnum;
  21. import com.emato.ich.databinding.FragmentChooseCabinetBinding;
  22. import com.emato.ich.entity.vo.ConfirmOrderVo;
  23. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  24. import com.emato.ich.entity.vo.ResponseData;
  25. import com.emato.ich.entity.vo.CabinetInfoVo;
  26. import com.emato.ich.local.LocalStorage;
  27. import com.emato.ich.utils.BaseUtils;
  28. import com.emato.ich.utils.JacksonUtils;
  29. import com.emato.ich.utils.StringUtils;
  30. import com.emato.ich.utils.TimeOutUtils;
  31. import com.fasterxml.jackson.core.JsonProcessingException;
  32. import com.fasterxml.jackson.core.type.TypeReference;
  33. import com.fasterxml.jackson.databind.ObjectMapper;
  34. import com.google.android.material.snackbar.Snackbar;
  35. import org.jetbrains.annotations.NotNull;
  36. import java.io.IOException;
  37. import java.util.List;
  38. import okhttp3.Call;
  39. import okhttp3.Callback;
  40. import okhttp3.Response;
  41. public class ChooseCabinetFragment extends Fragment {
  42. private static final String TAG = ChooseCabinetFragment.class.getName();
  43. private FragmentChooseCabinetBinding binding;
  44. private CountDownTimer timer;
  45. private void isDisable(Integer available, Button button){
  46. if (available <= 0) {
  47. button.setEnabled(false);
  48. } else {
  49. button.setBackgroundColor(500156);
  50. }
  51. }
  52. @Nullable
  53. @org.jetbrains.annotations.Nullable
  54. @Override
  55. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  56. binding = FragmentChooseCabinetBinding.inflate(inflater, container, false);
  57. return binding.getRoot();
  58. }
  59. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  60. try {
  61. MainActivity activity = (MainActivity) getActivity();
  62. timer = TimeOutUtils.timeout(activity, ChooseCabinetFragment.this, binding.timeout, 60);
  63. timer.start();
  64. } catch (Exception e) {
  65. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  66. }
  67. // 获取柜子信息
  68. ICSPClient.getCabinetInfo(LocalStorage.getInstance().getSession().getToken(), new Callback() {
  69. @Override
  70. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  71. // TODO 获取柜子信息失败处理
  72. Snackbar.make(view, "获取柜子信息失败!网络异常!", Snackbar.LENGTH_LONG).show();
  73. Log.e(TAG, "onFailure: 获取柜子信息调用失败! call: " + call.timeout().toString(), e);
  74. }
  75. @Override
  76. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  77. FragmentActivity activity = getActivity();
  78. if(activity != null) {
  79. activity.runOnUiThread(() -> {
  80. String cabinetInfo = ICSPClient.isSuccessfulAndParseResponse(response);
  81. if (!StringUtils.isNullOrEmpty(cabinetInfo)) {
  82. try {
  83. // 需要先请求ICSP获取柜子个数, 某类型无空柜子则按钮置灰
  84. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  85. ResponseData<List<CabinetInfoVo>> responseData = objectMapper.readValue(cabinetInfo, new TypeReference<ResponseData<List<CabinetInfoVo>>>() {
  86. });
  87. if (responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  88. List<CabinetInfoVo> cabinetInfoVos = responseData.getData();
  89. cabinetInfoVos.forEach(cabinetInfoVo -> {
  90. switch (cabinetInfoVo.getType()) {
  91. case "mini":
  92. binding.tinyCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  93. isDisable(cabinetInfoVo.getAvailable(), binding.tinyCabinetBtn);
  94. break;
  95. case "small":
  96. binding.smallCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  97. isDisable(cabinetInfoVo.getAvailable(), binding.smallCabinetBtn);
  98. break;
  99. case "medium":
  100. binding.mediumCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  101. isDisable(cabinetInfoVo.getAvailable(), binding.mediumCabinetBtn);
  102. break;
  103. case "large":
  104. binding.bigCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  105. isDisable(cabinetInfoVo.getAvailable(), binding.bigCabinetBtn);
  106. break;
  107. default:
  108. break;
  109. }
  110. });
  111. } else {
  112. ICSPResponseCodeEnum.responseHint(view, responseData);
  113. Log.w(TAG, "onResponse: " + responseData.getMsg());
  114. }
  115. } catch (IOException e) {
  116. Log.e(TAG, "onCreateView: 获取柜子剩余信息响应信息解析失败!", e);
  117. } catch (RuntimeException e) {
  118. Log.e(TAG, "onResponse: ", e);
  119. }
  120. } else {
  121. Snackbar.make(view, "获取柜子剩余信息出错!请重新进入此页面!", Snackbar.LENGTH_LONG).show();
  122. }
  123. });
  124. }
  125. }
  126. });
  127. Bundle bundle = new Bundle();
  128. // 大柜子
  129. binding.bigCabinetBtn.setOnClickListener(v -> preparedOrder(CabinetTypeEnum.LARGE.getType(), bundle, v));
  130. // 中柜子
  131. binding.mediumCabinetBtn.setOnClickListener(v -> preparedOrder(CabinetTypeEnum.MEDIUM.getType(), bundle, v));
  132. // 小柜子
  133. binding.smallCabinetBtn.setOnClickListener(v -> preparedOrder(CabinetTypeEnum.SMALL.getType(), bundle, v));
  134. // 微小柜子
  135. binding.tinyCabinetBtn.setOnClickListener(v -> preparedOrder(CabinetTypeEnum.MINI.getType(), bundle, v));
  136. binding.returnBtn.setOnClickListener(v -> {
  137. MainActivity activity = (MainActivity) getActivity();
  138. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  139. Bundle exceptionBundle = activity.getBundleMap().get(ExceptionFragment.class.getName());
  140. if (null != exceptionBundle) {
  141. NavHostFragment.findNavController(ChooseCabinetFragment.this).navigate(R.id.action_chooseCabinetFragment_to_exceptionFragment);
  142. } else {
  143. NavHostFragment.findNavController(ChooseCabinetFragment.this).navigate(R.id.action_chooseCabinetFragment_to_sendMainFragment);
  144. }
  145. });
  146. }
  147. public void preparedOrder(String sectionType, Bundle bundle, View view){
  148. bundle.putString("section_type", sectionType);
  149. Bundle exceptionBundle = null;
  150. try {
  151. MainActivity activity = (MainActivity) getActivity();
  152. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  153. exceptionBundle = activity.getBundleMap().get(ExceptionFragment.class.getName());
  154. // 不为空说明是异常页跳过来的, 更新柜子类型, 如果有订单号就更新预下单, 无则走预下单逻辑
  155. if (null != exceptionBundle) {
  156. Bundle inputInfoBundle = activity.getBundleMap().get(InputInfoFragment.class.getName());
  157. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  158. String orderResponse = inputInfoBundle.getString("preparedOrderResponse");
  159. if (null != orderResponse && orderResponse.trim().length() > 0) {
  160. PreparedOrderResponseVo preparedOrderResponse = objectMapper.readValue(orderResponse, PreparedOrderResponseVo.class);
  161. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  162. confirmOrderVo.setLockerType(sectionType);
  163. confirmOrderVo.setDecision(DecisionEnum.REOPEN_01.getDecision());
  164. confirmOrderVo.setClientId(BaseUtils.getClientId());
  165. confirmOrderVo.setOrderSn(preparedOrderResponse.getOrderSn());
  166. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  167. @Override
  168. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  169. //
  170. Snackbar.make(view, "换柜子失败!网络异常!", Snackbar.LENGTH_LONG).show();
  171. Log.e(TAG, "onFailure: 大小不合适, 换柜子更新预下单失败! ", e);
  172. }
  173. @Override
  174. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  175. // 换柜子成功, 跳转到确认投递页面
  176. ((MainActivity) getActivity()).runOnUiThread(() -> {
  177. try {
  178. String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
  179. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  180. ResponseData<PreparedOrderResponseVo> responseData = objectMapper.readValue(responseStr, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {});
  181. // 响应信息
  182. if (responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  183. PreparedOrderResponseVo data = responseData.getData();
  184. if (null != data) {
  185. data.setOrderSn(preparedOrderResponse.getOrderSn());
  186. data.setCollectorPhone(preparedOrderResponse.getCollectorPhone());
  187. String mailNo = inputInfoBundle.getString("takeNo");
  188. bundle.putString("preparedOrderResponse", objectMapper.writeValueAsString(data));
  189. bundle.putString("takeNo", mailNo);
  190. bundle.putString("section_type", data.getLockerName());
  191. // 跳转到确认投递
  192. try {
  193. MainActivity activity = (MainActivity) getActivity();
  194. activity.getBundleMap().put(InputInfoFragment.class.getName(), bundle);
  195. activity.getBundleMap().remove(ExceptionFragment.class.getName());
  196. NavHostFragment.findNavController(ChooseCabinetFragment.this)
  197. .navigate(R.id.action_chooseCabinetFragment_to_sendInfoConfirmFragment);
  198. } catch (RuntimeException e) {
  199. Log.e(TAG, "onResponse: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  200. }
  201. }
  202. } else {
  203. ICSPResponseCodeEnum.responseHint(view, responseData);
  204. Log.e(TAG, "onResponse: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), new RuntimeException("系统异常"));
  205. }
  206. } catch (JsonProcessingException e) {
  207. Log.e(TAG, "onResponse: 预下单转换成JSON出错! ", e);
  208. } catch (RuntimeException e) {
  209. Log.e(TAG, "onResponse: 换柜子错误! ", e);
  210. }
  211. });
  212. }
  213. });
  214. }
  215. } else {
  216. // 为空走正常投递流程
  217. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  218. NavHostFragment.findNavController(ChooseCabinetFragment.this)
  219. .navigate(R.id.action_chooseCabinetFragment_to_inputInfoFragment, bundle);
  220. }
  221. } catch (RuntimeException | JsonProcessingException e) {
  222. Log.e(TAG, "onViewCreated: ChooseCabinetFragment向InputInfoFragment页面传值错误! ", e);
  223. }
  224. }
  225. @Override
  226. public void onDestroy() {
  227. super.onDestroy();
  228. binding = null;
  229. timer.cancel();
  230. }
  231. }