package com.yem.tws.chart;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.EventObject;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.chart.LineSeries;
import kd.bos.form.chart.PointLineChart;
import kd.bos.form.chart.Position;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * 出口额-折线 按月
  获取《单一窗口报关单》单据类型为【出口报关单】的【金额合计】
 * @author zlp
 *
 */
public class AmountOfExportLine extends AbstractFormPlugin{
	private final static String[] xStr = new String[] {"01月","02月","03月","04月","05月","06月","07月","08月","09月","10月","11月","12月"};
	private static Map<String,BigDecimal> datas = null;
	private static Map<Integer,Map> map = null;
	private static Set<String> legendName = new HashSet<String>();
	private static int  year;
	@Override
	public void registerListener(EventObject e) {
		Button button = this.getControl("yem_buttonap1");
		Button button2 = this.getControl("yem_addbu");
		if(button != null) {
			button.addClickListener(this);
		}
		if(button2 != null) {
			button2.addClickListener(this);
		}

	}
	
	@Override
	public void click(EventObject evt) {
		Control control = (Control) evt.getSource();
		Control control2 = this.getControl("yem_pointlinechartap");
		PointLineChart lineChart = null;
		if(control2 instanceof PointLineChart) {
			lineChart = (PointLineChart) control2;
		}
		if("yem_buttonap1".equals(control.getKey())) {
			String tempYear = (String) this.getModel().getValue("yem_textfield");
			if(tempYear.isEmpty()) {
				this.getView().showMessage("年份不能为空");
				return;
			}
			if(year == (Integer.valueOf(tempYear))) {
				this.getView().showMessage("当前年份已默认查询，无需再次查询");
				return;
			}
			//清除旧数据
			Map<String,Object> map = new HashMap<String,Object>();
			for(String name:legendName) {
				map.put(name+"年", false);
					if((tempYear).equals(name)) {
						map.put(name+"年", true);
						continue;
					}
				}
			lineChart.setLegendPropValue("selected", map);
			year = Integer.valueOf(tempYear);
			datas = dateMakes(year);
			drawChart(lineChart, year);
		}else if("yem_addbu".equals(control.getKey())) {
			String tempYear = (String) this.getModel().getValue("yem_textfield");
			if(tempYear.isEmpty()) {
				this.getView().showMessage("年份不能为空");
				return;
			}
			if(year == (Integer.valueOf(tempYear))) {
				this.getView().showMessage("当前年份已默认查询，无需新增查询");
				return;
			}
			Map<Integer,Map> map =new LinkedHashMap<Integer,Map>();
			map.put(year, datas);
			int addYear = Integer.valueOf(tempYear);
			Map<String,BigDecimal> addDatas = dateMakes(addYear);
			map.put(addYear, addDatas);
			//将新增数据隐藏部分打开
			Map<String,Object> temp = new HashMap<String,Object>();
			temp.put(tempYear+"年", true);
			lineChart.setLegendPropValue("selected", temp);
			drawChart(lineChart, map);
			
		}
	}

