1
0

ChooseCabinetFragment.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 java.util.Objects;
  47. import okhttp3.Call;
  48. import okhttp3.Callback;
  49. import okhttp3.Response;
  50. /**
  51. * 选择柜子页面
  52. */
  53. public class ChooseCabinetFragment extends Fragment {
  54. private static final String TAG = ChooseCabinetFragment.class.getName();
  55. private FragmentChooseCabinetBinding binding;
  56. private CountDownTimer timer;
  57. private long start;
  58. @SuppressLint("ResourceAsColor")
  59. private void isDisable(Integer available, Button button, TextView textView){
  60. // textView.setBackgroundColor(android.graphics.Color.parseColor("#44CE3B"));
  61. button.setEnabled(available > 0);
  62. }
  63. @Nullable
  64. @org.jetbrains.annotations.Nullable
  65. @Override
  66. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  67. start = System.currentTimeMillis();
  68. binding = FragmentChooseCabinetBinding.inflate(inflater, container, false);
  69. return binding.getRoot();
  70. }
  71. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  72. long end = System.currentTimeMillis();
  73. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  74. // ColorConstant.setButtonColor(binding.returnBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
  75. try {
  76. MainActivity activity = (MainActivity) getActivity();
  77. timer = TimeOutUtils.timeout(activity, ChooseCabinetFragment.this, binding.timeout, 90);
  78. timer.start();
  79. } catch (Exception e) {
  80. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  81. LoggingUtils.sendErrorLog("业务异常: 选择柜子类型页面倒计时出现异常! ", e);
  82. }
  83. MainActivity activityTmp = (MainActivity) getActivity();
  84. Bundle mainBundle = activityTmp.getBundleMap().get(MainFragment.class.getName());
  85. String pageType = mainBundle.getString("pageType");
  86. // 用户寄送跳转不需要进行登录
  87. // 判断是否是直接投递的 how_to_send 如果不是直接投递跳转过来的 那就隐藏投递流程
  88. if (!"expressDelivery".equals(pageType)){
  89. view.findViewById(R.id.how_to_send).setVisibility(View.GONE);
  90. }else{
  91. view.findViewById(R.id.how_to_send).setVisibility(View.VISIBLE);
  92. }
  93. // 获取柜子信息
  94. ICSPClient.getCabinetInfo(LocalStorage.getInstance().getSession().getToken(), new Callback() {
  95. @Override
  96. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  97. // TODO 获取柜子信息失败处理
  98. Looper.prepare();
  99. ToastUtils.make(getContext(), "获取柜子信息失败!网络异常!");
  100. Looper.loop();
  101. Log.e(TAG, "onFailure: 获取柜子信息调用失败! call: " + call.timeout().toString(), e);
  102. LoggingUtils.sendErrorLog("业务异常: 获取柜子信息调用失败! ", e);
  103. }
  104. @Override
  105. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  106. FragmentActivity activity = getActivity();
  107. if(activity != null) {
  108. activity.runOnUiThread(() -> {
  109. String cabinetInfo = ICSPClient.isSuccessfulAndParseResponse(response);
  110. if (!StringUtils.isNullOrEmpty(cabinetInfo)) {
  111. try {
  112. // 需要先请求ICSP获取柜子个数, 某类型无空柜子则按钮置灰
  113. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  114. ResponseData<List<CabinetInfoVo>> responseData = objectMapper.readValue(cabinetInfo, new TypeReference<ResponseData<List<CabinetInfoVo>>>() {
  115. });
  116. if (responseData.getCode().equals(ICSPConstant.OK)) {
  117. List<CabinetInfoVo> cabinetInfoVos = responseData.getData();
  118. cabinetInfoVos.forEach(cabinetInfoVo -> {
  119. if (!StringUtils.isNullOrEmpty(cabinetInfoVo.getType())) {
  120. switch (cabinetInfoVo.getType()) {
  121. case "mini":
  122. binding.tinyCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  123. isDisable(cabinetInfoVo.getAvailable(), binding.tinyCabinetBtn, binding.tinyCabinetBg);
  124. break;
  125. case "small":
  126. binding.smallCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  127. isDisable(cabinetInfoVo.getAvailable(), binding.smallCabinetBtn, binding.smallCabinetBg);
  128. break;
  129. case "medium":
  130. binding.mediumCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  131. isDisable(cabinetInfoVo.getAvailable(), binding.mediumCabinetBtn, binding.mediumCabinetBg);
  132. break;
  133. case "large":
  134. binding.bigCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  135. isDisable(cabinetInfoVo.getAvailable(), binding.bigCabinetBtn, binding.bigCabinetBg);
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. });
  142. } else {
  143. ToastUtils.make(getContext(), responseData.getMsg());
  144. Log.w(TAG, "onResponse: " + responseData.getMsg());
  145. }
  146. } catch (IOException e) {
  147. Log.e(TAG, "onCreateView: 获取柜子剩余信息响应信息解析失败!", e);
  148. LoggingUtils.sendErrorLog("业务异常: 获取柜子剩余信息响应信息解析失败! ", e);
  149. } catch (RuntimeException e) {
  150. Log.e(TAG, "onResponse: ", e);
  151. LoggingUtils.sendErrorLog("业务异常: 获取柜子剩余信息未知错误! ", e);
  152. }
  153. } else {
  154. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  155. }
  156. });
  157. }
  158. }
  159. });
  160. Bundle bundle = new Bundle();
  161. // 大柜子
  162. binding.bigCabinetBtn.setOnClickListener(v -> {
  163. if (ButtonUtils.isFastClick()) {
  164. return;
  165. }
  166. preparedOrder(CabinetTypeConstant.LARGE, bundle, v);
  167. });
  168. // 中柜子
  169. binding.mediumCabinetBtn.setOnClickListener(v -> {
  170. if (ButtonUtils.isFastClick()) {
  171. return;
  172. }
  173. preparedOrder(CabinetTypeConstant.MEDIUM, bundle, v);
  174. });
  175. // 小柜子
  176. binding.smallCabinetBtn.setOnClickListener(v -> {
  177. if (ButtonUtils.isFastClick()) {
  178. return;
  179. }
  180. preparedOrder(CabinetTypeConstant.SMALL, bundle, v);
  181. });
  182. // 微小柜子
  183. binding.tinyCabinetBtn.setOnClickListener(v -> {
  184. if (ButtonUtils.isFastClick()) {
  185. return;
  186. }
  187. preparedOrder(CabinetTypeConstant.MINI, bundle, v);
  188. });
  189. binding.returnBtn.setOnClickListener(v -> {
  190. if (ButtonUtils.isFastClick()) {
  191. return;
  192. }
  193. MainActivity activity = (MainActivity) getActivity();
  194. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  195. Bundle MainFragmentBundle = activity.getBundleMap().get(MainFragment.class.getName());
  196. if (Objects.nonNull(MainFragmentBundle)){
  197. // 我要寄快递页面跳转
  198. if("expressDelivery".equals(MainFragmentBundle.getString("pageType"))){
  199. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_mainFragment);
  200. }
  201. }
  202. Bundle exceptionBundle = activity.getBundleMap().get(ExceptionFragment.class.getName());
  203. if (null != timer) {
  204. timer.cancel();
  205. }
  206. if (null != exceptionBundle) {
  207. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_exceptionFragment);
  208. } else {
  209. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_sendMainFragment);
  210. }
  211. });
  212. }
  213. public void preparedOrder(String sectionType, Bundle bundle, View view){
  214. bundle.putString("section_type", sectionType);
  215. Bundle exceptionBundle = null;
  216. try {
  217. MainActivity activity = (MainActivity) getActivity();
  218. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  219. exceptionBundle = activity.getBundleMap().get(ExceptionFragment.class.getName());
  220. // 不为空说明是异常页跳过来的, 更新柜子类型, 如果有订单号就更新预下单, 无则走预下单逻辑
  221. if (null != exceptionBundle) {
  222. Bundle inputInfoBundle = activity.getBundleMap().get(InputInfoKeyBoardFragment.class.getName());
  223. Bundle expressDeliveryBundle = activity.getBundleMap().get(ExpressDeliveryFragment.class.getName());
  224. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  225. if (inputInfoBundle == null && expressDeliveryBundle == null) {
  226. ToastUtils.make(getContext(), "您还没有投递!");
  227. } else {
  228. String orderResponse = null;
  229. if(inputInfoBundle != null ){
  230. orderResponse = inputInfoBundle.getString("preparedOrderResponse");
  231. }
  232. String expressDeliverOrderResponse = null;
  233. if(expressDeliveryBundle != null ){
  234. expressDeliverOrderResponse = expressDeliveryBundle.getString("preparedOrderResponse");
  235. }
  236. if ((null != orderResponse && orderResponse.trim().length() > 0 )
  237. || (null != expressDeliverOrderResponse && expressDeliverOrderResponse.trim().length() >0)) {
  238. String str = "";
  239. if(null != orderResponse && orderResponse.trim().length() > 0 ){
  240. str = orderResponse;
  241. }else if(null != expressDeliverOrderResponse && expressDeliverOrderResponse.trim().length() > 0 ){
  242. str = expressDeliverOrderResponse;
  243. }
  244. PreparedOrderResponseVo preparedOrderResponse = objectMapper.readValue(str, PreparedOrderResponseVo.class);
  245. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  246. confirmOrderVo.setLockerType(sectionType);
  247. confirmOrderVo.setDecision(DecisionConstant.REOPEN_01);
  248. confirmOrderVo.setClientId(BaseUtils.getClientId());
  249. confirmOrderVo.setOrderSn(preparedOrderResponse.getOrderSn());
  250. String pageType = activity.getBundleMap().get(MainFragment.class.getName()).getString("pageType");
  251. if ("expressDelivery".equals(pageType)){
  252. ICSPClient.confirmOrder(confirmOrderVo, new Callback() {
  253. @Override
  254. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  255. //
  256. Looper.prepare();
  257. ToastUtils.make(getContext(), "换柜子失败!网络异常!");
  258. Looper.loop();
  259. Log.e(TAG, "onFailure: 大小不合适, 换柜子更新预下单失败! ", e);
  260. LoggingUtils.sendErrorLog("业务异常: 大小不合适, 换柜子更新预下单失败! ", e);
  261. }
  262. @Override
  263. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  264. // 换柜子成功, 跳转到确认投递页面
  265. ((MainActivity) getActivity()).runOnUiThread(() -> {
  266. try {
  267. String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
  268. if (!StringUtils.isNullOrEmpty(responseStr)) {
  269. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  270. ResponseData<PreparedOrderResponseVo> responseData = objectMapper.readValue(responseStr, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {});
  271. // 响应信息
  272. if (responseData.getCode().equals(ICSPConstant.OK)) {
  273. PreparedOrderResponseVo data = responseData.getData();
  274. if (null != data) {
  275. data.setOrderSn(preparedOrderResponse.getOrderSn());
  276. data.setCollectorPhone(preparedOrderResponse.getCollectorPhone());
  277. String mailNo = expressDeliveryBundle.getString("takeNo");
  278. bundle.putString("preparedOrderResponse", objectMapper.writeValueAsString(data));
  279. bundle.putString("takeNo", mailNo);
  280. bundle.putString("section_type", data.getLockerName());
  281. // 跳转到确认投递
  282. try {
  283. MainActivity activity = (MainActivity) getActivity();
  284. activity.getBundleMap().put(InputInfoKeyBoardFragment.class.getName(), bundle);
  285. activity.getBundleMap().remove(ExceptionFragment.class.getName());
  286. if (null != timer) {
  287. timer.cancel();
  288. }
  289. NavUtils.navigate(ChooseCabinetFragment.this, R.id.action_chooseCabinetFragment_to_sendInfoConfirmFragment);
  290. } catch (RuntimeException e) {
  291. Log.e(TAG, "onResponse: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  292. LoggingUtils.sendErrorLog("业务异常: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  293. }
  294. }
  295. } else {
  296. ToastUtils.make(getContext(), responseData.getMsg());
  297. RuntimeException exception = new RuntimeException("系统异常");
  298. Log.e(TAG, "onResponse: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), exception);
  299. LoggingUtils.sendErrorLog("业务异常: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), exception);
  300. }
  301. } else {
  302. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  303. }
  304. } catch (JsonProcessingException e) {
  305. Log.e(TAG, "onResponse: 预下单转换成JSON出错! ", e);
  306. LoggingUtils.sendErrorLog("业务异常: 预下单转换成JSON出错! ", e);
  307. } catch (RuntimeException e) {
  308. Log.e(TAG, "onResponse: 换柜子错误! ", e);
  309. LoggingUtils.sendErrorLog("业务异常: 换柜子错误! ", e);
  310. }
  311. });
  312. }
  313. });
  314. }else {
  315. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  316. @Override
  317. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  318. //
  319. Looper.prepare();
  320. ToastUtils.make(getContext(), "换柜子失败!网络异常!");
  321. Looper.loop();
  322. Log.e(TAG, "onFailure: 大小不合适, 换柜子更新预下单失败! ", e);
  323. LoggingUtils.sendErrorLog("业务异常: 大小不合适, 换柜子更新预下单失败! ", e);
  324. }
  325. @Override
  326. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  327. // 换柜子成功, 跳转到确认投递页面
  328. ((MainActivity) getActivity()).runOnUiThread(() -> {
  329. try {
  330. String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
  331. if (!StringUtils.isNullOrEmpty(responseStr)) {
  332. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  333. ResponseData<PreparedOrderResponseVo> responseData = objectMapper.readValue(responseStr, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {});
  334. // 响应信息
  335. if (responseData.getCode().equals(ICSPConstant.OK)) {
  336. PreparedOrderResponseVo data = responseData.getData();
  337. if (null != data) {
  338. data.setOrderSn(preparedOrderResponse.getOrderSn());
  339. data.setCollectorPhone(preparedOrderResponse.getCollectorPhone());
  340. String mailNo = inputInfoBundle.getString("takeNo");
  341. bundle.putString("preparedOrderResponse", objectMapper.writeValueAsString(data));
  342. bundle.putString("takeNo", mailNo);
  343. bundle.putString("section_type", data.getLockerName());
  344. // 跳转到确认投递
  345. try {
  346. MainActivity activity = (MainActivity) getActivity();
  347. activity.getBundleMap().put(InputInfoKeyBoardFragment.class.getName(), bundle);
  348. activity.getBundleMap().remove(ExceptionFragment.class.getName());
  349. if (null != timer) {
  350. timer.cancel();
  351. }
  352. NavUtils.navigate(ChooseCabinetFragment.this, R.id.action_chooseCabinetFragment_to_sendInfoConfirmFragment);
  353. } catch (RuntimeException e) {
  354. Log.e(TAG, "onResponse: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  355. LoggingUtils.sendErrorLog("业务异常: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  356. }
  357. }
  358. } else {
  359. ToastUtils.make(getContext(), responseData.getMsg());
  360. RuntimeException exception = new RuntimeException("系统异常");
  361. Log.e(TAG, "onResponse: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), exception);
  362. LoggingUtils.sendErrorLog("业务异常: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), exception);
  363. }
  364. } else {
  365. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  366. }
  367. } catch (JsonProcessingException e) {
  368. Log.e(TAG, "onResponse: 预下单转换成JSON出错! ", e);
  369. LoggingUtils.sendErrorLog("业务异常: 预下单转换成JSON出错! ", e);
  370. } catch (RuntimeException e) {
  371. Log.e(TAG, "onResponse: 换柜子错误! ", e);
  372. LoggingUtils.sendErrorLog("业务异常: 换柜子错误! ", e);
  373. }
  374. });
  375. }
  376. });
  377. }
  378. }
  379. }
  380. } else {
  381. // 为空走正常投递流程
  382. if (null != timer) {
  383. timer.cancel();
  384. }
  385. String pageType = activity.getBundleMap().get(MainFragment.class.getName()).getString("pageType");
  386. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  387. if(!StringUtils.isNullOrEmpty(pageType) && "expressDelivery".equals(pageType)){
  388. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_expressDeliveryFragment, bundle);
  389. }else {
  390. String isPay = activity.getBundleMap().get(SendKeyBoardFragment.class.getName()).getString("isPay");
  391. if ("0".equals(isPay)){
  392. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_paymentFragment);
  393. }else {
  394. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_InputInfoKeyBoardFragment, bundle);
  395. }
  396. }
  397. }
  398. } catch (RuntimeException | JsonProcessingException e) {
  399. Log.e(TAG, "onViewCreated: ChooseCabinetFragment向InputInfoFragment页面传值错误! ", e);
  400. LoggingUtils.sendErrorLog("业务异常: ChooseCabinetFragment向InputInfoFragment页面传值错误! ", e);
  401. }
  402. }
  403. @Override
  404. public void onDestroy() {
  405. if (timer != null) {
  406. timer.cancel();
  407. timer = null;
  408. }
  409. super.onDestroy();
  410. binding = null;
  411. Log.i(TAG, TAG + "被销毁。。。");
  412. }
  413. }