SelectNetAcitivty.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package com.uas.hystorage.activity;
  2. import android.content.Intent;
  3. import android.text.TextUtils;
  4. import android.view.KeyEvent;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AdapterView;
  9. import android.widget.BaseAdapter;
  10. import android.widget.Button;
  11. import android.widget.CheckBox;
  12. import android.widget.ListView;
  13. import android.widget.TextView;
  14. import com.alibaba.fastjson.JSON;
  15. import com.alibaba.fastjson.JSONArray;
  16. import com.alibaba.fastjson.JSONObject;
  17. import com.android.volley.Request;
  18. import com.handmark.pulltorefresh.library.PullToRefreshBase;
  19. import com.handmark.pulltorefresh.library.PullToRefreshListView;
  20. import com.uas.hystorage.R;
  21. import com.uas.hystorage.global.GloableParams;
  22. import com.uas.hystorage.listener.MyEditorActionListener;
  23. import com.uas.hystorage.util.CommonUtil;
  24. import com.uas.hystorage.util.FastjsonUtil;
  25. import com.uas.hystorage.util.HttpCallback;
  26. import com.uas.hystorage.util.HttpParams;
  27. import com.uas.hystorage.util.ListUtils;
  28. import com.uas.hystorage.util.StringUtil;
  29. import com.uas.hystorage.util.VollyRequest;
  30. import com.uas.hystorage.view.ClearableEditText;
  31. import com.uas.hystorage.view.EmptyLayout;
  32. import java.util.ArrayList;
  33. import java.util.HashMap;
  34. import java.util.LinkedHashMap;
  35. import java.util.List;
  36. import java.util.Map;
  37. /**
  38. * Created by RaoMeng on 2019/5/22
  39. * Desc: dbfind选择页面
  40. */
  41. public class SelectNetAcitivty extends BaseActivity implements View.OnClickListener {
  42. private Button mBackButton;
  43. private TextView mTitleTextView, mSearchButton;
  44. private ClearableEditText mSearchEditText;
  45. private String lastKey;
  46. private int page = 1;
  47. //intent
  48. private String caller;//传进来的主表caller
  49. private String gCaller;//传进来的附表caller,当是明细表时候存在
  50. private String fieldKey;
  51. private String corekey;
  52. private String caption;
  53. private boolean dataForm;
  54. private PullToRefreshListView refreshListView;
  55. private EmptyLayout mEmptyLayout;
  56. private int groupId;
  57. private String mDefCondition;//默认的前提mDefCondition
  58. private boolean isForm;
  59. @Override
  60. protected void initViews() {
  61. setContentView(R.layout.acitivty_net_select);
  62. Intent intent = getIntent();
  63. if (intent != null) {
  64. fieldKey = intent.getStringExtra("fieldKey");
  65. caller = intent.getStringExtra("caller");
  66. gCaller = intent.getStringExtra("gCaller");
  67. dataForm = intent.getBooleanExtra("dataForm", false);
  68. corekey = intent.getStringExtra("corekey");
  69. groupId = intent.getIntExtra("groupId", 0);
  70. mDefCondition = intent.getStringExtra("mCondition");
  71. caption = intent.getStringExtra("caption");
  72. isForm = intent.getBooleanExtra("isForm", true);
  73. }
  74. mBackButton = findViewById(R.id.btn_actionbar_withback);
  75. mTitleTextView = findViewById(R.id.tv_actionbar_withback);
  76. mTitleTextView.setText(caption);
  77. mSearchButton = findViewById(R.id.common_search_btn);
  78. mSearchEditText = findViewById(R.id.common_search_et);
  79. mSearchEditText.setHint(caption);
  80. refreshListView = (PullToRefreshListView) findViewById(R.id.refreshListView);
  81. refreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  82. mEmptyLayout = new EmptyLayout(this, refreshListView.getRefreshableView());
  83. mEmptyLayout.setShowLoadingButton(false);
  84. mEmptyLayout.setShowEmptyButton(false);
  85. mEmptyLayout.setShowErrorButton(false);
  86. mEmptyLayout.setEmptyMessage("暂无数据");
  87. if (dataForm) {
  88. refreshListView.setMode(PullToRefreshBase.Mode.DISABLED);
  89. } else {
  90. refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
  91. @Override
  92. public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
  93. loadSearch(page = 1, lastKey);
  94. }
  95. @Override
  96. public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
  97. loadSearch(++page, lastKey);
  98. }
  99. });
  100. }
  101. }
  102. @Override
  103. protected void initEvents() {
  104. mBackButton.setOnClickListener(this);
  105. CommonUtil.setEditorActionListener(mSearchEditText, new MyEditorActionListener() {
  106. @Override
  107. public void MyEditorAction(String text, int actionId, KeyEvent event) {
  108. lastKey = text;
  109. showLoading();
  110. loadSearch(page = 1, lastKey);
  111. }
  112. });
  113. mSearchButton.setOnClickListener(this);
  114. }
  115. @Override
  116. protected void initDatas() {
  117. loadSearch(page = 1, lastKey = "");
  118. showLoading();
  119. }
  120. @Override
  121. public void onClick(View v) {
  122. switch (v.getId()) {
  123. case R.id.btn_actionbar_withback:
  124. finish();
  125. break;
  126. case R.id.common_search_btn:
  127. lastKey = mSearchEditText.getText().toString().trim();
  128. showLoading();
  129. loadSearch(page = 1, lastKey);
  130. break;
  131. }
  132. }
  133. private void findByLocal() {
  134. List<Bean> showModels = null;
  135. if (ListUtils.getSize(allModels) > 0) {
  136. if (TextUtils.isEmpty(lastKey)) {
  137. showModels = allModels;
  138. } else {
  139. showModels = new ArrayList<>();
  140. for (Bean e : allModels) {
  141. if (e != null && !TextUtils.isEmpty(e.showName) && e.showName.toUpperCase().contains(lastKey)) {
  142. showModels.add(e);
  143. }
  144. }
  145. }
  146. showByAdapter(showModels);
  147. }
  148. }
  149. private String getCondition(String keyWork) {
  150. StringBuilder builder = new StringBuilder(StringUtil.isEmpty(mDefCondition) ? "" : (mDefCondition + " and ("));
  151. if (configMap == null || configMap.isEmpty()) {
  152. builder.append("upper(" + (StringUtil.isEmpty(corekey) ? fieldKey : corekey) + ") like '%" + keyWork.toUpperCase() + "%'");
  153. } else {
  154. for (Map.Entry<String, String> e : configMap.entrySet()) {
  155. builder.append("upper(" + e.getKey() + ") like '%" + keyWork.toUpperCase() + "%' or ");
  156. }
  157. if (builder.length() > 3) {
  158. builder.delete(builder.length() - 3, builder.length() - 1);
  159. }
  160. }
  161. if (!StringUtil.isEmpty(mDefCondition)) {
  162. builder.append(" ) ");
  163. }
  164. return builder.toString();
  165. }
  166. private void loadSearch(int page, final String keyWork) {
  167. refreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  168. String condition = null;
  169. if (StringUtil.isEmpty(keyWork)) {
  170. if (StringUtil.isEmpty(mDefCondition)) {
  171. condition = "1=1";
  172. } else {
  173. condition = mDefCondition;
  174. }
  175. } else {
  176. condition = getCondition(keyWork);
  177. }
  178. boolean isForm = StringUtil.isEmpty(gCaller);
  179. Map<String, String> params = new HashMap<>();
  180. params.put("which", isForm ? "form" : "grid");
  181. params.put("caller", isForm ? caller : gCaller);
  182. params.put("field", fieldKey);
  183. params.put("condition", condition);
  184. params.put("ob", "");
  185. params.put("_config", "");
  186. params.put("page", page + "");
  187. params.put("pageSize", dataForm ? "10000" : "20");
  188. if (!isForm) {
  189. params.put("gridField", fieldKey);
  190. params.put("gridCaller", caller);//主从(单据caller 副从表传 本身的caller)
  191. }
  192. VollyRequest.getInstance().stringRequest(new HttpParams.Builder()
  193. .url(GloableParams.ADDRESS_COMMON_DBFIND)
  194. .method(Request.Method.GET)
  195. .tag("commondbfind")
  196. .flag(0)
  197. .setParams(params).build(), new HttpCallback() {
  198. @Override
  199. public void onSuccess(int flag, Object o) throws Exception {
  200. if (!lastKey.equals(keyWork)) return;
  201. try {
  202. handleData(o.toString());
  203. dimssLoading();
  204. } catch (Exception e) {
  205. dimssLoading();
  206. if (mAdapter == null || ListUtils.isEmpty(mAdapter.models)) {
  207. mEmptyLayout.showEmpty();
  208. }
  209. }
  210. refreshListView.onRefreshComplete();
  211. }
  212. @Override
  213. public void onFail(int flag, String failStr) throws Exception {
  214. dimssLoading();
  215. CommonUtil.toastNoRepeat(SelectNetAcitivty.this, failStr);
  216. refreshListView.onRefreshComplete();
  217. }
  218. });
  219. }
  220. private ListAdapter mAdapter = null;
  221. private void handleData(String messgae) throws Exception {
  222. JSONObject object = JSON.parseObject(messgae);
  223. JSONArray dbfinds = FastjsonUtil.getJSONArray(object, "dbfinds", "gridDbfinds");
  224. String dataStr = FastjsonUtil.getText(object, "data");
  225. JSONArray data = JSON.parseArray(dataStr);
  226. setData2Adapter(data, dbfinds);
  227. }
  228. private Map<String, String> configMap;
  229. private String fieldKeyLike = null;
  230. private void setData2Adapter(JSONArray data, JSONArray dbfinds) throws Exception {
  231. if (configMap == null || TextUtils.isEmpty(fieldKeyLike)) {
  232. configMap = new LinkedHashMap<>();
  233. //获取配置
  234. JSONObject config = null;
  235. for (int i = 0; i < dbfinds.size(); i++) {
  236. config = dbfinds.getJSONObject(i);
  237. String dbGridField = FastjsonUtil.getText(config, "dbGridField", "ds_dbfindfield");//显示值对应字段名
  238. String field = FastjsonUtil.getText(config, "field", "ds_gridfield");//实际字段名
  239. if (!StringUtil.isEmpty(dbGridField) && !StringUtil.isEmpty(field)) {
  240. if (field.equals(fieldKey)) {
  241. fieldKeyLike = dbGridField;
  242. }
  243. configMap.put(dbGridField, field);
  244. }
  245. }
  246. }
  247. JSONObject o = null;
  248. Bean b = null;
  249. List<Bean> models = new ArrayList<>();
  250. Map<String, String> jsonMap = null;
  251. for (int i = 0; i < data.size(); i++) {
  252. o = data.getJSONObject(i);
  253. b = new Bean();
  254. b.name = FastjsonUtil.getText(o, fieldKeyLike);
  255. jsonMap = new LinkedHashMap<>();
  256. for (Map.Entry<String, String> e : configMap.entrySet()) {
  257. jsonMap.put(e.getValue(), FastjsonUtil.getText(o, e.getKey()));
  258. }
  259. try {
  260. b.showName = getShowName(jsonMap);
  261. } catch (Exception e) {
  262. }
  263. b.json = FastjsonUtil.map2JSON(jsonMap);
  264. if (!StringUtil.isEmpty(b.name))
  265. models.add(b);
  266. }
  267. if (TextUtils.isEmpty(lastKey) && dataForm) {
  268. allModels = models;
  269. }
  270. showByAdapter(models);
  271. }
  272. private List<Bean> allModels = null;
  273. private void showByAdapter(List<Bean> models) {
  274. if (mAdapter == null) {
  275. mAdapter = new ListAdapter(models);
  276. refreshListView.setAdapter(mAdapter);
  277. refreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  278. @Override
  279. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  280. if (mAdapter != null) {
  281. Bean model = mAdapter.getModels((int) l);
  282. setResult(90, new Intent()
  283. .putExtra("data", model.json)
  284. .putExtra("groupId", groupId)
  285. .putExtra("isForm", isForm));
  286. finish();
  287. }
  288. }
  289. });
  290. } else {
  291. if (page == 1) {
  292. mAdapter.setModels(models);
  293. } else {
  294. mAdapter.addModls(models);
  295. }
  296. }
  297. if (ListUtils.isEmpty(models)) {
  298. mEmptyLayout.showEmpty();
  299. }
  300. }
  301. private <T> String getShowName(Map<String, T> object) throws Exception {
  302. StringBuilder builder = new StringBuilder();
  303. for (Map.Entry<String, T> e : object.entrySet()) {
  304. if (!TextUtils.isEmpty(e.getValue() + "")) {
  305. builder.append(e.getValue() + ",");
  306. }
  307. }
  308. StringUtil.removieLast(builder);
  309. return builder.toString();
  310. }
  311. class ListAdapter extends BaseAdapter {
  312. private List<Bean> models;
  313. public Bean getModels(int item) {
  314. if (ListUtils.getSize(models) > item) {
  315. return models.get(item);
  316. }
  317. return null;
  318. }
  319. public ListAdapter(List<Bean> models) {
  320. this.models = models;
  321. }
  322. public void setModels(List<Bean> models) {
  323. this.models = models;
  324. notifyDataSetChanged();
  325. }
  326. private void addModls(List<Bean> models) {
  327. if (this.models == null) {
  328. this.models = new ArrayList<>();
  329. }
  330. this.models.addAll(models);
  331. notifyDataSetChanged();
  332. }
  333. @Override
  334. public int getCount() {
  335. return ListUtils.getSize(this.models);
  336. }
  337. @Override
  338. public Object getItem(int i) {
  339. return models.get(i);
  340. }
  341. @Override
  342. public long getItemId(int i) {
  343. return i;
  344. }
  345. @Override
  346. public View getView(int i, View view, ViewGroup viewGroup) {
  347. ViewHoler holer = null;
  348. if (view == null) {
  349. holer = new ViewHoler();
  350. view = LayoutInflater.from(SelectNetAcitivty.this).inflate(R.layout.select_list_item, null);
  351. holer.name_tv = (TextView) view.findViewById(R.id.name_tv);
  352. holer.select_scb = (CheckBox) view.findViewById(R.id.select_scb);
  353. holer.select_scb.setVisibility(View.GONE);
  354. view.setTag(holer);
  355. } else {
  356. holer = (ViewHoler) view.getTag();
  357. }
  358. Bean bean = models.get(i);
  359. holer.name_tv.setText(bean.getShowName());
  360. return view;
  361. }
  362. class ViewHoler {
  363. TextView name_tv;
  364. CheckBox select_scb;
  365. }
  366. }
  367. private class Bean {
  368. String name;
  369. String json;
  370. String showName;
  371. public String getShowName() {
  372. return StringUtil.isEmpty(showName) ? name : showName;
  373. }
  374. }
  375. }