package com.izouma.nineth.web.rewardpoints;

import com.izouma.nineth.domain.User;
import com.izouma.nineth.domain.UserIntegral;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.service.rewardpoints.UserIntegralService;
import com.izouma.nineth.utils.excel.ExcelUtils;
import com.izouma.nineth.web.BaseController;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户积分
 *
 */
@RestController
@RequestMapping("/userIntegral")
@AllArgsConstructor
public class UserIntegralController extends BaseController {

    private UserIntegralService userIntegralService;


    @PostMapping("/all")
    public Page<UserIntegral> all(@RequestBody PageQuery pageQuery) {
        return userIntegralService.all(pageQuery);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<UserIntegral> data = userIntegralService.all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/get/{id}")
    public UserIntegral get(@PathVariable Long id) {
        //获取用户积分
        return userIntegralService.getAndInitUserIntegral(id);
    }

    /**
     * 获取用户积分信息
     * @return
     */
    @RequestMapping("getInfo")
    public UserIntegral getInfo(){
        User user = currentUser();
        UserIntegral userIntegral = userIntegralService.getAndInitUserIntegral(user.getId());
        return userIntegral;
    }




}
