package com.lightinit.hsdataplatformresdir.impl.admin;

import com.lightinit.hsdataplatformresdir.entity.Admin;
import com.lightinit.hsdataplatformresdir.mapper.AdminMapper;
import com.lightinit.hsdataplatformresdir.mapper.SentryRoleMapper;
import com.lightinit.hsdataplatformresdir.model.admin.LoginUser;
import com.lightinit.hsdataplatformresdir.service.admin.IAccountService;
import com.lightinit.hsdataplatformresdir.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by WangYao on 2018/5/28.
 */
@Service
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private SentryRoleMapper sentryRoleMapper;

    public LoginUser getLoginUserByUsername(String username) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        LoginUser loginUser = new LoginUser();
        Admin admin = adminMapper.selectByUserName(username);
        BeanUtils.copyProperties(admin, loginUser);

        List<String> roleList = sentryRoleMapper.selectByUserName(username).stream().map(q -> q.getRolecode()).collect(Collectors.toList());
//        String[] roles = new String[roleList.size()];
//        roleList.toArray(roles);
        loginUser.setRoles(roleList);

        return loginUser;
    }
}
