package com.cyksj.web.controller.market;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.UserGalaxyCoinRecordMapper;
import com.cyksj.mapper.market.sign.SignConfigMapper;
import com.cyksj.model.entity.SignConfig;
import com.cyksj.model.entity.SignUser;
import com.cyksj.model.entity.UserGalaxyCoinRecord;
import com.cyksj.service.market.MarketFrontService;
import com.cyksj.web.util.StpUserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/applets/market/sign")
@RequiredArgsConstructor
public class SignFrontController {
    private final MarketFrontService marketFrontService;

    private final UserGalaxyCoinRecordMapper userGalaxyCoinRecordMapper;

    private final SignConfigMapper signConfigMapper;

    /**
     * 用户积分详情
     */
    @GetMapping("/get/points/detail")
    public Result<Page<UserGalaxyCoinRecord>> getUserPointsDetail(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "5") Integer limit) {
        Long userId = StpUserUtil.getLoginIdAsLong();
        return GatewayResponse.SUCCESS.newBuilder().toResult(userGalaxyCoinRecordMapper.selectPage(new Page<>(start, limit), Wrappers.lambdaQuery(UserGalaxyCoinRecord.class)
                .eq(UserGalaxyCoinRecord::getUserId, userId)
                .orderByDesc(UserGalaxyCoinRecord::getId)));
    }

    /**
     * 签到详情
     */
    @GetMapping("/get/detail")
    public Result<SignUser> getUserSignDetail() {
        Long userId = StpUserUtil.getLoginIdAsLong();
        return GatewayResponse.SUCCESS.newBuilder().toResult(marketFrontService.getUserSignDetail(userId));
    }


    /**
     * 每日签到
     */
    @PostMapping("/post")
    public Result<SignUser> sign() {
        Long userId = StpUserUtil.getLoginIdAsLong();
        SignUser signUser = marketFrontService.signByUserId(userId);
        return GatewayResponse.SUCCESS.newBuilder().setMsg("签到成功").toResult(signUser);
    }

    /**
     * 积分配置
     */
    @GetMapping("/get/points/config")
    public Result<List<SignConfig>> getSign() {
        long userId = StpUserUtil.getLoginIdAsLong();
        return GatewayResponse.SUCCESS.newBuilder().toResult(signConfigMapper.selectList(Wrappers.lambdaQuery(SignConfig.class).orderByAsc(SignConfig::getId)));
    }

}
