package yem.wm.cm.csrm.form;


import cn.hutool.core.util.CreditCodeUtil;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.db.DB;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.Search;
import kd.bos.form.control.events.SearchEnterEvent;
import kd.bos.form.control.events.SearchEnterListener;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
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 yem.base.common.utils.YEM;
import yem.wm.cm.csrm.enums.PageIdEnum;
import yem.wm.cm.csrm.utils.BillCommonUtils;

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

/**
 * 企业风险评估搜索页
 *
 * @Author yunshuaiwei
 * @Date 2024/6/4 15:48
 **/
public class SearchFormPlugIn extends AbstractFormPlugin implements SearchEnterListener, BeforeF7SelectListener {
    private static final Log log = LogFactory.getLog(SearchFormPlugIn.class);
    private final static String TASK_KEY = "SearchFormPlugIn";
    //评分方案控件标识
    private final static String SCORE_SCHEME_CONTROL = "yem_basedatafield";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_buttonap");
        final Search search = (Search) this.getControl("yem_searchap");
        search.addEnterListener(this);
        BasedataEdit inspectionScheme = this.getControl(SCORE_SCHEME_CONTROL);
        inspectionScheme.addBeforeF7SelectListener(this);
    }

    @Override
    public void search(SearchEnterEvent arg0) {
    }

    @Override
    public void click(EventObject e) {
        super.click(e);
        Control c = (Control) e.getSource();
        String key = c.getKey();
        if (key.equals("yem_buttonap")) {
            Object value = this.getView().getModel().getValue(SCORE_SCHEME_CONTROL);
            if (YEM.isNotEmpty(value)) {
                Search search = this.getControl("yem_searchap");
                this.searchInfo(search.getSearchKey());
            } else {
                this.getView().showErrorNotification("请选择评分方案");
            }
        }
    }

    /**
     * 查询企业信息
     *
     * @Author yunshuaiwei
     * @Date 2024/6/4 17:07
     **/
    private void searchInfo(String searchKey) {
        log.debug("企业风险评估搜索关键字：searchKey={}", searchKey);
        IFormView view = this.getView();
        if (YEM.isEmpty(searchKey)) {
            view.showTipNotification("请输入要搜索的统一社会信用代码");
            return;
        } else {
            if (!CreditCodeUtil.isCreditCode(searchKey)) {
                String s = CreditCodeUtil.randomCreditCode();
//                view.showTipNotification("统一社会信用代码有误");
                searchKey = s;
//                return;
            }
        }
        DynamicObject merchantApply = BusinessDataServiceHelper.loadSingle(PageIdEnum.MERCHANT_APPLY.getPageId(), new QFilter[]{new QFilter("yem_societycreditcode", QCP.equals, searchKey)});
        //评分方案
        DynamicObject scorePlan = (DynamicObject) this.getView().getModel().getValue(SCORE_SCHEME_CONTROL);
        if (YEM.isEmpty(merchantApply)) {
            Long merchantId = DB.genLongId(PageIdEnum.MERCHANT_APPLY.getPageId());
            companyRiskAssessment(view, merchantId, searchKey, false, scorePlan.getLong("id"));
        } else {
            companyRiskAssessment(view, merchantApply.getLong("id"), searchKey, true, scorePlan.getLong("id"));
        }
    }

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load(PageIdEnum.SCORE_SCHEME.getPageId(), "*",
                new QFilter[]{new QFilter("enable", QCP.equals, true), new QFilter("status", QCP.equals, "C"), new QFilter("yem_pyramidsc", QCP.equals, true)});
        if (dynamicObjects != null && dynamicObjects.length > 0) {
            //评分方案给默认值
            this.getModel().setValue(SCORE_SCHEME_CONTROL, dynamicObjects[0]);
        }
    }


    /**
     * 企业风险评估
     * flag：是否为新数据
     * scorePlan：评分方案
     *
     * @Author yunshuaiwei
     * @Date 2024/6/6 12:19
     **/
    private void companyRiskAssessment(IFormView view, Long merchantId, String searchKey, Boolean flag, Long scorePlan) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("merchantId", merchantId);
        maps.put("searchKey", searchKey);
        maps.put("flag", flag);
        maps.put("scorePlan", scorePlan);
        BillCommonUtils.openTargetBill(view, maps, PageIdEnum.CM_PYRAMID.getPageId(), OperationStatus.VIEW, ShowType.MainNewTabPage);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent evt) {
        Control con = (Control) evt.getSource();
        String key = con.getKey();
        if (SCORE_SCHEME_CONTROL.equals(key)) {
            QFilter qFilter = new QFilter("yem_pyramidsc", QCP.equals, true);
            ListShowParameter showParameter = (ListShowParameter) evt.getFormShowParameter();
            showParameter.getListFilterParameter().setFilter(qFilter);
        }
    }
}
