/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.youchain.report_data.rest;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.youchain.annotation.Log;
import com.youchain.annotation.rest.AnonymousDeleteMapping;
import com.youchain.annotation.rest.AnonymousGetMapping;
import com.youchain.annotation.rest.AnonymousPostMapping;
import com.youchain.annotation.rest.AnonymousPutMapping;
import com.youchain.report_data.domain.Report;
import com.youchain.report_data.service.ReportService;
import com.youchain.report_data.service.dto.ReportDto;
import com.youchain.report_data.service.dto.ReportQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
* @website https://eladmin.vip
* @author liuxue
* @date 2023-11-24
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "data_report管理")
@RequestMapping("/api/report")
public class ReportController {

    private final ReportService reportService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @AnonymousGetMapping(value = "/download")
    public void exportReport(HttpServletResponse response, ReportQueryCriteria criteria) throws IOException {
        reportService.download(reportService.queryAll(criteria), response);
    }

    @AnonymousGetMapping(value = "/list")
    @Log("查询data_report")
    @ApiOperation("查询data_report")
    public ResponseEntity<Object> queryReportList(ReportQueryCriteria criteria){
        log.error("查询所有报表");
        return new ResponseEntity<>(reportService.queryAll(criteria),HttpStatus.OK);
    }
    @AnonymousGetMapping(value = "/list/{index}")
    @Log("查询data_report")
    @ApiOperation("根据Index查询data_report")
    public ResponseEntity<Object> queryReportList(@PathVariable String index){
        log.error("查询所有报表："+index);
        ReportQueryCriteria criteria=new ReportQueryCriteria();
        ReportDto reportDto=reportService.findById(Integer.parseInt(index));
        JSONObject obj=JSONUtil.parseObj(reportDto.getContent());
        return new ResponseEntity<>(obj,HttpStatus.OK);
    }


    @AnonymousGetMapping
    @Log("查询data_report")
    @ApiOperation("查询data_report")
    public ResponseEntity<Object> queryReport(ReportQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(reportService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousPostMapping
    @Log("新增data_report")
    @ApiOperation("新增data_report")
    public ResponseEntity<Object> createReport(@Validated @RequestBody String resources){
        log.error(resources);
        Report report=new Report();
        JSONObject obj=JSONUtil.parseObj(resources);
        report.setName(obj.getStr("name"));
        report.setThumbnail(obj.getStr("thumbnail"));
        report.setContent(resources);
        return new ResponseEntity<>(reportService.create(report),HttpStatus.CREATED);
    }

    @AnonymousPutMapping(value = "/{id}")
    @Log("修改data_report")
    @ApiOperation("修改data_report")
    public ResponseEntity<Object> updateReport(@PathVariable String id, @RequestBody String  resources){
        log.error(resources);
        log.error(id);
        Report report=new Report();
        JSONObject obj=JSONUtil.parseObj(resources);
        report.setId(Integer.parseInt(id));
        report.setName(obj.getStr("name"));
        report.setThumbnail(obj.getStr("thumbnail"));
        report.setContent(resources);
        reportService.update(report);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @AnonymousDeleteMapping
    @Log("删除data_report")
    @ApiOperation("删除data_report")
    public ResponseEntity<Object> deleteReport(@RequestBody Integer[] ids) {
        reportService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousDeleteMapping(value = "/{id}")
    @Log("删除data_report")
    @ApiOperation("删除data_report")
    public ResponseEntity<Object> deleteReport(@PathVariable int id) {
        Integer [] ids=new Integer [1];
        ids[0]=id;
        reportService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}