package com.fs.app.controller;


import com.fs.app.exception.FSException;
import com.fs.app.utils.JwtUtils;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.store.domain.FsUser;
import com.fs.store.service.IFsUserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;


public class AppBaseController {
	@Autowired
	JwtUtils jwtUtils;
	@Autowired
	RedisCache redisCache;

	@Autowired
	private ICompanyUserService companyUserService;
	@Autowired
	private IFsUserService userService;
	public Long getCompanyId()
	{
		String headValue =  ServletUtils.getRequest().getHeader("APPToken");
		Claims claims=jwtUtils.getClaimByToken(headValue);
		String userId = claims.getSubject().toString();
		Long companyId =(Long)redisCache.getCacheObject("companyId:"+userId);
		return companyId;
	}
	public String getUserId()
	{
		String headValue =  ServletUtils.getRequest().getHeader("APPToken");
		Claims claims=jwtUtils.getClaimByToken(headValue);
		String userId = claims.getSubject().toString();
		return userId;
	}
	//获取商城手机号
	public Long getUserId(Long companyUserId)
	{
		CompanyUser companyUser=companyUserService.selectCompanyUserById(companyUserId);
		FsUser user=userService.selectFsUserByPhone(companyUser.getPhonenumber());
		if(user==null){
			throw new FSException("未授权,请用员工手机号登录商城");
		}
		return user.getUserId();
	}




}
