ChooseCabinetFragment.java 17 KB

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