package com.yem.wm.fm.gathering.list;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.chart.ItemValue;
import kd.bos.form.chart.PieChart;
import kd.bos.form.chart.PieSeries;
import kd.bos.form.control.Label;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.permission.PermissionServiceHelper;
import kd.fi.cas.util.EmptyUtil;

import java.util.Arrays;
import java.util.EventObject;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author power
 * @ClassName
 * @description: 服务表单插件
 * @date 2023/9/1
 */

public class ClaimCentersListPlugin extends AbstractListPlugin{
    private List<QFilter> drawPieFilters;
    @Override
    public void setFilter(SetFilterEvent e) {
        List<QFilter> qFilters = e.getQFilters();
        qFilters.add(e.getMainOrgQFilter());
        this.drawPieFilters = qFilters;

    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);

        assignment(this.drawPieFilters);
//        this.assignment(drawPieFilters);
    }


    public void assignment(List<QFilter> qFilters){
        if (!qFilters.isEmpty()) {
            String selectedOrgIdList = this.getPageCache().get("selectedOrgIdList");
            if (EmptyUtil.isNotEmpty(selectedOrgIdList)) {
                String[] splits = selectedOrgIdList.split(",");
                List<Long> selectedOrgIdLongList = (List) Arrays.stream(splits).map((o) -> {
                    return Long.parseLong(o.trim());
                }).collect(Collectors.toList());
                qFilters.add(new QFilter("org", "in", selectedOrgIdLongList));
            }
            else {
                qFilters.add(new QFilter("org", "in", PermissionServiceHelper.getUserOrgs(Long.parseLong(RequestContext.get().getUserId()))));
            }
            DynamicObjectCollection claimcenters = QueryServiceHelper.query("yem_gathering",
                    "billno,billstatus,yem_financeaffirm,yem_claimstate", (QFilter[])qFilters.toArray(new QFilter[0]));
            Label claimtab = (Label)this.getControl("yem_claimtab");
            Label financeturetab = (Label)this.getControl("yem_financeturetab");

            PieChart piechartap = (PieChart)this.getControl("yem_piechartap");
            PieChart piechartaps = (PieChart)this.getControl("yem_piechartaps");
            piechartap.clearData();
            piechartaps.clearData();
            int i=0;
            int j=0;
            int unconfirmed =0;
            int confirmed =0;
            int Yetclaimed =0;
            int alotclaimed =0;
            int claimed =0;

            piechartap.setShowTooltip(true);
            piechartap.setShowLegend(true);

            piechartaps.setShowTooltip(true);
            piechartaps.setShowLegend(true);
            PieSeries serie = piechartap.createPieSeries("认领状态");
            piechartap.setLegendPropValue("left", "60%");
            piechartap.setLegendPropValue("top", "15%");

            PieSeries series = piechartaps.createPieSeries("财务状态");
            piechartaps.setLegendPropValue("right", "60%");
            piechartaps.setLegendPropValue("top", "15%");
            if (EmptyUtil.isNoEmpty(claimcenters)) {


                for (DynamicObject claimcenter:claimcenters){
                    String financeaffirm = claimcenter.getString("yem_financeaffirm");
                    String claimstate = claimcenter.getString("yem_claimstate");
                   if (financeaffirm!=null&&"A".equals(financeaffirm)){
                       i=i+1;
                       unconfirmed=unconfirmed+1;
                   }
                    if (financeaffirm!=null&&"B".equals(financeaffirm)){
                        confirmed=confirmed+1;
                    }
                    if (claimstate!=null&&"A".equals(claimstate)){
                        j=j+1;
                        Yetclaimed=Yetclaimed+1;
                    }
                    if (claimstate!=null&&"B".equals(claimstate)){
                        j=j+1;
                        alotclaimed=alotclaimed+1;
                    }
                    if (claimstate!=null&&"C".equals(claimstate)){
                        claimed=claimed+1;
                    }
                }
                claimtab.setText(String.valueOf(j));
                financeturetab.setText(String.valueOf(i));


            }
            else {
                claimtab.setText(String.valueOf(0));
                financeturetab.setText(String.valueOf(0));

            }

            ItemValue[] datas = null;
            datas = new ItemValue[] {

                    new ItemValue("财务未确认",unconfirmed),
                    new ItemValue("财务确认",confirmed),
            };
            series.setData(datas);
//            //饼图状态
//            series.setRoseType(RoseType.radius);

            ItemValue[] data = null;
            data = new ItemValue[] {
                    new ItemValue("未认领",Yetclaimed),
                    new ItemValue("部分认领",alotclaimed),
                    new ItemValue("已认领",claimed)
            };
            serie.setData(data);
//            //饼图状态
//            serie.setRoseType(RoseType.radius);
            piechartap.refresh();
            piechartaps.refresh();

        }
    }

}
