package com.yem.rf.salescommission;


import com.yem.wm.utils.RptUtil;
import com.yem.wm.utils.YEM;
import com.yem.wm.utils.groupfunc.GroupConcatFunction;
import com.yem.wm.utils.groupfunc.GroupConcatFunctionByComma;
import kd.bos.algo.*;
import kd.bos.algo.input.CollectionInput;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.report.AbstractReportColumn;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.FilterInfo;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.yem.rf.utils.RFUtils.formatDate;
import static com.yem.rf.utils.RFUtils.getBaseQFilter;
import static kd.fi.er.report.query.TripOrderRpt.algoKey;

/**
 * @author ljw
 * @date 2024/7/22 11:48
 * @description ShipmentsChedulePlugin
 */
public class ShipmentsChedulePlugin extends AbstractReportListDataPlugin {
    private static final String algoKey = SalesCommissionReportPlugin.class.getName();
    @Override
    public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable {
        DataSet resDataSet;
        DataSet rows = yemEsDeclaredocx(reportQueryParam);
        DataSet delivernotice = queryDeliverNotice(reportQueryParam);
        DataSet yemImTradeapp = yem_im_tradeapp(reportQueryParam);
        DataSet yemsDeclaredocx = yemsDeclaredocx(reportQueryParam);
        DataSet yemContainernumber = yemContainernumber(reportQueryParam);
        DataSet yemContainer = yemContainer(reportQueryParam);
        DataSet yem_es_delivernotice = yem_es_delivernotice(reportQueryParam);

        rows = rows.select(String.join(",", RptUtil.getDataSetFiledAlias(rows)) +
                ",case when  yem_bringbillno != '' THEN yem_bringbillno else yem_realbringbillno  end yem_ladingnumber");
        yemContainernumber = yemContainernumber.select(String.join(",", RptUtil.getDataSetFiledAlias(yemContainernumber)) +
                ",case when  yem_noticeno != '' THEN yem_noticeno else yem_housebillofladnum  end yem_ladingnumbers");
        //出口报关

        yemsDeclaredocx = yemsDeclaredocx
                .groupBy(new String[]{"yem_splitopbill", "yem_es_materialinfoid"})
                .finish();
        resDataSet = yemsDeclaredocx.leftJoin(rows)
                .on("yem_es_materialinfoid", "yem_es_declare_ecid")
                .select(RptUtil.getDataSetFiledAlias(yemsDeclaredocx), RptUtil.getDataSetFiledAlias(rows))
                .finish();
        //定仓通知
        resDataSet = resDataSet.leftJoin(yemContainernumber)
                .on("yem_ladingnumber", "yem_ladingnumbers")//提单号
                .select(RptUtil.getDataSetFiledAlias(resDataSet), RptUtil.getDataSetFiledAlias(yemContainernumber))
                .finish();
        resDataSet = resDataSet.leftJoin(yemContainer)
                .on("storagetransid", "storagetransids")
                .select(RptUtil.getDataSetFiledAlias(resDataSet), RptUtil.getDataSetFiledAlias(yemContainer))
                .finish();
        //发运明细
        resDataSet = resDataSet.leftJoin(delivernotice)
                .on("storagetransyemSourcebillid", "shippingdetailsid")//单据id  来源单据id
                .select(RptUtil.getDataSetFiledAlias(resDataSet), RptUtil.getDataSetFiledAlias(delivernotice))
                .finish();
        //销售出库
        resDataSet = resDataSet.leftJoin(yem_es_delivernotice)
                .on("shippingdetailsid", "delivernoticeyem_sourcebillid")//单据id  来源单据id
                .select(RptUtil.getDataSetFiledAlias(resDataSet), RptUtil.getDataSetFiledAlias(yem_es_delivernotice))
                .finish();
        //整机配件货款延付申请单
        resDataSet = resDataSet.leftJoin(yemImTradeapp)
                .on("shippingdetailsid", "tradeappyem_sourcebillid")//单据id  来源单据id
                .select(RptUtil.getDataSetFiledAlias(resDataSet), RptUtil.getDataSetFiledAlias(yemImTradeapp))
                .finish();
        DataSet selected = resDataSet.select(String.join(",", RptUtil.getDataSetFiledAlias(resDataSet)) +
                ",CASE WHEN DATEDIF(yem_lookoutdate,yem_datearrival,D) != -1  THEN DATEDIF(yem_lookoutdate,yem_datearrival,D) ELSE null END yem_flightnumberdays");


        FilterInfo filter = reportQueryParam.getFilter();
        String yemLading = filter.getString("yem_lading");
        String yemContract = filter.getString("yem_contract");
        DynamicObject yemClient = filter.getDynamicObject("yem_client");
        String yemShipping = filter.getString("yem_shipping");


        StringBuilder sb = new StringBuilder();
        sb.append("yem_customsdeclaration1 = 'A'");
        if (YEM.isNotEmpty(yemContract)) {
            sb.append("  AND  yem_contractnumber  like '").append("%").append(yemContract).append("%").append("'");
        }
        if (YEM.isNotEmpty(yemLading)) {
            sb.append("  AND  yem_ladingnumber  like '").append("%").append(yemLading).append("%").append("'");
        }
        if (YEM.isNotEmpty(yemClient)) {
            sb.append("  AND  yem_customername  = ").append("'"+yemClient.getString("name")+"'");
        }
        if (YEM.isNotEmpty(yemShipping)) {
            sb.append("  AND  yem_shipment  like  '").append("%").append(yemShipping).append("%").append("'");
        }
        DataSet where = selected.where(sb.toString());
        return where;
    }

