package com.yem.em.form;

import com.yem.em.utils.BaseDataFilterUtils;
import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IPageCache;
import kd.bos.form.chart.ItemValue;
import kd.bos.form.chart.Label;
import kd.bos.form.chart.PieChart;
import kd.bos.form.chart.PieSeries;
import kd.bos.form.chart.Position;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;


import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author zhouc
 * @date 2023/9/5 12:00
 * @className CartypepieEdit
 * @description 车型占比表单插件
 */
public class CartypepieEdit extends AbstractFormPlugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IDataModel model = this.getModel();
        ArrayList<QFilter> filters = new ArrayList<QFilter>();
        QFilter qFilter = BaseDataFilterUtils.getBaseDataFilter("yem_em_comlefttree", RequestContext.get().getOrgId());
        filters.add(qFilter);
        QFilter qFilter1 = new QFilter("enable", QCP.equals, "1");
        filters.add(qFilter1);
        archivesCommon(model, filters);
    }


    /**
     * @Date 2020年11月17日
     * @Description
     * @param model
     * @param
     * @param filters
     */
    private void archivesCommon(IDataModel model, ArrayList<QFilter> filters) {
        Map<String, Integer> vehiclenameMap = new HashMap<>();
        String selectFields = DynamicObjectUtil.getSelectfields("yem_em_comlefttree", false);
        DynamicObject[] billColl = BusinessDataServiceHelper.load("yem_em_comlefttree", selectFields,
                filters.toArray(new QFilter[filters.size()]));
        IPageCache pageCache = this.getView().getPageCache();
        pageCache.put("tpv_integer", "" + billColl.length);

        QFilter qFilters = new QFilter("enable", QCP.equals, "1");
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_equipment",
                "id,number,name,yem_brandmodel.name", new QFilter[]{qFilters});
        for(int i =0;i<data.size();i++){
            DynamicObject bill = data.get(i);
            String name = bill.getString("yem_brandmodel.name");
            if (vehiclenameMap.containsKey(name)) {
                vehiclenameMap.put(name, vehiclenameMap.get(name) + 1);
            } else {
                vehiclenameMap.put(name, 1);
            }

        }
        pieChar(model, vehiclenameMap);
    }
    /**
     * 构建饼图 车型占比
     *
     * @Date 2020年11月9日
     * @Description
     * @param model
     * @param dataMap
     */
    private void pieChar(IDataModel model, Map<String, Integer> dataMap) {
        PieChart pieChart = this.getControl("yem_piechartap");// 车型占比
        pieChart.clearData();
        pieChart.setShowTooltip(true);
        PieSeries series = pieChart
                .createPieSeries(ResManager.loadKDString("车型占比", "CarTypePieEdit_0", "yem_em_cartypepie")); // 创建饼图系列
        series.setRadius("60", "100"); // 设置饼图内外半径
        ItemValue[] itemValues01 = new ItemValue[dataMap.size()]; // 饼图数据 num表示元素个数
        String[] str = getRandColor(dataMap);
        // 设置每个元素的值 name：元素名称 value：元素值 color：元素颜色
        int i = 0;
        for (String key : dataMap.keySet()) {
            itemValues01[i] = new ItemValue(key, dataMap.get(key), str[i]);
            i++;
        }
        if (itemValues01.length == 0) {
            itemValues01 = new ItemValue[1];
            itemValues01[0] = new ItemValue(ResManager.loadKDString("无数据", "CarTypePieEdit_1", "yem_em_cartypepie"), 0,
                    "#cccccc");
        }
        series.setData(itemValues01); // 设置饼图数据

        String qty = this.getView().getPageCache().get("tpv_integer");
        PieSeries series2 = pieChart
                .createPieSeries(ResManager.loadKDString("车型占比", "CarTypePieEdit_0", "yem_em_cartypepie")); // 创建饼图系列
        series2.setRadius("60"); // 设置饼图内外半径
        ItemValue[] itemValues02 = new ItemValue[1]; // 饼图数据 num表示元素个数
        itemValues02[0] = new ItemValue("", Integer.valueOf(qty), "#ffffff");
        series2.setData(itemValues02); // 设置饼图数据
        Label label = new Label();
        label.setShow(true);
        label.setFontSize("12"); // 设置标签大小
        label.setFormatter(qty);
        // 设置标签格式
        label.setPosition(Position.inside); // 设置标签位置为inside
        label.setColor("#b2b2b2");
        series2.setLabel(label); // 设置饼图的标签
        pieChart.refresh();

    }

    /**
     *  随机生成颜色
     * @param dataMap
     * @return
     */
    public static String[] getRandColor(Map<String, Integer> dataMap) {
        String[] str = new String[dataMap.size()];
        String r, g, b;
        for (int i = 0; i < str.length; i++) {
            Random random = new Random();
            r = Integer.toHexString(random.nextInt(256)).toUpperCase();
            g = Integer.toHexString(random.nextInt(256)).toUpperCase();
            b = Integer.toHexString(random.nextInt(256)).toUpperCase();

            r = r.length() == 1 ? "0" + r : r;
            g = g.length() == 1 ? "0" + g : g;
            b = b.length() == 1 ? "0" + b : b;
            str[i] = "#" + r + g + b;
        }
        return str;
    }
}
