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.enums.BusinessType;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.WxTokenService;
import com.zhongzheng.modules.user.bo.RanKingUserQuery;
import com.zhongzheng.modules.user.bo.UserAppAccountLoginBo;
import com.zhongzheng.modules.user.bo.UserAppBindIdBo;
import com.zhongzheng.modules.user.bo.UserEditBo;
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.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
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());
        return toAjax(iUserService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 收藏数等数据
     */
    @ApiOperation("我的列表数据")
    @GetMapping("/infoAttached")
    public AjaxResult infoAttached() {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        Map<String,Object> map = new HashMap<>();
        Long planSum = iUserService.planSum(loginUser.getUser().getUserId());
        Long periodSum = iUserService.periodSum(loginUser.getUser().getUserId());
        Long subscribeSum = iUserService.subscribeSum(loginUser.getUser().getUserId());
        Long informSum = iUserService.informSum(loginUser.getUser().getUserId());
        Long orderSum = iUserService.orderSum(loginUser.getUser().getUserId());
        map.put("planSum",planSum);
        map.put("periodSum",periodSum);
        map.put("subscribeSum",subscribeSum);
        map.put("informSum",informSum);
        map.put("orderSum",orderSum);
        return AjaxResult.success(map);
    }

    @ApiOperation("绑定身份证信息")
    @PostMapping("/bind_idcard")
    public AjaxResult<Void> bind_id(@RequestBody UserAppBindIdBo bo) {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        bo.setUserId(loginUser.getUser().getUserId());
        return toAjax(iUserService.bindIdCard(bo) ? 1 : 0);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("登录用户信息")
    @GetMapping("getInfo")
    public AjaxResult<UserVo> getInfo()
    {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        UserVo vo = iUserService.queryById(loginUser.getUser().getUserId());
        vo.setNull();
        return AjaxResult.success(vo);
    }

}