    /**
     * 出口报关单
     */
    private DataSet yemsDeclaredocx(ReportQueryParam reportQueryParam) {
        FilterInfo filter = reportQueryParam.getFilter();
        QFilter qFilter = getBaseQFilter();
        Date start = filter.getDate("yem_Billofladingday");//提单日期-开始
        Date end = filter.getDate("yem_Lookoutdateawbday");//提单日期-结束
        qFilter.and("yem_sumopbill",QCP.equals,"B");
        if (YEM.isNotEmpty(start)) {
            qFilter.and("yem_tddate", ">=", start);
        }
        if (YEM.isNotEmpty(end)) {
            qFilter.and("yem_tddate", "<=", end);
        }
        List<String> selectfields = new ArrayList<>();
        selectfields.add("id yem_es_materialinfoid");
        selectfields.add("yem_es_declare_ec.yem_splitopbill yem_splitopbill");
        DataSet yemShippingdetails = ORM.create().queryDataSet(algoKey, "yem_es_declaredocx", String.join(",", selectfields), qFilter.toArray());
        return yemShippingdetails;
    }

    /**
     * 出口报关单
     */
    private DataSet yemEsDeclaredocx(ReportQueryParam reportQueryParam) {
        FilterInfo filter = reportQueryParam.getFilter();
        QFilter qFilter = getBaseQFilter();
        Date start = filter.getDate("yem_Billofladingday");//提单日期-开始
        Date end = filter.getDate("yem_Lookoutdateawbday");//提单日期-结束
        qFilter.and("yem_sumopbill",QCP.equals,"B");
        if (YEM.isNotEmpty(start)) {
            qFilter.and("yem_tddate", ">=", start);
        }
        if (YEM.isNotEmpty(end)) {
            qFilter.and("yem_tddate", "<=", end);
        }
        List<String> selectfields = new ArrayList<>();
        selectfields.add("yem_customsdeclaration1");
        selectfields.add("id yem_es_declare_ecid");
        selectfields.add("yem_bizdate");
        selectfields.add("billno");
        selectfields.add("yem_bringbillno yem_bringbillno");//提单号
        selectfields.add("yem_realbringbillno yem_realbringbillno");//提单号
        selectfields.add("TO_CHAR(yem_tddate, 'yyyy-MM-dd') yem_lookoutdate");//提单日期
        selectfields.add("TO_CHAR(yem_arrivaldate, 'yyyy-MM-dd') yem_datearrival");//到港日
        selectfields.add("TO_CHAR(yem_ddpdate, 'yyyy-MM-dd') yem_customsclearance");//清关提货日
        selectfields.add("yem_sourcebillid declaredocxyem_sourcebillid");
        DataSet yemShippingdetails = ORM.create().queryDataSet(algoKey, "yem_es_declaredocx", String.join(",", selectfields)
                , qFilter.toArray()).orderBy(new String[]{"yem_bizdate DESC"});
        return yemShippingdetails;
    }

