package yem.wmc.ca.chart;

import com.alibaba.druid.util.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
//import kd.ssc.task.formplugin.smartcs.AreaInitListPlugin;

import java.text.SimpleDateFormat;
import java.util.*;

public class Statisstics extends AbstractBillPlugIn {
    @Override
    public void afterCreateNewData(EventObject e) {
        //系统当前时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(new Date());
        int years=startCal.get(Calendar.YEAR);
        int month=startCal.get(Calendar.MONTH);
        int datE=startCal.get(Calendar.DATE);
        //系统当月第一天
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = formatter.format(calendar.getTime());
        this.getModel().setValue("daterangefield_startdate", firstDay);
        this.getModel().setValue("daterangefield_enddate", date);
        DynamicObjectCollection dy = QueryServiceHelper.query("yem_ca_cusdec", "null", null);
        int size = dy.size();
        this.getModel().setValue("yem_blueinvoicetax", size);
        Calendar endCal = Calendar.getInstance();
            startCal.set(years, month, 1, 0, 0, 0);
            endCal.set(Calendar.YEAR, years);
            endCal.set(Calendar.MONTH, month+1);
            endCal.set(Calendar.DATE,0);
            endCal.set(Calendar.HOUR, 23);
            endCal.set(Calendar.MINUTE, 59);
            endCal.set(Calendar.SECOND, 59);
        List<QFilter> list = new ArrayList<>();
            list.add(new QFilter("yem_fddatets",QCP.large_equals,startCal.getTime()));
            list.add(new QFilter("yem_fddatets",QCP.less_equals,endCal.getTime()));
        DynamicObjectCollection dym = QueryServiceHelper.query("yem_ca_cusdec", "yem_fddatets", list.toArray(new QFilter[0]));
        int size1= dym.size();
        this.getModel().setValue("yem_blueinvoiceamount",size1);
        Calendar cale = Calendar.getInstance();
        Calendar calen = Calendar.getInstance();
        cale.set(years, month, datE, 0, 0, 0);
        calen.set(Calendar.YEAR, years);
        calen.set(Calendar.MONTH, month);
        calen.set(Calendar.DATE,datE);
        calen.set(Calendar.HOUR, 23);
        calen.set(Calendar.MINUTE, 59);
        calen.set(Calendar.SECOND, 59);
        List<QFilter> cifList = new ArrayList<QFilter>();
        cifList.add(new QFilter("yem_fddatets",QCP.large_equals,cale.getTime()));
        cifList.add(new QFilter("yem_fddatets",QCP.less_equals,calen.getTime()));
        DynamicObjectCollection dynam = QueryServiceHelper.query("yem_ca_cusdec", "yem_fddatets", cifList.toArray(new QFilter[0]));
        int size2=dynam.size();
        this.getModel().setValue("yem_blueinvoicenum",size2);
    }
    @Override
    public void propertyChanged(PropertyChangedArgs e){
        String propertyName = e.getProperty().getName();
        if(propertyName.equals("yem_invoicetype")||propertyName.equals("daterangefield_startdate")||propertyName.equals("daterangefield_enddate")){
            Date startdate =(Date)this.getModel().getValue("daterangefield_startdate");
            Date enddate =(Date)this.getModel().getValue("daterangefield_enddate");
            Object receipt=this.getModel().getValue("yem_invoicetype");
            if(receipt.equals("026")){
                Calendar startCal = Calendar.getInstance();
                startCal.setTime(new Date());
                int years=startCal.get(Calendar.YEAR);
                int month=startCal.get(Calendar.MONTH);
                int datE=startCal.get(Calendar.DATE);
                Calendar cale = Calendar.getInstance();
                Calendar calen = Calendar.getInstance();
                cale.set(years, month, datE, 0, 0, 0);
                calen.set(Calendar.YEAR, years);
                calen.set(Calendar.MONTH, month);
                calen.set(Calendar.DATE,datE);
                calen.set(Calendar.HOUR, 23);
                calen.set(Calendar.MINUTE, 59);
                calen.set(Calendar.SECOND, 59);
                String rece="出口报关单";
                List<QFilter> list = new ArrayList<QFilter>();
                list.add(new QFilter("yem_billtypefield.name",QCP.equals,rece));
                list.add(new QFilter("yem_fddatets",QCP.large_equals,cale.getTime()));
                list.add(new QFilter("yem_fddatets",QCP.less_equals,calen.getTime()));
                DynamicObjectCollection dynam = QueryServiceHelper.query("yem_ca_cusdec", "yem_fddatets,yem_billtypefield.name", list.toArray(new QFilter[0]));
                this.getModel().setValue("yem_blueinvoicenum",dynam.size());
                List<QFilter> qfilist = new ArrayList<QFilter>();
                qfilist.add(new QFilter("yem_billtypefield.name",QCP.equals,rece));
                DynamicObjectCollection dy = QueryServiceHelper.query("yem_ca_cusdec", "yem_billtypefield", qfilist.toArray(new QFilter[0]));
                this.getModel().setValue("yem_blueinvoicetax",dy.size());
                List<QFilter> qflist = new ArrayList<>();
                qflist.add(new QFilter("yem_billtypefield.name",QCP.equals,rece));
                qflist.add(new QFilter("yem_fddatets",QCP.large_equals,startdate));
                qflist.add(new QFilter("yem_fddatets",QCP.less_equals,enddate));
                DynamicObjectCollection dym = QueryServiceHelper.query("yem_ca_cusdec", "yem_fddatets", qflist.toArray(new QFilter[0]));
                int size1= dym.size();
                this.getModel().setValue("yem_blueinvoiceamount",size1);
            }else if(receipt.equals("007")){
                String rece="进口报关单";
                Calendar startCal = Calendar.getInstance();
                startCal.setTime(new Date());
                int years=startCal.get(Calendar.YEAR);
                int month=startCal.get(Calendar.MONTH);
                int datE=startCal.get(Calendar.DATE);
                Calendar cale = Calendar.getInstance();
                Calendar calen = Calendar.getInstance();
                cale.set(years, month, datE, 0, 0, 0);
                calen.set(Calendar.YEAR, years);
                calen.set(Calendar.MONTH, month);
                calen.set(Calendar.DATE,datE);
                calen.set(Calendar.HOUR, 23);
                calen.set(Calendar.MINUTE, 59);
                calen.set(Calendar.SECOND, 59);
                List<QFilter> list = new ArrayList<QFilter>();
                list.add(new QFilter("yem_billtypefield.name",QCP.equals,rece));
                list.add(new QFilter("yem_fddatets",QCP.large_equals,cale.getTime()));
                list.add(new QFilter("yem_fddatets",QCP.less_equals,calen.getTime()));
                DynamicObjectCollection dynam = QueryServiceHelper.query("yem_ca_cusdec", "yem_fddatets,yem_billtypefield.name", list.toArray(new QFilter[0]));
                this.getModel().setValue("yem_blueinvoicenum",dynam.size());
                List<QFilter> qfilist = new ArrayList<QFilter>();
                qfilist.add(new QFilter("yem_billtypefield.name",QCP.equals,rece));
                DynamicObjectCollection dy = QueryServiceHelper.query("yem_ca_cusdec", "yem_billtypefield", qfilist.toArray(new QFilter[0]));
                this.getModel().setValue("yem_blueinvoicetax",dy.size());
                List<QFilter> qflist = new ArrayList<>();
                qflist.add(new QFilter("yem_billtypefield.name",QCP.equals,rece));
                qflist.add(new QFilter("yem_fddatets",QCP.large_equals,startdate));
                qflist.add(new QFilter("yem_fddatets",QCP.less_equals,enddate));
                DynamicObjectCollection dym = QueryServiceHelper.query("yem_ca_cusdec", "yem_fddatets", qflist.toArray(new QFilter[0]));
                int size1= dym.size();
                this.getModel().setValue("yem_blueinvoiceamount",size1);
            }else if(receipt.equals("004")){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date(System.currentTimeMillis());
                Calendar startCal = Calendar.getInstance();
                startCal.setTime(new Date());
                int years=startCal.get(Calendar.YEAR);
                int month=startCal.get(Calendar.MONTH);
                int datE=startCal.get(Calendar.DATE);
                //系统当月第一天
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(Calendar.MONTH, 0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                DynamicObjectCollection dy = QueryServiceHelper.query("yem_ca_cusdec", "null", null);
                int size = dy.size();
                this.getModel().setValue("yem_blueinvoicetax", size);
                List<QFilter> qflist = new ArrayList<>();
                qflist.add(new QFilter("yem_fddatets",QCP.large_equals,startdate));
                qflist.add(new QFilter("yem_fddatets",QCP.less_equals,enddate));
                DynamicObjectCollection dym = QueryServiceHelper.query("yem_ca_cusdec", "yem_fddatets", qflist.toArray(new QFilter[0]));
                int size1= dym.size();
                this.getModel().setValue("yem_blueinvoiceamount",size1);
                Calendar cale = Calendar.getInstance();
                Calendar calen = Calendar.getInstance();
                cale.set(years, month, datE, 0, 0, 0);
                calen.set(Calendar.YEAR, years);
                calen.set(Calendar.MONTH, month);
                calen.set(Calendar.DATE,datE);
                calen.set(Calendar.HOUR, 23);
                calen.set(Calendar.MINUTE, 59);
                calen.set(Calendar.SECOND, 59);
                List<QFilter> cifList = new ArrayList<QFilter>();
                cifList.add(new QFilter("yem_fddatets",QCP.large_equals,cale.getTime()));
                cifList.add(new QFilter("yem_fddatets",QCP.less_equals,calen.getTime()));
                DynamicObjectCollection dynam = QueryServiceHelper.query("yem_ca_cusdec", "yem_fddatets", cifList.toArray(new QFilter[0]));
                int size2=dynam.size();
                this.getModel().setValue("yem_blueinvoicenum",size2);
            }
        }
    }
}


