StatisticsMonActivity.java 14.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
package com.ectrip.cyt.ui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.ectrip.cyt.adapter.NavigationAdapter;
import com.ectrip.cyt.adapter.StatisticsAdapter;
import com.ectrip.cyt.base.DataTrans;
import com.ectrip.cyt.callback.HttpCallback;
import com.ectrip.cyt.center.PrintHandle;
import com.ectrip.cyt.config.MyApp;
import com.ectrip.cyt.constant.DeviceType;
import com.ectrip.cyt.constant.OrderCode;
import com.ectrip.cyt.response.StatisticInfos;
import com.ectrip.cyt.utils.SharedPreferences2Obj;
import com.ectrip.trips.check.R;
import com.ectrip.trips.net.DataTool;
import com.ectrip.trips.net.HttpHelper;

/**
 * @author jigo 月统计
 */
public class StatisticsMonActivity extends BaseActivity implements
		OnClickListener {
	private Integer type;//机型
	private StatisticsAdapter adapter;
	private ListView statistic_list;
	private TextView year_spinner;
	private TextView mon_spinner;
	private List<String> yearList = new ArrayList<String>();
	private List<String> monList = new ArrayList<String>();
	private Calendar mCalendar;
	private Button searchButton;
	private String year;
	private String month;
	private View errorLayout;
	private TextView errorInfo;
	private Button printBtn;
	private TextView title;
	private List<LinkedHashMap<String, String>> statisticsList;
	private String printDate;
	private View startLayout;
	private View endLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistics_mon_activity);
		initView();
		init();
	}

	private void initView() {
		startLayout = findViewById(R.id.startLayout);
		endLayout = findViewById(R.id.endLayout);
		title = (TextView) findViewById(R.id.title);
		title.setVisibility(View.VISIBLE);
		title.setText(R.string.statistics_mon);
		printBtn = (Button) findViewById(R.id.printBtn);//打印按钮
		errorLayout = findViewById(R.id.errorLayout);
		errorInfo = (TextView) errorLayout.findViewById(R.id.errorInfo);
		searchButton = (Button) findViewById(R.id.searchButton);//查询按钮
		statistic_list = (ListView) findViewById(R.id.statistic_list);
		year_spinner = (TextView) findViewById(R.id.year_spinner);
		mon_spinner = (TextView) findViewById(R.id.mon_spinner);
		addyear();
		addmon();
		startLayout.setOnClickListener(this);
		endLayout.setOnClickListener(this);
		searchButton.setOnClickListener(this);
		printBtn.setOnClickListener(this);

		findViewById(R.id.topBack).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void addyear() {
		mCalendar = Calendar.getInstance();
		int yearint = mCalendar.get(Calendar.YEAR);
		year = yearint + "";
		year_spinner.setText(year);
		mon_spinner.setText((mCalendar.get(Calendar.MONTH) + 1) + "");
		if ((mCalendar.get(Calendar.MONTH) + 1) < 10) {
			month = "0" + (mCalendar.get(Calendar.MONTH) + 1);
		} else {
			month = "" + (mCalendar.get(Calendar.MONTH) + 1);
		}
		for (int i = 5; i > 0; i--) {
			yearList.add((yearint - i) + "");
		}
		yearList.add(year + "");
		for (int i = 1; i <= 5; i++) {
			yearList.add((yearint + i) + "");
		}
	}

	private void addmon() {
		for (int i = 1; i <= 12; i++) {
			monList.add(i + "");
		}
	}

	private void init() {
		type = SharedPreferences2Obj.getInstance(StatisticsMonActivity.this)
				.setName("MachineType").getObject("type", Integer.class);
		//如果是黄色手持机
		if (printBtn != null && type != null
				&& type == DeviceType.HANDSET.getValue()) {
			printBtn.setVisibility(View.GONE);
		}
	}

	class MyHttpCallback extends HttpCallback<DataTrans> {
		ProgressDialog dialog = null;
		private String signed;
		private String data;

		@Override
		public void onStopCallback() {

		}

		@Override
		public void onPreCallback() {
			if (dialog == null) {
				// 显示ProgressDialog
				dialog = new ProgressDialog(StatisticsMonActivity.this);
				dialog.setMessage(getString(R.string.obtain_statistical_information));
				dialog.setCanceledOnTouchOutside(false);
				dialog.setCancelable(true);
				if (type == DeviceType.HANDSET.getValue()) {
					dialog.getWindow().setType(
							WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
				}
				try {
					dialog.show();
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				dialog.setMessage(getString(R.string.obtain_statistical_information));
			}
		}

		@Override
		public void onFailureCallback(String FailureStr) {
			if (dialog != null && dialog.isShowing()) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
					e.printStackTrace();
				} catch (Throwable e) {
					e.printStackTrace();
				}
			}
			statistic_list.setEmptyView(errorLayout);
			errorLayout.setVisibility(View.VISIBLE); // 错误信息提示
			if (FailureStr != null) {
				errorInfo.setText(FailureStr);
				MToast(StatisticsMonActivity.this, FailureStr,
						Toast.LENGTH_LONG);
			}
		}

		@Override
		public void onCompletedCallback(DataTrans result) {

		}

		@Override
		public void onAgainParseCallback(Object parse) {
			if (dialog != null && dialog.isShowing()) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
					e.printStackTrace();
				} catch (Throwable e) {
					e.printStackTrace();
				}
			}
			if (parse != null) {
				StatisticInfos statisticInfos = (StatisticInfos) parse;
				String code = statisticInfos.getCode();
				if (code != null && OrderCode.SUCESS.getValue().equals(code)) {
					if (signed.equals(DataTool.getSign(MyApp.getInstance()
							.getSignkey(), data))) {
						if (statisticInfos != null
								&& statisticInfos.getStatistics() != null
								&& statisticInfos.getStatistics().size() > 0) {
							statisticsList = statisticInfos.getStatistics();
							if (adapter == null) {
								adapter = new StatisticsAdapter(
										StatisticsMonActivity.this,
										statisticsList);
								statistic_list.setAdapter(adapter);
							} else {
								adapter.notifyDataSetChanged(statisticsList);
							}
						} else {
							onFailureCallback(getString(R.string.no_data));
							MToast(StatisticsMonActivity.this,
									getString(R.string.get_statistic_info_fail),
									Toast.LENGTH_SHORT);
							if (adapter != null) {
								statisticsList = null;
								adapter.notifyDataSetChanged(null);
							}
						}
					} else {
						statisticsList = null;
						onFailureCallback(getString(R.string.sign_not_pass));
						MToast(StatisticsMonActivity.this,
								getString(R.string.sign_not_pass),
								Toast.LENGTH_SHORT);
					}
				} else {
					statisticsList = null;
					onFailureCallback(statisticInfos.getDescribe());
					MToast(StatisticsMonActivity.this,
							statisticInfos.getDescribe(), Toast.LENGTH_SHORT);
				}
			}
		}

		@Override
		public void afterCompletedCallback(DataTrans result) {
			if (result != null) {
				signed = result.getSigned();
				data = result.getData();
				parse2(result.getData(), StatisticInfos.class,
						StatisticsMonActivity.this);
			} else {
				if (dialog != null && dialog.isShowing()) {
					try {
						dialog.dismiss();
					} catch (Exception e) {
						e.printStackTrace();
					} catch (Throwable e) {
						e.printStackTrace();
					}
				}
				MToast(StatisticsMonActivity.this,
						getString(R.string.get_statistic_info_fail),
						Toast.LENGTH_LONG);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.searchButton:
				if (year_spinner.getText() == null
						|| year_spinner.getText().toString() == null
						|| year_spinner.getText().toString().equals("")) {
					MToast(StatisticsMonActivity.this,
							getString(R.string.select_year), Toast.LENGTH_LONG);
					return;
				} else if (mon_spinner.getText() == null
						|| mon_spinner.getText().toString() == null
						|| mon_spinner.getText().toString().equals("")) {
					MToast(StatisticsMonActivity.this,
							getString(R.string.select_month), Toast.LENGTH_LONG);
					return;
				}
				printDate = year + "-" + month;
				HttpHelper.getInstance(StatisticsMonActivity.this)
						.statisticsOrder("MON", null, null, year + "-" + month,
								new MyHttpCallback());
				break;
			case R.id.printBtn://打印
				if (statisticsList == null || statisticsList.size() == 0) {
					MToast(StatisticsMonActivity.this,
							getString(R.string.no_data_print), Toast.LENGTH_LONG);
					return;
				}
				if (year_spinner != null && year_spinner.getText() == null
						|| year_spinner.getText().toString() == null) {
					MToast(StatisticsMonActivity.this,
							getString(R.string.select_year), Toast.LENGTH_SHORT);
					return;
				}
				if (mon_spinner != null && mon_spinner.getText() == null
						|| mon_spinner.getText().toString() == null) {
					MToast(StatisticsMonActivity.this,
							getString(R.string.select_month), Toast.LENGTH_SHORT);
					return;
				}
				PrintHandle.getInstance().print(list2Str(statisticsList),
						StatisticsMonActivity.this);
				break;
			case R.id.startLayout:
				showPopWindow(year_spinner, yearList);
				break;
			case R.id.endLayout:
				showPopWindow(mon_spinner, monList);
				break;
			default:
				break;
		}
	}

	private String list2Str(List<LinkedHashMap<String, String>> list) {
		StringBuffer sb = new StringBuffer();
		if (list != null && list.size() > 0) {
			for (Map<String, String> temp : list) {
				for (Map.Entry<String, String> entry : temp.entrySet()) {
					if (entry != null) {
						if (entry.getValue() != null) {
							sb.append(entry.getValue() + "\n");
						}
					}
				}
				sb.append("\n");
			}
			try {// 加了时间有问题
				if (type != null && type == DeviceType.HANDSET.getValue()) {

				} else {
					sb.append(getString(R.string.statistic_time) + printDate
							+ "\n");
					sb.append("\n");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/************************* 下拉弹框 ***************************/
	private View layout2;
	private ListView popList;
	private NavigationAdapter navAdapter;
	private PopupWindow popupWindow2;
	private PopupWindow popupWindow3;

	@SuppressWarnings("deprecation")
	@SuppressLint("InflateParams")
	protected void showPopWindow(View view, List<String> navigationItem) {
		layout2 = LayoutInflater.from(StatisticsMonActivity.this).inflate(
				R.layout.navigation_pop_list, null);
		popList = (ListView) layout2.findViewById(R.id.navigation_poplist);
		navAdapter = new NavigationAdapter(StatisticsMonActivity.this,
				navigationItem);
		popList.setAdapter(navAdapter);
		switch (view.getId()) {
			case R.id.year_spinner:
				if (popupWindow2 == null) {
					popupWindow2 = new PopupWindow(view);

					// 设置弹框的宽度为布局文件的宽
					popupWindow2.setWidth(view.getWidth());
					popupWindow2.setHeight(LayoutParams.WRAP_CONTENT);

					// 设置一个透明的背景,不然无法实现点击弹框外,弹框消失
					popupWindow2.setBackgroundDrawable(new BitmapDrawable());
					// 设置点击弹框外部,弹框消失
					popupWindow2.setOutsideTouchable(true);
					popupWindow2.setFocusable(true);
					popupWindow2.setContentView(layout2);
					// 设置弹框出现的位置,在v的正下方横轴偏移textview的宽度,为了对齐~纵轴不偏移
					popupWindow2.showAsDropDown(view, 0, 10);

					// 点击pop对应的条目

					MyOnIemClickListener myListener = new MyOnIemClickListener(
							navigationItem, view);
					popList.setOnItemClickListener(myListener);
				} else if (popupWindow2 != null && !popupWindow2.isShowing()) {
					popupWindow2.showAsDropDown(view, 0, 10);
				}
				break;
			case R.id.mon_spinner:
				if (popupWindow3 == null) {
					popupWindow3 = new PopupWindow(view);

					// 设置弹框的宽度为布局文件的宽
					popupWindow3.setWidth(view.getWidth());
					popupWindow3.setHeight(LayoutParams.WRAP_CONTENT);

					// 设置一个透明的背景,不然无法实现点击弹框外,弹框消失
					popupWindow3.setBackgroundDrawable(new BitmapDrawable());
					// 设置点击弹框外部,弹框消失
					popupWindow3.setOutsideTouchable(true);
					popupWindow3.setFocusable(true);
					popupWindow3.setContentView(layout2);
					// 设置弹框出现的位置,在v的正下方横轴偏移textview的宽度,为了对齐~纵轴不偏移
					popupWindow3.showAsDropDown(view, 0, 10);

					// 点击pop对应的条目

					MyOnIemClickListener myListener = new MyOnIemClickListener(
							navigationItem, view);
					popList.setOnItemClickListener(myListener);
				} else if (popupWindow3 != null && !popupWindow3.isShowing()) {
					popupWindow3.showAsDropDown(view, 0, 10);
				}
				break;
			default:
				break;
		}
	}

	private class MyOnIemClickListener implements OnItemClickListener {

		private View textView;

		public MyOnIemClickListener(List<String> navigationItem, View view) {
			this.textView = view;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
								long id) {
			try {
				switch (textView.getId()) {
					case R.id.year_spinner:
						if (yearList != null) {
							year_spinner.setText(yearList.get(position));
							year = yearList.get(position);
						}
						break;
					case R.id.mon_spinner:
						if (monList != null) {
							mon_spinner.setText(monList.get(position));
							if (position < 9) {
								month = "0" + (position + 1);
							} else {
								month = "" + (position + 1);
							}
						}
						break;
					default:
						break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (popupWindow2 != null) {
					popupWindow2.dismiss();
					popupWindow2 = null;
				}
				if (popupWindow3 != null) {
					popupWindow3.dismiss();
					popupWindow3 = null;
				}
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		PrintHandle.getInstance().printClose();
	}
}