package com.zkqy.web.controller.dragForm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zkqy.common.annotation.Log;
import com.zkqy.common.core.controller.BaseController;
import com.zkqy.common.core.domain.AjaxResult;
import com.zkqy.common.core.page.TableDataInfo;
import com.zkqy.common.enums.BusinessType;

import com.zkqy.common.utils.poi.ExcelUtil;
import com.zkqy.system.entity.DragForm;
import com.zkqy.system.service.IDragFormService;
import org.springframework.web.bind.annotation.*;

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

/**
 * 动态表单
 * Controller
 *
 * @author hzh
 * @date 2023-07-12
 */

@RestController
@RequestMapping("/dragform/form")
public class DragFormController extends BaseController {
    @Resource
    private IDragFormService dragFormService;

    /**
     * 查询动态表单
     * 列表
     */
    ////@PreAuthorize("@ss.hasPermi('system:form:list')")
    @GetMapping("/list")
    public TableDataInfo list(DragForm dragForm) {
        startPage();
        List<DragForm> list = dragFormService.selectDragFormList(dragForm);
        return getDataTable(list);
    }

    /**
     * 导出动态表单
     * 列表
     */
    ////@PreAuthorize("@ss.hasPermi('system:form:export')")
    @Log(title = "动态表单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DragForm dragForm) {
        List<DragForm> list = dragFormService.selectDragFormList(dragForm);
        ExcelUtil<DragForm> util = new ExcelUtil<DragForm>(DragForm.class);
        util.exportExcel(response, list, "动态表单数据");
    }

    /**
     * 获取动态表单
     * 详细信息
     */
    ////@PreAuthorize("@ss.hasPermi('system:form:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return success(dragFormService.selectDragFormByFId(fId));
    }

    @GetMapping(value = "/getInfoByFormKey/{formKey}")
    public AjaxResult getInfoByFormKey(@PathVariable("formKey") String formKey) {
        return success(dragFormService.selectDragFormByFormKey(formKey));
    }

    /**
     * 新增动态表单
     */
    ////@PreAuthorize("@ss.hasPermi('system:form:add')")
    @Log(title = "动态表单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DragForm dragForm) {
        return dragFormService.insertDragForm(dragForm);
    }

    /**
     * 修改动态表单
     */
    ////@PreAuthorize("@ss.hasPermi('system:form:edit')")
    @Log(title = "动态表单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DragForm dragForm) {
        return toAjax(dragFormService.updateDragForm(dragForm));
    }

    /**
     * 删除动态表单
     */
    ////@PreAuthorize("@ss.hasPermi('system:form:remove')")
    @Log(title = "动态表单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return toAjax(dragFormService.deleteDragFormByFIds(fIds));
    }

    /**
     * 获取动态表单
     * 详细信息
     */
    @GetMapping("/getInfoBySqlKey/{sqlKey}")
    public AjaxResult getInfoBySqlKey(@PathVariable("sqlKey") String sqlKey) throws JsonProcessingException {
        return success(dragFormService.selectDragFormBySqlKey(sqlKey));
    }

}
