/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.ldt.agent.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.common.constant.Agent;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.ldt.agent.entity.AgentRecord;
import org.springblade.ldt.agent.mapper.AgentRecordMapper;
import org.springblade.ldt.agent.service.IAgentRecordService;
import org.springblade.ldt.agent.vo.AgentInfoVO;
import org.springblade.ldt.agent.vo.AgentRecordVO;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2021-09-23
 */
@Service
public class AgentRecordServiceImpl extends BaseServiceImpl<AgentRecordMapper, AgentRecord> implements IAgentRecordService {
	@Autowired
	private ILoginUserService loginUserService;

	@Override
	public IPage<AgentRecordVO> selectAgentRecordPage(IPage<AgentRecordVO> page, AgentRecordVO agentRecord) {
		return page.setRecords(baseMapper.selectAgentRecordPage(page, agentRecord));
	}

	@Override
	@TenantIgnore
	public AgentInfoVO getAgentInfo(Long userId) {
		AgentInfoVO agentInfoVO = new AgentInfoVO();
		Long agentUserId = Agent.getAgentUserId(userId);
		LoginUser loginUser = null;
		if (ObjectUtil.isNotEmpty(agentUserId)) {
			loginUser = loginUserService.getById(agentUserId);
		}
		if (loginUser != null) {
			agentInfoVO.setTotalBalance(loginUser.getTotalBalance());
			agentInfoVO.setAvailableValance(loginUser.getAvailableBalance());
			agentInfoVO.setAccountName(loginUser.getNickName());
			agentInfoVO.setAvatar(loginUser.getAvatar());
			agentInfoVO.setUserType(loginUser.getUserType());
		}
		return agentInfoVO;
	}
}
