package com.yem.em.form;

import com.google.common.collect.Maps;
import com.yem.em.utils.BaseDataFilterUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.chart.Axis;
import kd.bos.form.chart.AxisType;
import kd.bos.form.chart.BarSeries;
import kd.bos.form.chart.ChartType;
import kd.bos.form.chart.HistogramChart;
import kd.bos.form.chart.Position;
import kd.bos.form.plugin.AbstractFormPlugin;


import java.math.BigDecimal;
import java.util.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author zhouc
 * @date 2023/9/5 10:34
 * @className ProductTypeEdit
 * @description 产品类别小部件表单插件
 */
public class ProductTypeEdit extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IDataModel model = this.getModel();
        Map<String, Integer> equtypeMap = new HashMap<>();
        equtypeMap.put("装载机",8);
        equtypeMap.put("拖拉机",5);
        equtypeMap.put("叉车",3);
        HistogramChart(model,equtypeMap);
    }

    /**
     * 设置柱状图格式
     * @param model 视图模型
     * @param equtypeMap 定义值map
     */
    private void HistogramChart(IDataModel model, Map<String, Integer> equtypeMap) {
        HistogramChart customchart = (HistogramChart) this.getControl("yem_histogramchartap");
        customchart.clearData();// 清除数据 避免重复增加数据
        customchart.setMargin(Position.left, "10px");
        customchart.setMargin(Position.right, "80px");
        customchart.setShowLegend(true); // 设置图例

        Axis xAxis = customchart.createXAxis(ResManager.loadKDString("", "DeviceTypeEdit_1", "yem_em_producttype"),
                AxisType.category); // 创建x轴，name表示轴名字，category 表示类目轴
        Map<String, Object> xAxisPointer = Maps.newHashMap(); // X轴指示器属性设置
        xAxisPointer.put("show", true);
        xAxisPointer.put("type", "shadow");
        xAxis.setPropValue("axisPointer", xAxisPointer);
        xAxis.setType(AxisType.category);

        HashMap<String, Object> axisTick = new HashMap<>();
        axisTick.put("interval", Integer.valueOf(0));
        axisTick.put("show", Boolean.valueOf(true));
        xAxis.setPropValue("axisTick", axisTick);// 设置X轴axisTick属性

        HashMap<String, Object> axisLabel = new HashMap<>();
        axisLabel.put("interval", Integer.valueOf(0));
        axisLabel.put("show", Boolean.valueOf(true));
        axisLabel.put("rotate", Integer.valueOf(45));
        xAxis.setPropValue("axisLabel", axisLabel);// 设置X轴axisLabel属性

        Axis yAxis = customchart.createYAxis(ResManager.loadKDString("(资源号数量)", "DeviceTypeEdit_2", "bfcl_equipment"),
                AxisType.value); // 创建y轴，name表示轴名字，value表示数值轴，适用于连续数据。
        Map<String, Object> axisLabel1 = Maps.newHashMap(); // Y轴指示器属性设置
        axisLabel1.put("formatter", "{value} "); // 设置Y轴数据显示格式
        yAxis.setPropValue("axisLabel", axisLabel1);
        Map<String, Object> yAxisPointer = Maps.newHashMap(); // Y轴指示器属性设置
        yAxisPointer.put("show", true); // Y轴指示器属性
        yAxisPointer.put("type", "line");
        // Y轴设置指示线类型
        Map<String, Object> lineStyle = Maps.newHashMap();
        lineStyle.put("type", "dotted");
        yAxisPointer.put("lineStyle", lineStyle);
        yAxis.setPropValue("axisPointer", yAxisPointer);
        // 设置Y轴axisTick属性
        yAxis.setPropValue("axisTick", axisTick);

        BarSeries barSeries = customchart
                .createBarSeries(ResManager.loadKDString("资源号数量", "DeviceTypeEdit_3", "bfcl_equipment"));// 创建柱状图系列，name表示名称
        barSeries.setType(ChartType.bar); // 设置类别展示类型
        barSeries.setAnimationDuration(2000);// 动画过度时间
        barSeries.setStack("stack");// 设置相同的stack会堆叠显示
        barSeries.setPropValue("yAxisIndex", 0); // 设置所属坐标轴
        barSeries.setBarWidth("20");

        List<String> categorys = new ArrayList<>(); // 获取默认x轴数据
        Number[] seriesData = new Number[equtypeMap.size()]; // 获取柱状图数据
        List<Integer> yAxisDataList = new ArrayList<>();// 用于设置y轴数据
        int i = 0;
        for (String key : equtypeMap.keySet()) {
          seriesData[i] = equtypeMap.get(key);
            categorys.add(key);
            yAxisDataList.add(equtypeMap.get(key));
            i++;
        }

        xAxis.setCategorys(categorys); // 将x轴数据写入x轴--参数类型(List<String> categorys)

        if (yAxisDataList.size() > 0) {
            int maxQty = Collections.max(yAxisDataList);
            BigDecimal num = new BigDecimal(maxQty);
            if (maxQty <= 100) {
                num = num.setScale(-1, BigDecimal.ROUND_UP);
            } else {
                num = num.setScale(-2, BigDecimal.ROUND_UP);
            }
            yAxis.setInterval(divideBigDecimal(num, new BigDecimal(10), 0));// 设置y间隔
            yAxis.setMax(addBigDecimal(num, divideBigDecimal(num, new BigDecimal(10), 0), 10));// 设置y最大值
        }
        barSeries.setData(seriesData); // 将每个点纵坐标的值写入--参数类型(Number[] data)
        barSeries.setColor("#5582F3"); // 设置柱颜色
        customchart.refresh(); // 刷新图表
    }

    /**
     *   BigDecimal除法运算
     * @param aVal 除数
     * @param bVal 被除数
     * @param scale 精度
     * @return BigDecimal
     */
    public  BigDecimal divideBigDecimal(BigDecimal aVal, BigDecimal bVal, int scale) {

        if (aVal == null)
            aVal = BigDecimal.ZERO;

        if (bVal == null)
            return BigDecimal.ZERO;

        if (bVal.compareTo(BigDecimal.ZERO) == 0)
            return BigDecimal.ZERO;

        return aVal.divide(bVal, scale, BigDecimal.ROUND_HALF_UP);
    }
    /***
     * BigDecimal加法运算
     *
     * @param aVal
     * @param bVal
     * @param scale
     *            精度
     * @return
     */
    public static BigDecimal addBigDecimal(BigDecimal aVal, BigDecimal bVal, int scale) {

        if (aVal == null)
            aVal = BigDecimal.ZERO;

        if (bVal == null)
            bVal = BigDecimal.ZERO;

        return aVal.add(bVal).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }
}
