package com.artfess.yhxt.util;

import com.artfess.base.context.BaseContext;
import com.artfess.base.query.FieldRelation;
import com.artfess.base.query.QueryFilter;
import com.artfess.base.query.QueryOP;
import com.artfess.uc.manager.OrgManager;
import com.artfess.uc.model.Org;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhx
 * @create 2022/4/7
 */
@Component
public class PermissionUtils<T extends Model<T>> {
    @Resource
    OrgManager orgManager;
    @Resource
    BaseContext baseContext;
    public   void addPermission(QueryFilter<T> queryFilter) {
        String currentOrgId = baseContext.getCurrentOrgId();
        List<Org> orgList = orgManager.getByParentId(currentOrgId);
        List<String> collect = orgList.stream().map(Org::getId).collect(Collectors.toList());
        collect.add(currentOrgId);
        String orgIds = collect.stream().collect(Collectors.joining(","));
        queryFilter.addFilter("COMPANY_ID_", String.join("','", orgIds), QueryOP.IN, FieldRelation.AND);
    }

    public   void addPermissionNo(QueryFilter<T> queryFilter) {
        String currentOrgId = baseContext.getCurrentOrgId();
        List<Org> orgList = orgManager.getByParentId(currentOrgId);
        List<String> collect = orgList.stream().map(Org::getId).collect(Collectors.toList());
        collect.add(currentOrgId);
        String orgIds = collect.stream().collect(Collectors.joining(","));
        queryFilter.addFilter("COMPANY_ID", String.join("','", orgIds), QueryOP.IN, FieldRelation.AND);
    }


    public   void addPermissionByCreateOrgId(QueryFilter<T> queryFilter) {
        String currentOrgId = baseContext.getCurrentOrgId();
        List<Org> orgList = orgManager.getByParentId(currentOrgId);
        List<String> collect = orgList.stream().map(Org::getId).collect(Collectors.toList());
        collect.add(currentOrgId);
        String orgIds = collect.stream().collect(Collectors.joining(","));
        queryFilter.addFilter("CREATE_ORG_ID_", String.join("','", orgIds), QueryOP.IN, FieldRelation.AND);
    }



}
