package fi.cas.opplugin;

import com.kingdee.util.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.*;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 代发单
 * @author wanghaiwu_kd
 * @date 2025/06/13
 */
public class AgentPaySynPayStatusOpPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(AgentPaySynPayStatusOpPlugin.class);

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

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

        fieldKeys.add("billno");
        fieldKeys.add("billstatus");
        fieldKeys.add("nckd_paystatus");
        fieldKeys.add("nckd_sbyy");
        fieldKeys.add("nckd_fqqdrq");
        fieldKeys.add("nckd_fkcs");
        fieldKeys.add("nckd_bbh");
        fieldKeys.add("entry");
        fieldKeys.add("entry.e_issuccess");
        fieldKeys.add("entry.bankreturnmsg");
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs args) {
        super.beforeExecuteOperationTransaction(args);

        String operationKey = args.getOperationKey();
        DynamicObject[] agentPayBillObjs = args.getDataEntities();
        logger.info("开始执行操作：{},实体:{}", operationKey, agentPayBillObjs);


        StringBuilder errMsg = new StringBuilder();

        //支付状态同步
        if ("nckd_synpaystatus".equals(operationKey)) {
            DynamicObject jkpzxx = BusinessDataServiceHelper.loadSingle("nckd_jkpzxx",new QFilter[]{new QFilter("number","=","paytoJHX")});
            String url =  jkpzxx.getString("nckd_servername");
            boolean isESBApi = jkpzxx.getBoolean("nckd_isesb");

            for (DynamicObject dataEntity : agentPayBillObjs) {
                String billNum = dataEntity.getString("billno");
                String billStatus = dataEntity.getString("billstatus");
                String payStatus = dataEntity.getString("nckd_paystatus");
                int fkcs = dataEntity.getInt("nckd_fkcs");

                if(!"C".equals(billStatus)){
                    logger.info("代发单同步失败，未提交资金付款。实体：{}", dataEntity);

                    errMsg.append("代发单(" + billNum + ")不是已审核状态，不需要同步付款状态").append("\r\n");
                    continue;
                }

                if(!payStatus.matches("2|6")){
                    logger.info("代发单同步失败，未提交资金付款。实体：{}", dataEntity);

                    errMsg.append("代发单(" + billNum + ")不是【支付中】【支付失败】状态，不需要同步付款状态").append("\r\n");
                    continue;
                }

                if(fkcs <= 0){
                    logger.info("代发单同步失败，未提交资金付款。实体：{}", dataEntity);

                    errMsg.append("代发单(" + billNum + ")未提交资金付款，不需要同步付款状态").append("\r\n");
                    continue;
                }

                //调用接口查询付款状态
                JSONObject param = new JSONObject();
                PayBillToolUtil.headJson(param);
                JSONObject data = new JSONObject();
                data.put("ERP_INS_ID",dataEntity.getPkValue()+"");

                param.put("data",data);
                param.put("txDateTime",Utils.getData(new Date(), "yyyyMMddHHmmss"));
                param.put("batchNo",TypeUtils.nullToString(dataEntity.get("billno"))+"-"+TypeUtils.nullToString(dataEntity.get("nckd_bbh"))+"-"+Utils.getData(new Date(), "yyyyMMddHHmmss"));
                param.put("code","JY-NSTC-Q001");

                String bodyjson=param.toString();
                logger.info("单据号："+billNum+"传入参数"+param.toString());


                Map<String, String> tokenMap = PayBillToolUtil.getNtscToken();

                if(tokenMap == null){
                    logger.info("代发单：" + billNum + "查询失败： 获取token失败");

                    errMsg.append("代发单(" + billNum + ")查询失败： 获取token失败").append("\r\n");
                    continue;
                }

                if(!"0".equals(tokenMap.get("code"))){
                    logger.info("代发单：" + billNum + "查询失败：获取token失败, " + tokenMap.get("msg"));

                    errMsg.append("代发单(" + billNum + ")查询失败： 获取token失败").append("\r\n");
                    continue;
                }

                String token = tokenMap.get("msg");

                //改成调用轻舟的方式
                Map<String, String> header = new HashMap<>();
                header.put("Content-Type", "application/json; charset=UTF-8");
                header.put("Authorization", "Bearer " + token);

                //如果是esb接口，需要包一层参数
                if(isESBApi) {
                    bodyjson = PayBillToolUtil.buildEsbApiBodyJSON(new JSONObject(bodyjson));
                }

                String apiResult = "";
                try {
                    logger.info("代发查询接口调用参数：" + bodyjson);

                    apiResult = HttpUtils.postjson(url, header, bodyjson);
                } catch (IOException e) {
                    logger.info("代发单：" + billNum + "查询失败：" + e.getMessage());

                    errMsg.append("代发单(" + billNum + ")查询失败：" + e.getMessage()).append("\r\n");
                    continue;
                }

                if(StringUtils.isEmpty(apiResult)){
                    logger.info("代发单：" + billNum + "查询结果为空");

                    errMsg.append("代发单(" + billNum + ")查询结果为空").append("\r\n");
                    continue;
                }

                logger.info("代发单：" + billNum + "返回参数" + apiResult);

//            JSONObject cbsReturnJson = new JSONObject(apiResult);

                JSONObject esbReturnJson = new JSONObject(apiResult);
                if(esbReturnJson.get("responseData") == null){
                    logger.info("代发单：" + billNum + ", 未正常返回responseData参数");

                    errMsg.append("代发单(" + billNum + "), 未正常返回responseData参数").append("\r\n");
                    return;
                }

                JSONArray esbJSONArray = esbReturnJson.getJSONArray("responseData");
                if(esbJSONArray == null || esbJSONArray.length() == 0){
                    logger.info("代发单：" + billNum + ", 未正常返回responseData参数");

                    errMsg.append("代发单(" + billNum + "), 未正常返回responseData参数").append("\r\n");
                    return;
                }

                JSONObject cbsReturnJson = esbJSONArray.getJSONObject(0);
                /************************************************************/

                logger.info("代发单："+billNum+"返回参数"+cbsReturnJson.toString());
                if(cbsReturnJson==null||cbsReturnJson.get("data")==null){
                }else{
                    String errcode = cbsReturnJson.get("resultCode")+"";
                    JSONObject bodyinfo = cbsReturnJson.getJSONObject("data");
                    if(bodyinfo!=null){
                        boolean isupdate = false;
                        if ("000000".equals(errcode)) {//已查询到，不代表支付成功
                            String zfstatus = bodyinfo.getString("RET_CODE");//0：付款成功  -1：付款失败 -2：流程中 -3:驳回 -4：部分成功
                            String errormsg =bodyinfo.getString("ERROR_MSG");
                            if("-2".equals(zfstatus)){//流程中
                                //判断原因是否有变化
                                String sbyy =  dataEntity.getString("nckd_sbyy");
                                if(errormsg == null || "".equals(errormsg)){//空不更新

                                }else if(errormsg.equals(sbyy)){//相等不更新

                                }else{
                                    dataEntity.set("nckd_sbyy",errormsg);//错误信息更新
                                    isupdate = true;
                                }

                            }else if("0".equals(zfstatus)){//成功
                                dataEntity.set("nckd_paystatus",3);
                                dataEntity.set("nckd_sbyy","支付成功");//错误信息更新
                                //分录信息
                                DynamicObjectCollection coll = dataEntity.getDynamicObjectCollection("entry");
                                for (int i = 0; i <coll.size() ; i++) {
                                    coll.get(i).set("e_issuccess",true);//分录是否成功
                                    coll.get(i).set("bankreturnmsg","成功");//返回信息
                                }
                                isupdate = true;
                            }else if("-1".equals(zfstatus)){//失败
                                dataEntity.set("nckd_paystatus",6);//支付失败
                                dataEntity.set("nckd_sbyy",errormsg);//错误信息更新
                                updateEntrys(dataEntity,bodyinfo);
                                isupdate = true;
                            }else if("-3".equals(zfstatus)){//驳回
                                dataEntity.set("nckd_paystatus",5);//驳回
                                dataEntity.set("nckd_sbyy",errormsg);//错误信息更新
                                updateEntrys(dataEntity,bodyinfo);
                                isupdate = true;
                            }else if("-4".equals(zfstatus)){//部分成功
                                dataEntity.set("nckd_paystatus",7);//部分成功
                                dataEntity.set("nckd_sbyy",errormsg);//错误信息更新
                                updateEntrys(dataEntity,bodyinfo);
                                isupdate = true;
                            }
                            if(isupdate){
                                SaveServiceHelper.update(new DynamicObject[]{dataEntity});
                                //存入日志表
                                PayBillToolUtil.saveAgentlog(dataEntity, bodyjson, cbsReturnJson.toString(), "6");

                            }
                        }
                    }
                }
            }
        }


        if (errMsg.length() > 0) {
            ////将错误信息返回到前端
            args.setCancelMessage(errMsg.toString());
            args.setCancel(true);

            System.out.println("AgentPaySynPayStatusOpPlugin 错误信息：" + errMsg.toString());
        }

    }

    //更新分录
    public void updateEntrys(DynamicObject dataEntity,JSONObject bodyinfo){
        DynamicObjectCollection coll = dataEntity.getDynamicObjectCollection("entry");
        JSONArray ITEM_LIST =  bodyinfo.getJSONArray("ITEM_LIST");
        for (int i = 0; i < ITEM_LIST.length(); i++) {
            JSONObject iteminfo =(JSONObject)ITEM_LIST.get(i);
            String itempk = iteminfo.get("ERP_PVT_ID")+"";
            String error_msg = iteminfo.get("ERROR_MSG")+"";
            String result_code = iteminfo.get("RESULT_CODE")+"";
            for (int j = 0; j <coll.size() ; j++) {
                String itempkvalue =  coll.get(j).getPkValue()+"";
                if(itempk.equals(itempkvalue)){
                    if("0".equals(result_code)){
                        coll.get(j).set("e_issuccess",true);
                        coll.get(j).set("bankreturnmsg","成功");//返回信息
                    }else{
                        coll.get(j).set("e_issuccess",false);
                        coll.get(j).set("bankreturnmsg",error_msg);//返回信息
                    }
                }
            }
        }
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
    }

    @Override
    public void rollbackOperation(RollbackOperationArgs e) {
        super.rollbackOperation(e);
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
    }

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