package org.springblade.system.expand.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.system.expand.entity.OrgPosition;
import org.springblade.system.expand.feign.IOrgPositionClient;
import org.springblade.system.expand.vo.OrgPositionVO;

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

/**
 * Created By lidexi in 2021/3/30
 **/
public class QueryWrapperUtil<T> {

	public  LambdaQueryWrapper<T> getBasicQueryWrapper(SFunction<T,String> function, T t, BladeUser bladeUser, IOrgPositionClient orgPositionClient) {
		String deptId = bladeUser.getDeptId();
		String[] deptIds = deptId.split(",");
		LambdaQueryWrapper<T> wrapper = Condition.getQueryWrapper(t).lambda();
		for (int i = 0; i < deptIds.length; i++) {
			OrgPosition select = new OrgPosition();
			select.setOrgId(Long.valueOf(deptIds[i]));
			List<OrgPositionVO> list = orgPositionClient.list(select);
			OrgPosition orgPosition = null;
			if(list.size()>0){
				orgPosition = list.get(0);
			}else{
				continue;
			}
			if (i == 0) {
				wrapper.like(function,orgPosition.getOrgPosition());
			}else{
				wrapper.or().like(function,orgPosition.getOrgPosition());
			}
		}
		return wrapper;
	}


	public LambdaQueryWrapper<T> getBasicQueryWrapperByMap(SFunction<T,String> function, Map map, T t, BladeUser bladeUser,IOrgPositionClient orgPositionClient) {
		String deptId = bladeUser.getDeptId();
		String[] deptIds = deptId.split(",");
		LambdaQueryWrapper<T> wrapper = Condition.getQueryWrapper(map,t.getClass()).lambda();
		for (int i = 0; i < deptIds.length; i++) {
			OrgPosition select = new OrgPosition();
			select.setOrgId(Long.valueOf(deptIds[i]));
			List<OrgPositionVO> list = orgPositionClient.list(select);
			OrgPosition orgPosition = null;
			if(list.size()>0){
				orgPosition = list.get(0);
			}else{
				continue;
			}
			if (i == 0) {
				wrapper.like(function,orgPosition.getOrgPosition());
			}else{
				wrapper.or().like(function,orgPosition.getOrgPosition());
			}
		}
		return wrapper;
	}
}
