package com.gxwebsoft.common.core.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.common.core.Constants;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.system.entity.Company;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.UserService;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Controller基类
 *
 * @author WebSoft
 * @since 2017-06-10 10:10:19
 */
public class BaseController {
  @Resource
  private HttpServletRequest request;
  @Resource
  private UserService userService;
  @Resource
  private CompanyService companyService;
  @Resource
  private RedisUtil redisUtil;

  /**
   * 获取当前登录的user
   *
   * @return User
   */
  public User getLoginUser() {
    try {
      Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
      if (authentication != null) {
        Object object = authentication.getPrincipal();
        if (object instanceof User) {
          return (User) object;
        }
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    return null;
  }

  /**
   * 获取当前登录的userId
   *
   * @return userId
   */
  public Integer getLoginUserId() {
    User loginUser = getLoginUser();
    return loginUser == null ? null : loginUser.getUserId();
  }

  /**
   * 获取当前登录的tenantId
   *
   * @return tenantId
   */
  public Integer getTenantId() {
    // 1 从登录用户拿tenantId
    User loginUser = getLoginUser();
    if (loginUser != null) {
      return loginUser.getTenantId();
    }
    // 2 从请求头拿ID
    String tenantId = request.getHeader("tenantId");
    if(StrUtil.isNotBlank(tenantId)){
      return Integer.valueOf(tenantId);
    }
    // 3 从域名拿ID
    String Domain = request.getHeader("Domain");
    if (StrUtil.isNotBlank(Domain)) {
      String key = "Domain:" + Domain;
      tenantId = redisUtil.get(key);
      if(tenantId != null){
        System.out.println("从域名拿ID = " + tenantId);
        return Integer.valueOf(tenantId);
      }
    }
    return null;
  }


  /**
   * 获取当前登录的企业信息
   *
   * @return Company
   */
  public Company getCompany() {
    List<Company> list = companyService.list(new LambdaQueryWrapper<Company>().eq(Company::getAuthoritative, 1));
    if (!CollectionUtils.isEmpty(list)) {
      final Company company = list.get(0);
      return company;
    }
    return null;
  }

  public Integer getCompanyId() {
    Company company = getCompany();
    return company.getCompanyId();
  }

  /**
   * 返回成功
   *
   * @return ApiResult
   */
  public ApiResult<?> success() {
    return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG);
  }

  /**
   * 返回成功
   *
   * @param message 状态信息
   * @return ApiResult
   */
  public ApiResult<?> success(String message) {
    return success().setMessage(message);
  }

  /**
   * 返回成功
   *
   * @param data 返回数据
   * @return ApiResult
   */
  public <T> ApiResult<T> success(T data) {
    return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG, data);
  }

  /**
   * 返回成功
   *
   * @param message 状态信息
   * @return ApiResult
   */
  public <T> ApiResult<T> success(String message, T data) {
    return success(data).setMessage(message);
  }

  /**
   * 返回分页查询数据
   *
   * @param list  当前页数据
   * @param count 总数量
   * @return ApiResult
   */
  public <T> ApiResult<PageResult<T>> success(List<T> list, Long count) {
    return success(new PageResult<>(list, count));
  }

  /**
   * 返回分页查询数据
   *
   * @param iPage IPage
   * @return ApiResult
   */
  public <T> ApiResult<PageResult<T>> success(IPage<T> iPage) {
    return success(iPage.getRecords(), iPage.getTotal());
  }

  /**
   * 返回失败
   *
   * @return ApiResult
   */
  public ApiResult<?> fail() {
    return new ApiResult<>(Constants.RESULT_ERROR_CODE, Constants.RESULT_ERROR_MSG);
  }

  /**
   * 返回失败
   *
   * @param message 状态信息
   * @return ApiResult
   */
  public ApiResult<?> fail(String message) {
    return fail().setMessage(message);
  }

  /**
   * 返回失败
   *
   * @param data 返回数据
   * @return ApiResult
   */
  public <T> ApiResult<T> fail(T data) {
    return fail(Constants.RESULT_ERROR_MSG, data);
  }

  /**
   * 返回失败
   *
   * @param message 状态信息
   * @param data    返回数据
   * @return ApiResult
   */
  public <T> ApiResult<T> fail(String message, T data) {
    return new ApiResult<>(Constants.RESULT_ERROR_CODE, message, data);
  }

  /**
   * 请求参数的空字符串转为null
   */
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
  }

  // 自定义函数
  public String getAuthorization() {
    return request.getHeader("Authorization");
  }

  public String getSign() {
    return request.getParameter("sign");
  }



  /**
   * 根据账号|手机号码|邮箱查找用户ID
   *
   * @return userId
   */
  public Integer getUserIdByUsername(String username, Integer tenantId) {
    // 按账号搜素
    User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username).eq(User::getTenantId, tenantId));
    if (user != null && user.getUserId() > 0) {
      return user.getUserId();
    }
    // 按手机号码搜索
    User userByPhone = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, username).eq(User::getTenantId, tenantId));
    if (userByPhone != null && userByPhone.getUserId() > 0) {
      return userByPhone.getUserId();
    }
    // 按邮箱搜索
    User userByEmail = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, username).eq(User::getTenantId, tenantId));
    if (userByEmail != null && userByEmail.getUserId() > 0) {
      return userByEmail.getUserId();
    }
    throw new BusinessException("找不到该用户");
  }

}
