package com.izouma.jmrh.web;

import com.izouma.jmrh.domain.ExceptionLog;
import com.izouma.jmrh.dto.PageQuery;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.repo.AuthorityRepo;
import com.izouma.jmrh.repo.DataCenterAuthorityRepo;
import com.izouma.jmrh.security.Authority;
import com.izouma.jmrh.security.DataCenterAuthority;
import com.izouma.jmrh.service.AuthorityService;
import com.izouma.jmrh.utils.ObjUtils;
import com.izouma.jmrh.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/dataCenterAuthority")
@AllArgsConstructor
public class DataCenterAuthorityController extends BaseController {
    private DataCenterAuthorityRepo authorityRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public DataCenterAuthority save(@RequestBody DataCenterAuthority record) {
        return authorityRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<DataCenterAuthority> all(PageQuery pageQuery) {
        return authorityRepo.findAll(toSpecification(pageQuery, DataCenterAuthority.class), toPageRequest(pageQuery));
    }

    @GetMapping("/get/{id}")
    public DataCenterAuthority get(@PathVariable String id) {
        return authorityRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable String id) {
        authorityRepo.deleteById(id);
    }

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

