package com.meas.system.controller;

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

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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 com.meas.common.annotation.Log;
import com.meas.common.core.controller.BaseController;
import com.meas.common.core.domain.AjaxResult;
import com.meas.common.enums.BusinessType;
import com.meas.system.domain.MeasDataResults;
import com.meas.system.service.IMeasDataResultsService;
import com.meas.common.utils.poi.ExcelUtil;
import com.meas.common.core.page.TableDataInfo;

/**
 * 数据结果Controller
 *
 * @author wgk
 * @date 2024-06-13
 */
@RestController
@RequestMapping("/meas/dataResults")
public class MeasDataResultsController extends BaseController {
    @Autowired
    private IMeasDataResultsService measDataResultsService;

    /**
     * 查询数据结果列表
     */
    @PreAuthorize("@ss.hasPermi('meas:dataResults:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeasDataResults measDataResults) {
        startPage();
        List<MeasDataResults> list = measDataResultsService.selectMeasDataResultsList(measDataResults);
        return getDataTable(list);
    }

    /**
     * 导出数据结果列表
     */
    @PreAuthorize("@ss.hasPermi('meas:dataResults:export')")
    @Log(title = "数据结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeasDataResults measDataResults) {
        List<MeasDataResults> list = measDataResultsService.selectMeasDataResultsList(measDataResults);
        ExcelUtil<MeasDataResults> util = new ExcelUtil<MeasDataResults>(MeasDataResults.class);
        util.exportExcel(response, list, "数据结果数据");
    }

    /**
     * 获取数据结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('meas:dataResults:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(measDataResultsService.selectMeasDataResultsById(id));
    }

    /**
     * 新增数据结果
     */
    @PreAuthorize("@ss.hasPermi('meas:dataResults:add')")
    @Log(title = "数据结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeasDataResults measDataResults) {
        MeasDataResults returnValue = measDataResultsService.insertMeasDataResults(measDataResults);
        if(returnValue == null){
            return error();
        } else {
            return success(returnValue);
        }
    }

    /**
     * 修改数据结果
     */
    @PreAuthorize("@ss.hasPermi('meas:dataResults:edit')")
    @Log(title = "数据结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeasDataResults measDataResults) {
        return toAjax(measDataResultsService.updateMeasDataResults(measDataResults));
    }

    /**
     * 删除数据结果
     */
    @PreAuthorize("@ss.hasPermi('meas:dataResults:remove')")
    @Log(title = "数据结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(measDataResultsService.deleteMeasDataResultsByIds(ids));
    }

    /**
     * 获取数据结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('meas:dataResults:query')")
    @GetMapping(value = "/getInfoByEnvDataId/{envDataId}")
    public AjaxResult getInfoByEnvDataId(@PathVariable("envDataId") Long envDataId) {
        return success(measDataResultsService.selectMeasDataResultsByEnvDataId(envDataId));
    }
}
