/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.ruoyi.api.controller.common;

import cn.hutool.core.util.ObjectUtil;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.UserExtendInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.user.ApiUserException;
import com.ruoyi.framework.interceptor.UserContext;
import com.ruoyi.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Controller公共组件
 *
 * @author Mark sunlightcs@gmail.com
 */
public abstract class AbstractApiController {

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    /**
     * 获取会员id
     *
     * @return
     */
    protected Long getUserId() {
        //isThrow 是否抛异常，不抛的话返回常量0
        User user = UserContext.currentUser();
        if (ObjectUtil.isNull(user)){
            throw new ServiceException(ApiUserException.USER_NOT_EXISTS);
        }
        return user.getId();

    }
    protected Long getUserId(Boolean tw) {
        //isThrow 是否抛异常，不抛的话返回常量0
        User user = UserContext.currentUser();
        if (tw && ObjectUtil.isNull(user)){
            throw new ServiceException(ApiUserException.USER_NOT_EXISTS);
        }
        return ObjectUtil.isNull(user)?null:user.getId();
    }

    /**
     * 获取当前登录老师
     *
     * @return
     */
    protected User getLoginUser() {
        return UserContext.currentUser();
    }

    protected Long getTeacherId() {
        //isThrow 是否抛异常，不抛的话返回常量0
        User user = UserContext.currentUser();
        if (ObjectUtil.isNull(user)){
            throw new ServiceException(ApiUserException.USER_NOT_EXISTS);
        }
        UserExtendInfo userExtendInfo = user.getUserExtendInfo();
        if (ObjectUtil.isNull(userExtendInfo)){
            throw new ServiceException(ApiUserException.USER_IS_NOT_TEACHER);
        }
        Long teacherId = userExtendInfo.getTeacherId();
        if (ObjectUtil.isNull(teacherId)){
            throw new ServiceException(ApiUserException.USER_IS_NOT_TEACHER);
        }
        return teacherId;
    }

    protected Long getWorkerId() {
        //isThrow 是否抛异常，不抛的话返回常量0
        User user = UserContext.currentUser();
        if (ObjectUtil.isNull(user)){
            throw new ServiceException(ApiUserException.USER_NOT_EXISTS);
        }
        UserExtendInfo userExtendInfo = user.getUserExtendInfo();
        if (ObjectUtil.isNull(userExtendInfo)){
            throw new ServiceException(ApiUserException.USER_IS_NOT_WORKER);
        }
        Long workerId = userExtendInfo.getWorkerId();
        if (ObjectUtil.isNull(workerId)){
            throw new ServiceException(ApiUserException.USER_IS_NOT_WORKER);
        }
        return workerId;
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected R<Void> toAjax(int rows) {
        return rows > 0 ? R.ok() : R.fail();
    }

}
