package nckd.jimin.jyyy.fi.plugin.form.travelcheck;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.ShowType;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.events.HyperLinkClickEvent;
import kd.bos.form.events.HyperLinkClickListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListFilterParameter;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.util.CollectionUtils;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.BusTravelCheckBillConstant;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.FinanceBillConfirmConstant;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.TravelFinanDraftConstant;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.TripReqBillConstant;
import nckd.jimin.jyyy.fi.common.util.CommonUtils;

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

public class BusTravelCommonEdit extends AbstractFormPlugin implements FinanceBillConfirmConstant, HyperLinkClickListener {


    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        EntryGrid bzEntryGrid = this.getControl(FinanceBillConfirmConstant.NCKD_BZ_ENTRY.ENTITYID);
        bzEntryGrid.addHyperClickListener(this);

        EntryGrid slEntryGrid = this.getControl(FinanceBillConfirmConstant.NCKD_SL_ENTRY.ENTITYID);
        slEntryGrid.addHyperClickListener(this);

        EntryGrid ycEntryGrid = this.getControl(FinanceBillConfirmConstant.NCKD_YC_ENTRY.ENTITYID);
        ycEntryGrid.addHyperClickListener(this);
    }



    @Override
    public void hyperLinkClick(HyperLinkClickEvent event) {
        String fieldName = event.getFieldName();
        int rowIndex = event.getRowIndex();
        DynamicObject dataEntity = getModel().getDataEntity();
        String name = dataEntity.getDynamicObjectType().getName();
        Long confirmBilId = ENTITYID.equals(name) ? dataEntity.getLong(ID) : dataEntity.getLong(BusTravelCheckBillConstant.NCKD_NCKD_SOURCEBILLID);
        // 补助 打开 商旅补助列表
        if (NCKD_BZ_ENTRY.NCKD_BZ_TRAVELER.equals(fieldName)) {
            DynamicObject entryInfo = getModel().getEntryRowEntity(NCKD_BZ_ENTRY.ENTITYID, rowIndex);

            DynamicObject traveler = entryInfo.getDynamicObject(NCKD_BZ_ENTRY.NCKD_BZ_TRAVELER);

            ListShowParameter showParameter = new ListShowParameter();
            showParameter.setBillFormId(TravelFinanDraftConstant.ENTITYID);
            showParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            showParameter.setShowFilter(false);
            showParameter.setCustomParam("showdetail",true);
            // 查询出差人下的单据
            ListFilterParameter listFilterParameter = showParameter.getListFilterParameter();
            List<QFilter> qFilters = listFilterParameter.getQFilters();
            qFilters.add(new QFilter(NCKD_BZ_CONFRMID, QCP.equals, confirmBilId));
            qFilters.add(new QFilter(CommonUtils.join(TravelFinanDraftConstant.NCKD_TRIPREQBILL, TripReqBillConstant.APPLIER), QCP.equals, traveler.getLong(ID)));
            this.getView().showForm(showParameter);
        }

        // 商旅费用 打开 全部结算单列表界面
        if (NCKD_SL_ENTRY.NCKD_SL_TRAVELER.equals(fieldName)) {
            DynamicObject entryInfo = getModel().getEntryRowEntity(NCKD_SL_ENTRY.ENTITYID, rowIndex);

            DynamicObject traveler = entryInfo.getDynamicObject(NCKD_SL_ENTRY.NCKD_SL_TRAVELER);
            String period = dataEntity.getString(NCKD_CHECKMONTH);
            Date periodStartDate = CommonUtils.parseDate(period);
            Date periodEndDate = CommonUtils.getFirstDayOfNextMonth(periodStartDate);

            DynamicObjectCollection checkingBillCol = QueryServiceHelper.query("er_checkingbill", "billnum", new QFilter[]{
                    new QFilter("periodstartdate", QCP.large_equals, periodStartDate),
                    new QFilter("periodenddate", QCP.less_than, periodEndDate),
                    new QFilter("operationtype", QFilter.in, Arrays.asList("1","2","4","5","6")),
            });
            if(CollectionUtils.isEmpty(checkingBillCol)){
                return;
            }
            List<String> billNumList = checkingBillCol.stream().map(r -> r.getString("billnum")).collect(Collectors.toList());
            DynamicObjectCollection slOrderDataCol = QueryServiceHelper.query("er_allcheckingbill",
                    "id,oabillnum,server,sourcebookedid.id applierId,totalamount,operationtype,ordernum", new QFilter[]{
                            new QFilter("checkingbillnum", QCP.in, billNumList),
                            new QFilter("sourcebookedid", QCP.equals, traveler.getLong(ID))
                    });
            List<Long> idList = slOrderDataCol.stream().map(r -> r.getLong("id")).collect(Collectors.toList());

            ListShowParameter showParameter = new ListShowParameter();
            showParameter.setBillFormId("er_allcheckingbill");
            showParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            showParameter.setShowFilter(false);

            // 查询出差人下的单据
            ListFilterParameter listFilterParameter = showParameter.getListFilterParameter();
            List<QFilter> qFilters = listFilterParameter.getQFilters();
            qFilters.add(new QFilter("id", QCP.in, idList));
            this.getView().showForm(showParameter);
        }

        // 用车 打开 用车结算单列表界面
        if (NCKD_YC_ENTRY.NCKD_YC_TRAVELER.equals(fieldName)) {
            DynamicObject entryInfo = getModel().getEntryRowEntity(NCKD_YC_ENTRY.ENTITYID, rowIndex);

            DynamicObject traveler = entryInfo.getDynamicObject(NCKD_YC_ENTRY.NCKD_YC_TRAVELER);

            String period = dataEntity.getString(NCKD_CHECKMONTH);
            Date periodStartDate = CommonUtils.parseDate(period);
            Date periodEndDate = CommonUtils.getFirstDayOfNextMonth(periodStartDate);
            DynamicObjectCollection checkingBillCol = QueryServiceHelper.query("er_checkingbill", "billnum", new QFilter[]{
                    new QFilter("periodstartdate", QCP.large_equals, periodStartDate),
                    new QFilter("periodenddate", QCP.less_than, periodEndDate),
                    new QFilter("operationtype", QFilter.equals, "3"),
            });
            List<String> billNumList = checkingBillCol.stream().map(r -> r.getString("billnum")).collect(Collectors.toList());
            // 查询用车结算单
            DynamicObjectCollection vehicleCheckCol = QueryServiceHelper.query("er_vehiclecheckingbill",
                    "id,oabillnum,server,sourcebookedid.id applierId,totalamount,vehicletype", new QFilter[]{
                            new QFilter("checkingbillnum", QCP.in, billNumList),
                            new QFilter("sourcebookedid", QCP.equals, traveler.getLong(ID))
                    });

            List<Long> idList = vehicleCheckCol.stream().map(r -> r.getLong("id")).collect(Collectors.toList());

            ListShowParameter showParameter = new ListShowParameter();
            showParameter.setBillFormId("er_vehiclecheckingbill");
            showParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            showParameter.setShowFilter(false);
            // 查询出差人下的单据
            ListFilterParameter listFilterParameter = showParameter.getListFilterParameter();
            List<QFilter> qFilters = listFilterParameter.getQFilters();
            qFilters.add(new QFilter("id", QCP.in, idList));
            this.getView().showForm(showParameter);
        }
    }
}
