ChooseCabinetFragment.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. ViewGroup.LayoutParams layoutParams = view.findViewById(R.id.background_layout).getLayoutParams();
  91. layoutParams.height = 1500;
  92. view.findViewById(R.id.background_layout).setLayoutParams(layoutParams);
  93. }else{
  94. view.findViewById(R.id.how_to_send).setVisibility(View.VISIBLE);
  95. }
  96. // 获取柜子信息
  97. ICSPClient.getCabinetInfo(LocalStorage.getInstance().getSession().getToken(), new Callback() {
  98. @Override
  99. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  100. // TODO 获取柜子信息失败处理
  101. Looper.prepare();
  102. ToastUtils.make(getContext(), "获取柜子信息失败!网络异常!");
  103. Looper.loop();
  104. Log.e(TAG, "onFailure: 获取柜子信息调用失败! call: " + call.timeout().toString(), e);
  105. LoggingUtils.sendErrorLog("业务异常: 获取柜子信息调用失败! ", e);
  106. }
  107. @Override
  108. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  109. FragmentActivity activity = getActivity();
  110. if(activity != null) {
  111. activity.runOnUiThread(() -> {
  112. String cabinetInfo = ICSPClient.isSuccessfulAndParseResponse(response);
  113. if (!StringUtils.isNullOrEmpty(cabinetInfo)) {
  114. try {
  115. // 需要先请求ICSP获取柜子个数, 某类型无空柜子则按钮置灰
  116. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  117. ResponseData<List<CabinetInfoVo>> responseData = objectMapper.readValue(cabinetInfo, new TypeReference<ResponseData<List<CabinetInfoVo>>>() {
  118. });
  119. if (responseData.getCode().equals(ICSPConstant.OK)) {
  120. List<CabinetInfoVo> cabinetInfoVos = responseData.getData();
  121. cabinetInfoVos.forEach(cabinetInfoVo -> {
  122. if (!StringUtils.isNullOrEmpty(cabinetInfoVo.getType())) {
  123. switch (cabinetInfoVo.getType()) {
  124. case "mini":
  125. binding.tinyCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  126. isDisable(cabinetInfoVo.getAvailable(), binding.tinyCabinetBtn, binding.tinyCabinetBg);
  127. break;
  128. case "small":
  129. binding.smallCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  130. isDisable(cabinetInfoVo.getAvailable(), binding.smallCabinetBtn, binding.smallCabinetBg);
  131. break;
  132. case "medium":
  133. binding.mediumCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  134. isDisable(cabinetInfoVo.getAvailable(), binding.mediumCabinetBtn, binding.mediumCabinetBg);
  135. break;
  136. case "large":
  137. binding.bigCabinet.append("可用" + cabinetInfoVo.getAvailable() + "个");
  138. isDisable(cabinetInfoVo.getAvailable(), binding.bigCabinetBtn, binding.bigCabinetBg);
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. });
  145. } else {
  146. ToastUtils.make(getContext(), responseData.getMsg());
  147. Log.w(TAG, "onResponse: " + responseData.getMsg());
  148. }
  149. } catch (IOException e) {
  150. Log.e(TAG, "onCreateView: 获取柜子剩余信息响应信息解析失败!", e);
  151. LoggingUtils.sendErrorLog("业务异常: 获取柜子剩余信息响应信息解析失败! ", e);
  152. } catch (RuntimeException e) {
  153. Log.e(TAG, "onResponse: ", e);
  154. LoggingUtils.sendErrorLog("业务异常: 获取柜子剩余信息未知错误! ", e);
  155. }
  156. } else {
  157. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  158. }
  159. });
  160. }
  161. }
  162. });
  163. Bundle bundle = new Bundle();
  164. // 大柜子
  165. binding.bigCabinetBtn.setOnClickListener(v -> {
  166. if (ButtonUtils.isFastClick()) {
  167. return;
  168. }
  169. preparedOrder(CabinetTypeConstant.LARGE, bundle, v);
  170. });
  171. // 中柜子
  172. binding.mediumCabinetBtn.setOnClickListener(v -> {
  173. if (ButtonUtils.isFastClick()) {
  174. return;
  175. }
  176. preparedOrder(CabinetTypeConstant.MEDIUM, bundle, v);
  177. });
  178. // 小柜子
  179. binding.smallCabinetBtn.setOnClickListener(v -> {
  180. if (ButtonUtils.isFastClick()) {
  181. return;
  182. }
  183. preparedOrder(CabinetTypeConstant.SMALL, bundle, v);
  184. });
  185. // 微小柜子
  186. binding.tinyCabinetBtn.setOnClickListener(v -> {
  187. if (ButtonUtils.isFastClick()) {
  188. return;
  189. }
  190. preparedOrder(CabinetTypeConstant.MINI, bundle, v);
  191. });
  192. binding.returnBtn.setOnClickListener(v -> {
  193. if (ButtonUtils.isFastClick()) {
  194. return;
  195. }
  196. MainActivity activity = (MainActivity) getActivity();
  197. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  198. Bundle MainFragmentBundle = activity.getBundleMap().get(MainFragment.class.getName());
  199. if (Objects.nonNull(MainFragmentBundle)){
  200. // 我要寄快递页面跳转
  201. if("expressDelivery".equals(MainFragmentBundle.getString("pageType"))){
  202. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_mainFragment);
  203. }
  204. }
  205. Bundle exceptionBundle = activity.getBundleMap().get(ExceptionFragment.class.getName());
  206. if (null != timer) {
  207. timer.cancel();
  208. }
  209. if (null != exceptionBundle) {
  210. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_exceptionFragment);
  211. } else {
  212. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_sendMainFragment);
  213. }
  214. });
  215. }
  216. public void preparedOrder(String sectionType, Bundle bundle, View view){
  217. bundle.putString("section_type", sectionType);
  218. Bundle exceptionBundle = null;
  219. try {
  220. MainActivity activity = (MainActivity) getActivity();
  221. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  222. exceptionBundle = activity.getBundleMap().get(ExceptionFragment.class.getName());
  223. // 不为空说明是异常页跳过来的, 更新柜子类型, 如果有订单号就更新预下单, 无则走预下单逻辑
  224. if (null != exceptionBundle) {
  225. Bundle inputInfoBundle = activity.getBundleMap().get(InputInfoKeyBoardFragment.class.getName());
  226. Bundle expressDeliveryBundle = activity.getBundleMap().get(ExpressDeliveryFragment.class.getName());
  227. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  228. if (inputInfoBundle == null && expressDeliveryBundle == null) {
  229. ToastUtils.make(getContext(), "您还没有投递!");
  230. } else {
  231. String orderResponse = null;
  232. if(inputInfoBundle != null ){
  233. orderResponse = inputInfoBundle.getString("preparedOrderResponse");
  234. }
  235. String expressDeliverOrderResponse = null;
  236. if(expressDeliveryBundle != null ){
  237. expressDeliverOrderResponse = expressDeliveryBundle.getString("preparedOrderResponse");
  238. }
  239. if ((null != orderResponse && orderResponse.trim().length() > 0 )
  240. || (null != expressDeliverOrderResponse && expressDeliverOrderResponse.trim().length() >0)) {
  241. String str = "";
  242. if(null != orderResponse && orderResponse.trim().length() > 0 ){
  243. str = orderResponse;
  244. }else if(null != expressDeliverOrderResponse && expressDeliverOrderResponse.trim().length() > 0 ){
  245. str = expressDeliverOrderResponse;
  246. }
  247. PreparedOrderResponseVo preparedOrderResponse = objectMapper.readValue(str, PreparedOrderResponseVo.class);
  248. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  249. confirmOrderVo.setLockerType(sectionType);
  250. confirmOrderVo.setDecision(DecisionConstant.REOPEN_01);
  251. confirmOrderVo.setClientId(BaseUtils.getClientId());
  252. confirmOrderVo.setOrderSn(preparedOrderResponse.getOrderSn());
  253. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  254. @Override
  255. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  256. //
  257. Looper.prepare();
  258. ToastUtils.make(getContext(), "换柜子失败!网络异常!");
  259. Looper.loop();
  260. Log.e(TAG, "onFailure: 大小不合适, 换柜子更新预下单失败! ", e);
  261. LoggingUtils.sendErrorLog("业务异常: 大小不合适, 换柜子更新预下单失败! ", e);
  262. }
  263. @Override
  264. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  265. // 换柜子成功, 跳转到确认投递页面
  266. ((MainActivity) getActivity()).runOnUiThread(() -> {
  267. try {
  268. String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
  269. if (!StringUtils.isNullOrEmpty(responseStr)) {
  270. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  271. ResponseData<PreparedOrderResponseVo> responseData = objectMapper.readValue(responseStr, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {});
  272. // 响应信息
  273. if (responseData.getCode().equals(ICSPConstant.OK)) {
  274. PreparedOrderResponseVo data = responseData.getData();
  275. if (null != data) {
  276. data.setOrderSn(preparedOrderResponse.getOrderSn());
  277. data.setCollectorPhone(preparedOrderResponse.getCollectorPhone());
  278. String mailNo = inputInfoBundle.getString("takeNo");
  279. bundle.putString("preparedOrderResponse", objectMapper.writeValueAsString(data));
  280. bundle.putString("takeNo", mailNo);
  281. bundle.putString("section_type", data.getLockerName());
  282. // 跳转到确认投递
  283. try {
  284. MainActivity activity = (MainActivity) getActivity();
  285. activity.getBundleMap().put(InputInfoKeyBoardFragment.class.getName(), bundle);
  286. activity.getBundleMap().remove(ExceptionFragment.class.getName());
  287. if (null != timer) {
  288. timer.cancel();
  289. }
  290. NavUtils.navigate(ChooseCabinetFragment.this, R.id.action_chooseCabinetFragment_to_sendInfoConfirmFragment);
  291. } catch (RuntimeException e) {
  292. Log.e(TAG, "onResponse: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  293. LoggingUtils.sendErrorLog("业务异常: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  294. }
  295. }
  296. } else {
  297. ToastUtils.make(getContext(), responseData.getMsg());
  298. RuntimeException exception = new RuntimeException("系统异常");
  299. Log.e(TAG, "onResponse: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), exception);
  300. LoggingUtils.sendErrorLog("业务异常: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), exception);
  301. }
  302. } else {
  303. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  304. }
  305. } catch (JsonProcessingException e) {
  306. Log.e(TAG, "onResponse: 预下单转换成JSON出错! ", e);
  307. LoggingUtils.sendErrorLog("业务异常: 预下单转换成JSON出错! ", e);
  308. } catch (RuntimeException e) {
  309. Log.e(TAG, "onResponse: 换柜子错误! ", e);
  310. LoggingUtils.sendErrorLog("业务异常: 换柜子错误! ", e);
  311. }
  312. });
  313. }
  314. });
  315. }
  316. }
  317. } else {
  318. // 为空走正常投递流程
  319. if (null != timer) {
  320. timer.cancel();
  321. }
  322. String pageType = activity.getBundleMap().get(MainFragment.class.getName()).getString("pageType");
  323. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  324. if(!StringUtils.isNullOrEmpty(pageType) && "expressDelivery".equals(pageType)){
  325. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_expressDeliveryFragment, bundle);
  326. }else {
  327. String isPay = activity.getBundleMap().get(SendKeyBoardFragment.class.getName()).getString("isPay");
  328. if ("0".equals(isPay)){
  329. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_paymentFragment);
  330. }else {
  331. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_InputInfoKeyBoardFragment, bundle);
  332. }
  333. }
  334. }
  335. } catch (RuntimeException | JsonProcessingException e) {
  336. Log.e(TAG, "onViewCreated: ChooseCabinetFragment向InputInfoFragment页面传值错误! ", e);
  337. LoggingUtils.sendErrorLog("业务异常: ChooseCabinetFragment向InputInfoFragment页面传值错误! ", e);
  338. }
  339. }
  340. @Override
  341. public void onDestroy() {
  342. if (timer != null) {
  343. timer.cancel();
  344. timer = null;
  345. }
  346. super.onDestroy();
  347. binding = null;
  348. Log.i(TAG, TAG + "被销毁。。。");
  349. }
  350. }