package com.xingxi.api.common;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;


@Slf4j
@RequiredArgsConstructor
public abstract class BaseApiController {

    private Authentication getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            return authentication;
        } else {
            throw new RuntimeException("未登录");
        }
    }

    protected UserContext getUserContext() {
        Authentication authentication = getAuthentication();
        return (UserContext) authentication.getPrincipal();

//        Object principal = authentication.getPrincipal();
//        if (!(principal instanceof SysUser)) {
//            log.debug("未登录[" + principal + "]");
//            throw new RuntimeException("未登录");
//        }
//
//        SysUser sysUser = (SysUser) principal;
//        Long sellerId = sysUser.getSellerId();
//
//        MercDTO mercDTO = merchantRepository.selectMercByMercId(sellerId);
//        if (mercDTO == null) {
//            log.debug("商户[" + sellerId + "]不存在");
//            throw new RuntimeException("商户不存在");
//        }
//
//        SysDept sysDept = sysDeptMapper.selectDeptById(sellerId);
//        mercDTO.setParentMercId(sysDept.getParentId());
//        mercDTO.setAfterContacter(sysDept.getAfterContacter());
//        mercDTO.setAfterMobile(sysDept.getAfterMobile());
//        mercDTO.setAfterAddress(sysDept.getAfterAddress());
//
//        return new UserContext(
//                sysUser.getUserId(),
//                sysUser.getUserName(),
//                sysUser.getSex(),
//                sysUser.getAvatar(),
//                sysUser.getUserType(),
//                sysUser.getWxOpenId(),
//                sysUser.getPhonenumber(),
//                sysUser.getWxSessionKey(),
//                mercDTO
//        );


    }

}
