package nckd.jimin.jyyy.hr.tsrsc.plugin.form;

import kd.bos.entity.datamodel.IDataModel;
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.bos.servicehelper.permission.PermissionServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.hrmp.hrpi.business.domian.service.impl.PersonServiceImpl;
import kd.sdk.plugin.Plugin;

import java.util.List;
import java.util.Map;

/**
 * 临时/年度招聘申请列表过滤当插件
 * 默认按照制单人=当前用户 or 招聘需求负责人 = 当前HR人员
 * 如果当前用户在 角色编码 = S-001 下，不受控制
 * @author ：Tyx
 * @since ：2025-06-30
 */
public class CommonFilterListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void setFilter(SetFilterEvent e) {
        super.setFilter(e);
        //当前用户ID
        Long userId = UserServiceHelper.getCurrentUserId();
        //查询角色是否在 编码=S-001 的角色下 在的话不做任何过滤
        List<Long> userIds = PermissionServiceHelper.getUsersByRoleNum("S-001");
        if(userIds.contains(userId))
            return;

        Long personId = 999999L;
        Map<String, Object> map = PersonServiceImpl.getInstance().getPersonModelIdByUserId(userId);
        if(Boolean.valueOf(map.get("success").toString())) {
            personId = Long.valueOf(((Map) map.get("data")).get("person").toString());
        }
        QFilter filter = new QFilter("creator.id", QCP.equals, userId);
        filter.or("nckd_person.id",QCP.equals, personId);
        e.addCustomQFilter(filter);
    }
}