package org.springblade.gateway.web_gateway.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.secure.BladeUser;
import org.springblade.gateway.web_gateway.service.ICensusService;
import org.springblade.ldt.agent.entity.AgentRecord;
import org.springblade.ldt.agent.service.IAgentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ICensusServiceImpl implements ICensusService {
	@Autowired
	private IAgentRecordService agentRecordService;

	@Override
	public Long getAgentUserId(BladeUser bladeUser) {
		return getAgentUserId(bladeUser.getUserId());
	}

	@Override
	public Long getAgentUserId(Long userId) {
		AgentRecord agentRecord = agentRecordService.getOne(Wrappers.<AgentRecord>lambdaQuery()
			.select(AgentRecord::getAccountId) //只查询代理的id
			.eq(AgentRecord::getUserId, userId) //根据用户id去查询代理id
			.last("limit 0,1")); //如果有多个默认取第一个
		if(agentRecord!=null){
			return agentRecord.getAccountId();
		}
		return null;
	}
}
