package com.kym.miniapp.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.kym.common.R;
import com.kym.common.utils.IDGenerator;
import com.kym.entity.common.PageBean;
import com.kym.entity.miniapp.User;
import com.kym.entity.miniapp.queryParams.WxLoginParams;
import com.kym.entity.miniapp.vo.UserVo;
import com.kym.service.miniapp.CollectService;
import com.kym.service.miniapp.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;

    private final IDGenerator idGenerator;

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

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

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

    /**
     * 刷新token
     *
     * @return
     */
    @GetMapping("/refresh")
    public R<?> refreshToken() {
        // 365天有效期，微信手机号接口开始收费了，有效期给长一点。
        StpUtil.renewTimeout(3600 * 24 * 365L);
        return R.success();
    }

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

    /**
     * 获取单个用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    User getUser(@PathVariable("id") Long id) {
        return userService.getById(id);
    }

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

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping
    Boolean saveUser(@RequestBody User user) {
        user.setId(idGenerator.nextId());
        return userService.save(user);
    }

    /**
     * 更新用户信息
     *
     * @param userVo
     * @return
     */
    @PutMapping
    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();
    }

}
