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

import kd.bos.common.enums.EnableEnum;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;
import nckd.jxccl.base.orm.helper.QFilterCommonHelper;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 年度调整解锁单据-解锁/反解锁
* @author W.Y.C
* @date 2025/10/16 16:33
* @version 1.0
*/
public class PosFileUnlockBillOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().add(PositionStructureConstant.NCKD_PERSONPOSFILE);
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        String operateKey = e.getOperationKey();
        List<Long> ids = new ArrayList<>();
        for (DynamicObject data : e.getDataEntities()) {
            DynamicObjectCollection entrys = data.getDynamicObjectCollection(PositionStructureConstant.NCKD_POSFILEUNLOCKENTRY);

            for (DynamicObject entry : entrys) {
                DynamicObject personPosFile = entry.getDynamicObject(PositionStructureConstant.NCKD_PERSONPOSFILE);
                long id = personPosFile.getLong(FormConstant.ID_KEY);
                ids.add(id);
            }
        }
        QueryFieldBuilder queryFieldBuilder = QueryFieldBuilder.create()
                .add(FormConstant.ID_KEY)
                .add(PositionStructureConstant.NCKD_LOCKSTATUS)
                .add(PositionStructureConstant.KEY_NCKD_LOCKUSER)
                .add(PositionStructureConstant.NCKD_LOCKDATETIME);
        DynamicObject[] load = BusinessDataServiceHelper.load(PositionStructureConstant.PERSONPOSFILE_ENTITYID, queryFieldBuilder.buildSelect(), new QFilter[]{QFilterCommonHelper.getIdInFilter(ids)});
        for (DynamicObject dynamicObject : load) {
            if(PositionStructureConstant.UNLOCKED_OP.equals(operateKey)) {
                //审批通过-解锁
                dynamicObject.set(PositionStructureConstant.NCKD_LOCKSTATUS, EnableEnum.NO.getCode());
                dynamicObject.set(PositionStructureConstant.KEY_NCKD_LOCKUSER, null);
                dynamicObject.set(PositionStructureConstant.NCKD_LOCKDATETIME, null);
            }else{
                //反向撤回-反解锁
                dynamicObject.set(PositionStructureConstant.NCKD_LOCKSTATUS, EnableEnum.YES.getCode());
                dynamicObject.set(PositionStructureConstant.KEY_NCKD_LOCKUSER, RequestContext.get().getCurrUserId());
                dynamicObject.set(PositionStructureConstant.NCKD_LOCKDATETIME, new Date());
            }
        }
        SaveServiceHelper.update(load);
    }
}