package com.hzs.system.sys.provider;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.sys.SysUser;
import com.hzs.common.domain.system.sys.SysUserCountry;
import com.hzs.common.domain.system.sys.SysUserVertex;
import com.hzs.system.sys.IUserServiceApi;
import com.hzs.system.sys.dto.LoginUser;
import com.hzs.system.sys.dto.UserAuthorityDTO;
import com.hzs.system.sys.service.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户涉及dubbo服务
 */
@DubboService
public class UserServiceProvider implements IUserServiceApi {

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysUserCountryService iSysUserCountryService;
    @Autowired
    private ISysRoleScopeService iSysRoleScopeService;
    @Autowired
    private ISysUserVertexService iSysUserVertexService;
    @Autowired
    private ISysUserTeamService iSysUserTeamService;

    @Override
    public R<SysUser> selectUserById(Long userId) {
        return R.ok(iSysUserService.selectUserById(userId));
    }

    @Override
    public R<LoginUser> getUserInfo(String username) {
        SysUser sysUser = iSysUserService.selectUserByUserName(username);
        if (StringUtils.isNull(sysUser)) {
            return R.fail("用户名或密码错误");
        }

        // 处理多国权限
        Integer pkCountry = sysUser.getPkCountry();
        if (CountryConstants.DEFAULT_ADMIN_COUNTRY == pkCountry) {
            SysUserCountry userCountry = iSysUserCountryService.queryDefaultUserCountry(sysUser.getUserId());
            if (null != userCountry) {
                pkCountry = userCountry.getPkCountry();
            }
        }

        LoginUser loginUser = new LoginUser();
        loginUser.setLoginSysUser(sysUser);
        loginUser.setDataCountry(pkCountry);
        return R.ok(loginUser);
    }

    @Override
    public R<UserAuthorityDTO> getUserAuthority(Long userId) {
        UserAuthorityDTO resultDto = UserAuthorityDTO.builder()
                // 角色地区范围
                .roleAreaScopeList(iSysRoleScopeService.queryScopeByUser(userId))
                // 管理员团队会员
                .userTeamList(iSysUserTeamService.queryTeamByUser(userId))
                .build();

        List<SysUserVertex> userVertexList = iSysUserVertexService.queryVertexByUser(userId);
        if (CollectionUtil.isNotEmpty(userVertexList)) {
            // 会员ID列表
            List<Long> userIdList = new ArrayList<>(userVertexList.size());
            // 体系ID列表
            List<Integer> vertexIdList = new ArrayList<>(userVertexList.size());

            for (SysUserVertex sysUserVertex : userVertexList) {
                userIdList.add(sysUserVertex.getUserId());
                vertexIdList.add(sysUserVertex.getPkVertex());
            }
            // 管理员体系会员ID列表
            resultDto.setUserVertexList(userIdList);
            // 管理员体系ID列表
            resultDto.setVertexIdList(vertexIdList);
        }

        return R.ok(resultDto);
    }

}
