package com.flossom.miniProgram.controller;

import com.flossom.common.core.constant.Constants;
import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.SysFile;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.miniProgram.domain.vo.UserMemberUpdateVo;
import com.flossom.miniProgram.service.IWxUserMemberService;
import com.flossom.system.api.RemoteFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@RestController
@RequestMapping("/user")
public class UserMemberController extends BaseController {

    @Autowired
    private IWxUserMemberService wxUserMemberService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 登录接口
     *
     * @param code 临时凭证
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestParam(value = "code") String code) throws Exception {
        if (StringUtils.isBlank(code)) {
            logger.error("登录code不能为空");
            return AjaxResult.error("登录失败");
        }
        // 判断注册
        WxUserMember wxUserMember = wxUserMemberService.wxRegister(code);
        if (wxUserMember == null) {
            return AjaxResult.error("登录失败");
        }
        return AjaxResult.success(wxUserMemberService.login(wxUserMember));
    }

    /**
     * 添加手机号，升级会员
     * 1、不再需要提前调用 wx.login 进行登录
     * 2、getPhoneNumber 返回的 code 与 wx.login 返回的 code 作用是不一样的
     *
     * @param code
     * @return
     */
    @PostMapping("/upgradeMember")
    public AjaxResult upgradeMember(@RequestParam(value = "code") String code) throws Exception {
        if (StringUtils.isBlank(code)) {
            logger.error("获取手机号code不能为空");
            return AjaxResult.error("获取手机号失败");
        }
        return AjaxResult.success("获取成功", wxUserMemberService.upgradeMember(code));
    }

    /**
     * 完善用户信息
     *
     * @return
     */
    @PostMapping("/updateUser")
    public R updateUser(@RequestParam(value = "file", required = false) MultipartFile file, @Validated UserMemberUpdateVo userMemberUpdateVo) {
        if (file != null) {
            R<SysFile> result = remoteFileService.upload(file);
            if (result.getCode() != Constants.SUCCESS) {
                logger.error("上传头像失败");
                throw new ServiceException("上传头像失败");
            }
            userMemberUpdateVo.setHeadimg(result.getData().getUrl());
        }
        return R.ok(wxUserMemberService.updateUser(userMemberUpdateVo));
    }

    /**
     * 刷新用户信息
     * 1、重构用户缓存信息
     * 2、返回用户信息给前端缓存
     */
    @GetMapping("/refreshWxUserInfo")
    public R refreshWxUserInfo() {
        return R.ok(wxUserMemberService.refreshWxUserInfo());
    }
}
