package cn.wisenergy.web.admin.controller.app;

import cn.wisenergy.common.constant.RedisConsts;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.common.utils.RedisUtils;
import cn.wisenergy.common.utils.StringUtil;
import cn.wisenergy.mapper.RecommendUserMapper;
import cn.wisenergy.mapper.TeamUserInfoMapper;
import cn.wisenergy.model.app.RecommendUser;
import cn.wisenergy.model.app.TeamUserInfo;
import cn.wisenergy.model.app.User;
import cn.wisenergy.model.app.UsersDto;
import cn.wisenergy.model.vo.UserAndRecommendVo;
import cn.wisenergy.service.app.RecommendUserService;
import cn.wisenergy.service.app.TeamUserInfoService;
import cn.wisenergy.service.app.UserForestStatusService;
import cn.wisenergy.service.app.UserService;
import cn.wisenergy.web.common.BaseController;
import cn.wisenergy.web.config.JwtConfig;
import cn.wisenergy.web.shiro.JwtUtil;
import cn.wisenergy.web.sms.*;
import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Month;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 86187
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserForestStatusService userForestStatusService;

    @Autowired
    RecommendUserService recommendUserService;

    @Autowired
    TeamUserInfoService teamUserInfoService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private JwtConfig jwtConfig;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String")
    @GetMapping("/getByUserId")
    public User getByUserId(String userId){
        return userService.getById(userId);
    }

    /**
     * shiro登录
     *
     * @return
     */
    @ApiOperation(value = "获取token接口", notes = "获取token接口", httpMethod = "POST")
    @PostMapping(value = "/login")
    public R<String> login(String id) {
        if (null == id) {
            return R.error("入参为空!");
        }

        //用户信息
        User user = userService.getByUserId(id);

        // 创建token
        String token = jwtUtil.generateToken(user);
        // 保存Redis
        redisTemplate.opsForValue().set(RedisConsts.JWT_ACCESS_TOKEN + token, token);
        return R.ok(token);
    }

    @ApiOperation(value = "获取用户森林状态接口", notes = "获取用户森林状态接口", httpMethod = "GET")
    @GetMapping("/forestStatus")
    public R<Map> forestStatus(String userId){
        if (null == userId){
            return R.error("入参为空");
        }

        try {
            Map userForestStatus = userForestStatusService.getUserForestStatus(userId);
            return R.ok(userForestStatus);
        } catch (Exception e) {
            return R.error(1,"请重新尝试");
        }
    }

    @ApiOperation(value = "获取用户我的直推信息", notes = "获取用户我的直推信息", httpMethod = "GET")
    @GetMapping("/myRecommend")
    public R<Map> getMyRecommend(String userId){
        int userLevel = userService.getByUserId(userId).getUserLevel();
        RecommendUser myRecommed = recommendUserService.getMyRecommed(userId);
        Map myRecommedMap = new HashMap();
        myRecommedMap.put("userlevel",userLevel);
        myRecommedMap.put("myRecommed",myRecommed);
        return R.ok(myRecommedMap);
    }

    @ApiOperation(value = "获取用户我的团队信息", notes = "获取用户我的团队推信息", httpMethod = "GET")
    @GetMapping("/myTeam")
    public R<TeamUserInfo> getMyTeam(String userId){
        TeamUserInfo myTeam = teamUserInfoService.getOneById(userId);
        return R.ok(myTeam);
    }

    @ApiOperation(value = "获取用户我的直推信息详情", notes = "获取用户我的直推信息详情", httpMethod = "GET")
    @GetMapping("/myRecommendInfo")
    public R<List<UserAndRecommendVo>> getMyRecommendInfo(@RequestParam("userid") String userid, @RequestParam("userlevel") int userlevel){
        List<UserAndRecommendVo> myRecommendInfo = recommendUserService.getMyRecommendInfo(userid, userlevel);
        return R.ok(myRecommendInfo);
    }


}