    /**
     * 订舱通知单
     */
    private DataSet yemContainernumber(ReportQueryParam reportQueryParam) {
        FilterInfo filter = reportQueryParam.getFilter();
        QFilter qFilter = getBaseQFilter();
        List<String> selectfields = new ArrayList<>();
        selectfields.add("id storagetransid");
        selectfields.add("yem_noticeno yem_noticeno");//提单号/运单号
        selectfields.add("yem_es_materialinfo.yem_sourcebillno_d yem_sourcebillno_ds");//提单号/运单号
        selectfields.add("yem_housebillofladnum yem_housebillofladnum");//提单号/运单号
//        selectfields.add("yem_encasement.yem_encasenum yem_containernumber");//集装箱号
        selectfields.add("yem_docreview.fbasedataid.id yem_documenterss");//单证审核
        selectfields.add("yem_docreview.fbasedataid.name yem_documenter");//单证审核
        selectfields.add("yem_cfscompnay.name yem_forwardingcompanies");//货代公司
        selectfields.add("yem_sctcompnayname.name yem_shippingcompanies");//船公司
        selectfields.add("yem_sourcebillid storagetransyemSourcebillid");
        DataSet yemShippingdetails = ORM.create().queryDataSet(algoKey, "yem_es_storagetrans", String.join(",", selectfields), qFilter.toArray());
        List<String> selectfield = new ArrayList<>();
        selectfield.add("yem_mainladingno yem_mainladingno");//提单号/运单号
        selectfield.add("yem_im_encasement_ci.yem_encasenum yem_containernumbers");
        DataSet yemImEncasement = ORM.create().queryDataSet(algoKey, "yem_im_encasement", String.join(",", selectfield), qFilter.toArray());
        yemImEncasement = yemImEncasement.groupBy(new String[]{"yem_mainladingno"}).agg(new GroupConcatFunction(),"yem_containernumbers","yem_containernumber").finish();
        yemShippingdetails = yemShippingdetails.leftJoin(yemImEncasement)
                .on("yem_noticeno","yem_mainladingno")
                .select(RptUtil.getDataSetFiledAlias(yemShippingdetails),RptUtil.getDataSetFiledAlias(yemImEncasement))
                .finish();
        return yemShippingdetails;
    }
//    private DataSet yemEsStoragetrans(ReportQueryParam reportQueryParam) {
//        QFilter qFilter = getBaseQFilter();
//        List<String> selectfields = new ArrayList<>();
//        selectfields.add("id storagetransid");
//        selectfields.add("yem_docreview.fbasedataid.id yem_documenterss");//单证审核
//        selectfields.add("yem_docreview.fbasedataid.name yem_documenter");//单证审核
//        selectfields.add("yem_cfscompnay.name yem_forwardingcompanies");//货代公司
//        selectfields.add("yem_sctcompnayname.name yem_shippingcompanies");//船公司
//        selectfields.add("yem_sourcebillid storagetransyemSourcebillid");
//        DataSet yemShippingdetails = ORM.create().queryDataSet(algoKey, "yem_es_storagetrans", String.join(",", selectfields), qFilter.toArray());
//        yemShippingdetails = yemShippingdetails.groupBy(new String[]{"storagetransid"}).agg(new GroupConcatFunction(),"yem_containernumbers","yem_containernumber").finish();
//        return yemShippingdetails;
//    }

    /**
     *
     * @param reportQueryParam
     * @return
     */
    private DataSet yemContainer(ReportQueryParam reportQueryParam) {
        QFilter qFilter = getBaseQFilter();
        List<String> selectfields = new ArrayList<>();
        selectfields.add("id storagetransids");
        selectfields.add("yem_es_materialinfo.yem_sourcebillno_d yem_sourcebillno_d");//提单号/运单号
        DataSet yemShippingdetails = ORM.create().queryDataSet(algoKey, "yem_es_storagetrans", String.join(",", selectfields), qFilter.toArray());
        yemShippingdetails = yemShippingdetails
                .groupBy(new String[]{"storagetransids"}).agg(new GroupConcatFunctionByComma(),"yem_sourcebillno_d","yem_shipment")
                .finish();
        return yemShippingdetails;
    }

    /**
     * 发运明细单
     *
     * @param reportQueryParam
     * @return
     */
    private DataSet queryDeliverNotice(ReportQueryParam reportQueryParam) {
        QFilter qFilter = getBaseQFilter();
        List<String> selectfields = new ArrayList<>();
        selectfields.add("id shippingdetailsid");
        selectfields.add("yem_saleorderno yem_contractnumber");//外销合同号
        selectfields.add("yem_issinosurecon yem_insurancecontract");//是否信保合同
        selectfields.add("yem_operator.name yem_salesman");//销售员
        selectfields.add("yem_customer.name yem_customername");//客户名称
        selectfields.add("yem_customer.group.name yem_customercountry");//客户所属国家
        selectfields.add("yem_clientcountry.name yem_tradingcountry");//贸易国别
        selectfields.add("yem_sourcebillid shippingdetailsyem_sourcebillid");
        return ORM.create().queryDataSet(algoKey, "yem_shippingdetails", String.join(",", selectfields), qFilter.toArray());
    }

    /**
     * 整机配件货款延付申请单
     *
     * @param reportQueryParam
     * @return
     */
    private DataSet yem_im_tradeapp(ReportQueryParam reportQueryParam) {
        QFilter qFilter = getBaseQFilter();
        List<String> selectfields = new ArrayList<>();
        selectfields.add("id tradeappid");
        selectfields.add("billno yem_money");//款项
        selectfields.add("yem_sourcebillid tradeappyem_sourcebillid");//来源单据ID
        DataSet yemShippingdetails = ORM.create().queryDataSet(algoKey, "yem_im_tradeapp", String.join(",", selectfields), qFilter.toArray());
        return yemShippingdetails;
    }

    /**
     * 销售出库
     */
    private DataSet yem_es_delivernotice(ReportQueryParam reportQueryParam) {
        QFilter qFilter = getBaseQFilter();
        List<String> selectfields = new ArrayList<>();
        selectfields.add("id delivernoticeid");
        selectfields.add("TO_CHAR(yem_bizdate, 'yyyy-MM-dd') yem_datebusiness");//发货日期
        selectfields.add("Year(yem_bizdate) yem_annual");//发货日期
        selectfields.add("yem_sourcebillid delivernoticeyem_sourcebillid");
        DataSet yemShippingdetails = ORM.create().queryDataSet(algoKey, "yem_es_delivernotice", String.join(",", selectfields), qFilter.toArray());
        return yemShippingdetails;
    }
}