package com.xunwang.ucenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.ucenter.entity.MpOpenidUid;
import com.xunwang.ucenter.service.MiniProgramOpenidUidService;
import com.xunwang.ucenter.utils.WxConstantUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Long
 * @since 2022-11-09
 */
@RestController
@RequestMapping("ucenter/mini-program-openid-uid")
@Api("用户控制器")
@ApiSupport(author = "Long")
public class MiniProgramOpenidUidController {

    @Autowired
    private MiniProgramOpenidUidService uidService;

    @ApiOperation("小程序登录")
    @GetMapping("wxlogin")
    public R wxloginUser(HttpServletRequest request) throws Exception {
        String code = request.getParameter("code");
        String recoUID = request.getParameter("recoUID");

        Map<String, Object> map = uidService.getOpenId(request, WxConstantUtils.APPID, WxConstantUtils.SECRET, code, WxConstantUtils.GRANT_TYPE,recoUID);
        return R.ok().data(map);
    }

    @ApiOperation("修改昵称和头像")
    @GetMapping("wxAlias")
    public R wxEditAlias(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String alias = request.getParameter("alias");
        String avatar = request.getParameter("avatar");
        uidService.editAlias(uid,alias,avatar);
        return R.ok().message("更新昵称成功");
    }

    @ApiOperation("获取微信手机号")
    @GetMapping("wxGetPhone")
    public R wxPhone(HttpServletRequest request) throws Exception {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String code = request.getParameter("code");
        String phone = uidService.getUserPhone(uid, code);
        return R.ok().data("phone", phone);
    }

    @ApiOperation("根据uid获取openid")
    @PostMapping("getOpenIdByUid")
    public R getOpenIdByUid(@RequestBody String uid) {
        MpOpenidUid one = uidService.getOne(new LambdaQueryWrapper<MpOpenidUid>().eq(MpOpenidUid::getUid, uid));
        String openid = one.getOpenid();
        return R.ok().data("openid", openid);
    }

    @ApiOperation("根据uid获取Phone")
    @PostMapping("getPhoneByUid")
    public String getPhoneByUid(@RequestBody String uid) {
        MpOpenidUid one = uidService.getOne(new LambdaQueryWrapper<MpOpenidUid>().eq(MpOpenidUid::getUid, uid));
        return one.getPhone();
    }

    @ApiOperation("根据uid获取openid")
    @PostMapping("getUnionIdByUid")
    public String getUnionIdByUid(@RequestBody String uid) {
        MpOpenidUid one = uidService.getOne(new LambdaQueryWrapper<MpOpenidUid>().eq(MpOpenidUid::getUid, uid));
        return one.getUnionid();
    }

    @ApiOperation("根据uid获取recommendUid")
    @PostMapping("getRecommendUidByUid")
    public R getRecommendUidByUid(@RequestBody String uid) {
        LambdaQueryWrapper<MpOpenidUid> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MpOpenidUid::getUid, uid);
        MpOpenidUid one = uidService.getOne(wrapper);
        String recommendUid = one.getRecommendUid();
        return R.ok().data("recommendUid", recommendUid);
    }

    @ApiOperation("根据uid获取手机号")
    @GetMapping("/getPhoneByUid")
    public R getPhoneByUid(HttpServletRequest request) {
        String uid = request.getParameter("uid");
        MpOpenidUid one = uidService.getOne(new LambdaQueryWrapper<MpOpenidUid>().eq(MpOpenidUid::getUid, uid));
        String phone = one.getPhone();
        return R.ok().data("phone", phone);
    }

    @ApiOperation("通过Uid获取用户昵称")
    @RequestMapping("/getUserName")
    public R getUserName(@RequestBody String uid) {
        List<MpOpenidUid> list = uidService.list(new QueryWrapper<MpOpenidUid>().eq("uid", uid).select("alias"));
        return R.ok().data("name", list.get(0).getAlias());
    }

    @ApiOperation("根据uid获取 推荐成功注册的条数")
    @GetMapping("/getShareCount")
    public R getShareCount(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        int result = uidService.getShareCount(uid);
        return R.ok().data("result",result);
    }

    @ApiOperation("根据uid获取 推荐成功，发布的 课程和需求 条数")
    @GetMapping("/getSharePublishCount")
    public R getSharePublishCount(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        int result = uidService.getSharePublishCount(uid);
        return  R.ok().data("result",result);
    }

    @ApiOperation("根据uid获取 推荐成功 对应的 未退款的订单条数")
    @GetMapping("/getShareOrderCount")
    public R getShareOrderCount(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        int result = uidService.getShareOrderCount(uid);
        return  R.ok().data("result",result);
    }

}

