package com.fs.app.controller;


import cn.hutool.core.util.ObjectUtil;
import com.fs.app.exception.FSException;
import com.fs.app.utils.JwtUtils;
import com.fs.common.constant.HttpStatus;
import com.fs.common.core.page.PageDomain;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.core.page.TableSupport;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.sql.SqlUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.his.domain.FsUser;
import com.fs.his.service.IFsUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.TimeUnit;


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);
		if (ObjectUtil.isEmpty(claims)){
			throw new FSException("未授权,请先登录！");
		}
		String userId = claims.getSubject().toString();
		Long companyId =(Long)redisCache.getCacheObject("companyId:"+userId);
		if (companyId==null){
			CompanyUser companyUser = companyUserService.selectCompanyUserById(Long.parseLong(userId));
			companyId = companyUser.getCompanyId();
			redisCache.setCacheObject("companyId:" + companyUser.getUserId(), companyUser.getCompanyId(), 604800, TimeUnit.SECONDS);
		}
		return companyId;
	}
	public String getUserId()
	{
		String headValue =  ServletUtils.getRequest().getHeader("APPToken");
		Claims claims=jwtUtils.getClaimByToken(headValue);
		if (ObjectUtil.isEmpty(claims)){
			throw new FSException("未授权,请先登录！");
		}
		String userId = claims.getSubject();
		return userId;
	}
	public Long getCompanyUserId() {
		String headValue =  ServletUtils.getRequest().getHeader("APPToken");
		Claims claims=jwtUtils.getClaimByToken(headValue);
		String userId = claims.getSubject();
		return Long.parseLong(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();
	}
    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            Boolean reasonable = pageDomain.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
}
