package com.izouma.nineth.web;

import com.izouma.nineth.domain.Invite;
import com.izouma.nineth.domain.InviteDate;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.InviteDateRepo;
import com.izouma.nineth.utils.JpaUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
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("/inviteDate")
@AllArgsConstructor
public class InviteDateController  extends BaseController{
    private InviteDateRepo inviteDateRepo;


    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<InviteDate> all(@RequestBody PageQuery pageQuery) {
        return inviteDateRepo.findAll(JpaUtils.toSpecification(pageQuery, InviteDate.class), JpaUtils.toPageRequest(pageQuery));
    }

    @GetMapping("/get/{id}")
    public InviteDate get(@PathVariable Long id) {
        return inviteDateRepo.findById(id).orElseThrow(new BusinessException("邀请无记录"));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        inviteDateRepo.softDelete(id);
    }

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


}
