package nckd.jxccl.sit.hcsi.formplugin.web.selfsearch;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.swc.helper.SWCHelper;

import java.util.EventObject;

/**
 * Tyx 2025-10-27
 * 个人权益单查询表单插件
 */
public class SelfSearchFormPlugin extends AbstractFormPlugin implements Plugin {

    private static final HRBaseServiceHelper EMPLOYEE_HELPER = new HRBaseServiceHelper("hrpi_employee");
    private static final HRBaseServiceHelper INSURANCE_HELPER = new HRBaseServiceHelper("hcsi_sibase");
    private static final HRBaseServiceHelper SINSURFILE_HELPER = new HRBaseServiceHelper("hcsi_sinsurfile");

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Button button = this.getView().getControl("nckd_btnsearch");
        button.addClickListener(this);
    }



    /**
     * 按钮点击事件
     * @param evt
     */
    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        // 点击按钮的标识
        String key = source.getKey();
        if("nckd_btnsearch".equals(key)){
            // 获取到期间：
            DynamicObject period = (DynamicObject) this.getModel().getValue("nckd_sinsurperiod");
            if(ObjectUtils.isEmpty(period)) {
                this.getView().showTipNotification("请选择查询期间");
            }
            else {
                // 获取当前登录用户对应的HR人员
                Long employeeId = SWCHelper.queryEmployeeIdByUserId(RequestContext.getOrCreate().getCurrUserId());
                if (employeeId != 0L) {
                    doSearch(period.getLong("id"), employeeId);
                }
            }
        }
    }

    /**
     * 查询实际执行方法
     */
    private void doSearch(Long periodId, Long employeeId) {
        // 查询社保档案
        DynamicObject sinsurFile = searchSinsurFileInfo(employeeId);
        if(!ObjectUtils.isEmpty(sinsurFile)) {
            // 查询基本信息：工号、姓名、性别、社保账号
            searchEmpBaseInfo(employeeId);
            // 查询险种信息：
            // searchInsranceInfo(periodId, employeeId);
        }
    }

    private DynamicObject searchSinsurFileInfo(Long employeeId) {

        // 社保档案上有employee
        // 社保档案上的字段+employee上所有的字段；

        // 查询字段
        String selectFields = "employee.name,employee.number,employee.gender.name,sinsuraccount";
        // 过滤条件
        QFilter filter = new QFilter("employee.id", QCP.equals, employeeId);
        // 实际查询
        DynamicObject sinsurFile = SINSURFILE_HELPER.queryOriginalOne(selectFields, new QFilter[]{filter});
        return sinsurFile;


    }

    /**
     * 查询基本信息：工号、姓名、性别、社保账号
     * @param employeeId
     */
    private void searchEmpBaseInfo(Long employeeId) {
        String selectFields = "employee.name,employee.number,employee.gender.name,sinsuraccount";
        QFilter filter = new QFilter("employee.id", QCP.equals, employeeId);
        DynamicObject sinsurFile = SINSURFILE_HELPER.queryOriginalOne(selectFields, new QFilter[]{filter});
        setLabelText("nckd_nameval", sinsurFile.getString("employee.name"));
        setLabelText("nckd_nunmberval", sinsurFile.getString("employee.number"));
        setLabelText("nckd_sexval", sinsurFile.getString("employee.gender.name"));
        setLabelText("nckd_socnumberval", sinsurFile.getString("sinsuraccount"));

    }

    /**
     * 查询险种信息：险种，个人缴纳基数，单位缴纳基数，缴纳单位，备注
     * @param employeeId
     */
    private void searchInsranceInfo(Long employeeId) {

    }

    /**
     * 设置标签文本值
     * @param lblKey
     * @param value
     */
    private void setLabelText(String lblKey, String value) {
        Label label = (Label)this.getView().getControl(lblKey);
        label.setText(value);
    }
}