1
0

ChooseCabinetFragment.java 18 KB

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