package nckd.jimin.jyyy.fi.webapi;

import com.kingdee.util.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.openapi.api.plugin.ApiSavePlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * API保存扩展插件，处理入参问题
 * 接口编码：SAPsjs
 * 接口名称：SAP实际数中间表
 * 接口URL：/v2/nckd/eb/nckd_sapsjszjb/SAPsjs
 * @author wanghaiwu_kd
 * @date 2025/07/22
 */
public class SapVoucher2MidApiSavePlugin implements ApiSavePlugin {
    @Override
    public List<Map<String, Object>> preHandleRequestData(List<Map<String, Object>> reqData) {
        List<String> bxBillNoList = new ArrayList<>();
//
//        QFilter itemfilter = new QFilter("number", QCP.equals, "FYXM01.02.01");
//        itemfilter.and(new QFilter("enable", QCP.equals, "1"));
//        DynamicObject tripItem = BusinessDataServiceHelper.loadSingle("er_expenseitemedit", itemfilter.toArray());

        for(Map<String, Object> map : reqData){
            if(map.get("entryentity") == null){
                continue;
            }

            List<Map<String, Object>> entrys = (List<Map<String, Object>>)map.get("entryentity");
            for(Map<String, Object> entry : entrys){
                if(entry.get("nckd_bxbillno") == null || StringUtils.isEmpty(entry.get("nckd_bxbillno").toString())){
                    continue;
                }

                String billNo = entry.get("nckd_bxbillno").toString();

                bxBillNoList.add(billNo);
            }
        }

        if(bxBillNoList.size() > 0){
            QFilter qFilter = new QFilter("billno", QCP.in, bxBillNoList);
            //查找对公报销单
            DynamicObject[] publicDatas = BusinessDataServiceHelper.load("er_publicreimbursebill", "id, billno, description, nckd_expenseitem", qFilter.toArray());

            //查差旅报销单
            DynamicObject[] tripDatas = BusinessDataServiceHelper.load("er_tripreimbursebill", "id, billno, description", qFilter.toArray());

            Map<String, Object> billMaps = new HashMap<String, Object>();
            for(DynamicObject publicData : publicDatas){
                billMaps.put(publicData.getString("billno"), publicData);
            }

            for(DynamicObject tripData : tripDatas){
                billMaps.put(tripData.getString("billno"), tripData);
            }

            if(billMaps.size() == 0){
                return reqData;
            }

            for(Map<String, Object> map : reqData){
                if(map.get("entryentity") == null){
                    continue;
                }

                List<Map<String, Object>> entrys = (List<Map<String, Object>>)map.get("entryentity");
                for(Map<String, Object> entry : entrys){
                    if(entry.get("nckd_bxbillno") == null || StringUtils.isEmpty(entry.get("nckd_bxbillno").toString())){
                        continue;
                    }

                    String billNo = entry.get("nckd_bxbillno").toString();
                    if(billMaps.get(billNo) != null){
                        DynamicObject data = (DynamicObject)billMaps.get(billNo);

                        entry.put("nckd_bxbillid", data.getLong("id"));
                        entry.put("nckd_description", data.getString("description"));

                        Map<String, Object> expenseItem = new HashMap<String, Object>();
                        if("CL".equals(billNo.substring(0, 2))){
                            expenseItem.put("number", "FYXM01.02.01");
                        } else{
                            expenseItem.put("number", data.getDynamicObject("nckd_expenseitem").getString("number"));
                        }

                        entry.put("nckd_expenseitem", expenseItem);
                    }
                }
            }
        }

        return reqData;
    }
}
