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

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jimin.jyyy.bd.task.SynSupplierFromSAPTask;
import nckd.jimin.jyyy.bd.task.impl.SynSapServiceImpl;
//import nckd.jimin.jyyy.bd.webapi.BeiTransdetailConstantInfo;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * 标准单据列表插件
 */
public class CusCostCenterListPlugin extends AbstractListPlugin implements Plugin {
    private static final Log logger = LogFactory.getLog(SynSupplierFromSAPTask.class);
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("_toolbar_");
    }
    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        if(evt.getItemKey().equalsIgnoreCase("nckd_sap")){
            SynSapServiceImpl synSapService = new SynSapServiceImpl();
            //使用状态=启用
            QFilter qFilter = new QFilter("enable", QCP.equals, "1");
            //数据状态=已审核
            qFilter.and(new QFilter("status", QCP.equals, "C"));
            //属性核算组织=是
            qFilter.and(new QFilter("fisaccounting", QCP.equals, "1"));
            //获取当前组织，将成本信息缓存到Map中
            Long orgId = RequestContext.get().getOrgId();
            qFilter.and(new QFilter("id", QCP.equals, orgId));
            DynamicObject orgDy = BusinessDataServiceHelper.loadSingle("bos_org", "id, number", qFilter.toArray());
            if(orgDy==null){
                this.getView().showMessage("没有找到对应的核算组织");
                return;
            }
            logger.info("------同步成本 " + orgDy.getString("number") + "----------");
            String orgNumber = orgDy.getString("number");
            Map<String, String> result = new HashMap<>();
            result=synSapService.synCostCenterForSap(orgNumber);
            this.getView().showMessage(result.get("msg"));

        }
    }

}