package nckd.jxccl.hr.tsc.plugin.form;

import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.bos.base.AbstractBasePlugIn;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.swc.helper.SWCHelper;
import java.util.EventObject;
import java.util.List;

/**
 * 政策公告插件
 * @author turborao
 * 时间 2026/1/13 16:05
 */
@SuppressWarnings("unused")
public class AnnouncementsBasePlugin extends AbstractBasePlugIn implements Plugin {


    @Override
    public void afterCreateNewData(EventObject e) {

        Long userId = UserServiceHelper.getCurrentUserId();
        DynamicObject personUserDyn = SWCHelper.queryOne(FormConstant.HRPI_PERSONUSERREL, "employee.id","user",userId);

        Long empId = 0L;
        if(personUserDyn == null){
            /**
             * 没有员工信息时，返回常量，用于测试
             */
            //empId = 2313784485549715456L;
            this.getView().showMessage("未找到员工！");
            return;
        }else {
            empId = personUserDyn.getLong("employee.id");
        }
        List<Long> employeeIds = new java.util.ArrayList<>();
        employeeIds.add(empId);
        DynamicObject[] empDyns = SWCHelper.queryEmpPosOrgRelDyns(employeeIds);
        if(empDyns != null && empDyns.length > 0){
            Long companyId = empDyns[0].getLong("company.id");
            this.getModel().setValue("nckd_hradminorg", companyId);
            this.getModel().setValue("nckd_billstatus", "A");
            this.getModel().setValue("nckd_istop", false);
        }

    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs args) {
        ///如果有合法检验，需要有判断是否操作成功！
        OperationResult operationResult = args.getOperationResult();
        if (operationResult != null && !operationResult.isSuccess()) {
            return;
        }
        FormOperate source = (FormOperate)args.getSource();
        String formOp = source.getOperateKey();
        DynamicObject data = this.getModel().getDataEntity(true);
        String  billstatus =  data.getString("nckd_billstatus");
        HRBaseServiceHelper hrBaseServiceHelper = new HRBaseServiceHelper(FormConstant.NCKD_ANNOUNCEMENTS);
        if ("publish".equals(formOp)) {
            data.set("nckd_billstatus", "C");
            data.set("nckd_publishdate", new java.util.Date());
            data.set("nckd_publishuser", UserServiceHelper.getCurrentUserId());
            Object billPk = hrBaseServiceHelper.saveOne(data);
            this.getView().showSuccessNotification("发布成功", 1000);
        }
        if("withdraw".equals(formOp)){
            data.set("nckd_billstatus", "A");
            Object billPk = hrBaseServiceHelper.saveOne(data);
            this.getView().showSuccessNotification("撤回成功", 1000);
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        DynamicObject data = this.getModel().getDataEntity(true);
        setEnable(data);
    }

    public void setEnable(DynamicObject data) {
        String  billstatus =  data.getString("nckd_billstatus");
        if("A".equals(billstatus)){
            this.getView().setStatus(OperationStatus.EDIT);
            this.getView().setEnable(Boolean.TRUE, "nckd_downdate");
            this.getView().setVisible(Boolean.TRUE, "bar_save");
            this.getView().setVisible(Boolean.TRUE, "bar_modify");
            this.getView().setVisible(Boolean.TRUE, "nckd_publish");
            this.getView().setVisible(Boolean.FALSE, "nckd_withdrawn");
        }else {
            this.getView().setStatus(OperationStatus.VIEW);
            this.getView().setEnable(Boolean.FALSE, "nckd_downdate");
            this.getView().setVisible(Boolean.FALSE, "bar_save");
            this.getView().setVisible(Boolean.FALSE, "bar_delete");
            this.getView().setVisible(Boolean.FALSE, "bar_modify");
            this.getView().setVisible(Boolean.FALSE, "nckd_publish");
            this.getView().setVisible(Boolean.TRUE, "nckd_withdrawn");

        }
    }
}