package com.willalp.web.controller.clockingin;

import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.flow.domain.HsFlowRunningRecord;
import com.willalp.flow.service.IHsFlowRunningRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 流程运行记录Controller
 *
 * @author willalp
 * @date 2022-10-12
 */
@RestController
@RequestMapping("/flow/flowRecord")
public class HsFlowRunningRecordController extends BaseController {
    @Resource
    private IHsFlowRunningRecordService hsFlowRunningRecordService;

    /**
     * 查询流程运行记录列表
     */
    @GetMapping("/gf")
    public TableDataInfo list(String taskId) {
        startPage();
        List<HsFlowRunningRecord> list = hsFlowRunningRecordService.getFlowRecordByBusinessId(taskId);
        return getDataTable(list);
    }

    /**
     * 查询流程运行记录列表
     */
    @PreAuthorize("@ss.hasPermi('flow:flowRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsFlowRunningRecord hsFlowRunningRecord) {
        startPage();
        List<HsFlowRunningRecord> list = hsFlowRunningRecordService.selectHsFlowRunningRecordList(hsFlowRunningRecord);
        return getDataTable(list);
    }

    /**
     * 导出流程运行记录列表
     */
    @PreAuthorize("@ss.hasPermi('flow:flowRecord:export')")
    @Log(title = "流程运行记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsFlowRunningRecord hsFlowRunningRecord) {
        List<HsFlowRunningRecord> list = hsFlowRunningRecordService.selectHsFlowRunningRecordList(hsFlowRunningRecord);
        ExcelUtil<HsFlowRunningRecord> util = new ExcelUtil<HsFlowRunningRecord>(HsFlowRunningRecord.class);
        return util.exportExcel(list, "流程运行记录数据");
    }

    /**
     * 获取流程运行记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:flowRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsFlowRunningRecordService.getById(id));
    }

    /**
     * 新增流程运行记录
     */
    @PreAuthorize("@ss.hasPermi('flow:flowRecord:add')")
    @Log(title = "流程运行记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsFlowRunningRecord hsFlowRunningRecord) {
        hsFlowRunningRecord.setCreateBy(SecurityUtils.getUsername());
        hsFlowRunningRecord.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsFlowRunningRecordService.save(hsFlowRunningRecord));
    }

    /**
     * 修改流程运行记录
     */
    @PreAuthorize("@ss.hasPermi('flow:flowRecord:edit')")
    @Log(title = "流程运行记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsFlowRunningRecord hsFlowRunningRecord) {
        hsFlowRunningRecord.setUpdateBy(SecurityUtils.getUsername());
        hsFlowRunningRecord.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsFlowRunningRecordService.updateById(hsFlowRunningRecord));
    }

    /**
     * 删除流程运行记录
     */
    @PreAuthorize("@ss.hasPermi('flow:flowRecord:remove')")
    @Log(title = "流程运行记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsFlowRunningRecordService.removeByIds(Arrays.asList(ids)));
    }
}
