ChooseCabinetFragment.java 15 KB

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