1
0

ChooseCabinetFragment.java 18 KB

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