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

import com.alibaba.druid.util.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.form.IClientViewProxy;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.AbstractOperate;
import kd.bos.list.ListShowParameter;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import nckd.jimin.jyyy.fi.plugin.operate.SRMHelperUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 对公报销单(nckd_er_publicreimbur_ext)、预付单
 * 由srm系统产生的单据，退回srm操作
 * @author wanghaiwu_kd
 * @date 2025/05/15
 */
public class PayApplyBillSRMListPlugin extends AbstractListPlugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        Object source = args.getSource();
        String opKey = ((AbstractOperate)source).getOperateKey();

        Set<Long> selectedDatas = (Set)args.getListSelectedData().stream().map((data) -> {
            return (Long)data.getPrimaryKeyValue();
        }).collect(Collectors.toSet());

        if(StringUtils.equals("nckd_ssosrm", opKey)){
            if (selectedDatas.size() > 1 || selectedDatas.size() == 0) {
                this.getView().showTipNotification(ResManager.loadKDString("仅支持选择单条记录联查", "TripReqBillList_7", "fi-er-formplugin", new Object[0]));
                args.setCancel(true);
                return;
            } else {
                if (selectedDatas.size() == 1) {
                    ListSelectedRow row = args.getListSelectedData().get(0);
                    String entityName = ((ListShowParameter)this.getView().getFormShowParameter()).getBillFormId();

                    if("er_layout_publicreimburse".equals(entityName)){
                        entityName = "er_publicreimbursebill";
                    } else if("er_layout_prepay".equals(entityName)){
                        entityName = "er_prepaybill";
                    }

                    if(!StringUtils.isEmpty(entityName)) {
                        DynamicObject objcetInfo = QueryServiceHelper.queryOne(entityName, "nckd_srmurl", new QFilter[]{new QFilter("id", "=", row.getPrimaryKeyValue())});
                        if (StringUtils.isEmpty(objcetInfo.getString("nckd_srmurl"))) {
                            this.getView().showTipNotification(ResManager.loadKDString("不是SRM生成的单据，不能单点到SRM系统", "TripReqBillList_11", "fi-er-formplugin", new Object[0]));
                            args.setCancel(true);
                            return;
                        } else {
                            openSRMSSOLink(objcetInfo.getString("nckd_srmurl"));
                        }
                    }
                }
            }
        }
    }

    /**
     * 单点登录到srm系统
     */
    public void openSRMSSOLink(String ssourl){
        Map<String, String> returnMap = SRMHelperUtils.buildSSOUrl(ssourl);

        if(returnMap != null){
            if("0".equals(returnMap.get("code"))) {
                ssourl = returnMap.get("msg");
                IClientViewProxy proxy = this.getView().getService(IClientViewProxy.class);
                Map<String, String> mpUrl = new HashMap();
                mpUrl.put("url", ssourl);
                proxy.addAction("openUrl", mpUrl);
            } else {
                this.getView().showTipNotification(returnMap.get("msg"));
            }
        } else {
            this.getView().showTipNotification("获取单点url失败");
        }
    }
}
