package com.kym.miniapp.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.kym.common.R;
import com.kym.entity.common.PageBean;
import com.kym.entity.User;
import com.kym.entity.queryParams.WxLoginParams;
import com.kym.entity.vo.UserVo;
import com.kym.service.CollectService;
import com.kym.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author skyline
 * @since 2023-06-27
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    private final CollectService collectService;


    public UserController(UserService userService, CollectService collectService) {
        this.userService = userService;
        this.collectService = collectService;
    }

    /**
     * 微信登录/注册
     *
     * @param params
     * @return
     */
    @PostMapping("/wxLogin")
    @SaIgnore
    public R<?> login(@RequestBody @Validated WxLoginParams params) {
        return userService.wxLogin(params);
    }

    /**
     * 登出
     *
     * @return
     */
    @GetMapping("/logout")
    public R<?> logout() {
        StpUtil.logout();
        return R.success();
    }

    /**
     * 当前用户信息
     *
     * @return
     */
    @GetMapping("/me")
    public R<?> me() {
        return R.success(userService.getMe());
    }

    /**
     * 用户列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("listUser")
    PageBean<UserVo> listUser(@RequestParam int pageNum, @RequestParam int pageSize) {
        return userService.listUserVo(pageNum, pageSize);
    }

    /**
     * 更新用户信息
     *
     * @param userVo
     * @return
     */
    @PostMapping("updateUser")
    R<?> updateUser(@RequestBody UserVo userVo) {
        userService.updateUser(userVo);
        return R.success();
    }

    /**
     * 收藏列表
     *
     * @return
     */
    @GetMapping("/collectList")
    R<?> listCollect() {
        return R.success(collectService.listCollect());
    }

    /**
     * 添加/取消收藏
     *
     * @param json
     * @return
     */
    @PostMapping("/collect")
    R<?> saveCollect(@RequestBody JSONObject json) {
        collectService.updateCollect(json.getString("stationId"), json.getInteger("status"));
        return R.success();
    }

}
