ChooseCabinetFragment.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  251. @Override
  252. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  253. //
  254. Looper.prepare();
  255. ToastUtils.make(getContext(), "换柜子失败!网络异常!");
  256. Looper.loop();
  257. Log.e(TAG, "onFailure: 大小不合适, 换柜子更新预下单失败! ", e);
  258. LoggingUtils.sendErrorLog("业务异常: 大小不合适, 换柜子更新预下单失败! ", e);
  259. }
  260. @Override
  261. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  262. // 换柜子成功, 跳转到确认投递页面
  263. ((MainActivity) getActivity()).runOnUiThread(() -> {
  264. try {
  265. String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
  266. if (!StringUtils.isNullOrEmpty(responseStr)) {
  267. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  268. ResponseData<PreparedOrderResponseVo> responseData = objectMapper.readValue(responseStr, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {});
  269. // 响应信息
  270. if (responseData.getCode().equals(ICSPConstant.OK)) {
  271. PreparedOrderResponseVo data = responseData.getData();
  272. if (null != data) {
  273. data.setOrderSn(preparedOrderResponse.getOrderSn());
  274. data.setCollectorPhone(preparedOrderResponse.getCollectorPhone());
  275. String mailNo = inputInfoBundle.getString("takeNo");
  276. bundle.putString("preparedOrderResponse", objectMapper.writeValueAsString(data));
  277. bundle.putString("takeNo", mailNo);
  278. bundle.putString("section_type", data.getLockerName());
  279. // 跳转到确认投递
  280. try {
  281. MainActivity activity = (MainActivity) getActivity();
  282. activity.getBundleMap().put(InputInfoKeyBoardFragment.class.getName(), bundle);
  283. activity.getBundleMap().remove(ExceptionFragment.class.getName());
  284. if (null != timer) {
  285. timer.cancel();
  286. }
  287. NavUtils.navigate(ChooseCabinetFragment.this, R.id.action_chooseCabinetFragment_to_sendInfoConfirmFragment);
  288. } catch (RuntimeException e) {
  289. Log.e(TAG, "onResponse: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  290. LoggingUtils.sendErrorLog("业务异常: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  291. }
  292. }
  293. } else {
  294. ToastUtils.make(getContext(), responseData.getMsg());
  295. RuntimeException exception = new RuntimeException("系统异常");
  296. Log.e(TAG, "onResponse: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), exception);
  297. LoggingUtils.sendErrorLog("业务异常: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg(), exception);
  298. }
  299. } else {
  300. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  301. }
  302. } catch (JsonProcessingException e) {
  303. Log.e(TAG, "onResponse: 预下单转换成JSON出错! ", e);
  304. LoggingUtils.sendErrorLog("业务异常: 预下单转换成JSON出错! ", e);
  305. } catch (RuntimeException e) {
  306. Log.e(TAG, "onResponse: 换柜子错误! ", e);
  307. LoggingUtils.sendErrorLog("业务异常: 换柜子错误! ", e);
  308. }
  309. });
  310. }
  311. });
  312. }
  313. }
  314. } else {
  315. // 为空走正常投递流程
  316. if (null != timer) {
  317. timer.cancel();
  318. }
  319. String pageType = activity.getBundleMap().get(MainFragment.class.getName()).getString("pageType");
  320. activity.getBundleMap().put(ChooseCabinetFragment.class.getName(), bundle);
  321. if(!StringUtils.isNullOrEmpty(pageType) && "expressDelivery".equals(pageType)){
  322. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_expressDeliveryFragment, bundle);
  323. }else {
  324. String isPay = activity.getBundleMap().get(SendKeyBoardFragment.class.getName()).getString("isPay");
  325. if ("0".equals(isPay)){
  326. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_paymentFragment);
  327. }else {
  328. NavUtils.navigate(this, R.id.action_chooseCabinetFragment_to_InputInfoKeyBoardFragment, bundle);
  329. }
  330. }
  331. }
  332. } catch (RuntimeException | JsonProcessingException e) {
  333. Log.e(TAG, "onViewCreated: ChooseCabinetFragment向InputInfoFragment页面传值错误! ", e);
  334. LoggingUtils.sendErrorLog("业务异常: ChooseCabinetFragment向InputInfoFragment页面传值错误! ", e);
  335. }
  336. }
  337. @Override
  338. public void onDestroy() {
  339. if (timer != null) {
  340. timer.cancel();
  341. timer = null;
  342. }
  343. super.onDestroy();
  344. binding = null;
  345. Log.i(TAG, TAG + "被销毁。。。");
  346. }
  347. }