package nckd.jimin.jyyy.fi.plugin.operate;

import com.kingdee.util.StringUtils;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import java.util.ArrayList;
import java.util.List;

/**
 * 插件说明：保存按钮，触发分录对应报销单的预算返还操作
 * 表单标识：SAP凭证(nckd_sapsjszjb)
 * @author wanghaiwu_kd
 * @date 2025/07/31
 */
public class SapVoucherSaveOpPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(AbstractOperationServicePlugIn.class);

    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);

        List<String> fieldKeys = e.getFieldKeys();

        fieldKeys.add("entryentity");
        fieldKeys.add("entryentity.nckd_bxbillno");
        fieldKeys.add("entryentity.nckd_bxbillid");
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);

        DynamicObject[] billEntities = e.getDataEntities();

        List<Long> publicIds = new ArrayList<>();
        List<Long> tripIds = new ArrayList<>();

        for (DynamicObject billInfo : billEntities) {
            DynamicObjectCollection entrys = billInfo.getDynamicObjectCollection("entryentity");
            for(DynamicObject entry : entrys){
                if(StringUtils.isEmpty(entry.getString("nckd_bxbillno"))){
                    continue;
                }

                String billNo = entry.getString("nckd_bxbillno");
                Long billId = entry.getLong("nckd_bxbillid");

                //差旅报销单
                if("CL".equals(billNo.substring(0, 2))){
                    tripIds.add(billId);
                }
                //对公报销单
                else {
                    publicIds.add(billId);
                }
            }
        }

        String operationKey = "nckd_returnbudge";

        //对公报销单返还
        if(publicIds.size() > 0) {
            String entityName = "er_publicreimbursebill";
            MainEntityType entityType= EntityMetadataCache.getDataEntityType(entityName);
            DynamicObject[] publicBills = BusinessDataServiceHelper.load(publicIds.toArray(), entityType);

            //调用报销付款单预算返还操作
            OperationResult returnResult = OperationServiceHelper.executeOperate(operationKey, entityName, publicBills, OperateOption.create());
        }

        //差旅报销单返还
        if(publicIds.size() > 0) {
            String entityName = "er_tripreimbursebill";
            MainEntityType entityType= EntityMetadataCache.getDataEntityType(entityName);
            DynamicObject[] publicBills = BusinessDataServiceHelper.load(publicIds.toArray(), entityType);

            //调用报销付款单预算返还操作
            OperationResult returnResult = OperationServiceHelper.executeOperate(operationKey, entityName, publicBills, OperateOption.create());
        }
    }
}
