package com.ichangzuo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ichangzuo.common.result.PageResult;
import com.ichangzuo.core.security.util.SecurityUtils;
import com.ichangzuo.system.model.query.IntegralPageQuery;
import com.ichangzuo.system.model.vo.IntegralAdminPageVO;
import com.ichangzuo.system.model.vo.IntegralUserPageVO;
import com.ichangzuo.system.service.impl.IntegralServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/integral")
@RequiredArgsConstructor
public class IntegralController {
    private final IntegralServiceImpl integralService;

    @GetMapping("/pageAdmin")
    public PageResult<IntegralAdminPageVO> listAdminPage(
            IntegralPageQuery queryParams
    ){
        IPage<IntegralAdminPageVO> page = integralService.listPagedAdmin(queryParams);
        return PageResult.success(page);
    }

    @GetMapping("/pageUser")
    public PageResult<IntegralUserPageVO> listUserPage(
            IntegralPageQuery queryParams
    ){
        Long userId = SecurityUtils.getUserId();
        if(queryParams.getUserId()==null){
            queryParams.setUserId(userId);
        }
        else if(!userId.equals(queryParams.getUserId())) {
            return PageResult.success(new Page<>());
        }

        return PageResult.success(integralService.listPagedUser(queryParams));
    }
}
