package yem.wmc.ca.chart;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.BizDataEventArgs;
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.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * 报关行的差错率=报关行（删掉 改单）/同期报关行的申报数量
 * @author zlp
 *
 */
public class CustomsBrokerTolerate extends AbstractFormPlugin{

	@Override
	public void createNewData(BizDataEventArgs e) {

		Control control = this.getControl("yem_histogramchartap");
		HistogramChart chart = null;
		if(control instanceof HistogramChart) {
			chart = (HistogramChart) control;
		}
		//获取当前年份
		Date date = new Date();
		Calendar nowCal = Calendar.getInstance();
		nowCal.setTime(date);

		List<QFilter> qlist = new ArrayList<QFilter>();
		Calendar startCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();
		startCal.set(nowCal.get(Calendar.YEAR), 0, 1, 0, 0, 0);
		endCal.set(Calendar.YEAR, nowCal.get(Calendar.YEAR));
		endCal.set(Calendar.MONTH, 12);
		endCal.set(Calendar.DATE,-1);
		endCal.set(Calendar.HOUR, 23);
		endCal.set(Calendar.MINUTE, 59);
		endCal.set(Calendar.SECOND, 59);
		qlist.add(new QFilter("yem_updatetime",QCP.large_equals,startCal.getTime()));
		qlist.add(new QFilter("yem_updatetime",QCP.less_equals,endCal.getTime()));
		Map<Object, DynamicObject> map = BusinessDataServiceHelper.loadFromCache("yem_ca_repairnote","yem_agentname",qlist.toArray(new QFilter[0]));
		Map<String,Integer> datas = new LinkedHashMap<String,Integer>();
		for(Object temp:map.keySet()) {
			DynamicObject dy = map.get(temp);
			String db = dy.getString("yem_agentname");
			if(!datas.containsKey(db)) {
				datas.put(db, 1);
			}else {
				datas.put(db, datas.get(db) + 1);
			}
		}


		//查总数
		List<QFilter> totallist = new ArrayList<QFilter>();
		//	totallist.add(new QFilter("yem_cusdecstatusname",QCP.equals,"删单"));
		Calendar startCal1 = Calendar.getInstance();
		Calendar endCal1 = Calendar.getInstance();
		startCal1.set(nowCal.get(Calendar.YEAR), 0, 1, 0, 0, 0);
		endCal1.set(Calendar.YEAR, nowCal.get(Calendar.YEAR));
		endCal1.set(Calendar.MONTH, 12);
		endCal1.set(Calendar.DATE,-1);
		endCal1.set(Calendar.HOUR, 23);
		endCal1.set(Calendar.MINUTE, 59);
		endCal1.set(Calendar.SECOND, 59);
		totallist.add(new QFilter("yem_fiedatets",QCP.large_equals,startCal1.getTime()));
		totallist.add(new QFilter("yem_fiedatets",QCP.less_equals,endCal1.getTime()));

		//统计图显示数据
		Map<String,BigDecimal> totalDatas = new LinkedHashMap<String,BigDecimal>();
		for(String key :datas.keySet()) {

			List<QFilter> temp = new ArrayList<QFilter>();
			temp.addAll(totallist);
//			//数据量
			temp.add(new QFilter("yem_agentname",QCP.equals,key));
			int count = BusinessDataServiceHelper.loadFromCache("yem_ca_cusdec","yem_entryid",temp.toArray(new QFilter[0])).size();
			double yValue = count == 0 ? 0.0:((double) datas.get(key) / count) ;
			BigDecimal tempDe = new BigDecimal(yValue).divide(new BigDecimal(1.0),4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
			totalDatas.put(key , tempDe);
		}
		if(chart != null) {

			drawChart(chart,totalDatas);
		}
	}
	public void drawChart(HistogramChart chart,Map<String,BigDecimal> map) {
		Axis y = chart.createYAxis("",AxisType.category);
		Axis x = chart.createXAxis("", AxisType.value);
		x.setPropValue("name","差错率(%)");
		Map<String,Object> axisLine = new HashMap<String,Object>();
		axisLine.put("show", true);
		String[] symbolArr = {"none", "arrow"};
		axisLine.put("symbol", symbolArr);
		Map<String,Object> lineStyle = new HashMap<String,Object>();
		lineStyle.put("color", "#333");
		axisLine.put("lineStyle", lineStyle);
		x.setPropValue("axisLine", axisLine);
		//设置柱状图左右离容器距离
		Map<String,Object> grid = new HashMap<String,Object>();
		grid.put("right","110");
		grid.put("left","10");
		grid.put("top", "10");
		grid.put("containLabel",true);
		chart.addProperty("grid", grid);
		//每次取10个国家
		BarSeries barSeries = chart.createSeries("差错率");

		List<Map.Entry<String,BigDecimal>> tempList = new ArrayList<>(map.entrySet());
		Collections.sort(tempList,new Comparator<Map.Entry<String,BigDecimal>>() {

			@Override
			public int compare(Entry<String, BigDecimal> o1, Entry<String, BigDecimal> o2) {
				// TODO Auto-generated method stub
				return o1.getValue().compareTo(o2.getValue());
			}

		});

		Number[] values = new Number[map.size()];
		List<String> yList = new ArrayList<String>();
		int i = 0;
		for(Map.Entry<String, BigDecimal> entry:tempList) {
			values[i] = entry.getValue();
			yList.add(entry.getKey());
			i++;
		}
		barSeries.setData(values);
		y.setCategorys(yList);

		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();
	}
}
