package com.izouma.wenlvju.web;

import com.izouma.wenlvju.domain.EzvizToken;
import com.izouma.wenlvju.service.EzvizTokenService;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.EzvizTokenRepo;
import com.izouma.wenlvju.utils.ObjUtils;
import com.izouma.wenlvju.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
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("/ezvizToken")
@AllArgsConstructor
public class EzvizTokenController extends BaseController {
    private final EzvizTokenService ezvizTokenService;
    private final EzvizTokenRepo    ezvizTokenRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public EzvizToken save(@RequestBody EzvizToken record) {
        if (record.getId() != null) {
            EzvizToken orig = ezvizTokenRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return ezvizTokenRepo.save(orig);
        }
        return ezvizTokenRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<EzvizToken> all(@RequestBody PageQuery pageQuery) {
        return ezvizTokenService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public EzvizToken get(@PathVariable Long id) {
        return ezvizTokenRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

//    @PostMapping("/del/{id}")
//    public void del(@PathVariable Long id) {
//        ezvizTokenRepo.softDelete(id);
//    }

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

    @GetMapping("/getToken")
    @ApiOperation("获取token")
    public String getToken() {
        return ezvizTokenService.getToken();
    }
}

