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

import com.kingdee.util.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import java.util.List;
import java.util.Map;

/**
 * 表单标识：对公报销单(nckd_er_publicreimbur_ext)
 * @author wanghaiwu_kd
 * @date 2025/05/12
 */
public class PublicReimBillReturnOpPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(PublicReimBillReturnOpPlugin.class);

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

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

        fieldKeys.add("billno");
        fieldKeys.add("billstatus");
        fieldKeys.add("nckd_srmbillno");
        fieldKeys.add("nckd_srmurl");
    }

    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
    }

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

        StringBuffer errMessage = new StringBuffer();

        DynamicObject[] billEntities = e.getDataEntities();
        String operationKey = e.getOperationKey();

        logger.info("开始执行操作：{},实体:{}", operationKey, billEntities);

        if (StringUtils.equals( "nckd_returnsrm", operationKey)){//退回
            for (DynamicObject billInfo : billEntities) {
                String billno = billInfo.getString("billno");
                String billstatus = billInfo.getString("billstatus");
                String smrBillNo = billInfo.getString("nckd_srmbillno");

                if(!"A".equals(billstatus) || StringUtils.isEmpty(smrBillNo)){
                    if(errMessage.length() > 0){
                        errMessage.append(",");
                    }
                    errMessage.append("单据(" + billno + ")不是暂存状态或不是由srm生成");

                    continue;
                }

                Map<String, String> returnMap = SRMHelperUtils.writeBackSRMWfStatus(billInfo.getDataEntityType().getName(), billInfo.getLong("id"), operationKey);

                if(returnMap != null){
                    if("1".equals(returnMap.get("code"))){
                        if(errMessage.length() > 0){
                            errMessage.append(",");
                        }
                        errMessage.append("单据(" + billno + ")，" + returnMap.get("msg"));
                    }
//                    else {
//                        errMessage.append("获取单点地址:" + returnMap.get("msg"));
//                    }
                }
            }

            if(errMessage.length() > 0) {
                ////将错误信息返回到前端
                String msg = errMessage.toString();
                e.setCancelMessage(msg);
                e.setCancel(true);

                logger.info("PublicReimBillReturnOpPlugin 退回失败：" + msg);
            }
        }
    }

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

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

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

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

        switch (operationKey) {
            case "nckd_return"://退回，只有保存状态的单据可以退回
                for (DynamicObject billInfo : billEntities) {
                    if(!"A".equals(billInfo.getString("billstatus"))){
                        String billno = billInfo.getString("billno");
                    }

                    billInfo.set("billstatus", "H");
                }
                break;
            default:
                break;
        }
    }
}
