ChooseCabinetFragment.java 17 KB

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