package yem.wmc.ydn.formplugin.dynamic;

import kd.bos.algo.DataSet;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.IFormView;
import kd.bos.form.chart.ItemValue;
import kd.bos.form.chart.PieChart;
import kd.bos.form.chart.PieSeries;
import kd.bos.form.chart.RoseType;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;
import yem.base.common.utils.DateUtil;

import java.util.*;

/**
 * 首页中部TFM饼图表单插件
 *  @author  lib
 */
public class CenterHomeTFM extends AbstractBillPlugIn implements Plugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        String className = this.getClass().getName();
        IFormView view = this.getView();
        setData(className,view,"TFM");
    }

    public void setData(String className, IFormView view,String param){
        Date yearFirst = DateUtil.getYearFirst(new Date());
        Date yearLast = DateUtil.getYearLast(new Date());
        // 查询KFM数据
        DataSet data = ORM.create().queryDataSet(className,"yem_ca_homedate","yem_needtype,yem_qty",new QFilter[]{
                new QFilter("yem_diggings", QCP.equals,param),
                new QFilter("yem_date",QCP.large_equals,yearFirst),
                new QFilter("yem_date",QCP.less_equals,yearLast)});
        data = data.groupBy(new String[]{"yem_needtype"}).sum("yem_qty").finish();
        DynamicObjectCollection result = ORM.create().toPlainDynamicObjectCollection(data);
        String[] str = getRandColor(result);
        // 构建饼图
        PieChart pieChart = this.getControl("yem_piechartap");
        pieChart.clearData();
        pieChart.setShowTooltip(true);
        PieSeries series = pieChart
                .createPieSeries(param+"需求占比"); // 创建饼图系列
        series.setRadius("0", "100"); // 设置饼图内外半径
        List<ItemValue> dataList = new ArrayList<>();
        for(DynamicObject obj:result){
            String needtype = obj.getString("yem_needtype");
            String msg = needtype.equals("A") ?"运营类需求":"项目类需求";
            String color = needtype.equals("A") ?"#FFC107":"#00A0E9";
            int i = result.indexOf(obj);
            ItemValue itemValues = new ItemValue(msg,obj.getBigDecimal("yem_qty"),color);
            dataList.add(itemValues);
        }
        series.setData(dataList.toArray(new ItemValue[0]));
//        series.setRoseType(RoseType.radius);
    }


    /**
     * 随机生成颜色
     */
    public static String[] getRandColor(DynamicObjectCollection 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;
    }
}