	@Override
	public void afterCreateNewData(EventObject e) {
		Control control = this.getControl("yem_pointlinechartap");
		PointLineChart lineChart = null;
		if(control instanceof PointLineChart) {
			lineChart = (PointLineChart) control;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		year = calendar.get(calendar.YEAR);
		datas = dateMakes(year);
		drawChart(lineChart, year);
	}
	//只查1年数据
	public void drawChart(PointLineChart lineChart,int year) {
		Axis x = lineChart.createXAxis("月份",AxisType.category);
		x.setPropValue("nameLocation","end");
		Map<String,Object> nameTextStyle = new HashMap<String,Object>();
		nameTextStyle.put("color","black");
		Map<String, Object> axisLine = new HashMap<>();
		String[] symbolArr = {"none", "arrow"};
		axisLine.put("symbol", symbolArr);
		x.setPropValue("axisLine", axisLine);
		x.setCategorys(xStr);
		x.setPropValue("nameTextStyle", nameTextStyle);
		Axis y = lineChart.createYAxis("金额(万元)",AxisType.value);
		singleDrawChar(lineChart, datas, year, "#6890FB");
		lineChart.setShowTooltip(true);
		lineChart.setShowLegend(true);
		Map<String,Object> grid = new HashMap<String,Object>();
		grid.put("right","50");
		grid.put("left","10");
		grid.put("containLabel",true);
		lineChart.addProperty("grid", grid);
		lineChart.refresh();
	
	}
	//查询多年数据
	public void drawChart(PointLineChart lineChart,Map<Integer,Map> years) {
		
		Axis x = lineChart.createXAxis("月份",AxisType.category);
		x.setPropValue("nameLocation","end");
		Map<String,Object> nameTextStyle = new HashMap<String,Object>();
		nameTextStyle.put("color","black");
		Map<String, Object> axisLine = new HashMap<>();
		String[] symbolArr = {"none", "arrow"};
		axisLine.put("symbol", symbolArr);
		x.setPropValue("axisLine", axisLine);
		x.setCategorys(xStr);
		x.setPropValue("nameTextStyle", nameTextStyle);
		Axis y = lineChart.createYAxis(" 金额合计/万元",AxisType.value);
		lineChart.setShowTooltip(true);
		lineChart.setShowLegend(true);
		Map<String,Object> grid = new HashMap<String,Object>();
		grid.put("right","50");
		grid.put("left","10");
		grid.put("containLabel",true);
		lineChart.addProperty("grid", grid);
		String[] colors = new String[] {"#6890FB","#A3FEB5"};
		int colorIndex = 0;
		for(int temp :years.keySet()) {
		singleDrawChar(lineChart, years.get(temp), temp,colors[colorIndex]);
		colorIndex++;
		}
		lineChart.refresh();
		
	}
	//查询金额本位币合计(即本位币)
	public Map<String,BigDecimal> dateMakes(int years){
		Map<String,BigDecimal> datas = new LinkedHashMap<String,BigDecimal>();
		int[] months = new int[] {0,1,2,3,4,5,6,7,8,9,10,11};
		Calendar startCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();
		for(int month:months) {
			startCal.set(years, month, 1, 0, 0, 0);
			endCal.set(Calendar.YEAR, years);
			endCal.set(Calendar.MONTH, month+1);
			endCal.set(Calendar.DATE,0);
			endCal.set(Calendar.HOUR, 23);
			endCal.set(Calendar.MINUTE, 59);
			endCal.set(Calendar.SECOND, 59);
			List<QFilter> cifList = new ArrayList<QFilter>(); 
			cifList.add(QFilter.of("yem_billtypefield.name = ?","出口报关单"));
			cifList.add(QFilter.of("yem_cusdecstatusname = ?","结关"));
			cifList.add(new QFilter("yem_fiedatets",QCP.large_equals,startCal.getTime()));
			cifList.add(new QFilter("yem_fiedatets",QCP.less_equals,endCal.getTime()));
			Map<Object, DynamicObject> map = BusinessDataServiceHelper.loadFromCache("yem_ca_cusdec","yem_sumlocdecltotal",cifList.toArray(new QFilter[0]));
			//出口总额
			BigDecimal sum = BigDecimal.ZERO;
			for(Object temp:map.keySet()) {
				DynamicObject dy = map.get(temp);
				BigDecimal db = dy.getBigDecimal("yem_sumlocdecltotal");
				sum = sum.add(db);
			}
			datas.put(month+1+"", sum);
		}
		return datas;
	}
	public void singleDrawChar(PointLineChart lineChart,Map<String,BigDecimal> map,int year,String color) {
		Number[] values = new Number[xStr.length];
		for(int i=0;i<xStr.length;i++) {
			if(i < 9) {
			values[i] = map.get(String.valueOf((xStr[i].charAt(1)))).divide(new BigDecimal(10000.0),2, BigDecimal.ROUND_HALF_UP);
			}else {
			values[i] = map.get(xStr[i].substring(0, 2)).divide(new BigDecimal(10000.0),2,BigDecimal.ROUND_HALF_UP)	;
			}
		}
		LineSeries series =  lineChart.createLineSeries(year+"年");
		legendName.add(year+"");
		series.setData(values);
		Map<String,Object> lineStyle = new HashMap<String,Object>();
		lineStyle.put("color", color);
		Map<String,Object> itemStyle = new HashMap<String,Object>();
		itemStyle.put("color", color);
		series.setPropValue("lineStyle", lineStyle);
		series.setPropValue("itemStyle", itemStyle);

		
	}
	
}
