package nckd.jxccl.hr.psms.plugin.form.annualadjust;

import kd.bos.common.enums.EnableEnum;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.ShowType;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.control.events.ItemClickListener;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 已生成年度调整-查询列表插件
* @author W.Y.C
* @date 2025/10/14 17:03
* @version 1.0
*/
public class AnnualAdjustQueryListPlugin extends AbstractListPlugin implements Plugin, ItemClickListener {

    @Override
    public void setFilter(SetFilterEvent e) {
        QFilter qFilter = new QFilter(PositionStructureConstant.NCKD_DISABLE, QCP.equals, EnableEnum.NO.getCode())
        //只查询年度调整的数据
        .and(new QFilter(PositionStructureConstant.NCKD_TYPESTATE, QCP.equals, "3"));
        e.addCustomQFilter(qFilter);
    }

    @Override
    public void packageData(PackageDataEvent e) {
        String colKey = e.getColKey();
        if("nckd_postallowancediff".equalsIgnoreCase(colKey)){
            BigDecimal itemAmtEx = e.getRowData().getBigDecimal("nckd_postallowance");
            BigDecimal itemAmt = e.getRowData().getBigDecimal("nckd_lastpersonposfile.nckd_postallowance");

            // 空值处理
            if (itemAmtEx == null) {
                itemAmtEx = BigDecimal.ZERO;
            }
            if (itemAmt == null) {
                itemAmt = BigDecimal.ZERO;
            }

            e.setFormatValue("¥"+itemAmtEx.subtract(itemAmt).setScale(2, RoundingMode.HALF_UP).toString());
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey();
        String operationKey = evt.getOperationKey();
        if("nckd_effective".equals(itemKey) ) {
            ListSelectedRowCollection selectedRows = this.getSelectedRows();
            if (selectedRows.isEmpty()) {
                this.getView().showTipNotification("请至少选择一条数据");
                return;
            }
            List<Long> personIds = new ArrayList<Long>();
            for (ListSelectedRow selectedRow : selectedRows) {
                personIds.add(ConvertUtil.toLong(selectedRow.getPrimaryKeyValue()));
            }

            if ("nckd_effective".equals(itemKey)) {
                //弹出【批量】在职人员初定窗口
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId(PositionStructureConstant.ANNUALEFFECTIVE_ENTITYID);
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCaption("年度调整生效");
                showParameter.setSendToClient(true);
                showParameter.setCustomParam(FormConstant.NCKD_PERSON, personIds);
                showParameter.setCloseCallBack(new CloseCallBack(this, PositionStructureConstant.ANNUALEFFECTIVE_ENTITYID));
                this.getView().showForm(showParameter);

            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        String operateKey = afterDoOperationEventArgs.getOperateKey();
        boolean success = afterDoOperationEventArgs.getOperationResult() != null && afterDoOperationEventArgs.getOperationResult().isSuccess();
        if(success){
            if("setinactive".equalsIgnoreCase(operateKey)){
                Map<String, String> customData = afterDoOperationEventArgs.getOperationResult().getCustomData();
                String message = customData.get("message");
                if(StringUtils.isNotBlank( message)){
                    this.getView().showConfirm("提示","置回未生效成功！",message, MessageBoxOptions.OK,null,null,null,null);
                }else{
                    this.getView().showSuccessNotification("置回未生效成功！");
                }
                //刷新列表
                this.getView().invokeOperation(FormConstant.REFRESH_OP);
            }else if(PositionStructureConstant.ISLOCKED_OP.equalsIgnoreCase(operateKey)){
                Map<String, String> customData = afterDoOperationEventArgs.getOperationResult().getCustomData();
                String message = customData.get("message");
                if(StringUtils.isNotBlank( message)){
                    this.getView().showConfirm("提示","锁定成功！",message, MessageBoxOptions.OK,null,null,null,null);
                }else{
                    this.getView().showSuccessNotification("锁定成功！");
                }
                this.getView().invokeOperation(FormConstant.REFRESH_OP);
            }else if(PositionStructureConstant.UNLOCKED_OP.equalsIgnoreCase(operateKey)){
                Map<String, String> customData = afterDoOperationEventArgs.getOperationResult().getCustomData();
                if(customData != null && customData.containsKey("message")){
                    String message = customData.get("message");
                    if(StringUtils.isNotBlank( message)){
                        this.getView().showConfirm("提示","解锁成功！",message, MessageBoxOptions.OK,null,null,null,null);
                    }else{
                        this.getView().showSuccessNotification("解锁成功！");
                    }
                    this.getView().invokeOperation(FormConstant.REFRESH_OP);
                }else{
                    this.getView().showSuccessNotification("发起流程成功，待审批通过后自动解锁！");
                }
            }
        }

    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if(StringUtils.equalsAny(actionId,PositionStructureConstant.ANNUALEFFECTIVE_ENTITYID)){
            Object returnData = closedCallBackEvent.getReturnData();
            if(returnData != null) {
                //刷新列表
                this.getView().invokeOperation(FormConstant.REFRESH_OP);
            }
        }
    }
}