package com.dcs.hnyz.controller;

import com.dcs.common.annotation.Log;
import com.dcs.common.core.controller.BaseController;
import com.dcs.common.core.domain.AjaxResult;
import com.dcs.common.core.page.TableDataInfo;
import com.dcs.common.enums.BusinessType;
import com.dcs.common.utils.poi.ExcelUtil;
import com.dcs.hnyz.domain.Flow;
import com.dcs.hnyz.service.IFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 流程Controller
 * 
 * @author hmy
 * @date 2025-03-08
 */
@RestController
@RequestMapping("/hnyz/flow")
@Api(tags = "流程管理")
public class FlowController extends BaseController
{
    @Autowired
    private IFlowService flowService;

    /**
     * 获取流程列表（用于下拉框）
     */
    @GetMapping("/getFlowList")
    @ApiOperation(value = "获取设备对应流程列表（用于下拉框）")
    public AjaxResult getFlowList(@ApiParam(name = "equipmentId" ,value="设备id") Long equipmentId,
    @ApiParam(name="flowType" ,value = "流程类型1普通流程2组态页面流程")String flowType){
        return success(flowService.getFlowList(equipmentId,flowType));
    }

    /**
     * 查询流程列表
     */
    @PreAuthorize("@ss.hasPermi('hnyz:flow:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询流程列表")
    public TableDataInfo list(@ApiParam(name = "流程" ,value="流程",required = true) Flow flow)
    {
        startPage();
        List<Flow> list = flowService.selectFlowList(flow);
        return getDataTable(list);
    }

    /**
     * 导出流程列表
     */
    @PreAuthorize("@ss.hasPermi('hnyz:flow:export')")
    @Log(title = "流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出流程列表")
    public void export(HttpServletResponse response, Flow flow)
    {
        List<Flow> list = flowService.selectFlowList(flow);
        ExcelUtil<Flow> util = new ExcelUtil<Flow>(Flow.class);
        util.exportExcel(response, list, "流程数据");
    }

    /**
     * 获取流程详细信息
     */
    @PreAuthorize("@ss.hasPermi('hnyz:flow:query')")
    @GetMapping(value = "/{flowId}")
    @ApiOperation(value = "获取流程详细信息")
    public AjaxResult getInfo(@ApiParam("流程id") @PathVariable("flowId") Long flowId)
    {
        return success(flowService.selectFlowByFlowId(flowId));
    }

    /**
     * 新增流程
     */
    @PreAuthorize("@ss.hasPermi('hnyz:flow:add')")
    @Log(title = "流程", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增流程")
    public AjaxResult add(@RequestBody Flow flow)
    {
        return toAjax(flowService.insertFlow(flow));
    }

    /**
     * 修改流程
     */
    @PreAuthorize("@ss.hasPermi('hnyz:flow:edit')")
    @Log(title = "流程", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改流程")
    public AjaxResult edit(@RequestBody Flow flow)
    {
        return toAjax(flowService.updateFlow(flow));
    }

    /**
     * 删除流程
     */
    @ApiOperation(value = "删除流程")
    @PreAuthorize("@ss.hasPermi('hnyz:flow:remove')")
    @Log(title = "流程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{flowIds}")
    public AjaxResult remove(@ApiParam("流程id数组") @PathVariable Long[] flowIds)
    {
        return toAjax(flowService.deleteFlowByFlowIds(flowIds));
    }

    /**
     * 修改启用状态
     */
    @PreAuthorize("@ss.hasPermi('hnyz:flow:edit')")
    @Log(title = "流程", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Flow flow){
        return toAjax(flowService.updateFlow(flow));
    }
}
