package yem.wmc.ca.chart;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.EventObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.kingdee.bos.util.backport.Collections;

import kd.bos.algo.DataSet;
import kd.bos.algo.GroupbyDataSet;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.chart.Axis;
import kd.bos.form.chart.AxisType;
import kd.bos.form.chart.BarSeries;
import kd.bos.form.chart.HistogramChart;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;import kd.bos.metric.Histogram;
import kd.bos.orm.ORM;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * 国家排行（柱状
	柱状-横向 国家
《单一窗口报关单》金额信息【金额合计】根据【贸易国别】分组
 * @author zlp
 *
 */
public class CountryRank extends AbstractFormPlugin{
	
	private static Map<String,BigDecimal> datas = null;
	private static int index = 0;
	@Override
	public void registerListener(EventObject e) {
		Button topButton = this.getControl("yem_top");
		Button nextButton = this.getControl("yem_next");
		if(topButton != null) {
			topButton.addClickListener(this);
		}
		if(nextButton != null) {
			nextButton.addClickListener(this);
		}

	}
	@Override
	public void afterCreateNewData(EventObject e) {
		
		index = 0;
		Control control = this.getControl("yem_histogramchartap");
		HistogramChart chart = null;
		if(control instanceof HistogramChart) {
			chart = (HistogramChart) control;
		}
		
		String select2 = "yem_sumlocdecltotal sumdecltotal,yem_custradecodbase.name temp";
		DataSet data2 =  ORM.create().queryDataSet(this.getClass().getName(),"yem_ca_cusdec",select2,null).distinct()
				.select(new String[] {"sumdecltotal","temp"});
		GroupbyDataSet grData = data2.groupBy(new String[] {"temp"}, new boolean[] {true}).sum("sumdecltotal");
		DataSet data3 = grData.finish().orderBy(new String[] {"sumdecltotal desc"});
		datas = new LinkedHashMap<String,BigDecimal>();
		DynamicObjectCollection  dy2 = ORM.create().toPlainDynamicObjectCollection(data3);
		for (DynamicObject temp : dy2) {
			datas.put(temp.getString(0),temp.getBigDecimal(1));
		}

		List<String> listNow = new ArrayList<String>();

		listNow.addAll(datas.keySet());
		//国家数据最多为10
		listNow = listNow.subList(index, 10);
		Collections.reverse(listNow);
		this.getModel().setValue("yem_textfield","当前页数为:1 " );
		
		this.getModel().setValue("yem_textfield1"," /总页数:" + ((datas.keySet().size()/10)+1));

		drawChart(chart,listNow);
	}
	public void drawChart(HistogramChart chart,List<String> listNow) {
		Axis y = chart.createYAxis("",AxisType.category);
		Axis x = chart.createXAxis("", AxisType.value);
		x.setPropValue("name","金额(万元)");
	//	x.setPropValue("nameLocation", "en");
		Map<String,Object> axisLine = new HashMap<String,Object>();
		axisLine.put("show", true);
		String[] symbolArr = {"none", "arrow"};
		axisLine.put("symbol", symbolArr);
		x.setPropValue("axisLine", axisLine);
//		Map<String,Object> axisLabel= new HashMap<String,Object>();
////		axisLabel.put("rotate", 20);
//		x.setPropValue("axisLabel", axisLabel);
		//设置柱状图左右离容器距离
		Map<String,Object> grid = new HashMap<String,Object>();
		grid.put("right","150");
		grid.put("left","10");
		grid.put("top", "10");
		grid.put("containLabel",true);
		chart.addProperty("grid", grid);
		//每次取10个国家
		BarSeries barSeries = chart.createSeries("国家排行");
		
		y.setCategorys(listNow);
		Number[] values = new Number[listNow.size()];
		
		int i = 0;
		for(String temp:listNow) {
			values[i] = datas.get(temp).divide(new BigDecimal(10000.0),2, RoundingMode.UP);
			i++;
		}
		barSeries.setData(values);
		Map<String,Object> label= new HashMap<String,Object>();
		label.put("position", "right");
		label.put("show", true);
		barSeries.setPropValue("label", label);
		barSeries.setColor("#6890FB");
		chart.setShowTooltip(true);
		chart.refresh();
//		chart.setShowLegend(true);
		
	}
	@Override
	public void click(EventObject evt) {
		Control control = this.getControl("yem_histogramchartap");
		HistogramChart chart = null;
		if(control instanceof HistogramChart) {
			chart = (HistogramChart) control;
		}
		if("yem_top".equals(((Control) evt.getSource()).getKey())) {
			List<String> listNow = new ArrayList<String>();
			listNow.addAll(datas.keySet());
			int end = index;
			//国家数据最多为10
			if((index - 10) >= 0) {
				index = index - 10;
			}else {
				this.getView().showMessage("已达到最小页数");
				return;
			}
			listNow = listNow.subList(index, end);
			this.getModel().setValue("yem_textfield","当前页数为: "+  ((index/10) + 1) );
			
			this.getModel().setValue("yem_textfield1"," /总页数:" + ((datas.keySet().size()/10)+1));
			drawChart(chart,listNow);
		}
		if("yem_next".equals(((Control) evt.getSource()).getKey())) {
			int end = 0;
			List<String> listNow = new ArrayList<String>();
			listNow.addAll(datas.keySet());
			//国家数据最多为10
	
			if((index + 20) <= (datas.size() - 1)) {
				index = index + 10;
				end = index + 10;
			}else if(index + 10 <= (datas.size() - 1)) {
				index = index + 10;
				end = datas.size() - 1;
			}else {
				this.getView().showMessage("已达到最大页数");
				return;
			}
		
		
			listNow = listNow.subList(index, end);
			this.getModel().setValue("yem_textfield","当前页数为: " + ((index/10) + 1));
			
			this.getModel().setValue("yem_textfield1"," /总页数:" + ((datas.keySet().size()/10)+1));
			drawChart(chart,listNow);
		
		}
	}
	
	
}
