ChooseCabinetFragment.java 15 KB

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