package com.pdaaphm.system.controller;

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

import com.pdaaphm.common.utils.StringUtils;
import com.pdaaphm.system.domain.Data;
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.pdaaphm.common.annotation.Log;
import com.pdaaphm.common.core.controller.BaseController;
import com.pdaaphm.common.core.domain.AjaxResult;
import com.pdaaphm.common.enums.BusinessType;
import com.pdaaphm.system.domain.TDataProcess;
import com.pdaaphm.system.service.ITDataProcessService;
import com.pdaaphm.common.utils.poi.ExcelUtil;
import com.pdaaphm.common.core.page.TableDataInfo;

/**
 * 数据处理Controller
 * 
 * @author ruoyi
 * @date 2024-08-20
 */
@RestController
@RequestMapping("/system/process")
public class TDataProcessController extends BaseController
{
    @Autowired
    private ITDataProcessService tDataProcessService;

    /**
     * 查询数据处理列表
     */
    @PreAuthorize("@ss.hasPermi('system:process:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDataProcess tDataProcess)
    {
        startPage();
        List<TDataProcess> list = tDataProcessService.selectTDataProcessList(tDataProcess);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/preprocessList")
    public TableDataInfo preprocessList(TDataProcess tDataProcess)
    {
        startPage();
        List<TDataProcess> list = tDataProcessService.selectPreprocessDataList(tDataProcess);
        return getDataTable(list);
    }

    /**
     * 导出数据处理列表
     */
    @PreAuthorize("@ss.hasPermi('system:process:export')")
    @Log(title = "数据处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDataProcess tDataProcess)
    {
        List<TDataProcess> list = tDataProcessService.selectTDataProcessList(tDataProcess);
        ExcelUtil<TDataProcess> util = new ExcelUtil<TDataProcess>(TDataProcess.class);
        util.exportExcel(response, list, "数据处理数据");
    }

    /**
     * 导出数据处理列表
     */
    @PreAuthorize("@ss.hasPermi('system:process:export')")
    @Log(title = "数据处理", businessType = BusinessType.EXPORT)
    @PostMapping("/preprocessExport")
    public void preprocessExport(HttpServletResponse response, TDataProcess tDataProcess)
    {
        List<TDataProcess> list = tDataProcessService.selectPreprocessDataList(tDataProcess);
        ExcelUtil<TDataProcess> util = new ExcelUtil<TDataProcess>(TDataProcess.class);
        util.exportExcel(response, list, "数据处理数据");
    }

    /**
     * 获取数据处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:process:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tDataProcessService.selectTDataProcessById(id));
    }

    /**
     * 新增数据处理
     */
    @PreAuthorize("@ss.hasPermi('system:process:add')")
    @Log(title = "数据处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDataProcess tDataProcess)
    {
        int rows = tDataProcessService.insertTDataProcess(tDataProcess);
        if (rows > 0) {
            this.runDataProcess(tDataProcess.getId());
        }
        return toAjax(rows);
    }

    /**
     * 修改数据处理
     */
    @PreAuthorize("@ss.hasPermi('system:process:edit')")
    @Log(title = "数据处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDataProcess tDataProcess)
    {
        return toAjax(tDataProcessService.updateTDataProcess(tDataProcess));
    }

    /**
     * 删除数据处理
     */
    @PreAuthorize("@ss.hasPermi('system:process:remove')")
    @Log(title = "数据处理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tDataProcessService.deleteTDataProcessByIds(ids));
    }

    @GetMapping("/getDataProcessByType/{type}")
    public AjaxResult getDataProcessByType(@PathVariable String type)
    {
        return success(tDataProcessService.getDataProcessByType(type));
    }

    @PostMapping("/runDataProcess/{id}")
    public AjaxResult runDataProcess(@PathVariable Long id)
    {
        String errorMsg = tDataProcessService.runDataProcess(id);
        if(StringUtils.isNotEmpty(errorMsg)){
            return error(errorMsg);
        } else {
            return success();
        }
    }
}
