package com.ruoyi.api.controller.user;

import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.RegGroup;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.framework.web.service.ApiTokenService;
import com.ruoyi.system.enums.SequencePrefixEnum;
import com.ruoyi.system.service.ISysSequenceService;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.enums.UserThirdType;
import com.ruoyi.user.service.IUserService;
import com.ruoyi.weixin.domain.WxUserDto;
import com.ruoyi.weixin.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 小程序用户管理Controller
 *
 * @author wuchao
 * @date 2022-03-03
 */
@Validated
@Api(value = "小程序用户管理控制器", tags = {"小程序用户管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/miniapp/user")
public class ApiUserLoginController extends AbstractApiController {
    @Autowired
    private IUserService userService;

    @Autowired
    private ApiTokenService apiTokenService;

    private final WxUserService wxUserService;

    private final ISysSequenceService sysSequenceService;

    private final StringRedisTemplate stringRedisTemplate;

    /**
     * 获取小程序openId
     */
    @ApiOperation("获取小程序openId")
    @PostMapping("/user/getOpenId")
    public R<Map<String, Object>> getOpenId(@RequestBody WxUserDto dto) {
        String openId = userService.getOpenId(dto.getCode());
        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("openId", openId);
        return R.ok(stringObjectHashMap);
    }


    /**
     * 小程序授权登录
     */
    @ApiOperation("小程序授权登录")
    @PostMapping("/user/authorization/login")
    public R<Map<String, Object>> loginAuthorization(@RequestBody  WxUserDto dto) {
        User user = userService.loginAuthorization(dto.getOpenId());
        String token = apiTokenService.generateToken(user);
        HashMap<String, Object> result = new HashMap<>();
        result.put("token", token);
        return R.ok(result);
    }

    /**
     * 小程序获取手机号
     */
    @ApiOperation("小程序获取手机号")
    @PostMapping("/user/phone")
    public R<Map<String, Object>> wxPhone(@RequestBody WxUserDto dto) {
        WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxUserService.getPhoneByCode(dto.getCode());
        HashMap<String, Object> result = new HashMap<>();
        result.put("phone", wxMaPhoneNumberInfo.getPhoneNumber());
        return R.ok(result);
    }

    /**
     * 注册用户，小程序授权且获取了手机号
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("注册用户，小程序授权且获取了手机号")
    @PostMapping("/user/authorization/register")
    public R<Map<String, Object>> registerAuthorization(@RequestBody @Validated(RegGroup.class) WxUserDto wxUserDto) {
        wxUserDto.setBusinessId(sysSequenceService.generateSequence(SequencePrefixEnum.PRE_USER, DateUtil.format(new Date(), "yyMMdd"), 6));
        User user = userService.authorization(wxUserDto);
        String token = apiTokenService.generateToken(user);
        HashMap<String, Object> result = new HashMap<>();
        result.put("token", token);
        return R.ok(result);
    }

    @ApiOperation("登出方法")
    @PostMapping("/wxLogout")
    public R<Void> wxLogout() {
        userService.logout(getUserId(true), UserThirdType.WX_MINI_PROGRAM.getCode());
        return R.ok("退出成功");
    }
}
