package com.ruoyi.web.controller.dragForm;

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

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.entity.DragTableBtn;
import com.ruoyi.system.entity.DragTableBtnRelevance;
import com.ruoyi.system.service.IDragTableBtnRelevanceService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IDragTableBtnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 格绑定的自定义按钮Controller
 *
 * @author ruoyi
 * @date 2023-11-07
 */
@RestController
@RequestMapping("/system/btn")
public class DragTableBtnController extends BaseController {
    @Autowired // 按钮组
    private IDragTableBtnService dragTableBtnService;

    @Autowired // 按钮表单关联
    private IDragTableBtnRelevanceService dragTableBtnRelevanceService;

    /**
     * 查询格绑定的自定义按钮列表
     */
    @PreAuthorize("@ss.hasPermi('system:btn:list')")
    @GetMapping("/list")
    public TableDataInfo list(DragTableBtn dragTableBtn) {
        startPage();
        List<DragTableBtn> list = dragTableBtnService.selectDragTableBtnList(dragTableBtn);
        return getDataTable(list);
    }

    /**
     * 导出格绑定的自定义按钮列表
     */
    @PreAuthorize("@ss.hasPermi('system:btn:export')")
    @Log(title = "格绑定的自定义按钮", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DragTableBtn dragTableBtn) {
        List<DragTableBtn> list = dragTableBtnService.selectDragTableBtnList(dragTableBtn);
        ExcelUtil<DragTableBtn> util = new ExcelUtil<DragTableBtn>(DragTableBtn.class);
        util.exportExcel(response, list, "格绑定的自定义按钮数据");
    }

    /**
     * 获取格绑定的自定义按钮详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:btn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dragTableBtnService.selectDragTableBtnById(id));
    }

    /**
     * 新增格绑定的自定义按钮
     */
    @PreAuthorize("@ss.hasPermi('system:btn:add')")
    @Log(title = "格绑定的自定义按钮", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DragTableBtn dragTableBtn) {
        dragTableBtnService.insertDragTableBtn(dragTableBtn);
        return AjaxResult.success();
    }

    /**
     * 修改格绑定的自定义按钮
     */
    @PreAuthorize("@ss.hasPermi('system:btn:edit')")
    @Log(title = "格绑定的自定义按钮", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DragTableBtn dragTableBtn) {
        return toAjax(dragTableBtnService.updateDragTableBtn(dragTableBtn));
    }

    /**
     * 删除格绑定的自定义按钮
     */
    @PreAuthorize("@ss.hasPermi('system:btn:remove')")
    @Log(title = "格绑定的自定义按钮", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        if (dragTableBtnService.hasChildByBtnId(id)) {
            return warn("存在子按钮,不允许删除");
        }
        return toAjax(dragTableBtnService.deleteDragTableBtnById(id));
    }

    /**
     * 根据当前按钮组的btnkey得到绑定的表格
     *
     * @param btnKey 按钮唯一标识
     * @return
     */
    @GetMapping("/checkBtn")
    public TableDataInfo checkBtn(String btnKey) {
         return getDataTable(dragTableBtnRelevanceService.selectDragTableBtnRelevanceList(new DragTableBtnRelevance(btnKey)));
    }
}
