package com.zhongzheng.controller.user;

import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.core.page.TableDataInfo;
import com.zhongzheng.common.enums.BusinessType;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.common.utils.poi.ExcelUtil;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.course.vo.CourseVo;
import com.zhongzheng.modules.user.bo.RanKingUserQuery;
import com.zhongzheng.modules.user.bo.UserAddBo;
import com.zhongzheng.modules.user.bo.UserEditBo;
import com.zhongzheng.modules.user.bo.UserQueryBo;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import com.zhongzheng.modules.user.service.IUserService;
import com.zhongzheng.modules.user.vo.RanKingUser;
import com.zhongzheng.modules.user.vo.UserBankRecordVo;
import com.zhongzheng.modules.user.vo.UserStudyRecordVo;
import com.zhongzheng.modules.user.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户端用户Controller
 *
 * @author hjl
 * @date 2021-06-08
 */
@Api(value = "用户控制器", tags = {"客户端用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/app/user")
public class UserController extends BaseController {

    private final IUserService iUserService;

    private final WxTokenService wxTokenService;

    /**
     * 修改客户端用户
     */
    @ApiOperation("修改客户端用户")
    @Log(title = "客户端用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult<Void> edit(@RequestBody UserEditBo bo) throws IllegalAccessException {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        bo.setNull();
        return toAjax(iUserService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 收藏数等数据
     */
    @ApiOperation("收藏数等数据")
    @GetMapping("/infoAttached")
    public AjaxResult infoAttached() {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        int collectTotal = iUserService.getCollectTotal(loginUser.getUser().getUserId());
        Integer rank = iUserService.getRank(loginUser.getUser().getUserId());
        Integer issue = iUserService.getIssue(loginUser.getUser().getUserId());
        Map<String,Object> map = new HashMap<>();
        map.put("collectTotal",collectTotal);
        map.put("rank",rank);
        map.put("issue",issue);
        return AjaxResult.success(map);
    }

    /**
     * 排行榜
     */
    @ApiOperation("排行榜")
    @GetMapping("/rankingList")
    public AjaxResult<RanKingUser> rankingList(RanKingUserQuery bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        RanKingUser list = iUserService.rankingList(bo);
        return AjaxResult.success(list);
    }

}
