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

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.org.utils.DynamicObjectUtils;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
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.TripReqBillConstant;

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

public class BusTravelCheckSecretaryEdit extends AbstractBillPlugIn implements FinanceBillConfirmConstant {
    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        loadDataFromConfirmBill();

    }

    /**
     * 从财务确认单上带出数据
     */
    protected void loadDataFromConfirmBill(){
        long currUserId = RequestContext.get().getCurrUserId();
        DynamicObject dataEntity = getModel().getDataEntity(true);
        DynamicObjectCollection bzEntryCol = dataEntity.getDynamicObjectCollection(NCKD_BZ_ENTRY.ENTITYID);
        bzEntryCol.removeIf(bzEntry -> bzEntry.getDynamicObject(NCKD_BZ_ENTRY.NCKD_BZ_DEPSECRETARY).getLong(ID) != currUserId);

        // 部门秘书补助关联的出差人信息
        List<Object> bzApplierIdList = bzEntryCol.stream()
                .filter(bzEntry -> bzEntry.getDynamicObject(NCKD_BZ_ENTRY.NCKD_BZ_TRAVELER) != null && bzEntry.getDynamicObject(NCKD_BZ_ENTRY.NCKD_BZ_DEPSECRETARY) != null)
                .filter(bzEntry -> bzEntry.getDynamicObject(NCKD_BZ_ENTRY.NCKD_BZ_DEPSECRETARY).getLong(ID) == currUserId)
                .map(bzEntry -> bzEntry.getDynamicObject(NCKD_BZ_ENTRY.NCKD_BZ_TRAVELER).getPkValue())
                .collect(Collectors.toList());

        DynamicObjectCollection supEntryCol = dataEntity.getDynamicObjectCollection(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.ENTITYID);
        supEntryCol.removeIf(supEntry -> supEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL) != null
                && supEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL).getDynamicObject(TripReqBillConstant.APPLIER) != null
                && !bzApplierIdList.contains(supEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL).getDynamicObject(TripReqBillConstant.APPLIER).getLong(ID)));

        DynamicObjectCollection slEntryCol = dataEntity.getDynamicObjectCollection(NCKD_SL_ENTRY.ENTITYID);
        slEntryCol.removeIf(slEntry -> slEntry.getDynamicObject(NCKD_SL_ENTRY.NCKD_SL_DEPSECRETARY).getLong( ID) != currUserId);

        DynamicObjectCollection ycEntryCol = dataEntity.getDynamicObjectCollection(NCKD_YC_ENTRY.ENTITYID);
        ycEntryCol.removeIf(ycEntry -> ycEntry.getDynamicObject(NCKD_YC_ENTRY.NCKD_YC_DEPSECRETARY).getLong( ID) != currUserId);

        DynamicObjectCollection cbEntryCol = dataEntity.getDynamicObjectCollection(NCKD_CB_ENTRY.ENTITYID);
        cbEntryCol.removeIf(cbEntry -> cbEntry.getDynamicObject(NCKD_CB_ENTRY.NCKD_CB_DEPSECRETARY).getLong( ID) != currUserId);

        DynamicObjectCollection tgEntryCol = dataEntity.getDynamicObjectCollection(NCKD_TG_ENTRY.ENTITYID);
        tgEntryCol.removeIf(tgEntry -> tgEntry.getDynamicObject(NCKD_TG_ENTRY.NCKD_TG_DEPSECRETARY).getLong( ID) != currUserId);

        DynamicObjectCollection zsEntryCol = dataEntity.getDynamicObjectCollection(NCKD_ZS_ENTRY.ENTITYID);
        zsEntryCol.removeIf(zsEntry -> zsEntry.getDynamicObject(NCKD_ZS_ENTRY.NCKD_ZS_DEPSECRETARY).getLong( ID) != currUserId);

    }
    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate operate = (FormOperate)args.getSource();
        String operateKey = operate.getOperateKey();
        // 财务会审保存
        if("approve_save".equals(operateKey)){
            // 反写对账单
            writeBackCheckBill();
            // 反写确认单
            writeBackConfirmBill();
        }
    }

    protected void writeBackCheckBill(){
        DynamicObject dataEntity = getModel().getDataEntity(true);
        Object billId = dataEntity.getPkValue();
        DynamicObject checkBillInfo = BusinessDataServiceHelper.loadSingle(billId, BusTravelCheckBillConstant.ENTITYID);
        DynamicObjectCollection bzEntryCol = dataEntity.getDynamicObjectCollection(NCKD_BZ_ENTRY.ENTITYID);
        long currUserId = RequestContext.get().getCurrUserId();
        Map<Long, DynamicObject> bzSrcEntryMap = bzEntryCol.stream().collect(Collectors.toMap(bzEntry -> bzEntry.getLong(ID), bzEntry -> bzEntry));
        DynamicObjectCollection checkBzEntryCol = checkBillInfo.getDynamicObjectCollection(NCKD_BZ_ENTRY.ENTITYID);
        for(DynamicObject checkBzEntry : checkBzEntryCol){
            if(bzSrcEntryMap.containsKey(checkBzEntry.getLong(ID))){
                DynamicObject bzEntryInfo = bzSrcEntryMap.get(checkBzEntry.getLong(ID));
                DynamicObjectUtils.copy(bzEntryInfo, checkBzEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        // 通过出差人 来创建
        DynamicObjectCollection supEntryCol = dataEntity.getDynamicObjectCollection(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.ENTITYID);
        DynamicObjectCollection checkSupEntryCol = checkBillInfo.getDynamicObjectCollection(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.ENTITYID);

        // 部门秘书补助关联的出差人信息
        List<Object> bzApplierIdList = bzEntryCol.stream()
                .map(bzEntry -> bzEntry.getDynamicObject(NCKD_BZ_ENTRY.NCKD_BZ_TRAVELER).getPkValue())
                .collect(Collectors.toList());

        checkSupEntryCol.removeIf(checkSupEntry -> checkSupEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL) != null
                && checkSupEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL).getDynamicObject(TripReqBillConstant.APPLIER) != null
                && bzApplierIdList.contains(checkSupEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL).getDynamicObject(TripReqBillConstant.APPLIER).getLong(ID)));

        for(DynamicObject supEntry : supEntryCol){
            DynamicObject checkSupEntry = checkSupEntryCol.addNew();
            DynamicObjectUtils.copy(supEntry, checkSupEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
        }

        DynamicObjectCollection slSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_SL_ENTRY.ENTITYID);
        Map<Long, DynamicObject> slSrcEntryMap = slSrcEntryCol.stream().collect(Collectors.toMap(slEntry -> slEntry.getLong(ID), slEntry -> slEntry));
        DynamicObjectCollection checkSlEntryCol = checkBillInfo.getDynamicObjectCollection(NCKD_SL_ENTRY.ENTITYID);
        for(DynamicObject checkSlEntry : checkSlEntryCol){
            if(slSrcEntryMap.containsKey(checkSlEntry.getLong(ID))){
                DynamicObject slEntryInfo = slSrcEntryMap.get(checkSlEntry.getPkValue());
                DynamicObjectUtils.copy(slEntryInfo, checkSlEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        DynamicObjectCollection ycSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_YC_ENTRY.ENTITYID);
        Map<Long, DynamicObject> ycSrcEntryMap = ycSrcEntryCol.stream().collect(Collectors.toMap(ycEntry -> ycEntry.getLong(ID), ycEntry -> ycEntry));
        DynamicObjectCollection checkYcEntryCol = checkBillInfo.getDynamicObjectCollection(NCKD_YC_ENTRY.ENTITYID);
        for(DynamicObject checkYcEntry : checkYcEntryCol){
            if(ycSrcEntryMap.containsKey(checkYcEntry.getLong(ID))){
                DynamicObject ycEntryInfo = ycSrcEntryMap.get(checkYcEntry.getLong(ID));
                DynamicObjectUtils.copy(ycEntryInfo, checkYcEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }


        DynamicObjectCollection cbSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_CB_ENTRY.ENTITYID);
        Map<Long, DynamicObject> cbSrcEntryMap = cbSrcEntryCol.stream().collect(Collectors.toMap(cbEntry -> cbEntry.getLong(ID), cbEntry -> cbEntry));
        DynamicObjectCollection checkCbEntryCol = checkBillInfo.getDynamicObjectCollection(NCKD_CB_ENTRY.ENTITYID);
        for(DynamicObject checkCbEntry : checkCbEntryCol){
            if(cbSrcEntryMap.containsKey(checkCbEntry.getLong(ID))){
                DynamicObject cbEntryInfo = cbSrcEntryMap.get(checkCbEntry.getLong(ID));
                DynamicObjectUtils.copy(cbEntryInfo, checkCbEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        DynamicObjectCollection tgSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_TG_ENTRY.ENTITYID);
        Map<Long, DynamicObject> tgSrcEntryMap = tgSrcEntryCol.stream().collect(Collectors.toMap(tgEntry -> tgEntry.getLong(ID), tgEntry -> tgEntry));
        DynamicObjectCollection checkTgEntryCol = checkBillInfo.getDynamicObjectCollection(NCKD_TG_ENTRY.ENTITYID);
        for(DynamicObject checkTgEntry : checkTgEntryCol){
            if(tgSrcEntryMap.containsKey(checkTgEntry.getLong(ID))){
                DynamicObject tgEntryInfo = tgSrcEntryMap.get(checkTgEntry.getLong(ID));
                DynamicObjectUtils.copy(tgEntryInfo, checkTgEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        DynamicObjectCollection zsSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_ZS_ENTRY.ENTITYID);
        Map<Long, DynamicObject> zsSrcEntryMap = zsSrcEntryCol.stream().collect(Collectors.toMap(zsEntry -> zsEntry.getLong(ID), zsEntry -> zsEntry));
        DynamicObjectCollection checkZsEntryCol = checkBillInfo.getDynamicObjectCollection(NCKD_ZS_ENTRY.ENTITYID);
        for(DynamicObject checkZsEntry : checkZsEntryCol){
            if(zsSrcEntryMap.containsKey(checkZsEntry.getLong(ID))){
                DynamicObject zsEntryInfo = zsSrcEntryMap.get(checkZsEntry.getLong(ID));
                DynamicObjectUtils.copy(zsEntryInfo, checkZsEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        SaveServiceHelper.save(new DynamicObject[]{ checkBillInfo});
    }
    protected void writeBackConfirmBill(){
        DynamicObject dataEntity = getModel().getDataEntity(true);
        long checkBillId = dataEntity.getLong(BusTravelCheckBillConstant.ID);
        long confirmBillId = dataEntity.getLong(BusTravelCheckBillConstant.NCKD_NCKD_SOURCEBILLID);
        DynamicObject confirmBillInfo = BusinessDataServiceHelper.loadSingle(confirmBillId, FinanceBillConfirmConstant.ENTITYID);
        DynamicObjectCollection bzEntryCol = dataEntity.getDynamicObjectCollection(NCKD_BZ_ENTRY.ENTITYID);
        long currUserId = RequestContext.get().getCurrUserId();
        Map<Long, DynamicObject> bzSrcEntryMap = bzEntryCol.stream().collect(Collectors.toMap(bzEntry -> bzEntry.getLong(NCKD_BZ_ENTRY.NCKD_BZ_SRCENTRYID), bzEntry -> bzEntry));
        DynamicObjectCollection confirmBzEntryCol = confirmBillInfo.getDynamicObjectCollection(NCKD_BZ_ENTRY.ENTITYID);
        for(DynamicObject confirmBzEntry : confirmBzEntryCol){
            if(bzSrcEntryMap.containsKey(confirmBzEntry.getLong(ID))){
                DynamicObject bzEntryInfo = bzSrcEntryMap.get(confirmBzEntry.getLong(ID));
                DynamicObjectUtils.copy(bzEntryInfo, confirmBzEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        DynamicObjectCollection supEntryCol = dataEntity.getDynamicObjectCollection(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.ENTITYID);

        DynamicObjectCollection confirmSupEntryCol = confirmBillInfo.getDynamicObjectCollection(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.ENTITYID);

        // 部门秘书补助关联的出差人信息
        List<Object> bzApplierIdList = bzEntryCol.stream()
                .map(bzEntry -> bzEntry.getDynamicObject(NCKD_BZ_ENTRY.NCKD_BZ_TRAVELER).getPkValue())
                .collect(Collectors.toList());

        confirmSupEntryCol.removeIf(confirmSupEntry -> confirmSupEntry.getLong(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_SRCCHECKID) == checkBillId
                && confirmSupEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL) != null
                && confirmSupEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL).getDynamicObject(TripReqBillConstant.APPLIER) != null
                && bzApplierIdList.contains(confirmSupEntry.getDynamicObject(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_TRIPREQBILL).getDynamicObject(TripReqBillConstant.APPLIER).getLong(ID)));

        for(DynamicObject supEntry : supEntryCol){
            DynamicObject confirmSupEntry = confirmSupEntryCol.addNew();
            DynamicObjectUtils.copy(supEntry, confirmSupEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            confirmSupEntry.set(BusTravelCheckBillConstant.NCKD_BZ_SUPDATA.NCKD_SRCCHECKID, checkBillId);
        }

        DynamicObjectCollection slSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_SL_ENTRY.ENTITYID);
        Map<Long, DynamicObject> slSrcEntryMap = slSrcEntryCol.stream().collect(Collectors.toMap(slEntry -> slEntry.getLong(NCKD_SL_ENTRY.NCKD_SL_SRCENTRYID), slEntry -> slEntry));
        DynamicObjectCollection confirmSlEntryCol = confirmBillInfo.getDynamicObjectCollection(NCKD_SL_ENTRY.ENTITYID);
        for(DynamicObject confirmSlEntry : confirmSlEntryCol){
            if(slSrcEntryMap.containsKey(confirmSlEntry.getLong(ID))){
                DynamicObject slEntryInfo = slSrcEntryMap.get(confirmSlEntry.getLong(ID));
                DynamicObjectUtils.copy(slEntryInfo, confirmSlEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        DynamicObjectCollection ycSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_YC_ENTRY.ENTITYID);
        Map<Long, DynamicObject> ycSrcEntryMap = ycSrcEntryCol.stream().collect(Collectors.toMap(ycEntry -> ycEntry.getLong(NCKD_YC_ENTRY.NCKD_YC_SRCENTRYID), ycEntry -> ycEntry));
        DynamicObjectCollection confirmYcEntryCol = confirmBillInfo.getDynamicObjectCollection(NCKD_YC_ENTRY.ENTITYID);
        for(DynamicObject confirmYcEntry : confirmYcEntryCol){
            if(ycSrcEntryMap.containsKey(confirmYcEntry.getLong(ID))){
                DynamicObject ycEntryInfo = ycSrcEntryMap.get(confirmYcEntry.getLong(ID));
                DynamicObjectUtils.copy(ycEntryInfo, confirmYcEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }


        DynamicObjectCollection cbSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_CB_ENTRY.ENTITYID);
        Map<Long, DynamicObject> cbSrcEntryMap = cbSrcEntryCol.stream().collect(Collectors.toMap(cbEntry -> cbEntry.getLong(NCKD_CB_ENTRY.NCKD_CB_SRCENTRYID), cbEntry -> cbEntry));
        DynamicObjectCollection confirmCbEntryCol = confirmBillInfo.getDynamicObjectCollection(NCKD_CB_ENTRY.ENTITYID);
        for(DynamicObject confirmCbEntry : confirmCbEntryCol){
            if(cbSrcEntryMap.containsKey(confirmCbEntry.getLong(ID))){
                DynamicObject cbEntryInfo = cbSrcEntryMap.get(confirmCbEntry.getLong(ID));
                DynamicObjectUtils.copy(cbEntryInfo, confirmCbEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        DynamicObjectCollection tgSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_TG_ENTRY.ENTITYID);
        Map<Long, DynamicObject> tgSrcEntryMap = tgSrcEntryCol.stream().collect(Collectors.toMap(tgEntry -> tgEntry.getLong(NCKD_TG_ENTRY.NCKD_TG_SRCENTRYID), tgEntry -> tgEntry));
        DynamicObjectCollection confirmTgEntryCol = confirmBillInfo.getDynamicObjectCollection(NCKD_TG_ENTRY.ENTITYID);
        for(DynamicObject confirmTgEntry : confirmTgEntryCol){
            if(tgSrcEntryMap.containsKey(confirmTgEntry.getLong(ID))){
                DynamicObject tgEntryInfo = tgSrcEntryMap.get(confirmTgEntry.getLong(ID));
                DynamicObjectUtils.copy(tgEntryInfo, confirmTgEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        DynamicObjectCollection zsSrcEntryCol = dataEntity.getDynamicObjectCollection(NCKD_ZS_ENTRY.ENTITYID);
        Map<Long, DynamicObject> zsSrcEntryMap = zsSrcEntryCol.stream().collect(Collectors.toMap(zsEntry -> zsEntry.getLong(NCKD_ZS_ENTRY.NCKD_ZS_SRCENTRYID), zsEntry -> zsEntry));
        DynamicObjectCollection confirmZsEntryCol = confirmBillInfo.getDynamicObjectCollection(NCKD_ZS_ENTRY.ENTITYID);
        for(DynamicObject confirmZsEntry : confirmZsEntryCol){
            if(zsSrcEntryMap.containsKey(confirmZsEntry.getLong(ID))){
                DynamicObject zsEntryInfo = zsSrcEntryMap.get(confirmZsEntry.getLong(ID));
                DynamicObjectUtils.copy(zsEntryInfo, confirmZsEntry,BusTravelCheckBillConstant.COPY_IGNOREFILED);
            }
        }

        SaveServiceHelper.save(new DynamicObject[]{ confirmBillInfo});
    }
}
